简体   繁体   中英

Javascript regex syntax for HTML5 input validation

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 '.' will 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 [Az] .

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 = "[Az\s]{3,}" ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM