Question

I have a from with two text box (username, cell #)

I have two JavaScript functions to validate the user input with onKeyPress but I don't know why it is not working in FF while it works fine in IE and Chrome!

Here is my code:

<!DOCTYPE HTML><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>JavaSCript Input Test</title>

<script>
/* Only numbers in mobile text-box */
function ValidateKeyNumber(event) 
{   
    var key=(event||window.event).keyCode;
    var allowed='0123456789';

    return allowed.indexOf(String.fromCharCode(key)) !=-1 ;
}
function ValidateKeyText(event) 
{   
    var key=(event||window.event).keyCode;
    var allowed='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ';

    return allowed.indexOf(String.fromCharCode(key)) !=-1 ;
}
</script>
</head>

<body>
<form>
<label>Userame:</label>
<input type="text" size="30" name="Username" onKeyPress="return ValidateKeyText(event)" />
<br/><br/>
<label>Cell Number:</label>
<input type="text" name="cell" onKeyPress="return ValidateKeyNumber(event)"/>
</form>
</body>
</html>

Can you please help in this? Please I need the solution in JavaScript

Thanks in advance.

Was it helpful?

Solution

try this

var key = event.which || event.keyCode || event.charCode;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top