Question about Key Codes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James Nicholas

    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="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
    }



  • swp

    #2
    Re: Question about Key Codes

    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.fromChar Code(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" <wastrilith@ver izon.net> wrote in message news:<OGQcb.700 6$FH3.2465@nwrd dc02.gnilink.ne t>...[color=blue]
    > 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
    > }[/color]

    Comment

    • James Nicholas

      #3
      Re: Question about Key Codes


      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" <DSAsteve@aol.c om> wrote in message
      news:a59bd1d2.0 309260458.76dc7 8a0@posting.goo gle.com...[color=blue]
      > 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.fromChar Code(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" <wastrilith@ver izon.net> wrote in message[/color]
      news:<OGQcb.700 6$FH3.2465@nwrd dc02.gnilink.ne t>...[color=blue][color=green]
      > > I have the below code in a form to re-form the characters entered into[/color][/color]
      it[color=blue][color=green]
      > > into a dollar amount and also only accept numeric characters. However,[/color][/color]
      when[color=blue][color=green]
      > > I enter the numbers "113" (which appears after the reformatting process[/color][/color]
      as[color=blue][color=green]
      > > 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[/color][/color]
      anyone[color=blue][color=green]
      > > 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:
      > >
      > >[/color][/color]
      //*************** *************** *************** *************** **************[color=blue][color=green]
      > > *************** ********
      > > // ACCEPTCURRENCYO NLY - Accepts only numbers and makes sure there are[/color][/color]
      always[color=blue][color=green]
      > > two decimal places
      > > // fld The field effected
      > > // evt Usually the 'event' statement. I have not had the occasion of[/color][/color]
      it[color=blue][color=green]
      > > not being this
      > > // maxval Maximum value of this field
      > >[/color][/color]
      //*************** *************** *************** *************** **************[color=blue][color=green]
      > > *************** ********
      > > 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 <[/color][/color]
      96 ||[color=blue][color=green]
      > > charCode > 105)) {
      > > return false
      > > } else {
      > > if (charCode > 47) {
      > > if (charCode < 96 ) {
      > > assessthis = Number((assesst his + "" + Number((charCod e -[/color][/color]
      48),10)),10)[color=blue][color=green]
      > > } 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[/color][/color]
      a[color=blue][color=green]
      > > value
      > > if (charCode == 9) { return true}
      > > return false
      > > }[/color][/color]


      Comment

      Working...