Monday, November 24, 2014

JavaScript RegEx to check for strong password

The following code will match RegEx against a password to check if it meets strong password complexity requirements (Passwords must be at least eight characters including at least one number and one special character (punctuation, symbol or upper/lower case character).
var password = "Mypassword01";
var regexString = "^((?=.{8,}$)(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*|(?=.{8,}$)(?=.*\d)(?=.*[a-zA-Z])(?=.*[!\u0022#$%&'()*+,./:;⇔?@[\]\^_`{|}~-]).*)";
var regexx = new RegExp(regexString);

if (!regexx.test(password)) {
  // password did not match regex

No comments:

Post a Comment