Here exactly I am trying to make my textbox to accept only numbers. I am able to detect keyCode but event.preventdefault() is not working.
plete html + javascript
<HTML>
<HEAD>
<script src=".7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#textbox1").keydown(function(event) {
try
{
if (event.keyCode >= 48 && event.keyCode <= 57)
{
alert("valid number");
}
else
{
event.preventDefault();
alert("It's NAN");
}
}
catch(ex) {
alert('An error occurred and I need to write some code to handle this!');
}
event.preventDefault();
});
});
</script>
</HEAD>
<BODY>
<FORM>
<input type="text" id="textbox1">
</FORM>
</BODY>
</HTML>
here is the fiddle.
How do I achieve the same?
Here exactly I am trying to make my textbox to accept only numbers. I am able to detect keyCode but event.preventdefault() is not working.
plete html + javascript
<HTML>
<HEAD>
<script src="http://ajax.googleapis./ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#textbox1").keydown(function(event) {
try
{
if (event.keyCode >= 48 && event.keyCode <= 57)
{
alert("valid number");
}
else
{
event.preventDefault();
alert("It's NAN");
}
}
catch(ex) {
alert('An error occurred and I need to write some code to handle this!');
}
event.preventDefault();
});
});
</script>
</HEAD>
<BODY>
<FORM>
<input type="text" id="textbox1">
</FORM>
</BODY>
</HTML>
here is the fiddle.
How do I achieve the same?
Share Improve this question asked Mar 3, 2013 at 19:47 IT pplIT ppl 2,6472 gold badges41 silver badges57 bronze badges 02 Answers
Reset to default 5Use the keypress
event to intercept the change.
Demonstration
As @dystroy said, you should change your keydown to keypress and you should delete the event.preventDefault();
near the end (the second one) to make the code work.
JSFiddle Updated
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745339824a4623273.html
评论列表(0条)