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

Seany84Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.