Skip to content

Commit

Permalink
netteForms.js: validates fucking HTML5 type 'number'
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jun 14, 2016
1 parent 662d7d7 commit 31b9298
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/assets/netteForms.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ Nette.validateControl = function(elem, rules, onlyCheck, value) {
return false;
}
}

if (!onlyCheck && elem.type === 'number' && !elem.validity.valid) {
Nette.addError(elem, 'Please enter a valid value.');
return false;
}

return true;
};

Expand Down Expand Up @@ -292,6 +298,9 @@ Nette.validateRule = function(elem, op, arg, value) {

Nette.validators = {
filled: function(elem, arg, val) {
if (elem.type === 'number' && elem.validity.badInput) {
return true;
}
return val !== '' && val !== false && val !== null
&& (!Nette.isArray(val) || !!val.length)
&& (!window.FileList || !(val instanceof window.FileList) || val.length);
Expand Down Expand Up @@ -337,14 +346,35 @@ Nette.validators = {
},

minLength: function(elem, arg, val) {
if (elem.type === 'number') {
if (elem.validity.tooShort) {
return false
} else if (elem.validity.badInput) {
return null;
}
}
return val.length >= arg;
},

maxLength: function(elem, arg, val) {
if (elem.type === 'number') {
if (elem.validity.tooLong) {
return false
} else if (elem.validity.badInput) {
return null;
}
}
return val.length <= arg;
},

length: function(elem, arg, val) {
if (elem.type === 'number') {
if (elem.validity.tooShort || elem.validity.tooLong) {
return false
} else if (elem.validity.badInput) {
return null;
}
}
arg = Nette.isArray(arg) ? arg : [arg, arg];
return (arg[0] === null || val.length >= arg[0]) && (arg[1] === null || val.length <= arg[1]);
},
Expand Down Expand Up @@ -378,10 +408,16 @@ Nette.validators = {
},

integer: function(elem, arg, val) {
if (elem.type === 'number' && elem.validity.badInput) {
return false;
}
return (/^-?[0-9]+$/).test(val);
},

'float': function(elem, arg, val, value) {
if (elem.type === 'number' && elem.validity.badInput) {
return false;
}
val = val.replace(' ', '').replace(',', '.');
if ((/^-?[0-9]*[.,]?[0-9]+$/).test(val)) {
value.value = val;
Expand All @@ -391,14 +427,35 @@ Nette.validators = {
},

min: function(elem, arg, val) {
if (elem.type === 'number') {
if (elem.validity.rangeUnderflow) {
return false
} else if (elem.validity.badInput) {
return null;
}
}
return Nette.validators.range(elem, [arg, null], val);
},

max: function(elem, arg, val) {
if (elem.type === 'number') {
if (elem.validity.rangeOverflow) {
return false
} else if (elem.validity.badInput) {
return null;
}
}
return Nette.validators.range(elem, [null, arg], val);
},

range: function(elem, arg, val) {
if (elem.type === 'number') {
if (elem.validity.rangeUnderflow || elem.validity.rangeOverflow) {
return false
} else if (elem.validity.badInput) {
return null;
}
}
return Nette.isArray(arg) ?
((arg[0] === null || parseFloat(val) >= arg[0]) && (arg[1] === null || parseFloat(val) <= arg[1])) : null;
},
Expand Down

0 comments on commit 31b9298

Please sign in to comment.