Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
125
rated 0 times [  127] [ 2]  / answers: 1 / hits: 114616  / 11 Years ago, mon, august 19, 2013, 12:00:00

I am using lots of HTML 5 input controls in my pages. One of my requirement is to have a textbox with currency feature. For this i tried this:



<input type=number pattern=(d{3})([.])(d{2}) />


This allows me to type values like 10,000.00



But still all its not meeting all my requirements. I want that if the user types in 10000
it should convert it to currency format like 10,000 onblur.



And when i read the value from the input type in my Javascript, it should give me a float instead of a string value which i cannot use to calculate without parsing.


More From » html

 Answers
42

Here's a workaround with an additional input type=text:



http://jsfiddle.net/UEVv6/2/



HTML



<input type=text id=userinput pattern=[0-9]*>
<br>
<input type=number id=number>


JS



document.getElementById(userinput).onblur =function (){    

//number-format the user input
this.value = parseFloat(this.value.replace(/,/g, ))
.toFixed(2)
.toString()
.replace(/B(?=(d{3})+(?!d))/g, ,);

//set the numeric value to a number input
document.getElementById(number).value = this.value.replace(/,/g, )

}


regex is from here How to print a number with commas as thousands separators in JavaScript


[#76284] Friday, August 16, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
eanskylerg

Total Points: 524
Total Questions: 107
Total Answers: 100

Location: Colombia
Member since Mon, May 2, 2022
2 Years ago
;