Number Validation on text field in jquery/javascript

If you want client side validation on the text field to accept only numbers, then you can use the below function :

function allowNumbersOnly(event)
{
    var e = event;
    var keyCode = e.which || e.keyCode;

    if (keyCode> 31 && (keyCode< 48 || keyCode> 57))
        return false;

    return true;

}

The above function return true if number is entered, else false.
Call this function on keypress() event as $(“input#num”).keypress(allowNumbersOnly)

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>