Copy codeThe Code is as follows:
Function validateNum (obj ){
// Positive integer (cache is used here)
Var number = obj. data (validate). number;
// Decimal point (cache is used here)
Var decimal = obj. data (validate). decimal;
// Dynamic Basic verification Regular Expression
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 equal to the length of the current value and the value does not contain "."
If (maxnumlen = value. length & value. indexOf ('.') <= 0 ){
Return false;
}
// Try to get the index "."
Var valueindexof = value. indexOf ('.');
If (valueindexof> 0 ){
// If the last digit of the index is null, false is returned.
If (value. charAt (valueindexof + 1) = ""){
Return false;
}
}
// The split value can easily determine the content before and after the split
Var valuespenders = value. split ('.');
// If the value length is greater than the defined positive integer Length
If (value. length> number ){
If (valuesplit. length = 1 ){
Return false;
}
// Maximum length-the defined length is too long if it is greater than the maximum length, false.
If (maxnumlen-number> = maxnumlen ){
Return false;
}
}
Return true;
}
After caching A validate object for the element, you can call it.
Definition:
$ ("# Example"). data ("validate", {number: 2, decimal: 2 });
Call:
ValidateNum ($ ("# example ));