473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Question about Key Codes


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
}

Jul 20 '05 #1
2 5750
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
"James Nicholas" <wa********@verizon.net> wrote in message news:<OG*****************@nwrddc02.gnilink.net>...
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
}

Jul 20 '05 #2

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" <DS******@aol.com> wrote in message
news:a5**************************@posting.google.c 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" <wa********@verizon.net> wrote in message

news:<OG*****************@nwrddc02.gnilink.net>...
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
}

Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Dan | last post by:
Hi Gurus I got a very basic question to ask: When a .NET exe (MSIL) is first run, the JIT-compiler will converts the IL into native codes so that it can executes on the current machine. my...
1
by: Dave | last post by:
Is it possible to get <codes><code id="4"><name>abc</name></code></codes from the XML below in single SelectSingleNode/xPath expression step OR is going to have to be a multi=step process of...
1
by: Bruce | last post by:
Hi, there, I meet a problem about comboBox binding. -------------------- Database: Northwind Tables: 1) Products 2) Categories I create a form (named "form1") to edit the record from...
1
by: chen | last post by:
We're having an internal debate about the merits & demerits of returning status codes in the output message vs exceptions to signify errors in handling a Web method. The status code camp is...
2
by: andrew queisser | last post by:
Hi all, I'm working on a small embedded system (think microcontroller with uC/OS-II) and I'm considering which convention to use for error handling in our internal "API". Since I'm using two...
10
by: mattdaddym | last post by:
Hi all, I'm writing an application in asp .net and vb .net. The question I have just verifies I need to take an official programming course instead of this "learning on the fly" stuff :) ...
10
by: Kenneth Lantrip | last post by:
My question is: Is there a generally accepted way to clear the screen and perhaps move the cursor to a new location (other than newline) for the next printf in standard c? What about ANSI...
2
by: Dave.Sun.Moon | last post by:
Dear all, I am not a professional programmer. In stead, I am using C++ mostly for my research work. My knowledge of C++ is only good enough for my computation. I really don't use the advanced...
2
by: Dorish3 | last post by:
I have a query of which the criteria is set to ....it looks up the event number in a single table called tblEvents and gives me the Event Number, Date of Event, Time of Event, and Event Name. I...
6
by: Andy Leese | last post by:
Beginner Question: ASCII Symbols I am using Borland C++ and programming under DOS. I wish to display the symbols of the early ASCII character set... For example: cout << char(7); ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.