3

Possible Duplicate:
HTML Text Input allow only Numeric input

I used this script

function isNumberKey(evt)
  {
    var e = event || evt; // for trans-browser compatibility
    var charCode = e.which || e.keyCode;                        
    if (charCode > 31 && (charCode < 47 || charCode > 57))
    return false;
    if (e.shiftKey) return false;
    return true;
 }

But this script not working in firefox alone. Any alternatives to restrict user for shift key in javascript?

10
  • 2
    Did you actually use .shiftkey? If yes: It needs to be .shiftKey with an uppercase K Commented Jun 20, 2012 at 6:38
  • I used both with uppercase and lowercase. Not working in Firefox. It allows. Commented Jun 20, 2012 at 6:42
  • It would help if you say what you're trying to accomplish by "restricting" the shift key. Do you want to block any keypress while the shift key is down? Do you want the shift key to have no effect (e.g. shift+a to produce a instead of A)? Commented Jun 20, 2012 at 6:43
  • @itzArun: Then you need to show more code. For example, the binding of the event handler. Maybe you are using the wrong one. Commented Jun 20, 2012 at 6:43
  • I've updated my script. I want to allow only numbers in my textbox field Commented Jun 20, 2012 at 6:46

3 Answers 3

2

event is not defined in Firefox, it's breaking your code. Put it after testing for evt:

function isNumberKey(evt)
  {
    var e = evt || window.event; //window.event is safer, thanks @ThiefMaster
    var charCode = e.which || e.keyCode;                        
    if (charCode > 31 && (charCode < 47 || charCode > 57))
    return false;
    if (e.shiftKey) return false;
    return true;
 }

Fiddle

Check your error console next time. :)

Sign up to request clarification or add additional context in comments.

2 Comments

Another option would be using window.event which is undefined but accessing it won't throw an error.
Nice suggestion, edited answer to include the other possible fix with given credit.
0

Try this code..

$(document).keydown(function (e) {
    if (e.shiftKey) {
        e.preventDefault();
    }
});

check the fiddle..

http://jsfiddle.net/kabichill/8FgH4/

1 Comment

check the fiddle ..jsfiddle.net/kabichill/8FgH4
-2

U can restrict any key press (a group of them also) by using jQuery

$(document).ready(function()
{
     $('#PHONE_NUMBER').keypress(function(e) {
            var a = [];
            var k = e.which;

            for (i = 48; i < 58; i++)
            a.push(i);
            a.push(8);
            if (!(a.indexOf(k)>=0))
                e.preventDefault();
            });
});

Where #phone number is the id of the input element and the for loop and push function pushes only allowed characters. 48 to 58 is key values for digits 0-9 on key board

4 Comments

Jquery is not the magical answer for everything. Stop using jquery for things that simple javescript can solve.
Using jQuery is fine, but creating an array on every key press?!
@ThiefMaster Congrats on the diamond

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.