Phone
Validating phone numbers is rather simple but time consuming task, valid8r phone validators provides a handful of options to best suit your validation requirements
Valid8r covers all the basic as well as advanced phone validations that covers 99% edge cases
Start by importing valid8r into your file:
import valid8r from '@c4code/valid8r';Configuration Options
Valid8r phone validator provides handful of options to best meet your requirements and validation needs, the options that can be configured in phone validator are:
import valid8r from '@c4code/valid8r';
const input: string = "+1 1234567890";
// The configuration can be defaulted globally
valid8r.phone(input, {
safe: false, // boolean
allowDashes: false, // boolean
allowParentheses: false, // boolean
minLen: 10, // number
maxLen: 15, // number
allowedCountryCodes: ["*"], // string[] | defaults to every country code
requireCountryCode: true, // boolean
throwErrorsAs: "throw-all", // "throw-first" | "throw-last" | "throw-all",
/*
"throw-first" => Throws or returns the first error encountered,
"throw-last" => Throws or returns the last error encountered,
"throw-all" => Throws or returns all errors in a Array of string
*/
}); // the second argument is optionalimport valid8r from '@c4code/valid8r';
const input = "+1 1234567890";
// The configuration can be defaulted globally
valid8r.phone(input, {
safe: false, // boolean
allowDashes: false, // boolean
allowParentheses: false, // boolean
minLen: 10, // number
maxLen: 15, // number
allowedCountryCodes: ["*"], // string[] | defaults to every country code
requireCountryCode: true, // boolean
throwErrorsAs: "throw-all", // "throw-first" | "throw-last" | "throw-all",
/*
"throw-first" => Throws or returns the first error encountered,
"throw-last" => Throws or returns the last error encountered,
"throw-all" => Throws or returns all errors in a Array of string
*/
}); // the second argument is optionalThe allowedCountryCodes field accepts country code that should be allowed in the phone number, ex.
allowedCountryCodes: ['+91', '+92']; // Accepts phone from these codes onlyIf not provided explicitly will accept every country code
The Configuration can be globally set for all the validators in your project, know more?
Basic Usage
Here's how we can use the email validator in it's most basic manner:
Start by importing valid8r:
import valid8r from '@c4code/valid8r';import valid8r from '@c4code/valid8r';
// Your input's value
const input2: string = "9876543210";
valid8r.phone(input); // throws PhoneValidationFailed errorimport valid8r from '@c4code/valid8r';
// Your input's value
const input2 = "sm#thjones@outlook.com";
valid8r.email(input); // throws EmailValidationFailed errorBy default the phone validator requires international format and a country code, if not provided will raise a PhoneValidationFailed error.
Safe handling
By default, all the validations in valid8r works on unsafe flag, meaning if any of the validation fails it will throw an Error.
However, if you don't want it to throw any error but return them. set the safe flag to true.
import valid8r from '@c4code/valid8r';
// Your input's value
const input2: string = "9876543210";
const [isValid, errors] = valid8r.phone(input, { safe: true });
if (!isValid) {
console.log(errors);
// [{ requireCountryCode: "Phone number must include a country code (e.g., +1, +91)." }]
}import valid8r from '@c4code/valid8r';
// Your input's value
const input2 = "9876543210";
const [isValid, errors] = valid8r.phone(input, { safe: true });
if (!isValid) {
console.log(errors);
// [{ requireCountryCode: "Phone number must include a country code (e.g., +1, +91)." }]
}Error Messages
If you don't like the default error message and want to set a custom error message for any specific fields, pass an object as a third argument of the phone function:
import valid8r from '@c4code/valid8r';
// Your input's value
const input2: string = "9876543210";
const [isValid, errors] = valid8r.email(input, {
safe: true
}, {
requireCountryCode: "Country code is required in valid phone number"
});
if (!isValid) {
console.log(errors);
// [{ requireCountryCode: "Country code is required in valid phone number" }]
}import valid8r from '@c4code/valid8r';
// Your input's value
const input2 = "9876543210";
const [isValid, errors] = valid8r.email(input, {
safe: true
}, {
requireCountryCode: "Country code is required in valid phone number"
});
if (!isValid) {
console.log(errors);
// [{ requireCountryCode: "Country code is required in valid phone number" }]
}Error Message Options
Valid8r phone validator provides handful of options to configure error messages to best fit your project requirements
import valid8r from '@bitbybit/valid8r';
const input: string = "+91 9876543210";
// The configuration can be defaulted globally
valid8r.phone(input, { /* Field Configurations */ }, {
format:
"Phone number is invalid, only spaces, digits, dashes and paranthesis are allowed!",
allowDashes:
"Dashes are not valid in phone number.",
allowParentheses:
"Parentheses are not valid in phone number.",
minLen:
"Phone number must be at least {minLen} digits long.",
maxLen:
"Phone number must not exceed {maxLen} digits.",
allowedCountryCodes:
"Phone number must start with one of the allowed country codes: {allowedCountryCodes}.",
requireCountryCode:
"Phone number must include a country code (e.g., +1, +91).",
}); // the second and third argument is optionalimport valid8r from '@bitbybit/valid8r';
const input = "william.j@hotmail.com";
// The configuration can be defaulted globally
valid8r.email(input, { /* Field Configurations */ }, {
format:
"Invalid Email Format.", // string
noSpChars:
"Email should not contain any sp. characters other than {allowedSpChars}, found {encounteredSpChar}.", // string
startWithNum:
"Email should not start with a number.", // string
endWithNum:
"Email should not end with a number.", // string
minLen:
"Email must be at least {minLen} characters long.", // string
maxLen:
"Email must not exceed {maxLen} characters.", // string
allowedDomains:
"Email's domain must be one of: {allowedDomains}", // string
allowDisposables:
"Disposable email addresses are not allowed.", // string
noLeading:
"Email should not have leading spaces.", // string
noTrailing:
"Email should not have trailing spaces.", // string
caseSensitive:
"Email should be in lowercase.", // string
}); // the second and third argument is optionalAll the options are optional, if passed will be used as the configuration for that specific validator
The Error messages can be globally set for all the validators in your project, know more?
Last updated