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?

Community
  • 1
  • 1
itzArun
  • 681
  • 5
  • 11
  • 19
  • 2
    Did you actually use `.shiftkey`? If yes: It needs to be `.shiftKey` with an uppercase `K` – ThiefMaster Jun 20 '12 at 06:38
  • I used both with uppercase and lowercase. Not working in Firefox. It allows. – itzArun Jun 20 '12 at 06: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`)? – lanzz Jun 20 '12 at 06:43
  • @itzArun: Then you need to show more code. For example, the binding of the event handler. Maybe you are using the wrong one. – ThiefMaster Jun 20 '12 at 06:43
  • I've updated my script. I want to allow only numbers in my textbox field – itzArun Jun 20 '12 at 06:46
  • See [HTML Text Input allow only Numeric input](http://stackoverflow.com/a/469419/298479) – ThiefMaster Jun 20 '12 at 06:48
  • @ThiefMaster, What about disabling the shiftkey? I dont think this question is functionally duplicated with the one you linked. – Starx Jun 20 '12 at 06:55
  • @Starx: According to his comment "I want to allow only numbers in my textbox field" that's what he actually wants to achieve. – ThiefMaster Jun 20 '12 at 06:56
  • @ThiefMaster, I understand the reason, why you did so. But the original question was to block `shift` key which was a different case IMO. Although, what he wanted was totally different from what he needed. – Starx Jun 20 '12 at 07:00
  • **USE REGULAR EXPRESSIONS AND NOT KEYCODES, DIFFERENT KEYBOARDS AND ANY BROWSERS ANSWER DIFFERENT RESPONSE. EXAMPLE THAT WORK IN DIFFERENT BROWSERS...** http://jsfiddle.net/rogeriodemoraes/pkaq2kng/ – Rogerio de Moraes Dec 26 '14 at 10:16

3 Answers3

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. :)

Fabrício Matté
  • 69,329
  • 26
  • 129
  • 166
0

Try this code..

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

check the fiddle..

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

Kabilan S
  • 1,104
  • 4
  • 15
  • 31
-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