RegEx for Password Validation (ASP)

前端 未结 3 647
北荒
北荒 2020-12-10 21:58

Can someone suggest a regular expression for validating a password with the following conditions.

  • Password must be at least 12 characters long
  • Passwo
3条回答
  •  失恋的感觉
    2020-12-10 22:20

    It really doesn't make much sense to try and do this with a single expression, as awesome as that sounds...

    I would do it with multiple expressions, because they will run faster, look cleaner, be more maintainable, and the results can be used to provide feedback as to WHY the password doesn't match (should you want to be that nice)

    These expressions will validate as indicated:

    .{12,}            # Password must be at least 12 characters long
    ^(?!\d)           # Password must not begin with a number
    (.*[A-Z].*)+      # At least one upper case letter (A-Z)
    (.*[a-z].*)+      # At least one lower case letter (a-z)
    (.*[0-9].*)+      # At least one number (0-9)
    (.*[-_$#].*)+     # At least one of the following symbols: hyphen ( - ), underscore ( _ ), dollar ( $ ), pound/hash ( # )
    

    Evaluate each one to a boolean, then provide feedback if enough of them are not satisfied

    That being said, this is a very restrictive password policy - just the not starting with a number makes it considerably easier to guess.

    I think it COULD be put into one regex, but idunno - I might get around to it

提交回复
热议问题