All samples are using Ink.requireModules, please read how to use it at Ink.requireModules section

Ink.Util.Validator_1 class

Functions
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

.ascii(s, [options]) method

Checks if a field only contains only ASCII alphanumeric characters.
Takes options for allowing singleline whitespace, cross-line whitespace and punctuation.

Accepts

  • s

    The validation string
  • options

    {}Optional configuration object. See createRegexp

Code

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 
});

.checkCharacterGroups(s, [groups]) method

Checks if a field has the required groups.

Accepts

  • s

    The validation string
  • groups

    {}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 
});

.codPostal(cp1, [cp2], [returnBothResults]) method

Validates if a zip code is valid in Portugal

Accepts

  • 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]
});

.createRegExp(groups) method

Creates a regular expression for several character groups.

Accepts

  • groups

    Groups to build regular expressions for. Possible keys are:

.email(email) method

Checks if an email address is valid

Accepts

  • email

    String containing the e-mail.

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 
});

.getEANCheckDigit(digits) method

Get the check digit of an EAN code without a check digit.

Accepts

  • digits

    The remaining digits, out of which the check digit is calculated.

.isAOPhone(phone) method

Checks if a phone is valid in Angola

Accepts

  • phone

    Phone number to be checked

Code

Ink.requireModules(['Ink.Util.Validator_1'], function(Validator) {

    Validator.isAOPhone('244222396385');  // true 
    Validator.isAOPhone('00244222396385');  // true 
    Validator.isAOPhone('+244222396385');  // true 
    Validator.isAOPhone('1');  // false 
});

.isCVPhone(phone) method

Checks if a phone is valid in Cabo Verde

Accepts

  • phone

    Phone number to be checked

Code

Ink.requireModules(['Ink.Util.Validator_1'], function(Validator) {

    Validator.isCVPhone('2610303');  // true 
    Validator.isCVPhone('002382610303');  // true 
    Validator.isCVPhone('+2382610303');  // true 
    Validator.isCVPhone('1');  // false 
});

.isColor(str) method

Checks if a string is a valid color

Accepts

  • str

    Color string to be checked

Code

Ink.requireModules(['Ink.Util.Validator_1'], function(Validator) {

    Validator.isColor('#FF00FF');  // true 
    Validator.isColor('rgb(200, 100, 150)');  // true 
    Validator.isColor('amdafasfs');  // false 
});

.isCreditCard(num, creditCardType) method

Checks if a number is of a specific credit card type

Accepts

  • num

    Number to be validates
  • creditCardType

    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 
});

.isDate(format, dateStr) method

Checks if a date is valid in a given format

Accepts

  • format

    Format defined in _dateParsers
  • dateStr

    Date string

Code

Ink.requireModules(['Ink.Util.Validator_1'], function(Validator) {

    Validator.isDate('yyyy-mm-dd', '2014-04-10');  // true 
});

.isEAN(code, [eanType]) method

Validate an EAN barcode string.

Accepts

  • code

    The code containing the EAN
  • eanType

    'ean-13'Select your EAN type. Can be 'ean-8' or 'ean-13'

.isIP(value, ipType) method

Checks if the value is a valid IP.

Accepts

  • value

    Value to be checked
  • ipType

    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 
});

.isMZPhone(phone) method

Checks if a phone is valid in Mozambique

Accepts

  • phone

    Phone number to be checked

Code

Ink.requireModules(['Ink.Util.Validator_1'], function(Validator) {

    Validator.isMZPhone('21426861');  // true 
    Validator.isMZPhone('0025821426861');  // true 
    Validator.isMZPhone('+25821426861');  // true 
    Validator.isMZPhone('1');  // false 
});

.isPTPhone(phone) method

Checks if a phone is valid in Portugal

Accepts

  • phone

    Phone number to be checked

Code

Ink.requireModules(['Ink.Util.Validator_1'], function(Validator) {

    Validator.isPTPhone('213919264');  // true 
    Validator.isPTPhone('00351213919264');  // true 
    Validator.isPTPhone('+351213919264');  // true 
    Validator.isPTPhone('1');  // false 
});

.isPhone(phone, [countryCode]) method

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

Accepts

  • phone

    Phone number to validate
  • countryCode

    Country code or array of countries to validate

Code

Ink.requireModules(['Ink.Util.Validator_1'], function(Validator) {

    Validator.isPhone('213919264');  // true 
    Validator.isPhone('213919264', 'PT');  // true 
    Validator.isPhone('213919264', 'MZ');  // false 
});

.isPortuguesePhone(phone) method

Alias function for isPTPhone

Accepts

  • phone

    Phone number to be checked

.isTLPhone(phone) method

Checks if a phone is valid in Timor

Accepts

  • phone

    Phone number to be checked

Code

Ink.requireModules(['Ink.Util.Validator_1'], function(Validator) {

    Validator.isTLPhone('3331234');  // true 
    Validator.isTLPhone('006703331234');  // true 
    Validator.isTLPhone('+6703331234');  // true 
    Validator.isTLPhone('1');  // false 
});

.latin1(s, [options]) method

Checks if a field only contains latin-1 alphanumeric characters.
Takes options for allowing singleline whitespace, cross-line whitespace and punctuation.

Accepts

  • s

    The validation string
  • options

    {}Optional configuration object. See createRegexp

Code

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 
});

.number(numb, [options]) method

Checks if a number is a valid

Accepts

  • numb

    The number
  • options

    Further options
  • options.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 number
  • options.min

    nullMinimum number
  • options.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
});

.unicode(s, [options]) method

Checks if a field contains unicode printable characters.

Accepts

  • s

    The validation string
  • options

    {}Optional configuration object. See createRegexp

.url(url, [full]) method

Checks if an url is valid

Accepts

  • url

    URL to be checked
  • full

    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 
});