Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Assistance With Password Regular Expression

Last post 07-02-2008, 7:30 PM by Aussie Susan. 2 replies.
Sort Posts: Previous Next
  •  07-02-2008, 5:42 PM 43704

    Assistance With Password Regular Expression

    We have new rules with for our password validation.  Which used to be fairly simple.  Now this doesn't seem to difficult but with my inexperience with regular expressions I am trying to out how to get this one to pass. 

     I am using javascript to do client side processing on the password before we allow it through when the user changes the password.  So the rule I am getting stuck on is the following:

    • Must contain at least 2 numbers and/or special characters
    • Special characters include % - _ ! +

    Let me add that we do have more rules to the password and I am checking all of the following rules before I am getting to this one.  And those rules include:

    • 8 characters minimum
    • 15 characters maximum
    • First character must be alpha

    Any help with this will be greatly appreciated!

    Thanks!

    Jeremy Wickham
     

  •  07-02-2008, 5:47 PM 43705 in reply to 43704

    Re: Assistance With Password Regular Expression

    var re = /(?=.*[\d%_!+-].*[\d%_!+-]).*/;

    For the 3 additional rules I missed on my first read of the question:

    var re = /(?=^[A-Za-z].{7,14}$)(?=.*[\d%_!+-].*[\d%_!+-]).*/;




    looking for a new regex book?
    Regular Expressions Cookbook
  •  07-02-2008, 7:30 PM 43711 in reply to 43704

    Re: Assistance With Password Regular Expression

    My suggestion would be:

    ^(?=(.*?[\d%_!+-]){2})[a-z].{7,14}$

    This assumes that the 'ignore case' option is set. If you want to, you can change the '[a-z]' to '[a-zA-Z]' to make just this part case insensitive. Also, this does all pf the tests you mention in the single regex.

    If the set of "special characters" is to be altered, then simply change the character set within the lookahead.

    Note that the final quantifier has the min and max values 1 less than those you specified because of the leading alpha check.

    I have not added any quoting etc that may be needed to make this suitable for javascript.

    Susan



     

View as RSS news feed in XML