phone validator
Validate a phone number
Options
Using with form field
(* 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 the ES6 module
import { phone } from '/vendors/@form-validation/cjs/validator-phone';
const result = phone().validate({
value: ...,
options: {
country: ...,
message: ...,
},
});
Using the npm package
- Install the validator package:
$ npm install @form-validation/validator-phone
import { phone } from '@form-validation/validator-phone';
const result = phone().validate({
value: ...,
options: {
country: ...,
message: ...,
},
});
Basic example
NPM package example
The following snippet shows how to use the phone validator with the npm package:
import { phone } from '@form-validation/validator-phone';
const res1 = phone().validate({
value: '246.555.8888',
options: {
country: 'US',
message: 'The value is not a valid phone number',
},
});
const res2 = phone().validate({
value: '(+55) 15 3702-7523',
options: {
country: 'BR',
message: 'The value is not a valid phone number',
},
});
See also
Changelog
- The validator doesn't work properly if the
message property isn't defined
- Fixed an issue that the
country option isn't passed to the placeholder message