Seany84
asked on
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.
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;
}
}
}
}
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.
var tmpKeyCode = evt.keyCode;
Then you can later use that variable instead of evt.keyCode and you can assign values to it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
keypress event also works, but not keyup
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