From e4fcd997ac47a69e9d1870eccfc78e7b663a637b Mon Sep 17 00:00:00 2001 From: Marc Binder Date: Fri, 22 Jan 2016 10:38:46 +0100 Subject: [PATCH 1/4] Support german stuff --- test/validators.js | 22 ++++++++++++++++++++++ validator.js | 7 ++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/test/validators.js b/test/validators.js index dc54e8fe4..ae749913b 100644 --- a/test/validators.js +++ b/test/validators.js @@ -546,6 +546,9 @@ describe('Validators', function () { 'abc' , 'ABC' , 'FoObar' + , 'ÄBC' + , 'FÜübar' + , 'Jön' ] , invalid: [ 'abc1' @@ -561,6 +564,9 @@ describe('Validators', function () { , valid: [ 'abc123' , 'ABC11' + , 'ÄBC' + , 'FÜübar' + , 'Jön' ] , invalid: [ 'abc ' @@ -1333,6 +1339,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..99502cf4d 100644 --- a/validator.js +++ b/validator.js @@ -64,8 +64,8 @@ , 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 = /^[A-ZÄÖÜ]+$/i + , alphanumeric = /^[0-9A-ZÄÖÜ]+$/i , numeric = /^[-+]?[0-9]+$/ , int = /^(?:[-+]?(?:0|[1-9][0-9]*))$/ , float = /^(?:[-+]?(?:[0-9]+))?(?:\.[0-9]*)?(?:[eE][\+\-]?(?:[0-9]+))?$/ @@ -99,7 +99,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': /^([\+][0-9]{1,3}[ \.\-])?([\(]{1}[0-9]{1,6}[\)])?([0-9 \.\-\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$/ }; // from http://goo.gl/0ejHHW From 18814663a68eb4b6c156a71767da9f4d4beb5ec8 Mon Sep 17 00:00:00 2001 From: Marc Binder Date: Mon, 1 Feb 2016 16:52:10 +0100 Subject: [PATCH 2/4] isAlpha and isAlphanumeric localization --- test/validators.js | 40 ++++++++++++++++++++++++++++++++++++---- validator.js | 22 +++++++++++++++------- 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/test/validators.js b/test/validators.js index ae749913b..a56004f30 100644 --- a/test/validators.js +++ b/test/validators.js @@ -546,13 +546,30 @@ describe('Validators', function () { 'abc' , 'ABC' , 'FoObar' + ] + , invalid: [ + 'abc1' + , ' foo ' + , '' , 'ÄBC' , 'FÜübar' , 'Jön' ] + }); + }); + + it('should validate german alpha strings', function () { + test({ + validator: 'isAlpha' + , args: ['de-DE'] + , valid: [ + 'äbc' + , 'ÄBC' + , 'FöÖbär' + ] , invalid: [ - 'abc1' - , ' foo ' + 'äbc1' + , ' föö ' , '' ] }); @@ -564,13 +581,28 @@ describe('Validators', function () { , valid: [ 'abc123' , 'ABC11' + ] + , 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: [ - 'abc ' - , 'foo!!' + 'äca ' + , 'föö!!' ] }); }); diff --git a/validator.js b/validator.js index 99502cf4d..4d4d6aedc 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]+))?$/ @@ -100,7 +106,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}$/, - 'de-DE': /^([\+][0-9]{1,3}[ \.\-])?([\(]{1}[0-9]{1,6}[\)])?([0-9 \.\-\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$/ + 'de-DE': /^(\+?49[ \.\-])?([\(]{1}[0-9]{1,6}[\)])?([0-9 \.\-\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$/ }; // from http://goo.gl/0ejHHW @@ -415,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) { From 592b24a135920c3311e4e053a8e81946ea4a890a Mon Sep 17 00:00:00 2001 From: Marc Binder Date: Wed, 3 Feb 2016 09:24:35 +0100 Subject: [PATCH 3/4] =?UTF-8?q?Support=20'=C3=9F'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/validators.js | 2 ++ validator.js | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/test/validators.js b/test/validators.js index a56004f30..276981271 100644 --- a/test/validators.js +++ b/test/validators.js @@ -554,6 +554,7 @@ describe('Validators', function () { , 'ÄBC' , 'FÜübar' , 'Jön' + , 'Heiß' ] }); }); @@ -566,6 +567,7 @@ describe('Validators', function () { 'äbc' , 'ÄBC' , 'FöÖbär' + , 'Heiß' ] , invalid: [ 'äbc1' diff --git a/validator.js b/validator.js index 4d4d6aedc..a1abef682 100644 --- a/validator.js +++ b/validator.js @@ -66,11 +66,11 @@ var alpha = { 'en-US': /^[A-Z]+$/i, - 'de-DE': /^[A-ZÄÖÜ]+$/i, + 'de-DE': /^[A-ZÄÖÜß]+$/i, } , alphanumeric = { 'en-US': /^[0-9A-Z]+$/i, - 'de-DE': /^[0-9A-ZÄÖÜ]+$/i + 'de-DE': /^[0-9A-ZÄÖÜß]+$/i } , numeric = /^[-+]?[0-9]+$/ , int = /^(?:[-+]?(?:0|[1-9][0-9]*))$/ From fe697ac18f26b3c132bd4c7f804dd61d7ed578f8 Mon Sep 17 00:00:00 2001 From: Marc Binder Date: Wed, 3 Feb 2016 09:28:08 +0100 Subject: [PATCH 4/4] Update README.md file --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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.