1

I am a novice in regex. I wan't the field to be only filled in English (I Don't want to allow any Chinese characters to be submitted) So far I've come up with this:

<input type="text" id="FirstName" data-val-regex="Please fill out in english." data-val-regex-pattern="[A-Za-z \\u4E00-\\u9FFF\\u3400-\\u4DFF\\uF900-\\uFAFF ]+"  />

How can I omit numbers and also characters like ?,- (question mark, hyphen, comma etc)

In other words - the field should only allow alphabets with a space (this would be ideal for a name field I suppose?)

Any advice would be highly appreciated.

3
  • Default regex patterns: a-zA-Z all alphabetic characters, \d any digit, - is a hyphen, and \? is a question mark. Commented Jan 13, 2017 at 6:26
  • By "restrict", do you mean the entire entry must be Chinese characters, or must not contain any Chinese characters, or...? Can you state clearly the criteria which you want to match? It might be helpful if you gave some examples which you would like to match or not match. Commented Jan 13, 2017 at 6:40
  • @torazaburo I've added more details in my question. Commented Jan 13, 2017 at 6:48

2 Answers 2

2

Doesn't this attribute data-val-regex-pattern represent a regex that specifies what is allowed? In that case wouldn't alphabet characters and a space suffice?

"[A-Za-z ]+"
Sign up to request clarification or add additional context in comments.

4 Comments

If it were the HTML5 pattern attribute, it would not need ^ and $ anchors, but if he has code which is handling the data-val-regex-pattern himself, it probably needs to be anchored, unless that code applies the anchors itself somehow.
@GantTheWanderer How can we ensure there is a space after every word? Because when I switch keyboards from Chinese to English, I'm unable to have a space between words.
Its hard to determine what you want to do when you say you Don't want to allow any Chinese characters to be submitted then somehow need to switch to a Chinese keyboard at the same time. Can you explain what you are trying to achieve?
Sorry my bad, forgot to remove some JS code I was testing out previously.
0

Try this:

^[a-zA-Z ]+$

^ ... $ - Mark string boundaries (start to end)

[ ]+ - Match atleast one character of characters inside [ ]

4 Comments

This doesn't allow a space between words anymore and doesn't show an error when a number is keyed in
@lily I misread your question. Those unicode ranges are completely unneccesarry. a-zA-Z(or \w, in this case) doesn't even allow chinese characters. I changed my regex, it should work for passwords like cheese 12?-d now.
I guess he doesn't want to allow dashes and question marks, nor the numbers and underscores that \w would allow.
That's right, the field should only allow alphabets with a space

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.