Js limit amount input box (the highest precision is to keep 6 decimal places), js6 bit

Source: Internet
Author: User

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>


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.