1

I want to allow a user to enter only characters and with single space in a textbox. What i have tried is :

^[\w\.:\(\)\[\]{}\-_](?: ?[\w\.:\(\)\[\]{}\-_])*$

it is blocking all but allowing digits also . How to make it correct such that it only allow characters and no digits but a single space between words? Thank you.

3 Answers 3

1

I'd use:

^[\p{L}.:()[\]{}_-]+(?: [\p{L}.:()[\]{}_-]+)*$

Where \p{L} stands for any letter.

Edit:
I've changed \pL to \p{L} because it's not supported by .NET.
Thanks to Alan Moore.

Sign up to request clarification or add additional context in comments.

1 Comment

+1, but .NET doesn't support the short form for "top level" Unicode properties like \pL; you have to use \p{L}. Also, don't make the space optional; that's already handled by the * on the group.
0

Is ([A-Za-z])+( [A-Za-z]+) what you're after? Or do you want the regex to only match once?

Comments

0

Assuming there must be either oneor two 'words' (i.e. sequences of non-space characters)

"\s*[A-Za-z] +(\s[A-Za-z] +)?\s*"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.