开发者

Javascript regex syntax for HTML5 input validation

开发者 https://www.devze.com 2023-03-12 06:01 出处:网络
So there have been plenty of questions, and filtering through a few, I still dont know how to go about this...

So there have been plenty of questions, and filtering through a few, I still dont know how to go about this...

Pattern for:

  • Alphabets ONLY, no case sensitivity, no limit on character count or words, minimum 3 characters...

I have

pattern="[A-z]{3,}"

That gives me everything, except that I'm limited to one word only... :-(

Edit: Let me be a little more clear on what I want the validation to achieve for me...

I'm using it to capture a person's name. But I do not want any special characters or numerals involved, so no "John Doe Jr.", as the '.' wil开发者_JAVA技巧l get rejected, but I want to be able to capture double, or even single character portions, whilst maintaining 'global' 3 char minimum limit...


All you have to do to allow spaces as well is to add a space to the character pattern where you have [A-z].

So it becomes:

pattern="[A-z ]{3,}"

Hope that helps.

Note, however, that this will prevent other types of white space characters. I assume this is what you want, since you're being quite restrictive with the rest of the character set, but it's worth pointing out that non-breaking spaces, carriage returns, and other white space will be blocked in the above. If you want to allow them, use \s instead of just a space: this will match any white space character.

Finally, it's worth pointing out that the standard alphabet is often insufficient even for plain English text. There are valid English words with accents, as well as apostrophes and other punctuation. You haven't specified what the field is being used for, so I'll assume this is not an issue, but I felt it was worth pointing out nevertheless.


It is difficult to see what is the question. You are matching a String, not a set of words. If your pattern is "a list of words, each of the words alphabetical only and separated by whitespace", then the regex would be

([A-Za-z]{3,}\\s*)+

Edited to answer to updated question.

[A-Za-z\\s]*([A-Za-z]{3,})+[A-Za-z\\s]* (works in Java)


How about pattern = "[A-z\s]{3,}"?

0

精彩评论

暂无评论...
验证码 换一张
取 消