I am trying like this:
(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}$/)
but it is not working.
Your can use /^(?=.*\d)(?=.*[a-zA-Z])(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]).{6,}$/ .
(?=.*\d) for at least one digit(?=.*[a-zA-Z]) for at least one letter(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]) for at least one special characterUse 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" /> 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);