user4510189
user4510189

Reputation:

How to accept only alphabets without space in regexed?

I want to validate full name in contact form. I want to restrict spaces in alphabets. textbox should only accept a-z characters.

I used this regular expression

ValidationExpression="[a-zA-Z ]*$"

But it allows spaces also.

Upvotes: 2

Views: 7805

Answers (3)

nu11p01n73R
nu11p01n73R

Reputation: 26667

Your regex doesn't work because it contains spaces in the character squance.

You can specify the pattern correctly as

ValidationExpression="^[a-z]*$"
  • ^ Anchors the regex at the start of the string.
  • [a-z]* Matches zero or more characters
  • $ Anchors the regex at the end of the string.

Regex Demo

EDIT

To restrict the characters to 50 we could use a quantifier as

ValidationExpression="^[a-z]{,50}$"
  • {,50} Quantifier ensures that there can be a maximum of 50 characters.

Upvotes: 2

Simon Darlow
Simon Darlow

Reputation: 550

I would just use "^[a-zA-Z]+$".

I think the issue you have is there is a space between the Z and ]. When I tested this it allowed spaces into the regular expression. I also changed the * to + to not allow a blank string.

Upvotes: 1

buckley
buckley

Reputation: 14079

Just remove the space inside your character class?

Also anchor the regex so that it matches at the start of a line :

^[a-zA-Z]*$

And take into consideration that ^ and $ can be influenced by the modifier that says it should match at a newline or not

Upvotes: 1

Related Questions