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

Abhishek
  • 4,190
  • 11
  • 39
  • 52
  • 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? – SJuan76 Jun 13 '11 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: http://stackoverflow.com/questions/3853346/how-to-validate-human-names-in-cakephp/3853820#3853820 – Spudley Jun 13 '11 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. – Mike Samuel Jun 13 '11 at 12:03
  • @SJuan76 - It wouldn't be favorable, but I wouldn't mind such additions... – Abhishek Jun 14 '11 at 01:26
  • @Abhishek you wouldn't mind `________Johny^420^YOLO___________`, but `John Doe Jr.` is not acceptable? – Maksim Vi. Oct 02 '13 at 00:57

3 Answers3

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.

Spudley
  • 166,037
  • 39
  • 233
  • 307
  • 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 Jun 13 '11 at 10:34
  • 1
    @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: http://stackoverflow.com/questions/3853346/how-to-validate-human-names-in-cakephp/3853820#3853820 – Spudley Jun 13 '11 at 11:10
  • 1
    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 Jun 14 '11 at 01:33
  • @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. :) – Spudley Jun 14 '11 at 08:03
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)
SJuan76
  • 24,532
  • 6
  • 47
  • 87
  • +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. – Spudley Jun 13 '11 at 10:16
  • 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? – Abhishek Jun 13 '11 at 10:29
1

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

cypher
  • 6,822
  • 4
  • 31
  • 48
  • That would allow words shorter than 3 chars; v.g. "a b" would match. – SJuan76 Jun 13 '11 at 10:15
  • @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"? – Abhishek Jun 13 '11 at 10:34
  • 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. – cypher Jun 13 '11 at 10:38