V.Vairaganesh V.Vairaganesh - 29 days ago 9
HTML Question

Password validation using Regex in Jquery


  1. User Name Field should accept Alphanumeric

  2. Maximum characters will be 16 and Starting should be character not a digit

    var usernametest=$("#userName").val()
    var letters = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,16}$/;


    if(usernametest.match(letters))
    {
    alert('accepted');
    }
    else
    {
    alert('not accepted');
    }



The above code is working fine for accepting alphanumeric charcter.
But it also accepts the starting digit as number.

I want starting digit must be an alphabet. I am new to regular expressions please help me...

Answer

Just change your regex like this,

var letters = /^(?=.*\d)[a-z][a-z\d]{7,15}$/i;

or

var letters = /^(?=[a-zA-Z])(?=.*\d)[A-Za-z\d]{8,16}$/; 

                  ^           ^
                  |           |-Ensures atleast one digit
           satisfies first char to be alphabet condition
Comments