From 44fe9de050504a46bb0eb975c31bc4b0f3b6f516 Mon Sep 17 00:00:00 2001 From: "Ghislain B." Date: Fri, 2 Jan 2015 01:14:56 -0500 Subject: [PATCH] Added Input Match (confirmation) Validator - Added Match validator (ex.: password confirmation) - Added throw error on validators (between_len & between_num) if it doesn't include 2 params --- angular-validation.js | 23 +++++++++++++++++ legacy-angular1.2.x/angular-validation.js | 23 +++++++++++++++++ ...gular-translate-loader-static-files.min.js | 2 +- locales/validation/en.json | 5 +++- locales/validation/fr.json | 3 +++ readme.md | 25 +++++++++++++------ templates/testingForm.html | 10 ++++++++ 7 files changed, 82 insertions(+), 9 deletions(-) diff --git a/angular-validation.js b/angular-validation.js index bfc42dc..69bffa6 100644 --- a/angular-validation.js +++ b/angular-validation.js @@ -107,6 +107,9 @@ case "betweenLen" : case "between_len" : var range = params[1].split(','); + if (range.length !== 2) { + throw "This validation must include exactly 2 params separated by a comma (,) ex.: between_len:1,5"; + } validators[i] = { pattern: "^.{" + range[0] + "," + range[1] + "}$", message: "INVALID_BETWEEN_CHAR", @@ -117,6 +120,9 @@ case "betweenNum" : case "between_num" : var range = params[1].split(','); + if (range.length !== 2) { + throw "This validation must include exactly 2 params separated by a comma (,) ex.: between_num:1,5"; + } validators[i] = { condition: [">=","<="], message: "INVALID_BETWEEN_NUM", @@ -246,6 +252,14 @@ type: "regex" }; break; + case "match" : + var args = params[1].split(','); + validators[i] = { + message: "INVALID_INPUT_MATCH", + params: args, + type: "match" + }; + break; case "maxLen" : case "max_len" : validators[i] = { @@ -438,6 +452,10 @@ }else { isValid = testCondition(validators[j].condition, parseFloat(strValue), parseFloat(validators[j].params[0])); } + }else if(validators[j].type === "match") { + var otherNgModel = validators[j].params[0]; + var otherNgModelVal = scope.$eval(otherNgModel); + isValid = (otherNgModelVal === strValue); }else { // a 'disabled' element should always be valid, there is no need to validate it if(elm.prop('disabled')) { @@ -455,6 +473,11 @@ // replace any error message params that were passed if(typeof validators[j].params !== "undefined") { for(var k = 0, kln = validators[j].params.length; k < kln; k++) { + if(validators[j].type === "match" && kln > 1 && k === 0) { + // if validation type is "match" and include more than 1 params + // then we'll skip param[0] to find our real text inside param[1] + continue; + } message = message.replace((':param'), validators[j].params[k]); } } diff --git a/legacy-angular1.2.x/angular-validation.js b/legacy-angular1.2.x/angular-validation.js index 2a618f3..9dce5f5 100644 --- a/legacy-angular1.2.x/angular-validation.js +++ b/legacy-angular1.2.x/angular-validation.js @@ -105,6 +105,9 @@ case "betweenLen" : case "between_len" : var range = params[1].split(','); + if (range.length !== 2) { + throw "This validation must include exactly 2 params separated by a comma (,) ex.: between_len:1,5"; + } validators[i] = { pattern: "^.{" + range[0] + "," + range[1] + "}$", message: "INVALID_BETWEEN_CHAR", @@ -115,6 +118,9 @@ case "betweenNum" : case "between_num" : var range = params[1].split(','); + if (range.length !== 2) { + throw "This validation must include exactly 2 params separated by a comma (,) ex.: between_num:1,5"; + } validators[i] = { condition: [">=","<="], message: "INVALID_BETWEEN_NUM", @@ -244,6 +250,14 @@ type: "regex" }; break; + case "match" : + var args = params[1].split(','); + validators[i] = { + message: "INVALID_INPUT_MATCH", + params: args, + type: "match" + }; + break; case "maxLen" : case "max_len" : validators[i] = { @@ -436,6 +450,10 @@ }else { isValid = testCondition(validators[j].condition, parseFloat(strValue), parseFloat(validators[j].params[0])); } + }else if(validators[j].type === "match") { + var otherNgModel = validators[j].params[0]; + var otherNgModelVal = scope.$eval(otherNgModel); + isValid = (otherNgModelVal === strValue); }else { // a 'disabled' element should always be valid, there is no need to validate it if(elm.prop('disabled')) { @@ -453,6 +471,11 @@ // replace any error message params that were passed if(typeof validators[j].params !== "undefined") { for(var k = 0, kln = validators[j].params.length; k < kln; k++) { + if(validators[j].type === "match" && kln > 1 && k === 0) { + // if validation type is "match" and include more than 1 params + // then we'll skip param[0] to find our real text inside param[1] + continue; + } message = message.replace((':param'), validators[j].params[k]); } } diff --git a/lib/angular-translate/angular-translate-loader-static-files.min.js b/lib/angular-translate/angular-translate-loader-static-files.min.js index c8938bd..87a619b 100644 --- a/lib/angular-translate/angular-translate-loader-static-files.min.js +++ b/lib/angular-translate/angular-translate-loader-static-files.min.js @@ -1,5 +1,5 @@ /*! - * angular-translate - v2.4.2 - 2014-10-21 + * angular-translate - v2.5.2 - 2014-12-10 * http://github.com/angular-translate/angular-translate * Copyright (c) 2014 ; Licensed MIT */ diff --git a/locales/validation/en.json b/locales/validation/en.json index d2d2d7b..2e576df 100644 --- a/locales/validation/en.json +++ b/locales/validation/en.json @@ -19,8 +19,9 @@ "INVALID_FLOAT": "May only contain a float value (integer excluded). ", "INVALID_FLOAT_SIGNED": "May only contain a positive or negative float value (integer excluded). ", "INVALID_IBAN": "Must a valid IBAN. ", + "INVALID_INPUT_MATCH": "Confirmation field does not match specified field \":param\". ", "INVALID_INTEGER": "Must be a positive integer. ", - "INVALID_INTEGER_SIGNED": "Must be a positive or negative integer. ", + "INVALID_INTEGER_SIGNED": "Must be a positive or negative integer. ", "INVALID_IPV4": "Must be a valid IP (IPV4). ", "INVALID_IPV6": "Must be a valid IP (IPV6). ", "INVALID_IPV6_HEX": "Must be a valid IP (IPV6 Hex). ", @@ -51,6 +52,8 @@ "INPUT12": "Time (hh:mm OR hh:mm:ss) -- NOT Required", "INPUT13": "AlphaDashSpaces + Required + Minimum(5) Characters -- MUST USE: validation-error-to=\" \"", "INPUT14": "Alphanumeric + Required -- NG-DISABLED", + "INPUT15": "Password", + "INPUT16": "Password Confirmation", "CHANGE_LANGUAGE": "Change language.", "SAVE": "Save", "SELECT1": "Required (select) -- NoEVENT default(keyup) -- Directive will validate has EVENT (blur)", diff --git a/locales/validation/fr.json b/locales/validation/fr.json index 4e5d85d..df13a83 100644 --- a/locales/validation/fr.json +++ b/locales/validation/fr.json @@ -19,6 +19,7 @@ "INVALID_FLOAT": "Doit être obligatoirement un nombre flottant (nombre entier exclu). ", "INVALID_FLOAT_SIGNED": "Doit être obligatoirement un nombre flottant positif ou négatif (nombre entier exclu). ", "INVALID_IBAN": "Doit être un IBAN valide. ", + "INVALID_INPUT_MATCH": "Le champs de confirmation ne correspond pas au champs spécifié \":param\". ", "INVALID_INTEGER": "Doit être un nombre entier positif. ", "INVALID_INTEGER_SIGNED": "Doit être un nombre entier positif ou négatif. ", "INVALID_IPV4": "Doit être un IP valide (IPV4). ", @@ -51,6 +52,8 @@ "INPUT12": "Time (hh:mm OU hh:mm:ss) -- NON Requis", "INPUT13": "AlphaDashSpaces + Requis + Minimum(5) Caractères -- DOIT UTILISER: validation-error-to=\" \"", "INPUT14": "Alphanumérique + Requis -- NG-DISABLED", + "INPUT15": "Mot de Passe", + "INPUT16": "Mot de Passe (Confirmation)", "CHANGE_LANGUAGE": "Changer de langue.", "SAVE": "Sauvegarder", "SELECT1": "Requis (select) -- Aucun EVENT défaut(keyup) -- Directive va valider avec EVENT (blur)", diff --git a/readme.md b/readme.md index 23bdbcd..42ffebb 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,6 @@ #Angular Validation ### Form validation after user inactivity (customizable timeout) -`Version: 1.3` +`Version: 1.3.1` Angular Validation made easy! Angular Validation is an angular directive with locales (languages) with a simple approach of defining your validation in 1 line and displaying the errors on another 1 line...that's it! @@ -35,13 +35,23 @@ P.S. For real live example, please download the Github project and run the `inde - - - + + + + + + + + + + + + - +
+
+ + + +
+
+ + + +