How to realize the regular expression of converting the format of number and amount by JavaScript
This article mainly introduces JavaScript how to achieve the regular expression of digital, amount format conversion, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
Regular expression for conversion of numeric and amount formats
Input input real-time judgment input is in amount format
Function checkInput (obj) {var t = obj.value.charAt (0); obj.value = obj.value.replace (/ [^\ d.] / g, "); / / clear" number "and". " Characters other than obj.value = obj.value.replace (/ ^ [0] + [0-9] * $/ gi, ""); / / the first digit cannot be 0 obj.value = obj.value.replace (/ ^\. / g, ""); / / verify that the first character is a number instead of. Obj.value = obj.value.replace (/\. {2,} / g, "."); / / keep only the first one. Clear the excess obj.value = obj.value.replace (".", "$# $"). Replace (/\. / g, "). Replace (" $# $",". "); obj.value = obj.value.replace (/ ^ (\ -) * (\ d+)\. (\ d\ d). * $,'$1 $2.03') / / you can enter only two decimal if (t = ='-') {obj.value ='-'+ obj.value;}}
Convert the number to the amount format and retain two decimal places: convert 1234567 to 1234567.00
/ / s is a number, n is the number of decimal places function fmoney (s, n) {n = n > 0 & & n