Whats wrong with this regular expression?
/^[a-zA-Z\d\s&#-\('"]{1,7}$/;
when I enter the following valid input, it fails:
a&'-#"2
Also check for 2 consecutive spaces within the input.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The dash needs to be either escaped (
\-) or placed at the end of the character class, or it will signify a range (as inA-Z), not a literal dash:would be a better regex.
N. B:
[#-\(]would have matched#,$,%,&,'or(.To address the added requirement of not allowing two consecutive spaces, use a lookahead assertion:
(?!.*\s{2})means “Assert that it’s impossible to match (from the current position) any string followed by two whitespace characters”. One caveat: The dot doesn’t match newline characters.