Regex Help needed with field validation
I need to validate the field with the following requirements:
- must be at least 6 characters
- must be no more than 50 characters.
- must contain at least one alphabetical character
- There must be at least one "Other" character. An "Other" character can only be either a number or any one of the following 8 characters: underscore, hyphen, period, ampersand, dollar, star, exclamation, the @ symbol
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}$/
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?
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_.&$*!@-]*$/
I would say you need the following:
(letters or symbols)* letters+ symbols+ (letters or symbols)*
OR
(letters or symbols)* symbols+ letters+ (letters or symbols)*
精彩评论