Colin Brazier
asked on
Regular expression with javascript, only numbers required
Hi experts,
Can someone tell me what the below javascript code is doing please? I want to accept only numbers in my form fields, and it seems they accept 'x' (but no other non-numeric characters). Ideally I'd like to accept negative numbers as well.
Here's the html:
Can someone tell me what the below javascript code is doing please? I want to accept only numbers in my form fields, and it seems they accept 'x' (but no other non-numeric characters). Ideally I'd like to accept negative numbers as well.
Here's the html:
<td align="left"><input style="text-align: center;" type="text" name="txtGames<?=$ctr?>" value="1" size="1" maxlength="1" onkeypress="return NumbersOnly(event)"/>
<script type="text/javascript">
function NumbersOnly(e)
{
var keynum;
var keychar;
var numcheck;
if(window.event) // IE
{
keynum = e.keyCode;
}
else if(e.which) // Firefox/Opera
{
keynum = e.which;
}
keychar = String.fromCharCode(keynum);
numcheck = /[^\d\b\0x7F]/;
return !numcheck.test(keychar);
}
</script>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You're welcome.
Glad I could help!
Glad I could help!
ASKER
Thanks Dan.