Simple Example of js verification integer plus decimal point retention

Source: Internet
Author: User

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 ));

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.