How to validate textbox for numeric entry only in jquery

searchsanjaysharma
searchsanjaysharma used Ask the Experts™
on
I have a  textbox txtage. I want user should be able to enter number only. otherwise raise an exception.
The number should be between 1 and 12 only
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
leakim971Multitechnician
Top Expert 2014
Commented:
<select>
<option val="1">1</option>
<option val="2">2</option>
<option val="3">3</option>
<option val="4">4</option>
<option val="5">5</option>
<option val="6">6</option>
<option val="7">7</option>
<option val="8">8</option>
<option val="9">9</option>
<option val="10">10</option>
<option val="11">11</option>
<option val="12">12</option>
</select>

Open in new window

Multitechnician
Top Expert 2014
Commented:
else check this page : http://jsfiddle.net/ps4a4/
$(document).ready(function() {
    window.old = $("#month").val();
    $("#month").keydown(function(e) {
        var k = e.keyCode;
        if((k<48||k>57)&&(k<96||k>105)) e.preventDefault(); 
    }).keyup(function(e) {
        var v = $(this).val() * 1;
        if(v<1||v>12) $(this).val(window.old);
        else window.old = v;
    });
})¿

Open in new window

Author

Commented:
ok
leakim971Multitechnician
Top Expert 2014

Commented:
Did I miss something?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial