• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1450
  • Last Modified:

Javascript 'keyCode' and 'onkeypress' Browser Incosistencies

I have this Javascript funtion that checks keyboard input to verify numbers only and then sets a textbox on the page.

I have this function working fine in IE7 but in FireFox 2.x it does not work.
I get an error message in the FireFox console: "setting a property that only has a getter" and the line in question contains:
evt.keyCode = 0;

I have tried changing the Javascript function from firing at 'onkeypress' to 'onkeyup' / 'onkeydown' but it still won't work in FireFox.
function cmvFireFox(maskControl, evt) {
     var strText = maskControl.value;
      if ((evt.keyCode < 48) || (evt.keyCode > 58))
      {
            evt.keyCode = 0;
      }
      if (evt.keyCode < 58)
      {            
            //It's a number
            if(strText.length == 1)
            {
                if(parseInt(strText) > 2)
                {
                    strText = '0' + strText + ':';
                    maskControl.value = strText;
                }
                else if(parseInt(strText) == 2)
                {
                    if (evt.keyCode > 51)
                    {
                        evt.keyCode = 0;
                    }
                }
            }
            
            if(strText.length == 2)
            {
                if (strText.search(/:/) < 0)
                {
                    strText = strText + ':';
                    maskControl.value = strText;
                }
            }
            
            if(strText.length == 3)
            {
                if (evt.keyCode > 53)
                {
                    evt.keyCode = 0;
                }
            }
      }
      else
      {
            //It's a ":"
            if (strText.search(/:/)>-1)
            {
            //Only Allow one ":"
                evt.keyCode = 0;
                return;
            }
            if (strText.length < 1 || strText.length > 2)
            {
                evt.keyCode = 0;
                return;
            }
            else
            {
                if(strText.length == 1)
                {
                    maskControl.value = '0' + strText;
                }
            }
      }
}

Open in new window

0
Seany84
Asked:
Seany84
2 Solutions
 
vibrazyCommented:
Hi,

I think its basically saying that you cannot assign a variable to a event keycode(evt.keyCode = 0;)

Why are you assigning this variable? can you replace it with var keyCodeTemp = 0;?

Regards,
Dan
0
 
svelarsenCommented:
You can use this:

var tmpKeyCode = evt.keyCode;

Then you can later use that variable instead of evt.keyCode and you can assign values to it.
0
 
SreejithGCommented:
0
 
JorisWCommented:
setting the keyCode or charCode is not supported in firefox. Since you all you are doing is setting it to 0, you could just prevent the default.

So the solution would be to call "evt.preventDefault()" instead of "evt.keyCode = 0", also this has to be done in the keydown event. (note that in IE, instead of "evt.preventDefault()", you need to use "evt.returnValue = false;")
0
 
JorisWCommented:
keypress event also works, but not keyup
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now