Determines whether the input character is an integer (contains integer format validation)

Source: Internet
Author: User

In the project, many times there will be the existence of the input box, in order to prevent user arbitrary input will have a limit, recently encountered a demand, enter the price can only enter a number type, and enter the number format to be correct, many online search verification number of the format is not verified, so I perfected a bit, The regular expression used, but because of their regular knowledge is really scarce, write relatively low, but also can achieve the final effect bar can be verified by the number type: 0.9, 1, 20 .... Numbers that cannot be verified: 00.9, 0, 09, 99., other strings containing letters, punctuation the input box in the item is the form form of element, and the form comes with a validation rule, which I write in a custom validation rule, but can be used separately if modified
varCheckprice = (rule, value, callback) = ={Let zero=/^0+\d*$///filter data starting with 0 (without decimal points)Let Dublue =/^0{2,}\.\d+$///filter two or more 0 digits before the decimal pointLet point =/^\d+\.? \d+$///starting with a number, you can allow the decimal point to occur once or 0 times, ending with a number (there must be two numbers here)Let Reg =/^[1-9]{1}$///match a case with only one number  if(!value) {Callback (NewError (' Please enter the unit price '))} setTimeout (()= {    if(zero.test (value) | | dublue.test (value)) {//first filter out the wrong dataCallbackNewError (' Enter the correct number format ')//method of adjusting input box verification}Else if(point.test (value) | | reg.test (value)) {//Match Data If the number entered is only one digit when the Reg matchCallback ()//Validation by}Else{Callback (NewError (' Enter the correct number format '))    }  }, 100)}
Can also be written as a function using
functionCheckprice (value) {Let zero=/^0+\d*$///filter data starting with 0 (without decimal points)Let Dublue =/^0{2,}\.\d+$///filter two or more 0 digits before the decimal pointLet point =/^\d+\.? \d+$///starting with a number, you can allow the decimal point to occur once or 0 times, ending with a number (there must be two numbers here)Let Reg =/^[1-9]{1}$///match a case with only one number  if(!value) {    return false  }  if(zero.test (value) | | dublue.test (value)) {//first filter out the wrong data    return false  } Else if(point.test (value) | | reg.test (value)) {//Match Data If the number entered is only one digit when the Reg match    return true  } Else {    return false  }}
Use effect
Because the regular is also smattering, so the writing is very repetitive, if there is an optimization opinion to be able to inform (*^▽^*)

Determines whether the input character is an integer (contains integer format validation)

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.