Reputation: 11
I am trying to validate a text box which needs to accept any character but needs to accept only single whitespace for entire text box field but not start with whitespace character.
Example:
"wall heel" & "wall " & "wall"
Here i am posting my code:
var alpha = (/^\w+ +\w*$/)||(/^\w*$/);
Note:Here it only accepts the characters like "wall heel"
but not accepts "wall"
.
Please suggest me some codes.
Upvotes: 0
Views: 636
Reputation: 8225
use this regex
/^[^\s]+([\s]([^\s]+)?)?$/
This will match everything except:
Upvotes: 0
Reputation: 7948
use this pattern ^[^ ](?!(.*? ){2}).*$
Demo
^[^ ]
start with a non space character.(?!
negative lookahead.(.*? ){2}
two spaces)
end of lookahead.*$
followed by anything to the endUpvotes: 1
Reputation: 12772
Make sure it is either empty, or does not start with space and have fewer than 2 spaces.
var valid = (s == "" || (s[0] != " " && s.split(" ").length < 2));
Upvotes: 0