1

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...

5
  • Should "ab c" or even "a b c" be accepted? I mean, are you requesting at least a word 3 characters long or just 3 alphabetical character in total? Commented Jun 13, 2011 at 11:02
  • 1
    As per my comment in my answer, I'd strongly recommend not being too restrictive with name validation. There are a lot of very common names which will fall foul of your rules as you have them at the moment. See also my answer here: stackoverflow.com/questions/3853346/… Commented Jun 13, 2011 at 11:12
  • 4
    [A-z] includes a lot of non alphabetic letters, for example square brackets, back-slashes, carets, underscores, and back-ticks. Try [A-Za-z] instead. Commented Jun 13, 2011 at 12:03
  • @SJuan76 - It wouldn't be favorable, but I wouldn't mind such additions... Commented Jun 14, 2011 at 1:26
  • @Abhishek you wouldn't mind ________Johny^420^YOLO___________, but John Doe Jr. is not acceptable? Commented Oct 2, 2013 at 0:57

3 Answers 3

3

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.

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

4 Comments

Thank you for pointing it out mate, I realize that the question was a tad too generic, and have made amends accordingly... If it still doesn't come across clear enough, please do let me know how I can fix 'er up...
@Abhishek - if it's for name validation, then I'd strongly recommend reconsidering the restrictive validation. John-Paul O'Reilly will hate you for it, as will François Du Toit, and a whole bunch of other people. See also an answer I gave previously on this topic here: stackoverflow.com/questions/3853346/…
Fair enough, this leads me to believe that instead of defining what the user can enter, I ought to be isolating uncommon characters. ($%^@!*, etc.) In addition to this, if I were to declare the field as 'What would you like to be called by' or something along the lines instead of explicitly asking for the patron's name, might give me some more flexibility, no?
@Abhishek - yes, you should be as forgiving as possible on the name field. You don't need to go as far as changing the caption; people will understand what you mean by "Name", and most of them will enter names that meet your expectations. But you need to allow for unusual characters, because they're not really that unusual, and people who have them will leave your site if they can't enter their name properly. you probably can validate against $%^&, but you need you need to allow most characters. :)
2

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)

2 Comments

+1 because it's a different solution to the others. This solution would give any number of words, each of which must be three characters or more. While I think the other solutions are correct, the question could be interpreted this way also, therefore I give +1 to this answer.
Fair enough, you've almost got my question answered, but there's one problem, I want the overall character count to exceed 3 characters, but that doesn't mean that every word should be at least 3 characters... What happens when someone enters the string "John Jr"? The validation will reject it because 'Jr' violates the minimum 3 char rule... Oh, and I'm sorry that the question was slightly vague... Anyway I can improve it?
1

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

3 Comments

That would allow words shorter than 3 chars; v.g. "a b" would match.
@SJuan76 - If you say so, but will it still enforce the minimum 3 char limit? Or are you saying that the above syntax will let the field accept a single character by itself? Other words, can the above syntax handle = "John A", but reject = "a"?
Yes. By the way, you should trim the input string for whitespaces before pattern testing, otherwise it could match for example five whitespaces in a row.

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.