Skip to content

Commit 122ce51

Browse files
committed
Merge pull request #492 from oss92/locales_and_locales_error
Added aliases and custom locales error
2 parents 57818d5 + b7676ad commit 122ce51

File tree

3 files changed

+86
-19
lines changed

3 files changed

+86
-19
lines changed

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#### HEAD
22

3+
- Added Spanish, French, Portuguese and Dutch support for `isAlpha()` and `isAlphanumeric()`
4+
([#492](https://github.com/chriso/validator.js/pull/492))
35
- Added a Brazilian locale to `isMobilePhone()`
46
([#489](https://github.com/chriso/validator.js/pull/489))
57
- Reject IPv4 addresses with invalid zero padding

Diff for: test/validators.js

+43
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,24 @@ describe('Validators', function () {
598598
});
599599
});
600600

601+
it('should validate defined english aliases', function () {
602+
test({
603+
validator: 'isAlphanumeric'
604+
, args: ['en-GB']
605+
, valid: [
606+
'abc123'
607+
, 'ABC11'
608+
]
609+
, invalid: [
610+
'abc '
611+
, 'foo!!'
612+
, 'ÄBC'
613+
, 'FÜübar'
614+
, 'Jön'
615+
]
616+
});
617+
});
618+
601619
it('should validate german alphanumeric strings', function () {
602620
test({
603621
validator: 'isAlphanumeric'
@@ -613,6 +631,31 @@ describe('Validators', function () {
613631
});
614632
});
615633

634+
it('should validate spanish alphanumeric strings', function () {
635+
test({
636+
validator: 'isAlphanumeric'
637+
, args: ['es-ES']
638+
, valid: [
639+
'ábcó123'
640+
, 'ÁBCÓ11'
641+
]
642+
, invalid: [
643+
'äca '
644+
, 'abcß'
645+
, 'föö!!'
646+
]
647+
});
648+
});
649+
650+
it('should error on invalid locale', function () {
651+
try {
652+
validator.isAlphanumeric("abc123", "in-INVALID");
653+
assert(false);
654+
} catch (err) {
655+
assert(true);
656+
}
657+
});
658+
616659
it('should validate numeric strings', function () {
617660
test({
618661
validator: 'isNumeric'

Diff for: validator.js

+41-19
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,28 @@
6767
var alpha = {
6868
'en-US': /^[A-Z]+$/i,
6969
'de-DE': /^[A-ZÄÖÜß]+$/i,
70+
'es-ES': /^[A-ZÁÉÍÑÓÚÜ]+$/i,
71+
'fr-FR': /^[A-ZÀÂÆÇÉÈÊËÏÎÔŒÙÛÜŸ]+$/i,
72+
'nl-NL': /^[A-ZÉËÏÓÖÜ]+$/i,
73+
'pt-PT': /^[A-ZÃÁÀÂÇÉÊÍÕÓÔÚÜ]+$/i
7074
}
7175
, alphanumeric = {
7276
'en-US': /^[0-9A-Z]+$/i,
73-
'de-DE': /^[0-9A-ZÄÖÜß]+$/i
74-
}
75-
, numeric = /^[-+]?[0-9]+$/
77+
'de-DE': /^[0-9A-ZÄÖÜß]+$/i,
78+
'es-ES': /^[0-9A-ZÁÉÍÑÓÚÜ]+$/i,
79+
'fr-FR': /^[0-9A-ZÀÂÆÇÉÈÊËÏÎÔŒÙÛÜŸ]+$/i,
80+
'nl-NL': /^[0-9A-ZÉËÏÓÖÜ]+$/i,
81+
'pt-PT': /^[0-9A-ZÃÁÀÂÇÉÊÍÕÓÔÚÜ]+$/i
82+
};
83+
84+
var englishLocales = ['AU', 'GB', 'HK', 'IN', 'NZ', 'ZA', 'ZM'];
85+
for (var locale, i = 0; i < englishLocales.length; i++) {
86+
locale = 'en-' + englishLocales[i];
87+
alpha[locale] = alpha['en-US'];
88+
alphanumeric[locale] = alphanumeric['en-US'];
89+
}
90+
91+
var numeric = /^[-+]?[0-9]+$/
7692
, int = /^(?:[-+]?(?:0|[1-9][0-9]*))$/
7793
, float = /^(?:[-+]?(?:[0-9]+))?(?:\.[0-9]*)?(?:[eE][\+\-]?(?:[0-9]+))?$/
7894
, hexadecimal = /^[0-9A-F]+$/i
@@ -89,27 +105,27 @@
89105
var base64 = /^(?:[A-Z0-9+\/]{4})*(?:[A-Z0-9+\/]{2}==|[A-Z0-9+\/]{3}=|[A-Z0-9+\/]{4})$/i;
90106

91107
var phones = {
92-
'zh-CN': /^(\+?0?86\-?)?((13\d|14[57]|15[^4,\D]|17[678]|18\d)\d{8}|170[059]\d{7})$/,
93-
'zh-TW': /^(\+?886\-?|0)?9\d{8}$/,
94-
'en-ZA': /^(\+?27|0)\d{9}$/,
108+
'en-US': /^(\+?1)?[2-9]\d{2}[2-9](?!11)\d{6}$/,
109+
'de-DE': /^(\+?49[ \.\-])?([\(]{1}[0-9]{1,6}[\)])?([0-9 \.\-\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$/,
110+
'el-GR': /^(\+?30)?(69\d{8})$/,
95111
'en-AU': /^(\+?61|0)4\d{8}$/,
112+
'en-GB': /^(\+?44|0)7\d{9}$/,
96113
'en-HK': /^(\+?852\-?)?[569]\d{3}\-?\d{4}$/,
114+
'en-IN': /^(\+?91|0)?[789]\d{9}$/,
115+
'en-NZ': /^(\+?64|0)2\d{7,9}$/,
116+
'en-ZA': /^(\+?27|0)\d{9}$/,
117+
'en-ZM': /^(\+?26)?09[567]\d{7}$/,
118+
'es-ES': /^(\+?34)?(6\d{1}|7[1234])\d{7}$/,
119+
'fi-FI': /^(\+?358|0)\s?(4(0|1|2|4|5)?|50)\s?(\d\s?){4,8}\d$/,
97120
'fr-FR': /^(\+?33|0)[67]\d{8}$/,
121+
'nb-NO': /^(\+?47)?[49]\d{7}$/,
122+
'nn-NO': /^(\+?47)?[49]\d{7}$/,
98123
'pt-BR': /^(\+?55|0)\-?[1-9]{2}\-?[2-9]{1}\d{3,4}\-?\d{4}$/,
99124
'pt-PT': /^(\+?351)?9[1236]\d{7}$/,
100-
'el-GR': /^(\+?30)?(69\d{8})$/,
101-
'en-GB': /^(\+?44|0)7\d{9}$/,
102-
'en-US': /^(\+?1)?[2-9]\d{2}[2-9](?!11)\d{6}$/,
103-
'en-ZM': /^(\+?26)?09[567]\d{7}$/,
104125
'ru-RU': /^(\+?7|8)?9\d{9}$/,
105-
'nb-NO': /^(\+?47)?[49]\d{7}$/,
106-
'nn-NO': /^(\+?47)?[49]\d{7}$/,
107126
'vi-VN': /^(\+?84|0)?((1(2([0-9])|6([2-9])|88|99))|(9((?!5)[0-9])))([0-9]{7})$/,
108-
'en-NZ': /^(\+?64|0)2\d{7,9}$/,
109-
'en-IN': /^(\+?91|0)?[789]\d{9}$/,
110-
'es-ES': /^(\+?34)?(6\d{1}|7[1234])\d{7}$/,
111-
'de-DE': /^(\+?49[ \.\-])?([\(]{1}[0-9]{1,6}[\)])?([0-9 \.\-\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$/,
112-
'fi-FI': /^(\+?358|0)\s?(4(0|1|2|4|5)?|50)\s?(\d\s?){4,8}\d$/
127+
'zh-CN': /^(\+?0?86\-?)?((13\d|14[57]|15[^4,\D]|17[678]|18\d)\d{8}|170[059]\d{7})$/,
128+
'zh-TW': /^(\+?886\-?|0)?9\d{8}$/
113129
};
114130

115131
// from http://goo.gl/0ejHHW
@@ -451,12 +467,18 @@
451467

452468
validator.isAlpha = function (str, locale) {
453469
locale = locale || 'en-US';
454-
return alpha[locale].test(str);
470+
if (locale in alpha) {
471+
return alpha[locale].test(str);
472+
}
473+
throw new Error('Invalid locale \'' + locale + '\'');
455474
};
456475

457476
validator.isAlphanumeric = function (str, locale) {
458477
locale = locale || 'en-US';
459-
return alphanumeric[locale].test(str);
478+
if (locale in alphanumeric) {
479+
return alphanumeric[locale].test(str);
480+
}
481+
throw new Error('Invalid locale \'' + locale + '\'');
460482
};
461483

462484
validator.isNumeric = function (str) {

0 commit comments

Comments
 (0)