In this blog I will illustrate a currency format($222.00) textbox using JavaScript with compatible all browser.
$222.00 in this format dollar ‘$’ and dot ‘.’ Will accept only one time in the textbox and rest of textbox content only numeric.
<html>
<head>
<script type="text/javascript">
function CurrencyFormat(val, id, e) {
var key = e.keyCode || e.charCode || e.which;
var currentChar = String.fromCharCode(key);
if (e.keyCode == 46 && e.charCode == 0 && e.which == 0) {
$(this).val("");
return true;
}
if (e.keyCode == 36 && e.charCode == 0 && e.which == 0) {
$(this).val("");
return true;
}
if (val.indexOf(currentChar) != -1 && currentChar == ".") {
return false;
}
if (val.indexOf(currentChar) != -1 && currentChar == "$") {
return false;
}
if (key >= 48 && key <= 57 || key == 46 || key == 36 || e.keyCode === 8 || e.keyCode === 9 || e.keyCode === 37 || e.keyCode === 35 || e.keyCode === 39) { $(this).val("");
return true;
}
return false;
}
</script>
</head>
<body>
<input type='text' id='t1' onkeypress='return validateForCharacter(value, id, event)' />
</body>
In this code have a CurrencyFormat() method which have three parameter.
· Value
· Id
· Event.
Value: is for give the textbox value to function.
Id: is for give the textbox id to function.
Event: id for give the Event to text box.
CurrencyFormat() method will call on onkeypress event.
This screen will appear when show your code on browser.
Leave Comment