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
It seems that you filter the 45 charcode
Will work better.
Start with hyphen when you specify a range in your regular expressions.
If you want to match a date the pattern is probably something like dd-dd-dd (but which format? ISO YYYY-MM-DD ? or something else)
A more correct pattern will be.
Probably this one is better
For the DD-MM-YYYY reverting the pattern is quite simple :