I have the below code in a form to re-form the characters entered into it
into a dollar amount and also only accept numeric characters. However, when
I enter the numbers "113" (which appears after the reformatting process as
1.13), it no longer accepts any other characters. I also am not able to
deleted from the text box that I entered it in. I was wondering if anyone
has any ideas why this is happening.
HTML code:
<input type="text" size="3" maxlength="7" name="taxCharge " value="0.00"
onKeyDown="retu rn acceptCurrencyO nly(this,event, 9999.99)">
JavaScript Code:
//*************** *************** *************** *************** **************
*************** ********
// ACCEPTCURRENCYO NLY - Accepts only numbers and makes sure there are always
two decimal places
// fld The field effected
// evt Usually the 'event' statement. I have not had the occasion of it
not being this
// maxval Maximum value of this field
//*************** *************** *************** *************** **************
*************** ********
function acceptCurrencyO nly(fld,evt,max val) {
var charCode = evt.keyCode ? event.keyCode : event.which
// Create the variable to be assessed (current value of the field) and
also create a backup
var assessthis = 0
var oldassessthis = 0
// For finding out the keyCode of a specific key
// If there has already been a character entered
if (fld.value != "") {
assessthis = Number(fld.valu e) * 100
oldassessthis = fld.value
}
// Accept only a numeric value
if (charCode > 31 && (charCode < 48 || charCode > 57) && (charCode < 96 ||
charCode > 105)) {
return false
} else {
if (charCode > 47) {
if (charCode < 96 ) {
assessthis = Number((assesst his + "" + Number((charCod e - 48),10)),10)
} else {
assessthis = Number((assesst his + "" + Number((charCod e - 96),10)),10)
}
} else {
if (charCode == 8) {
// if a backspace was entered, delete the last character.
assessthis = "" + assessthis
assessthis = assessthis.subs tr(0,assessthis .length - 1)
}
}
// Insert decimal
assessthis = assessthis / 100
// check it against the maximum allowed value
if (assessthis <= maxval) {
if (assessthis > 0) {
fld.value = roundIt(assesst his)
} else {
fld.value = "0.00"
}
} else {
fld.value = oldassessthis
}
}
// since we are re-populating the text field, we do not want to return a
value
if (charCode == 9) { return true}
return false
}
Comment