Js validates a simple example of an integer plus a reserved decimal point
- 2020-03-30 00:42:30
- OfStack
function validateNum(obj) {
//Positive integers (cache is used here)
var number = obj.data(validate).number;
//Decimal point (cache here)
var decimal = obj.data(validate).decimal;
//Dynamic basic verification regularization
eval("var reg = /^[0-9]{0," + number + "}([.]?[0-9]{0," + decimal + "})$" + "/g;");
var value = obj.val();
var maxnumlen = number + decimal + 1; //Maximum length + 1(decimal point)
if (!reg.test(obj.val())) return false;
//The maximum length is the length of the current value and the value has no "."
if (maxnumlen == value.length && value.indexOf('.') <= 0) {
return false;
}
//Try to get the index of ".
var valueindexof = value.indexOf('.');
if (valueindexof > 0) {
//If the last bit of the "." index is empty, then false is returned
if (value.charAt(valueindexof + 1) == "") {
return false;
}
}
//The split value is easy to determine before and after
var valuesplit = value.split('.');
//If the length of the value is greater than the length of the defined positive integer
if (value.length > number) {
if (valuesplit.length == 1) {
return false;
}
//Maximum length - defined length greater than the maximum length is false.
if (maxnumlen - number >= maxnumlen) {
return false;
}
}
return true;
}
After the element is cached with a validate object, it is called.
Definition:
$(" # example "). The data (the "validate", {a decimal number: 2:2});
Call:
ValidateNum ($(" # example));