2

I am trying like this:

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

but it is not working.

3 Answers 3

5

Your can use /^(?=.*\d)(?=.*[a-zA-Z])(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]).{6,}$/ .

  1. (?=.*\d) for at least one digit
  2. (?=.*[a-zA-Z]) for at least one letter
  3. (?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]) for at least one special character

Use match() to find pattern

$('#text').keyup(function() { $(this).css('border', this.value.match(/^(?=.*\d)(?=.*[a-zA-Z])(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]).{6,}$/) ? '5px solid green' : '5px solid red'); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type="text" id="text" />

Or you can also use test() to find match

$('#text').keyup(function() { var re = new RegExp(/^(?=.*\d)(?=.*[a-zA-Z])(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]).{6,}$/); $(this).css('border', re.test(this.value) ? '5px solid green' : '5px solid red'); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type="text" id="text" />

Sign up to request clarification or add additional context in comments.

2 Comments

Case problem - doesn't match for abc1ef. I would suggest /^(?=.*\d)(?=.*[a-zA-Z]).{6,}$/ or /^(?=.*\d)(?=.*[a-z]).{6,}$/i
/^(?=.*\d)(?=.*[a-zA-Z]).{6,}$/
1

You need to remove the parenthesis from around the opening and closing / in your regular expression.

Also, it is possible you wanted to combine the [a-z] and [A-Z] into [a-zA-Z] so that both upper and lower case letters don't have to found, just one of the two.

Comments

0

Try utilizing String.prototype.match

var str1 = "abc1e."; var str2 = "abc1e "; // match special character; any character not digit , // not any alphanumeric character , including `_` , // not space character var res1 = str1.match(/[^\d|\w|\s]/i); // if match found , concat digit , alphanumeric character // if resulting array length is 6 , return `true` , else return `false` res1 = !!res1 ? res1[0].concat(str1.match(/\d+|\w+/i)).length === 6 : false; var res2 = str2.match(/[^\d|\w|\s]/i); res2 = !!res2 ? res2[0].concat(str1.match(/\d+|\w+/i)).length === 6 : false; console.log(res1, res2);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.