Can someone show me some example for restricting user input (on input tag) in Javascript?
Something that when we set the input (type="text") to only accept numeric, so it will ignore any other input except for numeric...
I think it's handy for number input (such as zip, credit card, money, value, score, date etc...), and if you can please show me how to create input with pattern, something like:
Please Input Date: |-----------------| | / / | |-----------------|
PS: I heard WebForms 2.0 will support this in the future... (Acid 3 compliant browser?)
input type="date"
input type="time"
input type="number"
input type="money"
But it was only news from future :D
This might help you.
http://www.w3schools.com/jsref/event_onkeydown.asp
<html>
<body>
<script type="text/javascript">
function noNumbers(e)
{
var keynum;
var keychar;
var numcheck;
if(window.event) // IE
{
keynum = e.keyCode;
}
else if(e.which) // Netscape/Firefox/Opera
{
keynum = e.which;
}
keychar = String.fromCharCode(keynum);
numcheck = /\d/;
return !numcheck.test(keychar);
}
</script>
<form>
<input type="text" onkeydown="return noNumbers(event)" />
</form>
</body>
</html>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With