To enhance your code, replace [A-Za-z\d]
with {8,64}
.
Here is the updated version:
^(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@#$%^&*()_+])[A-Za-z\d!@#$%^&*()_+.]{8,64}$
Check out the demo
If you want to include the .
in the lookahead as well, consider this modification:
^(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@#$%^&*()_+.])[A-Za-z\d!@#$%^&*()_+.]{8,64}$
^
To prevent consecutive special symbols, add a (?!.*[!@#$%^&*()_+.]{2})
negative lookahead:
^(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@#$%^&*()_+.])(?!.*[!@#$%^&*()_+.]{2})[A-Za-z\d!@#$%^&*()_+.]{8,64}$
^^^^^^^^^^^^^^^^^^^^^^^^
View the demonstration at this link
Note that using such a long regex can lead to maintainability issues. You have the option to split the conditions or utilize a multiline regex with comments:
var rx = RegExp("^" + // Start of string
"(?=.*[a-zA-Z])" + // Require a letter
"(?=.*\\d)" + // Require a digit
"(?=.*[!@#$%^&*()_+])" + // Require a special symbol
"(?!.*[!@#$%^&*()_+.]{2})" + // Disallow consecutive special symbols
"[A-Za-z\\d!@#$%^&*()_+.]{8,64}" + // 8 to 64 symbols from the set
"$");
var re = RegExp("^" + // Start of string
"(?=.*[a-zA-Z])" + // Require a letter
"(?=.*\\d)" + // Require a digit
"(?=.*[!@#$%^&*()_+])" + // Require a special symbol
"(?!.*[!@#$%^&*()_+.]{2})" + // Disallow consecutive special symbols
"[A-Za-z\\d!@#$%^&*()_+.]{8,64}" + // 8 to 64 symbols from the set
"$", "gm");
var str = '.abc@1234\n*abc@1234\nabc@1234.\<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c5aba485a7eba6f4f7f6f1">[email protected]</a>\n*abc@1234\nabc@1234.\<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e08e81a082ce83d1d2d3d4">[email protected]</a>\na@b.#c123\na@__c1234';
while ((m = re.exec(str)) !== null) {
document.body.innerHTML += m[0] + "<br/>";
}