From 0bd1d138a8d37baf6f31b71b99eaa6e827b51f3b Mon Sep 17 00:00:00 2001 From: simivar Date: Mon, 29 Feb 2016 16:16:07 +0100 Subject: [PATCH 1/5] Added new module: Poland New module "Poland" with NIP, PESEL and REGON validators --- README.md | 5 + src/lang/pl.js | 5 +- src/main/utils.js | 5 +- src/modules/poland.js | 115 ++++++++++++++++++++++ test/form.html | 219 +++++++++++++++++++++++------------------- 5 files changed, 248 insertions(+), 101 deletions(-) create mode 100644 src/modules/poland.js diff --git a/README.md b/README.md index ca0d515..3c9022f 100644 --- a/README.md +++ b/README.md @@ -123,6 +123,11 @@ Read the documentation for the UK module at [http://formvalidator.net/#uk-valida * **brphone** — Validate a brazilian telephone number * **cep** * **cpf** + +### Module: poland + * **plpesel** - validate polish personal identity number (in Polish identity cards) + * **plnip** - validate polish VAT identification number + * **plregon** - validate polish bussiness identity number ### Module: sanitation * **trim** diff --git a/src/lang/pl.js b/src/lang/pl.js index ea1ebfe..811cd4c 100644 --- a/src/lang/pl.js +++ b/src/lang/pl.js @@ -58,7 +58,10 @@ imageRatioNotAccepted: 'Proporcje obrazu są niepoprawne', badBrazilTelephoneAnswer: 'Wprowadzono niepoprawny numer telefonu', badBrazilCEPAnswer: 'Wprowadzono niepoprawny CEP', - badBrazilCPFAnswer: 'Wprowadzono niepoprawny CPF' + badBrazilCPFAnswer: 'Wprowadzono niepoprawny CPF', + badPlPesel: 'Wprowadzono niepoprawny numer PESEL', + badPlNip: 'Wprowadzono niepoprawny numer NIP', + badPlRegon: 'Wprowadzono niepoprawny numer REGON' }; }); diff --git a/src/main/utils.js b/src/main/utils.js index 550465f..bb62c65 100644 --- a/src/main/utils.js +++ b/src/main/utils.js @@ -825,7 +825,10 @@ imageRatioNotAccepted : 'Image ratio is not be accepted', badBrazilTelephoneAnswer: 'The phone number entered is invalid', badBrazilCEPAnswer: 'The CEP entered is invalid', - badBrazilCPFAnswer: 'The CPF entered is invalid' + badBrazilCPFAnswer: 'The CPF entered is invalid', + badPlPesel: 'The PESEL entered is invalid', + badPlNip: 'The NIP entered is invalid', + badPlRegon: 'The REGON entered is invalid' } }); diff --git a/src/modules/poland.js b/src/modules/poland.js new file mode 100644 index 0000000..aa15954 --- /dev/null +++ b/src/modules/poland.js @@ -0,0 +1,115 @@ +/** + * jQuery Form Validator Module: Poland + * ------------------------------------------ + * Created by simivar + * + * This form validation module adds validators typically used on + * websites in Poland. This module adds the following validators: + * - plpesel + * + * @website http://formvalidator.net/#poland-validators + * @license MIT + */ +(function($) { + + /** + * PL PESEL - polish personal identity number (in Polish identity cards) validator + */ + $.formUtils.addValidator({ + name: 'plpesel', + validatorFunction: function(pesel){ + var weights = [1, 3, 7, 9, 1, 3, 7, 9, 1, 3], + checkSum = 0, checkDigit = 0; + + if( /\d{11}/.test( pesel ) && pesel.length === 11 ){ + for (var i = 0; i < 10; i++) { + checkSum += pesel[ i ] * weights[ i ]; + } + + if( checkSum % 10 !== 0 ){ + checkDigit = 10 - (checkSum % 10); + } + + if( parseInt( pesel.charAt( 10 ) ) === checkDigit ){ + return true; + } + } + + return false; + }, + errorMessage: '', + errorMessageKey: 'badPlPesel' + }); + + /** + * PL NIP - polish VAT identification number validator + */ + $.formUtils.addValidator({ + name: 'plnip', + validatorFunction: function(nip){ + var weights = [6, 5, 7, 2, 3, 4, 5, 6, 7], + checkSum = 0; + + if( /\d{10}/.test( nip ) && nip.length === 10 ){ + for (var i = 0; i < 9; i++) { + checkSum += nip[ i ] * weights[ i ]; + } + + if( parseInt( nip.charAt( 9 ) ) === checkSum % 11 ){ + return true; + } + } + + return false; + }, + errorMessage: '', + errorMessageKey: 'badPlNip' + }); + + /** + * PL REGON - polish bussiness identity number validator + */ + $.formUtils.addValidator({ + name: 'plregon', + validatorFunction: function(regon){ + var weightsNine = [8, 9, 2, 3, 4, 5, 6, 7], + weightsFourteen = [2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8], + checkSum = 0, checkDigit = 0; + + if( /(\d{14}|\d{9})/.test( regon ) && ( regon.length === 9 || regon.length === 14 ) ){ + for (var i = 0; i < 8; i++) { + checkSum += regon[ i ] * weightsNine[ i ]; + } + + if( checkSum % 11 !== 10 ){ + checkDigit = checkSum % 11; + } + + if( parseInt( regon.charAt( 8 ) ) === checkDigit ){ + if( regon.length === 14 ){ + checkSum = 0; + + for (i = 0; i < 13; i++) { + checkSum += regon[ i ] * weightsFourteen[ i ]; + } + + if( checkSum % 11 !== 10 ){ + checkDigit = checkSum % 11; + } + + if( parseInt( regon.charAt( 13 ) ) === checkDigit ){ + return true; + } + } else { + return true; + } + } + } + + return false; + }, + errorMessage: '', + errorMessageKey: 'badPlRegon' + }); + +})(jQuery); \ No newline at end of file diff --git a/test/form.html b/test/form.html index 0f3e308..966b7c9 100644 --- a/test/form.html +++ b/test/form.html @@ -101,7 +101,7 @@ data-validation-ignore="$€" data-validation-allowing="range[0;10], float" data-validation-decimal-separator="," - /> + />
@@ -184,7 +184,7 @@ data-validation="extension required" data-validation-error-msg="You must write a file name with extension jpg|png|ico" data-validation-allowing="jpg, png, ico" - /> + />
- +
@@ -278,19 +278,19 @@
+ data-validation="alphanumeric" + data-validation-error-msg="Invalid above..." + data-validation-depends-on="checker" />
Checkbox
+ data-validation="alphanumeric" + data-validation-error-msg="Invalid below..." + data-validation-depends-on="checker" + data-validation-depends-on-value="99" />
@@ -368,103 +368,124 @@

Validation Module: Brazil

+
+
+

Validation Module: Poland

+
+ + +
+
+ + +
+
+ + +
+

+ + +

+
From 92ffd3f3f47e0679c9babdbd72bb6201276b917e Mon Sep 17 00:00:00 2001 From: simivar Date: Mon, 29 Feb 2016 16:19:31 +0100 Subject: [PATCH 2/5] read.me decoration --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3c9022f..e6979f9 100644 --- a/README.md +++ b/README.md @@ -120,14 +120,14 @@ Read the documentation for the Swedish module at [http://formvalidator.net/#swed Read the documentation for the UK module at [http://formvalidator.net/#uk-validators](http://formvalidator.net/#uk-validators) ### Module: brazil - * **brphone** — Validate a brazilian telephone number + * **brphone** — *Validate a brazilian telephone number* * **cep** * **cpf** ### Module: poland - * **plpesel** - validate polish personal identity number (in Polish identity cards) - * **plnip** - validate polish VAT identification number - * **plregon** - validate polish bussiness identity number + * **plpesel** - *validate polish personal identity number (in Polish identity cards)* + * **plnip** - *validate polish VAT identification number* + * **plregon** - *validate polish bussiness identity number* ### Module: sanitation * **trim** From 195834778b45b2b10ef3e4da1dc47cd7b125af8c Mon Sep 17 00:00:00 2001 From: simivar Date: Mon, 29 Feb 2016 17:01:40 +0100 Subject: [PATCH 3/5] Test for Poland module --- test/qunit.html | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/test/qunit.html b/test/qunit.html index 0459f7e..3a080d7 100644 --- a/test/qunit.html +++ b/test/qunit.html @@ -921,6 +921,71 @@ ); }); + /* + * POLAND VALIDATION + */ + test("Polish PESEL validation", function(){ + + clearForm(); + + var links = [ + {val:'11310810104', isValid:true}, + {val:'61030604284', isValid:true}, + {val:'04212602595', isValid:true}, + {val:'15251410340', isValid:true}, + {val:'65788768888', isValid:false}, + {val:'12306789761', isValid:false}, + {val:'73896757737', isValid:false} + ]; + + $.each(links, function(i, obj) { + runTest(obj, 'plpesel'); + }); + }); + + test("Polish NIP validation", function(){ + + clearForm(); + + var links = [ + {val:'2236473023', isValid:true}, + {val:'6650872044', isValid:true}, + {val:'4622952623', isValid:true}, + {val:'7154300218', isValid:true}, + {val:'3467567875', isValid:false}, + {val:'8676876861', isValid:false}, + {val:'7568876872', isValid:false} + ]; + + $.each(links, function(i, obj) { + runTest(obj, 'plnip'); + }); + }); + + test("Polish REGON validation", function(){ + + clearForm(); + + var links = [ + {val:'714379260', isValid:true}, + {val:'951591192', isValid:true}, + {val:'018905922', isValid:true}, + {val:'134533221', isValid:true}, + {val:'21115383251129', isValid:true}, + {val:'05768114548441', isValid:true}, + {val:'71291247784457', isValid:true}, + {val:'87244008641050', isValid:true}, + {val:'8724400864105', isValid:false}, + {val:'010218305', isValid:false}, + {val:'773645358', isValid:false} + ]; + + $.each(links, function(i, obj) { + runTest(obj, 'plregon'); + }); + }); + + // TODO: Write more tests... } From a156613a92d5d7aed7268fd150cbc00138dbe495 Mon Sep 17 00:00:00 2001 From: simivar Date: Mon, 29 Feb 2016 17:56:34 +0100 Subject: [PATCH 4/5] Load poland module in qunit --- test/qunit.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/qunit.html b/test/qunit.html index 3a080d7..153dfca 100644 --- a/test/qunit.html +++ b/test/qunit.html @@ -991,7 +991,7 @@ } $.validate({ - modules : 'security, location, sweden, file, date, sanitize, uk', + modules : 'security, location, sweden, file, date, sanitize, uk, poland', onModulesLoaded: function( $form ) { if( window.console && window.console.log ) console.log('About to run all tests'); From 8e5118c2df3385ad4eecb026612ebfc6a8e530e3 Mon Sep 17 00:00:00 2001 From: simivar Date: Mon, 29 Feb 2016 18:19:20 +0100 Subject: [PATCH 5/5] fixed test case --- test/qunit.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/qunit.html b/test/qunit.html index 153dfca..3a5c1d1 100644 --- a/test/qunit.html +++ b/test/qunit.html @@ -974,7 +974,7 @@ {val:'21115383251129', isValid:true}, {val:'05768114548441', isValid:true}, {val:'71291247784457', isValid:true}, - {val:'87244008641050', isValid:true}, + {val:'87244008641050', isValid:false}, {val:'8724400864105', isValid:false}, {val:'010218305', isValid:false}, {val:'773645358', isValid:false}