Getting Started
Events

phone validator

Validate a phone number

Options

Using with form field
The HTML attributes are used to set the validator options via the Declarative plugin
(* denotes a required option)
Name HTML attribute Type Description
country * data-fv-phone___country String or Function An ISO-3166 country code
message data-fv-phone___message String The error message
Since the plugin doesn't support HTML 5 type="tel" attribute, you should use type="text" instead
If you want to support custom formats of a phone number, you should use the Transformer plugin .
The validator supports the following countries:
Country Country code
United States US
United Arab Emirates AE
Bulgaria BG
Brazil BR
China CN
Czech Republic CZ
Denmark DK
France FR
Germany DE
India IN
Morocco MA
Netherlands NL
Pakistan PK
Romania RO
Russia RU
Slovakia SK
Spain ES
Thailand TH
United Kingdom GB
Venezuela VE
Using with ES6 module
            
// You might need to change the importing path
import phone from 'formvalidation/dist/es6/validators/phone' ;
const result = phone ( ) . validate ( {
value : ... ,
options : {
// Can be a string or a function returns a string
country : ... ,
message : ... ,
} ,
} ) ;
/*
result is an object of
{
valid: true or false,
message: The error message
}
*/

Basic example

phone validator

ES6 Module Example

The following snippet shows how to use the phone validator with ES6 module:
            
// You might need to change the importing path
import phone from 'formvalidation/dist/es6/validators/phone' ;
const res1 = phone ( ) . validate ( {
value : '' ,
options : {
country : 'US' ,
message : 'The value is not a valid phone number' ,
} ,
} ) ;
// res1.valid === true
const res2 = phone ( ) . validate ( {
value : '(+55) 15 3702-7523' ,
options : {
country : 'BR' ,
message : 'The value is not a valid phone number' ,
} ,
} ) ;
// res2.valid === true

See also

Changelog

v1.6.0
  • Fixed an issue that the country option isn't passed to the placeholder message