Skip to content
This repository was archived by the owner on Jul 1, 2020. It is now read-only.

Commit 44fe9de

Browse files
committed
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
1 parent d106996 commit 44fe9de

File tree

7 files changed

+82
-9
lines changed

7 files changed

+82
-9
lines changed

‎angular-validation.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@
107107
case "betweenLen" :
108108
case "between_len" :
109109
var range = params[1].split(',');
110+
if (range.length !== 2) {
111+
throw "This validation must include exactly 2 params separated by a comma (,) ex.: between_len:1,5";
112+
}
110113
validators[i] = {
111114
pattern: "^.{" + range[0] + "," + range[1] + "}$",
112115
message: "INVALID_BETWEEN_CHAR",
@@ -117,6 +120,9 @@
117120
case "betweenNum" :
118121
case "between_num" :
119122
var range = params[1].split(',');
123+
if (range.length !== 2) {
124+
throw "This validation must include exactly 2 params separated by a comma (,) ex.: between_num:1,5";
125+
}
120126
validators[i] = {
121127
condition: [">=","<="],
122128
message: "INVALID_BETWEEN_NUM",
@@ -246,6 +252,14 @@
246252
type: "regex"
247253
};
248254
break;
255+
case "match" :
256+
var args = params[1].split(',');
257+
validators[i] = {
258+
message: "INVALID_INPUT_MATCH",
259+
params: args,
260+
type: "match"
261+
};
262+
break;
249263
case "maxLen" :
250264
case "max_len" :
251265
validators[i] = {
@@ -438,6 +452,10 @@
438452
}else {
439453
isValid = testCondition(validators[j].condition, parseFloat(strValue), parseFloat(validators[j].params[0]));
440454
}
455+
}else if(validators[j].type === "match") {
456+
var otherNgModel = validators[j].params[0];
457+
var otherNgModelVal = scope.$eval(otherNgModel);
458+
isValid = (otherNgModelVal === strValue);
441459
}else {
442460
// a 'disabled' element should always be valid, there is no need to validate it
443461
if(elm.prop('disabled')) {
@@ -455,6 +473,11 @@
455473
// replace any error message params that were passed
456474
if(typeof validators[j].params !== "undefined") {
457475
for(var k = 0, kln = validators[j].params.length; k < kln; k++) {
476+
if(validators[j].type === "match" && kln > 1 && k === 0) {
477+
// if validation type is "match" and include more than 1 params
478+
// then we'll skip param[0] to find our real text inside param[1]
479+
continue;
480+
}
458481
message = message.replace((':param'), validators[j].params[k]);
459482
}
460483
}

‎legacy-angular1.2.x/angular-validation.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@
105105
case "betweenLen" :
106106
case "between_len" :
107107
var range = params[1].split(',');
108+
if (range.length !== 2) {
109+
throw "This validation must include exactly 2 params separated by a comma (,) ex.: between_len:1,5";
110+
}
108111
validators[i] = {
109112
pattern: "^.{" + range[0] + "," + range[1] + "}$",
110113
message: "INVALID_BETWEEN_CHAR",
@@ -115,6 +118,9 @@
115118
case "betweenNum" :
116119
case "between_num" :
117120
var range = params[1].split(',');
121+
if (range.length !== 2) {
122+
throw "This validation must include exactly 2 params separated by a comma (,) ex.: between_num:1,5";
123+
}
118124
validators[i] = {
119125
condition: [">=","<="],
120126
message: "INVALID_BETWEEN_NUM",
@@ -244,6 +250,14 @@
244250
type: "regex"
245251
};
246252
break;
253+
case "match" :
254+
var args = params[1].split(',');
255+
validators[i] = {
256+
message: "INVALID_INPUT_MATCH",
257+
params: args,
258+
type: "match"
259+
};
260+
break;
247261
case "maxLen" :
248262
case "max_len" :
249263
validators[i] = {
@@ -436,6 +450,10 @@
436450
}else {
437451
isValid = testCondition(validators[j].condition, parseFloat(strValue), parseFloat(validators[j].params[0]));
438452
}
453+
}else if(validators[j].type === "match") {
454+
var otherNgModel = validators[j].params[0];
455+
var otherNgModelVal = scope.$eval(otherNgModel);
456+
isValid = (otherNgModelVal === strValue);
439457
}else {
440458
// a 'disabled' element should always be valid, there is no need to validate it
441459
if(elm.prop('disabled')) {
@@ -453,6 +471,11 @@
453471
// replace any error message params that were passed
454472
if(typeof validators[j].params !== "undefined") {
455473
for(var k = 0, kln = validators[j].params.length; k < kln; k++) {
474+
if(validators[j].type === "match" && kln > 1 && k === 0) {
475+
// if validation type is "match" and include more than 1 params
476+
// then we'll skip param[0] to find our real text inside param[1]
477+
continue;
478+
}
456479
message = message.replace((':param'), validators[j].params[k]);
457480
}
458481
}

‎lib/angular-translate/angular-translate-loader-static-files.min.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* angular-translate - v2.4.2 - 2014-10-21
2+
* angular-translate - v2.5.2 - 2014-12-10
33
* http://github.com/angular-translate/angular-translate
44
* Copyright (c) 2014 ; Licensed MIT
55
*/

‎locales/validation/en.json‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
"INVALID_FLOAT": "May only contain a float value (integer excluded). ",
2020
"INVALID_FLOAT_SIGNED": "May only contain a positive or negative float value (integer excluded). ",
2121
"INVALID_IBAN": "Must a valid IBAN. ",
22+
"INVALID_INPUT_MATCH": "Confirmation field does not match specified field \":param\". ",
2223
"INVALID_INTEGER": "Must be a positive integer. ",
23-
"INVALID_INTEGER_SIGNED": "Must be a positive or negative integer. ",
24+
"INVALID_INTEGER_SIGNED": "Must be a positive or negative integer. ",
2425
"INVALID_IPV4": "Must be a valid IP (IPV4). ",
2526
"INVALID_IPV6": "Must be a valid IP (IPV6). ",
2627
"INVALID_IPV6_HEX": "Must be a valid IP (IPV6 Hex). ",
@@ -51,6 +52,8 @@
5152
"INPUT12": "Time (hh:mm OR hh:mm:ss) -- NOT Required",
5253
"INPUT13": "AlphaDashSpaces + Required + Minimum(5) Characters -- MUST USE: validation-error-to=\" \"",
5354
"INPUT14": "Alphanumeric + Required -- NG-DISABLED",
55+
"INPUT15": "Password",
56+
"INPUT16": "Password Confirmation",
5457
"CHANGE_LANGUAGE": "Change language.",
5558
"SAVE": "Save",
5659
"SELECT1": "Required (select) -- NoEVENT default(keyup) -- Directive will validate has EVENT (blur)",

‎locales/validation/fr.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"INVALID_FLOAT": "Doit être obligatoirement un nombre flottant (nombre entier exclu). ",
2020
"INVALID_FLOAT_SIGNED": "Doit être obligatoirement un nombre flottant positif ou négatif (nombre entier exclu). ",
2121
"INVALID_IBAN": "Doit être un IBAN valide. ",
22+
"INVALID_INPUT_MATCH": "Le champs de confirmation ne correspond pas au champs spécifié \":param\". ",
2223
"INVALID_INTEGER": "Doit être un nombre entier positif. ",
2324
"INVALID_INTEGER_SIGNED": "Doit être un nombre entier positif ou négatif. ",
2425
"INVALID_IPV4": "Doit être un IP valide (IPV4). ",
@@ -51,6 +52,8 @@
5152
"INPUT12": "Time (hh:mm OU hh:mm:ss) -- NON Requis",
5253
"INPUT13": "AlphaDashSpaces + Requis + Minimum(5) Caractères -- DOIT UTILISER: validation-error-to=\" \"",
5354
"INPUT14": "Alphanumérique + Requis -- NG-DISABLED",
55+
"INPUT15": "Mot de Passe",
56+
"INPUT16": "Mot de Passe (Confirmation)",
5457
"CHANGE_LANGUAGE": "Changer de langue.",
5558
"SAVE": "Sauvegarder",
5659
"SELECT1": "Requis (select) -- Aucun EVENT défaut(keyup) -- Directive va valider avec EVENT (blur)",

‎readme.md‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Angular Validation
22
### Form validation after user inactivity (customizable timeout)
3-
`Version: 1.3`
3+
`Version: 1.3.1`
44

55
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!
66

@@ -35,13 +35,23 @@ P.S. For real live example, please download the Github project and run the `inde
3535
<input type="number" name="input3" ng-model="form1.input3" validation="numeric|between_num:6,99.9|required" />
3636
<span class="validation text-danger"></span>
3737

38-
<!-- example 4 - with Regular Expression (Date Code of YYWW) -->
39-
<label for="input4">Multiple Validations + Custom Regex of Date Code (YYWW)</label>
40-
<input type="text" name="input4" ng-model="form1.input4"
38+
<!-- example 4 -->
39+
<!-- input match confirmation, as for example: password confirmation -->
40+
<!-- match validation could use 1 or 2 params (match:field1 ...or... match:field1,Text to Display), if 2 params are passed it will use the 2nd param as text displayed -->
41+
<label for="input4">Password</label>
42+
<input type="password" name="input4" ng-model="form1.input4" validation="alpha|min_len:4|required" />
43+
<span class="validation text-danger"></span>
44+
<label for="input4c">Password Confirmation</label>
45+
<input type="password" name="input4c" ng-model="form1.input4c" validation="match:form1.input4,Password|required" />
46+
<span class="validation text-danger"></span>
47+
48+
<!-- example 5 - with Regular Expression (Date Code of YYWW) -->
49+
<label for="input5">Multiple Validations + Custom Regex of Date Code (YYWW)</label>
50+
<input type="text" name="input5" ng-model="form1.input5"
4151
validation="exact_len:4|regex:YYWW:=^(0[9]|1[0-9]|2[0-9]|3[0-9])(5[0-2]|[0-4][0-9])$:regex|required|integer" />
4252
<span class="validation text-danger"></span>
4353

44-
<!-- example 5 - required select option (dropdown) -->
54+
<!-- example 6 - required select option (dropdown) -->
4555
<div class="form-group">
4656
<label for="select1">Select Option Required</label>
4757
<select id="stk_type" name="stk_type" class="form-control" ng-model="form1.select1" validation="required">
@@ -154,6 +164,8 @@ Available Validators
154164
* `ipv4` Check for valid IP (IPv4)
155165
* `ipv6` Check for valid IP (IPv6)
156166
* `ipv6_hex` Check for valid IP (IPv6 Hexadecimal)
167+
* `match:n` Match another input field(n), where (n) must be the exact ngModel attribute of input field to compare to.
168+
* `match:n,t` Match another input field(n), same as (match:n) but also include (t) for alternative text to be displayed.
157169
* `max_len:n` Checks field length, no longer than specified length where (n) is length parameter.
158170
* `max_num:n` Checks numeric value to be lower or equal than the number (n).
159171
* `min_len:n` Checks field length, no shorter than specified length where (n) is length parameter.
@@ -201,8 +213,7 @@ License
201213
#### Any kind of help is welcome from the TODO list
202214
:memo: :point_up:
203215

204-
* Add `same` and `different` validators (same password)
205216
* Add `street_address` validator
206217
* Add more validators...
207-
* Add more locale languages
218+
* Add more locale languages... I need your help on that one!!!
208219
* Add online demo

‎templates/testingForm.html‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@
9292
<input type="text" class="form-control" name="input14" placeholder="alpha|required" ng-model="form1.input14" validation="alpha|required" ng-disabled="true" />
9393
<span class="validation text-danger"></span>
9494
</div>
95+
<div class="form-group">
96+
<label for="input15">{{ 'INPUT15' | translate }}</label>
97+
<input type="password" class="form-control" name="input14" placeholder="alpha|min_len:3|required" ng-model="form1.input15" validation="alpha|min_len:3|required" />
98+
<span class="validation text-danger"></span>
99+
</div>
100+
<div class="form-group">
101+
<label for="input16">{{ 'INPUT16' | translate }}</label>
102+
<input type="password" class="form-control" name="input14" placeholder="match:form1.input15|required" ng-model="form1.input16" validation="match:form1.input15,Password|required" />
103+
<span class="validation text-danger"></span>
104+
</div>
95105
<div class="form-group">
96106
<label for="area1">{{ 'AREA1' | translate }}</label>
97107
<textarea class="form-control" name="area1" rows="3" placeholder="alpha_dash_spaces|min_len:15|required" validation="alpha_dash_spaces|min_len:15|required" ng-model="form1.area1"></textarea>

0 commit comments

Comments
 (0)