Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Question about Key Codes

Reply
Thread Tools

Question about Key Codes

 
 
James Nicholas
Guest
Posts: n/a
 
      09-26-2003

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 - 4,10)),10)
} else {
assessthis = Number((assessthis + "" + Number((charCode - 96),10)),10)
}
} else {
if (charCode == {
// 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
}



 
Reply With Quote
 
 
 
 
swp
Guest
Posts: n/a
 
      09-26-2003
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


"James Nicholas" <(E-Mail Removed)> wrote in message news:<OGQcb.7006$(E-Mail Removed)>...
> 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 - 4,10)),10)
> } else {
> assessthis = Number((assessthis + "" + Number((charCode - 96),10)),10)
> }
> } else {
> if (charCode == {
> // 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
> }

 
Reply With Quote
 
 
 
 
James Nicholas
Guest
Posts: n/a
 
      09-26-2003

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!

"swp" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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
>
>
> "James Nicholas" <(E-Mail Removed)> wrote in message

news:<OGQcb.7006$(E-Mail Removed)>...
> > 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 -

4,10)),10)
> > } else {
> > assessthis = Number((assessthis + "" + Number((charCode - 96),10)),10)
> > }
> > } else {
> > if (charCode == {
> > // 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
> > }



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to write codes to connect access database using html codes hiralcp Software 0 12-18-2007 08:49 PM
How to use file I/o codes with form and controls codes Allen ASP .Net 1 12-03-2007 12:04 AM
Virtual Key Codes, Scan Codes and ASCII Codes in C gj_williams2000@yahoo.co.uk C Programming 2 08-20-2005 11:04 AM
RegEx replace of html codes to ascii codes Greg -- ASP .Net 4 08-09-2005 07:27 PM



Advertisments