Copy Code code as follows:
function Validatenum (obj) {
Positive integer (cache used here)
var number = Obj.data (validate). number;
Decimal point (cache used here)
var decimal = obj.data (validate). Decimal;
Dynamic Basic Validation Regular
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 equals 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
var valueindexof = Value.indexof ('. ');
if (Valueindexof > 0) {
If the "." The one after the index is empty then definitely returns false
if (Value.charat (valueindexof + 1) = "") {
return false;
}
}
The split value is easy to determine before and after the content
var valuesplit = Value.split ('. ');
If the length of the value is greater than the defined positive integer length
if (Value.length > number) {
if (valuesplit.length = = 1) {
return false;
}
Maximum length-The defined length is longer than the maximum length, false.
if (Maxnumlen-number >= maxnumlen) {
return false;
}
}
return true;
}
Once a validate object is cached for the element, it can be invoked.
Defined:
$ ("#example"). Data ("Validate", {number:2, decimal:2});
Call:
Validatenum ($ ("#example)");