I am using following code in my-eclipse IDE
.
<html>
<head>
<script>
function isNumberKey(evt){
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)){
return false;
return true;
}
}
</script>
</head>
<body>
<input name="form_number" onkeypress="return isNumberKey(event)" type="text" maxlength="4">
</body>
</html>
This code does not return the proper output when testing on my built-in browser in my IDE
.
I tried to run this snippet on IE (thus using an external browser) and everything seems to work just fine. I feel like there are not mistakes on my code, but I might be wrong. Is my code wrong or is this issue browser related ?
Use parenthesis and try
if (charCode > 31 && (charCode < 48 || charCode > 57)){
return false;
}
else{
return true;
}
You can try below code snippet. it's same as Click
<script type="text/javascript">
function myKeyPress(e){
var keynum;
if(window.event) { // IE
keynum = e.keyCode;
} else if(e.which){ // Netscape/Firefox/Opera
keynum = e.which;
}
if (keynum > 31 && (keynum < 48 || keynum > 57))
return false;
return true;
}
</script>
<form>
<input type="text" onkeypress="return myKeyPress(event)" />
</form>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.