Regular expression in javascript to match only letters or spaces

I have the following expression to test for "name":

/^([a-z\s?]{4,120})[^\s]$/i

      

But I don't know why it accepts special characters: Alex@

- valid match.

It should be invalid because I have not specified what contains special characters.

+3


source to share


1 answer


What you want is a minimum of 4 characters - you are trying to make it work expecting at least 5 characters because of the optional character [^\s]

at the end. What's more, [^\s]

will actually match any character that is not a space - I'd bet you want to restrict this to just letters?

Try this instead:



^[a-z\s?]{3,119}[a-z]$

      

+1


source







All Articles