Reputation: 55
I need to validate the field with the following requirements:
I came up with the following regex but it does not always works it allows some special characters which I want to exclude
/^(?!.*(.)\1)((?=.*[^\w\d\s])(?=.*\w)|(?=.*[\d])(?=.*\w)).{6,50}$/
Upvotes: 0
Views: 190
Reputation: 141887
It's because you match with .
at the end, so if all your conditions are met, then any leftover characters up to 50 can be anything. I would use:
/^(?=.{6,50}$)(?=.*[a-zA-Z])(?=.*[\d_.&$*!@-])[a-zA-Z\d_.&$*!@-]*$/
Upvotes: 0
Reputation: 690
I would say you need the following:
(letters or symbols)* letters+ symbols+ (letters or symbols)*
OR
(letters or symbols)* symbols+ letters+ (letters or symbols)*
Upvotes: 0
Reputation: 359966
Does it really have to be a regexp? I would just write a function that tests each of these criteria.
function isValid(password)
{
return password.length >= 6
&& password.length <= 50
&& password.match(/[A-Za-z]/)
&& password.match(/[0-9_\-.&$*!@]/);
}
Isn't that easier?
Upvotes: 7