Background:In the project, you need to process two decimal point numbers plus or minus floating point numbers,
Requirements:Non-numeric or. characters are automatically cleared, and 0 before. 12 is automatically patched.
Principle:Add two events to the input box. The keyup, blur, and keyup processes non-required characters in the string. The final string is processed in the ideal format 111.11 in blur.
Implementation:Remove the main function through seven regular expressions. val indicates the input string.
/^ \ D *\.? \ D {} $/verify whether val is in ddd. dd format. If yes, It is not processed. If no, it is processed.
/[^. 0-9] +/process all non-numbers and. Characters in val.
/(? : \ D * \. \ d {0, 2} | \ d +)/process val in the correct format.
/^ \ D + $/processing when it is an integer
/^ \. \ D {} $/when no number exists before the decimal point
/^ \ D + \. \ d {0, 2} $/processing when the number of digits after the decimal point is between 0 and 2
/^ 0 + [1-9] \ d *\.? \ D {} $/val processing with 0 in front
Test data:
Fdfd100.12 122.121 12t12. 57 12546 1245. 125.4 154.45.456. 126 005245.121 dfdffd, etc.
Download Demo