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)