JAVASCRIPT
Validation Utilities
All samples are using Ink.requireModules
, please read how to use it at Ink.requireModules section
Function name | Description |
---|---|
.ascii(s, [options]) | Checks if a field only contains only ASCII alphanumeric characters. |
.checkCharacterGroups(s, [groups]) | Checks if a field has the required groups. |
.codPostal(cp1, [cp2], [returnBothResults]) | Validates if a zip code is valid in Portugal |
.createRegExp(groups) | Creates a regular expression for several character groups. |
.email(email) | Checks if an email address is valid |
.getEANCheckDigit(digits) | Get the check digit of an EAN code without a check digit. |
.isAOPhone(phone) | Checks if a phone is valid in Angola |
.isCVPhone(phone) | Checks if a phone is valid in Cabo Verde |
.isColor(str) | Checks if a string is a valid color |
.isCreditCard(num, creditCardType) | Checks if a number is of a specific credit card type |
.isDate(format, dateStr) | Checks if a date is valid in a given format |
.isEAN(code, [eanType]) | Validate an EAN barcode string. |
.isIP(value, ipType) | Checks if the value is a valid IP. |
.isMZPhone(phone) | Checks if a phone is valid in Mozambique |
.isPTPhone(phone) | Checks if a phone is valid in Portugal |
.isPhone(phone, [countryCode]) | Checks if a number is a phone number. |
.isPortuguesePhone(phone) | Alias function for isPTPhone |
.isTLPhone(phone) | Checks if a phone is valid in Timor |
.latin1(s, [options]) | Checks if a field only contains latin-1 alphanumeric characters. |
.number(numb, [options]) | Checks if a number is a valid |
.unicode(s, [options]) | Checks if a field contains unicode printable characters. |
.url(url, [full]) | Checks if an url is valid |
Checks if a field only contains only ASCII alphanumeric characters.
Takes options for allowing singleline whitespace, cross-line whitespace and punctuation.
s
The validation stringoptions
{}Optional configuration object. See createRegexpCode
Ink.requireModules(['Ink.Util.Validator_1'], function(Validator) {
var result1 = Validator.ascii('foobar');
Ink.log(result1); // true
var result2 = Validator.ascii('foobar123');
Ink.log(result2); // false
});
Checks if a field has the required groups.
s
The validation stringgroups
{}What groups are included. See `createRegExp`Code
Ink.requireModules(['Ink.Util.Validator_1'], function(Validator) {
var result1 = Validator.checkCharacterGroups('123foobar', {asciiAlpha:[], numbers:[]});
Ink.log(result1); // true
var result2 = Validator.checkCharacterGroups('foo@bar', {asciiAlpha:[], numbers:[]});
Ink.log(result2); // false
});
Validates if a zip code is valid in Portugal
cp1
If passed alone, it's the full postal code. If passed with `cp2`, it's the first fragment of the zip code, which should have 4 numeric digits.cp2
Second fragment of the zip code, which should have 3 numeric digits.returnBothResults
When given both `cp1` and `cp2`, return an array `[Boolean, Boolean]`, indicating which of these were valid. For example `[true, true]` means both were valid, while `[true, false]` means `cp1` was valid, and `cp2` was invalid.Code
Ink.requireModules(['Ink.Util.Validator_1'], function(Validator) {
var result1 = Validator.codPostal('1069', '300');
Ink.log(result1); // true
var result2 = Validator.codPostal('1069', '300', true);
Ink.log(result2); // [true, true]
});
Creates a regular expression for several character groups.
groups
Groups to build regular expressions for. Possible keys are:Checks if an email address is valid
Code
Ink.requireModules(['Ink.Util.Validator_1'], function(Validator) {
var result1 = Validator.mail('inkdev@sapo.pt');
Ink.log(result1); // true
var result2 = Validator.mail('inkdev\u0040sapo.pt');
Ink.log(result2); // true - (\u0040 is at sign)
var result3 = Validator.mail('sometextnomail');
Ink.log(result3); // false
});
Get the check digit of an EAN code without a check digit.
digits
The remaining digits, out of which the check digit is calculated.Checks if a phone is valid in Angola
phone
Phone number to be checkedCode
Ink.requireModules(['Ink.Util.Validator_1'], function(Validator) {
Validator.isAOPhone('244222396385'); // true
Validator.isAOPhone('00244222396385'); // true
Validator.isAOPhone('+244222396385'); // true
Validator.isAOPhone('1'); // false
});
Checks if a phone is valid in Cabo Verde
phone
Phone number to be checkedCode
Ink.requireModules(['Ink.Util.Validator_1'], function(Validator) {
Validator.isCVPhone('2610303'); // true
Validator.isCVPhone('002382610303'); // true
Validator.isCVPhone('+2382610303'); // true
Validator.isCVPhone('1'); // false
});
Checks if a string is a valid color
str
Color string to be checkedCode
Ink.requireModules(['Ink.Util.Validator_1'], function(Validator) {
Validator.isColor('#FF00FF'); // true
Validator.isColor('rgb(200, 100, 150)'); // true
Validator.isColor('amdafasfs'); // false
});
Checks if a number is of a specific credit card type
num
Number to be validatescreditCardType
Credit card type or list of types. See _creditCardSpecs for the list of supported values.Code
Ink.requireModules(['Ink.Util.Validator_1'], function(Validator) {
Validator.isCreditCard('4000000000000002', 'visa'); // true
Validator.isCreditCard('4000000000000002', 'mastercard'); // false
Validator.isCreditCard('41111111111111', 'visa'); // false
});
Checks if a date is valid in a given format
format
Format defined in _dateParsersdateStr
Date stringCode
Ink.requireModules(['Ink.Util.Validator_1'], function(Validator) {
Validator.isDate('yyyy-mm-dd', '2014-04-10'); // true
});
Validate an EAN barcode string.
code
The code containing the EANeanType
'ean-13'Select your EAN type. Can be 'ean-8' or 'ean-13'Checks if the value is a valid IP.
value
Value to be checkedipType
Type of IP to be validated. The values are: ipv4, ipv6. By default is ipv4.Code
Ink.requireModules(['Ink.Util.Validator_1'], function(Validator) {
Validator.isIP('192.168.1.254'); // true
Validator.isIP('192.168.1001.254'); // false
Validator.isIP('2001:0db8:0000:0000:0000:ff00:0042:8329', 'ipv6'); // true
});
Checks if a phone is valid in Mozambique
phone
Phone number to be checkedCode
Ink.requireModules(['Ink.Util.Validator_1'], function(Validator) {
Validator.isMZPhone('21426861'); // true
Validator.isMZPhone('0025821426861'); // true
Validator.isMZPhone('+25821426861'); // true
Validator.isMZPhone('1'); // false
});
Checks if a phone is valid in Portugal
phone
Phone number to be checkedCode
Ink.requireModules(['Ink.Util.Validator_1'], function(Validator) {
Validator.isPTPhone('213919264'); // true
Validator.isPTPhone('00351213919264'); // true
Validator.isPTPhone('+351213919264'); // true
Validator.isPTPhone('1'); // false
});
Checks if a number is a phone number.
This method validates the number in all country codes available the ones set in the second param
phone
Phone number to validatecountryCode
Country code or array of countries to validateCode
Ink.requireModules(['Ink.Util.Validator_1'], function(Validator) {
Validator.isPhone('213919264'); // true
Validator.isPhone('213919264', 'PT'); // true
Validator.isPhone('213919264', 'MZ'); // false
});
Alias function for isPTPhone
phone
Phone number to be checkedChecks if a phone is valid in Timor
phone
Phone number to be checkedCode
Ink.requireModules(['Ink.Util.Validator_1'], function(Validator) {
Validator.isTLPhone('3331234'); // true
Validator.isTLPhone('006703331234'); // true
Validator.isTLPhone('+6703331234'); // true
Validator.isTLPhone('1'); // false
});
Checks if a field only contains latin-1 alphanumeric characters.
Takes options for allowing singleline whitespace, cross-line whitespace and punctuation.
s
The validation stringoptions
{}Optional configuration object. See createRegexpCode
Ink.requireModules(['Ink.Util.Validator_1'], function(Validator) {
var result1 = Validator.latin1('água');
Ink.log(result1); // true
var result2 = Validator.latin1('w@ter');
Ink.log(result2); // false
});
Checks if a number is a valid
numb
The numberoptions
Further optionsoptions.decimalSep
'.'Allow decimal separator.options.thousandSep
","Strip this character from the number.options.negative
falseAllow negative numbers.options.decimalPlaces
nullMaximum number of decimal places. Use `0` for an integer number.options.max
nullMaximum numberoptions.min
nullMinimum numberoptions.returnNumber
falseWhen this option is `true`, return the number itself when the value is valid.Code
Ink.requireModules(['Ink.Util.Validator_1'], function(Validator) {
var result1 = Validator.number(12345);
Ink.log(result1); // true
var result2 = Validator.number(255, {min: 50, max: 100});
Ink.log(result2); // false
var result3 = Validator.number(75, {min: 50, max: 100});
Ink.log(result3); // true
var result4 = Validator.number(42.255, {decimalPlaces: 2});
Ink.log(result4); // false
var result5 = Validator.number(42.25, {decimalPlaces: 2});
Ink.log(result5); // true
});
Checks if a field contains unicode printable characters.
s
The validation stringoptions
{}Optional configuration object. See createRegexpChecks if an url is valid
url
URL to be checkedfull
If true, validates a full URL (one that should start with 'http')Code
Ink.requireModules(['Ink.Util.Validator_1'], function(Validator) {
var result1 = Validator.url('ink.sapo.pt');
Ink.log(result1); // true
var result2 = Validator.url('https://ink.sapo.pt', true);
Ink.log(result2); // true
var result3 = Validator.url('ink.sapo.pt', true);
Ink.log(result3); // false
var result4 = Validator.url('sometextnourl');
Ink.log(result4); // false
});