Home  >  Article  >  Web Front-end  >  A simple example of js validating integers plus retained decimal points_javascript skills

A simple example of js validating integers plus retained decimal points_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:11:101455browse
Copy code The code is as follows:

function validateNum(obj) {
//Positive integer (this 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
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 have "."
if (maxnumlen == value.length && value.indexOf('.') <= 0) {
return false;
}
//Try to get the index of "."
var valueindexof = value.indexOf('.');
if (valueindexof > 0) { // If". "One after the index is empty, then it must be returned to false
if (value.charat (valueINDEXOF 1) ==" ") {
Return false;
}



}
//Split the value to easily determine the content before and after
var valuesplit = value.split('.');
//If the length of the value is greater than the defined positive integer length
if ( value.length > number) {
                                                                                                                                                                                                       .
        if (maxnumlen - number >= maxnumlen) {
                                                                                                                                                                                                        After caching a validate object, just call it.
Definition:
$("#example").data("validate", { number: 2, decimal: 2 });
Call:
validateNum($("#example)) ;
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn