Mozilla onkeypress issues...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rrocket
    New Member
    • Aug 2007
    • 116

    Mozilla onkeypress issues...

    Anyone know how to get onkeypress to work with keycode? I need to figure out what the keycode is before the character shows up in a textbox. Here is the code I have so far (see below). I can get the value to show with onkeydown or up, but it shows the typed value in the textbox even if it does not fit my criteria.

    [code=javascript]
    //Call it with onkeypress = return stripper(event)
    function stripper(e)
    {
    var bRes = false;
    if((e.keyCode >= 48)&&(e.keyCode <=57))
    {
    alert(e.keyCode );
    bRes=true;
    }
    else
    {
    alert("Numeric Values Only");

    }
    return bRes;
    }
    [/code]

    Basically what this is supposed to do is not show the typed character if it is anything but a number. With onkeypress Mozilla returns a 0 for the keyCode... No issues with IE.
    Last edited by rrocket; Jan 24 '08, 03:03 PM. Reason: Added a little extra info...
  • rrocket
    New Member
    • Aug 2007
    • 116

    #2
    Found the answer on another post... Sort of anyway.


    Here is the code just to make things easier:
    [code=javascript]
    //call it with onkeypress="ret urn stripper(event) "
    function stripper(e)
    {
    var bRes = false;
    if(e.keyCode > 0)
    {
    if((e.keyCode >= 48)&&(e.keyCode <= 57))
    {
    bRes=true
    }
    else
    {
    alert("Numeric values only");
    }
    }
    else
    {
    if((e.charCode >= 48)&&(e.charCod e<=57))
    {
    //alert(e.charCod e);
    bRes=true;
    }
    else
    {
    alert("Numeric Values Only");
    //alert(e.charCod e);
    }
    }
    return bRes;
    }
    [/code]

    keyCode works fine in IE and charCode works for Mozilla... keyCode comes out as 0 in Mozilla so I check for that first as sort of a browser check and everyone seems happy at the moment.

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5388

      #3
      hi ...

      glad to hear you got it working ... just a note in case you want to write it a little bit shorter :)

      [CODE=javascript]function stripper(e) {
      var bRes = false;
      var func = { 'true': 'keyCode', 'false': 'charCode' };
      var val = (e.keyCode > 0).toString();

      if (e[func[val]] >= 48 && e[func[val]] <= 57) {
      bRes=true
      } else {
      alert("Numeric values only");
      }

      return bRes;
      }
      [/CODE]
      kind regards

      Comment

      • rrocket
        New Member
        • Aug 2007
        • 116

        #4
        Cool thanks. Your version looks less like a hack, LOL. I am not familiar with what you actually did... What would I search for to get more information on that method?

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5388

          #5
          method? :) ... it simply uses a kind of dynamic function calls that would depend on the condition. the 'magic' is to use an object to store the function-names. the basics are:

          [CODE=javascript]
          var obj = {
          foo: function() { alert('foo'); },
          bar: function() { alert('bar'); }
          };

          // we may refer with:
          obj.foo();

          // or even:
          obj['foo']();
          [/CODE]
          kind regards

          Comment

          • rrocket
            New Member
            • Aug 2007
            • 116

            #6
            Great, thanks for your help.

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5388

              #7
              no problem :) post back to the forums anytime you have more questions ...

              kind regards

              Comment

              Working...