![Firefox logo](https://assets-prod.sumo.prod.webservices.mozgcp.net/media/uploads/products/2020-04-14-08-36-13-8dda6f.png)
Why Javascript is not working properly?
I'm working on a web page and trying this script but it is not working (although it is working fine in IE). The code is given below:
My function is:
function validateNumber() { var x=window.event.keyCode; //alert(x); if(!(x>=48 && x<=57)) { alert("Please type a number between 0 and 9 !!"); } }
I'm using it like:
<input type="text" name="cust_age" value=0 size="15" onkeypress="return validateNumber()" style="font-size: 16pt" />
Please let me know how to resolve this problem.
Geändert am
Ausgewählte Lösung
Firefox doesn't use a global window event. On Firefox you need to pass the event to the function.
I think that this should work (I'm not an expert):
<input type="text" name="cust_age" value=0 size="15" onkeypress="return validateNumber(event)" style="font-size: 16pt" /> function validateNumber(event) { var x=window.event? window.event.keyCode : event; //alert(x); if(!(x>=48 && x<=57)) { alert("Please type a number between 0 and 9 !!"); } }Diese Antwort im Kontext lesen 👍 3
Alle Antworten (2)
Ausgewählte Lösung
Firefox doesn't use a global window event. On Firefox you need to pass the event to the function.
I think that this should work (I'm not an expert):
<input type="text" name="cust_age" value=0 size="15" onkeypress="return validateNumber(event)" style="font-size: 16pt" /> function validateNumber(event) { var x=window.event? window.event.keyCode : event; //alert(x); if(!(x>=48 && x<=57)) { alert("Please type a number between 0 and 9 !!"); } }
Thanks cor-el, that helped! :)