HOW TO PREVENT ZERO AS FIRST DIGIT IN AN NUMERIC ONLY TEXTFIELD IN JQUERY?

Maria Susan

Total Post:29

Points:107
Posted by  Maria Susan
 1312  View(s)
Ratings:
Rate this:
How do I restrict if the first digit is zero (0)

$('.btn').keypress(function(e) {

      var code = e.which;

      if(($(this).val().indexOf(".") == -1 && code == 46) || (code >= 48 && code <= 57) || (code == 8) || (code >= 48 && code <= 57)){

          return true;

        }      

       return false;

    })

    .bind('paste',function(e) {

    e.preventDefault();

});   

  1. Allen Scott

    Post:46

    Points:324
    Re: How to Prevent zero as first digit in an numeric only textfield in jquery?

    just try this:


    if(pos == 0 && code == 48) {

        e.preventDefault();

    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!