0

Possible Duplicate:
Which is the proper way of filtering numeric values for a text field?

I have a text field in which only numbers are to be allowed. I used regex /^[0-9]/ , its blocking most of the characters. But not option +e , option +n etc.

Using Macosx 10.7.3

Tried blocking alt key altogether on keydown & keyup of Text Field, but no luck

function block(evt) {
if(evt.altKey==true)
return false;
return true;
}

Please suggest.

Community
  • 1
  • 1
Novice User
  • 3,552
  • 6
  • 31
  • 56
  • You should look at this [Stack Overflow Answer](http://stackoverflow.com/questions/10866444/which-is-the-proper-way-of-filtering-numeric-values-for-a-text-field). – Ray Cheng Jun 20 '12 at 19:30

1 Answers1

0

Try this function instead. It looks at the character codes and if it's not a number returns false. The 48 and 57 come from the ASCII table. If you use it on the onkeydown event, it'll prevent pasting too.

function isNumberKey(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode < 48 || charCode > 57)
        return false;

    return true;
}

DEMO

sachleen
  • 30,730
  • 8
  • 78
  • 73