Skip to content

Commit

Permalink
Added aliases and custom locales error
Browse files Browse the repository at this point in the history
  • Loading branch information
oss92 committed Feb 9, 2016
1 parent 57818d5 commit efda944
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#### HEAD

- Added Spanish, French, Portuguese and Dutch support for `isAlpha()` and `isAlphanumeric()`
([#492](https://github.com/chriso/validator.js/pull/492))
- Added a Brazilian locale to `isMobilePhone()`
([#489](https://github.com/chriso/validator.js/pull/489))
- Reject IPv4 addresses with invalid zero padding
Expand Down
43 changes: 43 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,24 @@ describe('Validators', function () {
});
});

it('should validate defined english aliases', function () {
test({
validator: 'isAlphanumeric'
, args: ['en-GB']
, valid: [
'abc123'
, 'ABC11'
]
, invalid: [
'abc '
, 'foo!!'
, 'ÄBC'
, 'FÜübar'
, 'Jön'
]
});
});

it('should validate german alphanumeric strings', function () {
test({
validator: 'isAlphanumeric'
Expand All @@ -613,6 +631,31 @@ describe('Validators', function () {
});
});

it('should validate spanish alphanumeric strings', function () {
test({
validator: 'isAlphanumeric'
, args: ['es-ES']
, valid: [
'ábcó123'
, 'ÁBCÓ11'
]
, invalid: [
'äca '
, 'abcß'
, 'föö!!'
]
});
});

it('should error on invalid locale', function () {
try {
validator.isAlphanumeric("abc123", "in-INVALID");
assert(false);
} catch (err) {
assert(true);
}
});

it('should validate numeric strings', function () {
test({
validator: 'isNumeric'
Expand Down
60 changes: 41 additions & 19 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,28 @@
var alpha = {
'en-US': /^[A-Z]+$/i,
'de-DE': /^[A-ZÄÖÜß]+$/i,
'es-ES': /^[A-ZÁÉÍÑÓÚÜ]+$/i,
'fr-FR': /^[A-ZÀÂÆÇÉÈÊËÏÎÔŒÙÛÜŸ]+$/i,
'nl-NL': /^[A-ZÉËÏÓÖÜ]+$/i,
'pt-PT': /^[A-ZÃÁÀÂÇÉÊÍÕÓÔÚÜ]+$/i
}
, alphanumeric = {
'en-US': /^[0-9A-Z]+$/i,
'de-DE': /^[0-9A-ZÄÖÜß]+$/i
}
, numeric = /^[-+]?[0-9]+$/
'de-DE': /^[0-9A-ZÄÖÜß]+$/i,
'es-ES': /^[0-9A-ZÁÉÍÑÓÚÜ]+$/i,
'fr-FR': /^[0-9A-ZÀÂÆÇÉÈÊËÏÎÔŒÙÛÜŸ]+$/i,
'nl-NL': /^[0-9A-ZÉËÏÓÖÜ]+$/i,
'pt-PT': /^[0-9A-ZÃÁÀÂÇÉÊÍÕÓÔÚÜ]+$/i
};

var englishLocales = ['AU', 'GB', 'HK', 'IN', 'NZ', 'ZA', 'ZM'];
for (var locale, i = 0; i < englishLocales.length; i++) {
locale = 'en-' + englishLocales[i];
alpha[locale] = alpha['en-US'];
alphanumeric[locale] = alphanumeric['en-US'];
}

var numeric = /^[-+]?[0-9]+$/
, int = /^(?:[-+]?(?:0|[1-9][0-9]*))$/
, float = /^(?:[-+]?(?:[0-9]+))?(?:\.[0-9]*)?(?:[eE][\+\-]?(?:[0-9]+))?$/
, hexadecimal = /^[0-9A-F]+$/i
Expand All @@ -89,27 +105,27 @@
var base64 = /^(?:[A-Z0-9+\/]{4})*(?:[A-Z0-9+\/]{2}==|[A-Z0-9+\/]{3}=|[A-Z0-9+\/]{4})$/i;

var phones = {
'zh-CN': /^(\+?0?86\-?)?((13\d|14[57]|15[^4,\D]|17[678]|18\d)\d{8}|170[059]\d{7})$/,
'zh-TW': /^(\+?886\-?|0)?9\d{8}$/,
'en-ZA': /^(\+?27|0)\d{9}$/,
'en-US': /^(\+?1)?[2-9]\d{2}[2-9](?!11)\d{6}$/,
'de-DE': /^(\+?49[ \.\-])?([\(]{1}[0-9]{1,6}[\)])?([0-9 \.\-\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$/,
'el-GR': /^(\+?30)?(69\d{8})$/,
'en-AU': /^(\+?61|0)4\d{8}$/,
'en-GB': /^(\+?44|0)7\d{9}$/,
'en-HK': /^(\+?852\-?)?[569]\d{3}\-?\d{4}$/,
'en-IN': /^(\+?91|0)?[789]\d{9}$/,
'en-NZ': /^(\+?64|0)2\d{7,9}$/,
'en-ZA': /^(\+?27|0)\d{9}$/,
'en-ZM': /^(\+?26)?09[567]\d{7}$/,
'es-ES': /^(\+?34)?(6\d{1}|7[1234])\d{7}$/,
'fi-FI': /^(\+?358|0)\s?(4(0|1|2|4|5)?|50)\s?(\d\s?){4,8}\d$/,
'fr-FR': /^(\+?33|0)[67]\d{8}$/,
'nb-NO': /^(\+?47)?[49]\d{7}$/,
'nn-NO': /^(\+?47)?[49]\d{7}$/,
'pt-BR': /^(\+?55|0)\-?[1-9]{2}\-?[2-9]{1}\d{3,4}\-?\d{4}$/,
'pt-PT': /^(\+?351)?9[1236]\d{7}$/,
'el-GR': /^(\+?30)?(69\d{8})$/,
'en-GB': /^(\+?44|0)7\d{9}$/,
'en-US': /^(\+?1)?[2-9]\d{2}[2-9](?!11)\d{6}$/,
'en-ZM': /^(\+?26)?09[567]\d{7}$/,
'ru-RU': /^(\+?7|8)?9\d{9}$/,
'nb-NO': /^(\+?47)?[49]\d{7}$/,
'nn-NO': /^(\+?47)?[49]\d{7}$/,
'vi-VN': /^(\+?84|0)?((1(2([0-9])|6([2-9])|88|99))|(9((?!5)[0-9])))([0-9]{7})$/,
'en-NZ': /^(\+?64|0)2\d{7,9}$/,
'en-IN': /^(\+?91|0)?[789]\d{9}$/,
'es-ES': /^(\+?34)?(6\d{1}|7[1234])\d{7}$/,
'de-DE': /^(\+?49[ \.\-])?([\(]{1}[0-9]{1,6}[\)])?([0-9 \.\-\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$/,
'fi-FI': /^(\+?358|0)\s?(4(0|1|2|4|5)?|50)\s?(\d\s?){4,8}\d$/
'zh-CN': /^(\+?0?86\-?)?((13\d|14[57]|15[^4,\D]|17[678]|18\d)\d{8}|170[059]\d{7})$/,
'zh-TW': /^(\+?886\-?|0)?9\d{8}$/
};

// from http://goo.gl/0ejHHW
Expand Down Expand Up @@ -451,12 +467,18 @@

validator.isAlpha = function (str, locale) {
locale = locale || 'en-US';
return alpha[locale].test(str);
if (locale in alpha) {
return alpha[locale].test(str);
}
throw new Error('Invalid locale \'' + locale + '\'');
};

validator.isAlphanumeric = function (str, locale) {
locale = locale || 'en-US';
return alphanumeric[locale].test(str);
if (locale in alphanumeric) {
return alphanumeric[locale].test(str);
}
throw new Error('Invalid locale \'' + locale + '\'');
};

validator.isNumeric = function (str) {
Expand Down

0 comments on commit efda944

Please sign in to comment.