Skip to content

Commit 681067a

Browse files
committed
Added aliases and custom locales error
1 parent 57818d5 commit 681067a

File tree

3 files changed

+94
-19
lines changed

3 files changed

+94
-19
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
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

test/validators.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,23 @@ describe('Validators', function () {
598598
});
599599
});
600600

601+
it('should validate defined english aliases', function () {
602+
test({
603+
validator: 'isAlphanumeric'
604+
, valid: [
605+
'abc123'
606+
, 'ABC11'
607+
]
608+
, invalid: [
609+
'abc '
610+
, 'foo!!'
611+
, 'ÄBC'
612+
, 'FÜübar'
613+
, 'Jön'
614+
]
615+
});
616+
});
617+
601618
it('should validate german alphanumeric strings', function () {
602619
test({
603620
validator: 'isAlphanumeric'
@@ -613,6 +630,31 @@ describe('Validators', function () {
613630
});
614631
});
615632

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

validator.js

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,37 @@
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+
alpha['en-AU'] = alpha['en-US'];
85+
alpha['en-GB'] = alpha['en-US'];
86+
alpha['en-HK'] = alpha['en-US'];
87+
alpha['en-IN'] = alpha['en-US'];
88+
alpha['en-NZ'] = alpha['en-US'];
89+
alpha['en-ZA'] = alpha['en-US'];
90+
alpha['en-ZM'] = alpha['en-US'];
91+
92+
alphanumeric['en-AU'] = alphanumeric['en-US'];
93+
alphanumeric['en-GB'] = alphanumeric['en-US'];
94+
alphanumeric['en-HK'] = alphanumeric['en-US'];
95+
alphanumeric['en-IN'] = alphanumeric['en-US'];
96+
alphanumeric['en-NZ'] = alphanumeric['en-US'];
97+
alphanumeric['en-ZA'] = alphanumeric['en-US'];
98+
alphanumeric['en-ZM'] = alphanumeric['en-US'];
99+
100+
var numeric = /^[-+]?[0-9]+$/
76101
, int = /^(?:[-+]?(?:0|[1-9][0-9]*))$/
77102
, float = /^(?:[-+]?(?:[0-9]+))?(?:\.[0-9]*)?(?:[eE][\+\-]?(?:[0-9]+))?$/
78103
, hexadecimal = /^[0-9A-F]+$/i
@@ -89,27 +114,27 @@
89114
var base64 = /^(?:[A-Z0-9+\/]{4})*(?:[A-Z0-9+\/]{2}==|[A-Z0-9+\/]{3}=|[A-Z0-9+\/]{4})$/i;
90115

91116
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}$/,
117+
'en-US': /^(\+?1)?[2-9]\d{2}[2-9](?!11)\d{6}$/,
118+
'de-DE': /^(\+?49[ \.\-])?([\(]{1}[0-9]{1,6}[\)])?([0-9 \.\-\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$/,
119+
'el-GR': /^(\+?30)?(69\d{8})$/,
95120
'en-AU': /^(\+?61|0)4\d{8}$/,
121+
'en-GB': /^(\+?44|0)7\d{9}$/,
96122
'en-HK': /^(\+?852\-?)?[569]\d{3}\-?\d{4}$/,
123+
'en-IN': /^(\+?91|0)?[789]\d{9}$/,
124+
'en-NZ': /^(\+?64|0)2\d{7,9}$/,
125+
'en-ZA': /^(\+?27|0)\d{9}$/,
126+
'en-ZM': /^(\+?26)?09[567]\d{7}$/,
127+
'es-ES': /^(\+?34)?(6\d{1}|7[1234])\d{7}$/,
128+
'fi-FI': /^(\+?358|0)\s?(4(0|1|2|4|5)?|50)\s?(\d\s?){4,8}\d$/,
97129
'fr-FR': /^(\+?33|0)[67]\d{8}$/,
130+
'nb-NO': /^(\+?47)?[49]\d{7}$/,
131+
'nn-NO': /^(\+?47)?[49]\d{7}$/,
98132
'pt-BR': /^(\+?55|0)\-?[1-9]{2}\-?[2-9]{1}\d{3,4}\-?\d{4}$/,
99133
'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}$/,
104134
'ru-RU': /^(\+?7|8)?9\d{9}$/,
105-
'nb-NO': /^(\+?47)?[49]\d{7}$/,
106-
'nn-NO': /^(\+?47)?[49]\d{7}$/,
107135
'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$/
136+
'zh-CN': /^(\+?0?86\-?)?((13\d|14[57]|15[^4,\D]|17[678]|18\d)\d{8}|170[059]\d{7})$/,
137+
'zh-TW': /^(\+?886\-?|0)?9\d{8}$/
113138
};
114139

115140
// from http://goo.gl/0ejHHW
@@ -451,12 +476,18 @@
451476

452477
validator.isAlpha = function (str, locale) {
453478
locale = locale || 'en-US';
454-
return alpha[locale].test(str);
479+
if (locale in alpha) {
480+
return alpha[locale].test(str);
481+
}
482+
throw new Error('Invalid locale \'' + locale + '\'');
455483
};
456484

457485
validator.isAlphanumeric = function (str, locale) {
458486
locale = locale || 'en-US';
459-
return alphanumeric[locale].test(str);
487+
if (locale in alphanumeric) {
488+
return alphanumeric[locale].test(str);
489+
}
490+
throw new Error('Invalid locale \'' + locale + '\'');
460491
};
461492

462493
validator.isNumeric = function (str) {

0 commit comments

Comments
 (0)