-1

I have a requirement to do a field validation using regEx. The requirement is not to have any special characters. It is working for me for a single white space, but not for multiple white spaces. It does not work if there is a white space at the beginning. Can you please help me with this?

This is what I used:

^(\w+ ?)*$
Stephen King
  • 581
  • 5
  • 18
  • 31

1 Answers1

0

You might be looking for

^[\w ]*$

The ^ and $ are anchors for the start/end of the string, the [...] is called a character class and would allow only [A-Za-z0-9_ ]. The * is a quantifier and means zero or more times, thus the expression would also allow an empty string. If this is not what you want, change it to + instead of the *. Please note that this would also allow a string with only spaces (it really depends on what you want).

Jan
  • 42,290
  • 8
  • 54
  • 79