I have the following code which will allow only numbers 0-9.
But i want to allow -(hyphon) also.[- ASCII code is 45]
I tried it.. But no use.. Can you update my code?
function isNumericKey(e)
{
if (window.event) { var charCode = window.event.keyCode; }
else if (e) { var charCode = e.which; }
else { return true; }
if (charCode > 31 && (charCode < 48 || charCode > 57)) { return false; }
return true;
}
function submitMyNumber()
{
var input = document.getElementById('myInput').value;
return input.match(/^[0-9-]+$/) != null;
}
<form>
<input type="text" id="myInput" name="myInput" onkeypress="return isNumericKey(event);" /><br />
<input type="submit" id="mySubmit" name="mySubmit" value="Submit My Number" onclick="return submitMyNumber();" />
</form></pre>
Laxman Chowdary
return true
when you get no event? shouldn'treturn false
? – Hachi