diff --git a/README.md b/README.md index 8eebd3b27..41097e371 100644 --- a/README.md +++ b/README.md @@ -36,8 +36,8 @@ $ bower install validator-js - **contains(str, seed)** - check if the string contains the seed. - **equals(str, comparison)** - check if the string matches the comparison. - **isAfter(str [, date])** - check if the string is a date that's after the specified date (defaults to now). -- **isAlpha(str)** - check if the string contains only letters (a-zA-Z). -- **isAlphanumeric(str)** - check if the string contains only letters and numbers. +- **isAlpha(str [, locale])** - check if the string contains only letters (a-zA-Z). +- **isAlphanumeric(str [, locale])** - check if the string contains only letters and numbers. - **isAscii(str)** - check if the string contains ASCII chars only. - **isBase64(str)** - check if a string is base64 encoded. - **isBefore(str [, date])** - check if the string is a date that's before the specified date. diff --git a/test/validators.js b/test/validators.js index dc54e8fe4..276981271 100644 --- a/test/validators.js +++ b/test/validators.js @@ -551,6 +551,28 @@ describe('Validators', function () { 'abc1' , ' foo ' , '' + , 'ÄBC' + , 'FÜübar' + , 'Jön' + , 'Heiß' + ] + }); + }); + + it('should validate german alpha strings', function () { + test({ + validator: 'isAlpha' + , args: ['de-DE'] + , valid: [ + 'äbc' + , 'ÄBC' + , 'FöÖbär' + , 'Heiß' + ] + , invalid: [ + 'äbc1' + , ' föö ' + , '' ] }); }); @@ -565,6 +587,24 @@ describe('Validators', function () { , invalid: [ 'abc ' , 'foo!!' + , 'ÄBC' + , 'FÜübar' + , 'Jön' + ] + }); + }); + + it('should validate german alphanumeric strings', function () { + test({ + validator: 'isAlphanumeric' + , args: ['de-DE'] + , valid: [ + 'äbc123' + , 'ÄBC11' + ] + , invalid: [ + 'äca ' + , 'föö!!' ] }); }); @@ -1333,6 +1373,22 @@ describe('Validators', function () { }); it('should validate mobile phone number', function () { + test({ + validator: 'isMobilePhone' + , valid: [ + '+49 (0) 123 456 789' + , '+49 (0) 123 456789' + , '0123/4567890' + , '+49 01234567890' + , '01234567890' + ] + , invalid: [ + '' + , 'Vml2YW11cyBmZXJtZtesting123' + ], + args: ['de-DE'] + }); + test({ validator: 'isMobilePhone' , valid: [ diff --git a/validator.js b/validator.js index 900e7180b..a1abef682 100644 --- a/validator.js +++ b/validator.js @@ -64,8 +64,14 @@ , all: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i }; - var alpha = /^[A-Z]+$/i - , alphanumeric = /^[0-9A-Z]+$/i + var alpha = { + 'en-US': /^[A-Z]+$/i, + 'de-DE': /^[A-ZÄÖÜß]+$/i, + } + , alphanumeric = { + 'en-US': /^[0-9A-Z]+$/i, + 'de-DE': /^[0-9A-ZÄÖÜß]+$/i + } , numeric = /^[-+]?[0-9]+$/ , int = /^(?:[-+]?(?:0|[1-9][0-9]*))$/ , float = /^(?:[-+]?(?:[0-9]+))?(?:\.[0-9]*)?(?:[eE][\+\-]?(?:[0-9]+))?$/ @@ -99,7 +105,8 @@ 'nn-NO': /^(\+?47)?[49]\d{7}$/, 'vi-VN': /^(0|\+?84)?((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}$/ + 'en-IN': /^(\+?91|0)?[789]\d{9}$/, + 'de-DE': /^(\+?49[ \.\-])?([\(]{1}[0-9]{1,6}[\)])?([0-9 \.\-\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$/ }; // from http://goo.gl/0ejHHW @@ -414,12 +421,14 @@ return (['true', 'false', '1', '0'].indexOf(str) >= 0); }; - validator.isAlpha = function (str) { - return alpha.test(str); + validator.isAlpha = function (str, locale) { + locale = locale || 'en-US'; + return alpha[locale].test(str); }; - validator.isAlphanumeric = function (str) { - return alphanumeric.test(str); + validator.isAlphanumeric = function (str, locale) { + locale = locale || 'en-US'; + return alphanumeric[locale].test(str); }; validator.isNumeric = function (str) {