Skip to content

Commit 6f8b533

Browse files
authored
feat(form): support range values for decimal and number validation
It was not possible to allow for an optional range check for the decimal or number validator. This was only supported for integer values. This PR now supports decimal and number validators to supply a range by making the check for a range value a separate range validator so it can be reused. adds 2 new validators maxValue and minValue accordingly displays the range values in the default error prompt as well if no prompt is given
1 parent 1af2588 commit 6f8b533

File tree

1 file changed

+43
-11
lines changed

1 file changed

+43
-11
lines changed

src/definitions/behaviors/form.js

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -551,8 +551,23 @@ $.fn.form = function(parameters) {
551551
requiresValue = (prompt.search('{value}') !== -1),
552552
requiresName = (prompt.search('{name}') !== -1),
553553
$label,
554-
name
554+
name,
555+
parts,
556+
suffixPrompt
555557
;
558+
if(ancillary && ancillary.indexOf('..') >= 0) {
559+
parts = ancillary.split('..', 2);
560+
if(!rule.prompt) {
561+
suffixPrompt = (
562+
parts[0] === '' ? settings.prompt.maxValue.replace(/\{ruleValue\}/g,'{max}') :
563+
parts[1] === '' ? settings.prompt.minValue.replace(/\{ruleValue\}/g,'{min}') :
564+
settings.prompt.range
565+
);
566+
prompt += suffixPrompt.replace(/\{name\}/g, ' ' + settings.text.and);
567+
}
568+
prompt = prompt.replace(/\{min\}/g, parts[0]);
569+
prompt = prompt.replace(/\{max\}/g, parts[1]);
570+
}
556571
if(requiresValue) {
557572
prompt = prompt.replace(/\{value\}/g, $field.val());
558573
}
@@ -1561,12 +1576,16 @@ $.fn.form.settings = {
15611576
},
15621577

15631578
text: {
1579+
and : 'and',
15641580
unspecifiedRule : 'Please enter a valid value',
15651581
unspecifiedField : 'This field',
15661582
leavingMessage : 'There are unsaved changes on this page which will be discarded if you continue.'
15671583
},
15681584

15691585
prompt: {
1586+
range : '{name} must be in a range from {min} to {max}',
1587+
maxValue : '{name} must have a maximum value of {ruleValue}',
1588+
minValue : '{name} must have a minimum value of {ruleValue}',
15701589
empty : '{name} must have a value',
15711590
checked : '{name} must be checked',
15721591
email : '{name} must be a valid e-mail',
@@ -1729,11 +1748,24 @@ $.fn.form.settings = {
17291748
}
17301749
return value.match( new RegExp(regExp, flags) );
17311750
},
1732-
1751+
minValue: function(value, range) {
1752+
return $.fn.form.settings.rules.range(value, range+'..', 'number');
1753+
},
1754+
maxValue: function(value, range) {
1755+
return $.fn.form.settings.rules.range(value, '..'+range, 'number');
1756+
},
17331757
// is valid integer or matches range
17341758
integer: function(value, range) {
1759+
return $.fn.form.settings.rules.range(value, range, 'integer');
1760+
},
1761+
range: function(value, range, regExp) {
1762+
if(typeof regExp == "string") {
1763+
regExp = $.fn.form.settings.regExp[regExp];
1764+
}
1765+
if(!(regExp instanceof RegExp)) {
1766+
regExp = $.fn.form.settings.regExp.integer;
1767+
}
17351768
var
1736-
intRegExp = $.fn.form.settings.regExp.integer,
17371769
min,
17381770
max,
17391771
parts
@@ -1742,34 +1774,34 @@ $.fn.form.settings = {
17421774
// do nothing
17431775
}
17441776
else if(range.indexOf('..') == -1) {
1745-
if(intRegExp.test(range)) {
1777+
if(regExp.test(range)) {
17461778
min = max = range - 0;
17471779
}
17481780
}
17491781
else {
17501782
parts = range.split('..', 2);
1751-
if(intRegExp.test(parts[0])) {
1783+
if(regExp.test(parts[0])) {
17521784
min = parts[0] - 0;
17531785
}
1754-
if(intRegExp.test(parts[1])) {
1786+
if(regExp.test(parts[1])) {
17551787
max = parts[1] - 0;
17561788
}
17571789
}
17581790
return (
1759-
intRegExp.test(value) &&
1791+
regExp.test(value) &&
17601792
(min === undefined || value >= min) &&
17611793
(max === undefined || value <= max)
17621794
);
17631795
},
17641796

17651797
// is valid number (with decimal)
1766-
decimal: function(value) {
1767-
return $.fn.form.settings.regExp.decimal.test(value);
1798+
decimal: function(value, range) {
1799+
return $.fn.form.settings.rules.range(value, range, 'decimal');
17681800
},
17691801

17701802
// is valid number
1771-
number: function(value) {
1772-
return $.fn.form.settings.regExp.number.test(value);
1803+
number: function(value, range) {
1804+
return $.fn.form.settings.rules.range(value, range, 'number');
17731805
},
17741806

17751807
// is value (case insensitive)

0 commit comments

Comments
 (0)