Question about Key Codes

J

James Nicholas

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="return acceptCurrencyOnly(this,event,9999.99)">

JavaScript Code:

//**************************************************************************
***********************
// ACCEPTCURRENCYONLY - 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 acceptCurrencyOnly(fld,evt,maxval) {
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.value) * 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((assessthis + "" + Number((charCode - 48),10)),10)
} else {
assessthis = Number((assessthis + "" + Number((charCode - 96),10)),10)
}
} else {
if (charCode == 8) {
// if a backspace was entered, delete the last character.
assessthis = "" + assessthis
assessthis = assessthis.substr(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(assessthis)
} 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
}
 
S

swp

change your INPUT tag to look like this (beware the line wrap):
<INPUT type=text name=samt size=3 maxlength=7 value='' onkeypress="var
keyCode = event.keyCode ? event.keyCode : event.charCode ?
event.charCode : event.which; var key = String.fromCharCode(keyCode);
return /^(\d)$/.test(key);">

this will limit the entered value to only digits and the decimal
point, assuming I haven't made a typo...

then you can validate that entry upon submission of the form,
confident that what you are looking at is a number and not letters or
FLKs. if you really want to validate within the field, add an
"onblur" event to the INPUT tag, just be careful about your return
value and where you set focus when doing so.

hope this helps,

swp
 
J

James Nicholas

Well, that is the thing. The form is not submitted. It is used to do
calculation and in the end, use an innerText property to write the
description of all the calculation. I have it update the innerText using an
onBlur event that calls the function that writes the innerText. So, I have
it only accepting certain characters, but it then bombs out when I enter a
certain series and I am unable to isolate the one character that does it. I
am able to send the actual file if anyone wants to look.

Thank you!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top