FormValidation provides the built-in phone validator to validate the phone number in various countries. Behind the scenes, it uses Javascript regular expressions to check if a given number matches the pattern of phone numbers in particular country. Therefore, despite the fact that it doesn't cover all possible formats of phone number in the world, you can use the regexp validator to test a phone number in your country.
This example helps you validate an international phone number by using a different approach. We will use the intl-tel-input plugin for that purpose.
intl-tel-input is a popular jQuery plugin for entering and validating international telephone numbers. Below is some of its advanced features:
Provides a very friendly user interface to enter a phone number. All countries are shown as a drop list with the flags and suggestion phone number
Provides up-to-date patterns of phone numbers in the world. The data are taken from Google libphonenumber library so they are completely stable
Has a few of APIs to validate and integrate with other tools
Using the intl-tel-input plugin
It's quite easy to use the intl-tel-input plugin for an input field:
In the next sections, you will see how to integrate the intl-tel-input plugin with FormValidation.
You should look at the basic principles when integrating FormValidation with other plugins
Using the callback validator
We can make the intl-tel-input plugin work with FormValidation easily by combining its isValidNumber() method and the callback validator:
There is an important note that the input must be revalidated when the user choose another country from the drop list. Unfortunately, intl-tel-input doesn't provide an event or callback that is executed after choosing a country. But it can be done by using a simple click event handler on the countries list element which has .country-list class:
The following table recaps the purpose of init, destroy and validate methods used above:
Method
Functionality
init
This method does anything you need to prepare the validation.
It's called once right after attaching the validator to field
destroy
This method is called when you destroy the FormValidation instance by using the destroy() method
validate
This is the validation method. It returns true or false that indicates the field is valid or invalid
These methods take three parameters:
validator is the FormValidation instance. So you can call public methods on it
$field is the field element
options is an object containing the validator options
Applying the new validator to field is easy:
There's still a small thing we should improve. As seen in the code above, there're some hard coded options when attaching intl-tel-input to field:
You don't need to worry about this. It's possible to define and pass options to the options parameter as following:
The html5Attributes property defines the HTML attributes which can be mapped with the validator options. These attributes can be used in the declarative mode, for example:
The snippet code below is a programmatic usage of passing the options:
You can take a look at the code tab to see how all of these things work together:
What if you want to provide more descriptive validation message. For example, how to inform the user that the number is too short, too long or even not a number.
We can archive this requirement by adding a dynamic message supported by the callback validator.
The message is determined based on the validation error that is retrieved by the getValidationError() method:
The patterns provided by Google libphonenumber covers most of possible types of a phone number such as mobile, fixed line, free phone lines, voice over IP, etc.
You can see the full list of these types from source code of phonenumberutil.js
The type can be retrieved by the getNumberType() method. By adding the type to the callback return value, and reuse it later, we can treat a phone number as invalid one if it doesn't match our type.
The following code illustrates how to accept the mobile phone numbers only:
For United States, getNumberType() returns FIXED_LINE_OR_MOBILE because there's no way to differentiate between fixed-line and mobile numbers
In short, it handles the success.validator.fv event which is triggered when the field passes a validator. Then depend on the type returned by the validation result, it can use the updateStatus() method to mark field as invalid one.
The updateMessage() method is also used to set or reset the validation message.