3
votes

I m using the following regex for the same: ^\S.*\S$

but this regex is not working in case of multiple spaces in between words. and one more thing, it doesnt allow you to enter a single character

I want to validate no leading and trailing space and single white space in between words, words can include anything alphanumeric as well as special character.

if i m trying to enter 'a' in the text box, its not accepting

5
"Working" and "not working" suggest you know what you want, but I can't tell from reading this what that is. Provide some examples of input and desired output and you'll get better answers.Edward
What are you trying to achieve? Post some examples for valid and invalid matches.Avinash Raj
this regex should give true for following: shashank shekharShashank Shekhar
It will be better if your provide examples here. For input A expected output will be B.fhnaseer
Add example in your question, not in comments,fhnaseer

5 Answers

5
votes

Seems like you want something like this,

^\S+(?: \S+)*$

\S matches any non-space character.

DEMO

4
votes

In case only charactors are needed, use this:

^[a-z|A-Z]+(?: [a-z|A-Z]+)*$
0
votes

[A-Za-z]+(\s[A-Za-z]+)? you can use this to put single space between words

0
votes

Just assert that two spaces don't appear;

^(?!.*\s\s)\S(.*\S)?$
0
votes

Below one allows one space between words with some special chars. It also prevent space at starting and end of string.

^([a-zA-Z0-9@.,]+\s)*[a-zA-Z0-9@.,]+$