javascript - HTML 5 Currency format -
this question has answer here:
- html5 form input pattern currency format 7 answers
i using lots of html 5 input controls in pages. 1 of requirement have textbox currency feature. tried this:
<input type="number" pattern="(d{3})([.])(d{2})" />
this allows me type values 10,000.00
but still not meeting requirements. want if user types in 10000 should convert currency format 10,000 onblur.
and when read value input type in javascript, should give me float instead of string value cannot use calculate without parsing.
here's workaround additional input type="text"
:
html
<input type="text" id="userinput" pattern="[0-9]*"> <br> <input type="number" id="number">
js
document.getelementbyid("userinput").onblur =function (){ //number-format user input this.value = parsefloat(this.value.replace(/,/g, "")) .tofixed(2) .tostring() .replace(/\b(?=(\d{3})+(?!\d))/g, ","); //set numeric value number input document.getelementbyid("number").value = this.value.replace(/,/g, "") }
regex here how print number commas thousands separators in javascript
Comments
Post a Comment