Js limit amount input box (the highest precision is to keep 6 decimal places), js6 bit
Js limit amount input box (the highest precision is to keep 6 decimal places)
Note: A project has a limit of 6 decimal places. Some bugs found during this test
1. If you can press or press the keyboard without entering multiple decimal points
2. You can enter '1... 234. 'by means of a long press .'
This time, I learned the regular expression while solving this bug. I used to think that the regular expression is only used for field research, but now I found that it is used to match characters, in addition, you can do a lot of things by combining the APIS provided by various languages. Here, we post the code for your reference.
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> <meta name = "Generator "CONTENT =" EditPlus "> <meta name =" Author "CONTENT =" "> <meta name =" Keywords "CONTENT =" "> <meta name =" Description "CONTENT = ""> <script> function formatMoneyWith6digts2 (obj) {var p1 =/[^ \ d \.] /g; // filter non-numbers and decimal places/g: Filter var p2 in all ranges = /(\. \ d {6}) \ d * $/g; var p4 = /(\.) (\ d *) \ 1/g; obj. value = obj. value. replace (p1 ,""). replace (p2, "$1 "). replace (p4, "$1 $2"); obj. value = obj. value. replace (/[^ 0-9.] /g, ''); // fix bug: Invalid char '. 'var p5 = /\. +/g; // If there are multiple vertices, only one vertex is taken, shielding 1 .... 234 case obj. value = obj. value. replace (p5 ,". ") var p6 = /(\. +) (\ d + )(\. +)/g; // mask 1 .... 234. obj. value = obj. value. replace (p6, "$1 $2") // mask the last one. // end fix bug: Invalid char '. '}/* testingvar p5 = /\. +/g; var testStr = '1 .... 234. 'matchstr = testStr. match (p5); // alert (matchStr); num = testStr. replace (p5 ,". ") // alert (num); // var p6 = /(\. +) (\ d + )(\. +)/g; matchStr = testStr. match (p6); alert (matchStr); num = testStr. replace (p6, "$1 $2") alert (num); */</script> </HEAD> <BODY> a maximum of six decimal places can be entered: <input type = 'text' onkeyup = "formatMoneyWith6digts2 (this); countShuilv ();"/> </BODY> </HTML>