JS verification integer Plus keep the decimal point simple example _javascript technique

Source: Internet
Author: User
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)");

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.