Regular expression for name with spaces allowed in between the text and avoid spaces when there is no text

javascript, regex

Solution

Just disallow a string that only consists of whitespaces/dots:

^(?![\s.]+$)[a-zA-Z\s.]*$
 ^^^^^^^^^^^ 

See the regex demo.

The `(?![\s.]+$)` is a negative lookahead that fails the match if the whole string contains whitespace or dot chars only.

Problem

Hello I'm trying to write a regular expression for user name in my form. The regular expression should allow spaces and . when the text is written Ex : S. Harish Kumar Check the regEx that I have written so far ``` ^[a-zA-Z\s\.]*$ ``` The above regEx also accepts any string with just spaces and . which I don't want. Can you help me with the perfect regEx which doesn't accept spaces and . when no text is entered? Thanks in advance :)

Original source