Skip to content

Commit

Permalink
feat(formvalidation): size rule to test string length range
Browse files Browse the repository at this point in the history
Added new size rule to check for a character length. It's a shortcut to avoid having 2 separate rules (minLength+maxLength)
  • Loading branch information
lubber-de authored Feb 3, 2023
1 parent e1559e8 commit 247703a
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/definitions/behaviors/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,9 +536,9 @@
parts,
suffixPrompt
;
if (ancillary && ['integer', 'decimal', 'number'].indexOf(ruleName) >= 0 && ancillary.indexOf('..') >= 0) {
if (ancillary && ['integer', 'decimal', 'number', 'size'].indexOf(ruleName) >= 0 && ancillary.indexOf('..') >= 0) {
parts = ancillary.split('..', 2);
if (!rule.prompt) {
if (!rule.prompt && ruleName !== 'size') {
suffixPrompt = parts[0] === ''
? settings.prompt.maxValue.replace(/{ruleValue}/g, '{max}')
: (parts[1] === ''
Expand Down Expand Up @@ -1638,6 +1638,7 @@
minLength: '{name} must be at least {ruleValue} characters',
exactLength: '{name} must be exactly {ruleValue} characters',
maxLength: '{name} cannot be longer than {ruleValue} characters',
size: '{name} must have a length between {min} and {max} characters',
match: '{name} must match {ruleValue} field',
different: '{name} must have a different value than {ruleValue} field',
creditCard: '{name} must be a valid credit card number',
Expand Down Expand Up @@ -1799,7 +1800,7 @@
integer: function (value, range) {
return $.fn.form.settings.rules.range(value, range, 'integer');
},
range: function (value, range, regExp) {
range: function (value, range, regExp, testLength) {
if (typeof regExp === 'string') {
regExp = $.fn.form.settings.regExp[regExp];
}
Expand Down Expand Up @@ -1828,6 +1829,9 @@
max = parts[1] - 0;
}
}
if (testLength) {
value = value.length;
}

return (
regExp.test(value)
Expand Down Expand Up @@ -1913,24 +1917,22 @@
},

// is at least string length
minLength: function (value, requiredLength) {
return value !== undefined
? value.length >= requiredLength
: false;
minLength: function (value, minLength) {
return $.fn.form.settings.rules.range(value, minLength + '..', 'integer', true);
},

// is exactly length
exactLength: function (value, requiredLength) {
return value !== undefined
? value.length === Number(requiredLength)
: false;
return $.fn.form.settings.rules.range(value, requiredLength + '..' + requiredLength, 'integer', true);
},

// is less than length
maxLength: function (value, maxLength) {
return value !== undefined
? value.length <= maxLength
: false;
return $.fn.form.settings.rules.range(value, '..' + maxLength, 'integer', true);
},

size: function (value, range) {
return $.fn.form.settings.rules.range(value, range, 'integer', true);
},

// matches another field
Expand Down

0 comments on commit 247703a

Please sign in to comment.