V.Vairaganesh V.Vairaganesh - 8 months ago 60
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}$/;

    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...


Just change your regex like this,

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


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

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