最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Regex for Password validation in Javascript - Stack Overflow

programmeradmin4浏览0评论

Regex Password complexity requires that any three of the following four characteristics must be applied when creating or changing a password.

  • Alpha characters - at least 1 upper case alpha character
  • Alpha characters - at least 1 lower case alpha character
  • Numeric characters - at least 1 numeric character
  • Special characters - at least 1 special character

I am trying with the following code, but its not working for special characters

(?=^.{6,}$)((?=.*\d)(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[^A-Za-z0-9])(?=.*[a-z])|(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9]))^.*

I want my regex to be validated against the following 4 cases

Match cases

  • P@ssword
  • Password1
  • p@ssword1
  • p@12345

Regex Password complexity requires that any three of the following four characteristics must be applied when creating or changing a password.

  • Alpha characters - at least 1 upper case alpha character
  • Alpha characters - at least 1 lower case alpha character
  • Numeric characters - at least 1 numeric character
  • Special characters - at least 1 special character

I am trying with the following code, but its not working for special characters

(?=^.{6,}$)((?=.*\d)(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[^A-Za-z0-9])(?=.*[a-z])|(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9]))^.*

I want my regex to be validated against the following 4 cases

Match cases

  • P@ssword
  • Password1
  • p@ssword1
  • p@12345
Share Improve this question asked Jun 14, 2013 at 7:07 sivanvsivanv 1711 gold badge2 silver badges6 bronze badges 1
  • possible duplicate of RegEx question for password strength validation – Toto Commented Jun 14, 2013 at 13:08
Add a comment  | 

7 Answers 7

Reset to default 11

I think that a regex you can use is:

(?=^.{6,}$)(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[^A-Za-z0-9]).*

I'm not sure why you have so many or operators in your regex but this one matches if:

  • (?=^.{6,}$) - String is > 5 chars
  • (?=.*[0-9]) - Contains a digit
  • (?=.*[A-Z]) - Contains an uppercase letter
  • (?=.*[a-z]) - Contains a lowercase letter
  • (?=.*[^A-Za-z0-9]) - A character not being alphanumeric.

I think a single regex will be messy in this case. You can easily do something like

var count = 0;

count += /[a-z]/.test(password) ? 1 : 0;
count += /[A-Z]/.test(password) ? 1 : 0;
count += /\d/.test(password) ? 1 : 0;
count += /[@]/.test(password) ? 1 : 0;

if(count > 2) {
    alert('valid')
}

Use this Regex :

(?=^.{6,10}$)(?=.\d)(?=.[a-z])(?=.[A-Z])(?=.[!@#$%^&*()_+}{":;'?/>.<,])(?!.\s).$**

I think you would need this for all special characters too : [updated to reject space]

    $(document).ready(function(){
    $('input').change(function(){
    var count = 0;
    var pass = $(this).val();
        count += /[a-z]/.test(pass) ? 1 : 0;
        count += /[A-Z]/.test(pass) ? 1 : 0;
        count += /\d/.test(pass) ? 1 : 0;
        count += /[^\w\d\s]/.test(pass) ? 1 : 0;
        (count>2 & !/[\s]+/.test(pass)) ? $(this).css('background-color','lime'):$(this).css('background-color','orange');
    });

});

and the fiddle : jsFiddle

var pattern = new RegExp(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]).{6,}$/);

    if(pattern.test(value)){
         return true;
    } else {
         return false;
    }

Its working fine with special character also.

Non English UTF-8

None of the solutions given allow international letters, i.e. éÉöÖæÆáÁ, but mainly focus on the english ASCII alphabet.

The following regEx uses unicode, UTF-8, to recognise upper and lower case and thus, allow international characters:

// Match uppercase, lowercase, digit or #$!%*?& and make sure the length is 6 to 36 in length  
const pwdFilter = /^(?=.*\p{Ll})(?=.*\p{Lu})(?=.*[\d|@#$!%*?&])[\p{L}\d@#$!%*?&]{6,36}$/gu

if (!pwdFilter.test(pwd)) {
    // Show error that password has to be adjusted to match criteria
}

The regEx:

/^(?=.*\p{Ll})(?=.*\p{Lu})(?=.*[\d|@#$!%*?&])[\p{L}\d@#$!%*?&]{6,36}$/gmu

checks if an uppercase, lowercase, digit or @#$!%*?& are used in the password. It also limits the length to be 6 minimum and maximum 36 (note that emojis,

发布评论

评论列表(0)

  1. 暂无评论