Question about Key Codes

Discussion in 'Javascript' started by James Nicholas, Sep 26, 2003.

  1. 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
    }
    James Nicholas, Sep 26, 2003
    #1
    1. Advertising

  2. James Nicholas

    swp Guest

    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" <> wrote in message news:<OGQcb.7006$>...
    > 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
    > }
    swp, Sep 26, 2003
    #2
    1. Advertising

  3. 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" <> wrote in message
    news:...
    > 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" <> wrote in message

    news:<OGQcb.7006$>...
    > > 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
    > > }
    James Nicholas, Sep 26, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg  --
    Replies:
    4
    Views:
    2,152
  2. Tor Iver Wilhelmsen

    J2ME: Getting key codes on SE K700i

    Tor Iver Wilhelmsen, Apr 24, 2005, in forum: Java
    Replies:
    1
    Views:
    679
    Darryl Pierce
    Apr 25, 2005
  3. Replies:
    2
    Views:
    2,818
    Malcolm
    Aug 20, 2005
  4. Allen
    Replies:
    1
    Views:
    641
    Mark Rae [MVP]
    Dec 3, 2007
  5. M P
    Replies:
    1
    Views:
    464
Loading...

Share This Page