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

Commit 41f9ed9

Browse files
committed
Fix #11 - input type=number & better floating test
- Fixed floating validator to also permit dot as first char (basically skipping the 0), which give 2 ways of writing a valid float number: 0.2 or .2 .... -0.2 or -.2 - Fixed & removed the keyboard blocking (sometimes upset user) of invalid character on input type="number", I am not instead displaying an error message of invalid keyboard character so that user is more informed. - Added a plunker live demo
1 parent 44fe9de commit 41f9ed9

File tree

5 files changed

+31
-17
lines changed

5 files changed

+31
-17
lines changed

‎angular-validation.js‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,15 @@
196196
break;
197197
case "float" :
198198
validators[i] = {
199-
pattern: "^\\d+[\\.]+\\d+$",
199+
pattern: "^\\d*\\.{1}\\d+$",
200200
message: "INVALID_FLOAT",
201201
type: "regex"
202202
};
203203
break;
204204
case "floatSigned" :
205205
case "float_signed" :
206206
validators[i] = {
207-
pattern: "^[+-]?\\d+[\\.]+\\d+$",
207+
pattern: "^[-+]?\\d*\\.{1}\\d+$",
208208
message: "INVALID_FLOAT_SIGNED",
209209
type: "regex"
210210
};
@@ -296,14 +296,14 @@
296296
break;
297297
case "numeric" :
298298
validators[i] = {
299-
pattern: "^\\d+[\\.]?\\d*$",
299+
pattern: "^\\d*\\.?\\d+$",
300300
message: "INVALID_NUMERIC",
301301
type: "regex"
302302
};
303303
break;
304304
case "numeric_signed" :
305305
validators[i] = {
306-
pattern: "^[-+]?\\d+[\\.]?\\d*$",
306+
pattern: "^[-+]?\\d*\\.?\\d+$",
307307
message: "INVALID_NUMERIC_SIGNED",
308308
type: "regex"
309309
};
@@ -347,7 +347,7 @@
347347

348348
/** We seem to have little problems validating a field of <input type="number">
349349
* as angular reports undefined value even though user could type invalid chars
350-
* Bind trigger to block alpha chars completely except these: numbers, decimal and dash
350+
* Bind trigger to block alpha chars completely except these: numbers, decimal, add and dash
351351
*/
352352
var bindBlockingCharsOnInputNumber = function() {
353353
// get some properties of the inspected element
@@ -362,7 +362,9 @@
362362
evt.preventDefault();
363363
return false;
364364
}
365-
if (charCode > 31 && (charCode != 46 && ((charCode < 48 || charCode > 57) && charCode < 96 || charCode > 105)) && (charCode != 190 && charCode != 110 && charCode != 109 && charCode != 173)) {
365+
// keycode: 8(backspace), 35(home), 36(end), 37(left arrow), 39(right arrow), 46(delete), 48-57(0-9), 96-105(numpad 0-9), 107(add), 109(substract), 110(decimal), 173(dash), 190(period)
366+
regexBlocking = new RegExp("^(8|3[5-7]|39|46|4[8-9]|5[0-7]|9[6-9]|10[0-5]|107|109|110|173|190)$");
367+
if(!regexBlocking.test(charCode)) {
366368
evt.preventDefault();
367369
return false;
368370
}
@@ -461,9 +463,15 @@
461463
if(elm.prop('disabled')) {
462464
isValid = true;
463465
} else {
464-
// run the Regex test through each iteration, if required (\S+) and is null then it's invalid automatically
465-
regex = new RegExp(validators[j].pattern, 'i');
466-
isValid = (validators[j].pattern === "\\S+" && (typeof strValue === "undefined" || strValue === null)) ? false : regex.test(strValue);
466+
// before running Regex test, we'll make sure that an input of type="number" doesn't hold an invalid keyboard character, if that is the case then no need to run Regex
467+
if(typeof strValue === "string" && strValue === "" && elm.prop('type').toUpperCase() === "NUMBER") {
468+
validators[0].message = "INVALID_KEY_CHAR"; // replace the first error message by the invalid keyboard error msg
469+
isValid = false;
470+
}else {
471+
// run the Regex test through each iteration, if required (\S+) and is null then it's invalid automatically
472+
regex = new RegExp(validators[j].pattern, 'i');
473+
isValid = (validators[j].pattern === "\\S+" && (typeof strValue === "undefined" || strValue === null)) ? false : regex.test(strValue);
474+
}
467475
}
468476
}
469477
if(!isValid) {
@@ -502,7 +510,7 @@
502510
}
503511

504512
// attach/bind trigger on a <input type="number"/> and only allow: numbers, decimal and dash
505-
bindBlockingCharsOnInputNumber();
513+
//bindBlockingCharsOnInputNumber();
506514

507515
// invalidate field before doing any validation
508516
if(isFieldRequired) {

‎locales/validation/en.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"INVALID_IPV4": "Must be a valid IP (IPV4). ",
2626
"INVALID_IPV6": "Must be a valid IP (IPV6). ",
2727
"INVALID_IPV6_HEX": "Must be a valid IP (IPV6 Hex). ",
28+
"INVALID_KEY_CHAR": "Invalid keyboard entry on a field of type \"number\". ",
2829
"INVALID_MAX_CHAR": "May not be greater than :param characters. ",
2930
"INVALID_MAX_NUM": "Needs to be a numeric value, equal to, or lower than :param. ",
3031
"INVALID_MIN_CHAR": "Must be at least :param characters. ",
@@ -39,7 +40,7 @@
3940

4041
"AREA1": "TextArea: Alphanumeric + Minimum(15) + Required",
4142
"INPUT1": "Alphanumeric + Exactly(3) + Required -- typing-limit(5sec)",
42-
"INPUT2": "Float (integer excluded) -- input type=\"number\" -- BLOCK non-numeric characters ",
43+
"INPUT2": "Number positive or negative -- input type=\"number\" -- Error on non-numeric characters ",
4344
"INPUT3": "Floating number range (integer excluded) -- between_num:x,y OR min_num:x|max_num:y ",
4445
"INPUT4": "Multiple Validations + Custom Regex of Date Code (YYWW)",
4546
"INPUT5": "Email",

‎locales/validation/fr.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"INVALID_IPV4": "Doit être un IP valide (IPV4). ",
2626
"INVALID_IPV6": "Doit être un IP valide (IPV6). ",
2727
"INVALID_IPV6_HEX": "Doit être un IP valide (IPV6 Hex). ",
28+
"INVALID_KEY_CHAR": "Entrée clavier invalide sur un champs de type \"nombre\". ",
2829
"INVALID_MAX_CHAR": "Doit être plus petit que :param caractères. ",
2930
"INVALID_MAX_NUM": "Doit être une valeur numérique, égale ou inférieure à :param. ",
3031
"INVALID_MIN_CHAR": "Doit avoir au moins :param caractères. ",
@@ -39,7 +40,7 @@
3940

4041
"AREA1": "TextArea: Alphanumérique + Minimum(15) + Required",
4142
"INPUT1": "Alphanumérique + Exactement(3) + Requis -- typing-limit(5sec)",
42-
"INPUT2": "Nombre Flottant (entier exclu) -- input type=\"number\" -- BLOQUER caractères non-numerique",
43+
"INPUT2": "Nombre positif ou négatif -- input type=\"number\" -- Erreur sur caractères non-numérique",
4344
"INPUT3": "Intervalle de Nombre Flottant (entier exclu) -- between_num:x,y OU min_num:x|max_num:y",
4445
"INPUT4": "Multiple Validations + Regex Personnalisé d'un Code Date (AASS)",
4546
"INPUT5": "Courriel",

‎readme.md‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ For a smoother user experience, I also added validation on inactivity (timer). S
1010

1111
Now also supporting AngularJS 1.3.x (see legacy folder for 1.2.x support)
1212

13+
## Live Demo
14+
http://plnkr.co/edit/jADq7H?p=preview
15+
1316
## Some Working Examples
1417

1518
Let's start with a simple example and then let's get down to real business.
@@ -139,11 +142,12 @@ P.S. If you define new Language set, please make a pull request and I would be h
139142

140143
Available Validators
141144
--------------------
142-
##### All validators are written as `snake_case` but it's up to the user's taste and could also be written as `camelCase`. So for example `alpha_dash_spaces` and `alphaDashSpaces` are both equivalent.
145+
All validators are written as `snake_case` but it's up to the user's taste and could also be written as `camelCase`. So for example `alpha_dash_spaces` and `alphaDashSpaces` are both equivalent.
146+
##### NOTE: on an input type="number", the "+" sign is an invalid character (browser limitation) even if you are using a `signed` validator. If you really wish to use the "+", then change your input to type="text".
143147
* `alpha` Only alpha characters (including latin) are present (a-z, A-Z)
144148
* `alpha_spaces` Only alpha characters (including latin) and spaces are present (a-z, A-Z)
145149
* `alpha_num` Only alpha-numeric characters (including latin) are present (a-z, A-Z, 0-9)
146-
* `alpha_num_spaces` Only alpha-numeric characters (with latin) and spaces are present (a-z, A-Z, 0-9)
150+
* `alpha_num_spaces` Only alpha-numeric characters (with latin & spaces) are present (a-z, A-Z, 0-9)
147151
* `alpha_dash` Only alpha-numeric characters + dashes, underscores are present (a-z, A-Z, 0-9, _-)
148152
* `alpha_dash_spaces` Alpha-numeric chars + dashes, underscores and spaces (a-z, A-Z, 0-9, _-)
149153
* `between_len:min,max` Ensures the length of a string is between a min,max string length.
@@ -155,7 +159,7 @@ Available Validators
155159
* `date_euro_long` Ensure date follows the Europe long format (dd-mm-yyyy) or (dd/mm/yyyy)
156160
* `date_euro_short` Ensure date follows the Europe short format (dd-mm-yy) or (dd/mm/yy)
157161
* `email` Checks for a valid email address
158-
* `exact_len:n` Ensures that field length precisely matches the specified length. n = length parameter.
162+
* `exact_len:n` Ensures that field length precisely matches the specified length (n).
159163
* `float` Only a positive floating point value (integer are excluded)
160164
* `float_signed` Only a floating point value (integer excluded), could be signed (-/+) positive/negative.
161165
* `iban` Check for a valid IBAN.

‎templates/testingForm.html‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
</div>
88
<div class="form-group">
99
<label for="input2">{{ 'INPUT2' | translate }}</label>
10-
<input type="number" class="form-control" name="input2" placeholder="float_signed|required" ng-model="form1.input2" validation="float_signed|required" />
10+
<input type="number" class="form-control" name="input2" placeholder="numeric_signed|required" ng-model="form1.input2" validation="numeric_signed|required" />
1111
<span class="validation text-danger"></span>
1212
</div>
1313
<div class="form-group">
1414
<label for="input3">{{ 'INPUT3' | translate }}</label>
15-
<input type="number" class="form-control" name="input3" placeholder="float_signed|between_num:6,99.5|required" ng-model="form1.input3" validation="float_signed|between_num:6,99.5|required" />
15+
<input type="number" class="form-control" name="input3" placeholder="float_signed|between_num:-0.6,99.5|required" ng-model="form1.input3" validation="float_signed|between_num:-0.6,99.5|required" />
1616
<span class="validation text-danger"></span>
1717
</div>
1818
<div class="form-group">

0 commit comments

Comments
 (0)