-2

I have field "first and second name" and I need RegEx for capitalizing first letter in each word in this field.

With this pattern, only first word is capitalized, but second is not

<form action="/action_page.php">
  First and Second name <input type="text" name="f_and_s_name" pattern="[A-zA-Z]{1,13}" title="">
  <input type="submit">
</form>
Ovakos
  • 15
  • 1
  • 5
  • Your code is not complete. Please post the whole code. The pattern attribute does not modify the value inside input, it only validates it before form submission. – Wiktor Stribiżew Apr 26 '18 at 16:54
  • I added html code to first post. – Ovakos Apr 26 '18 at 16:55
  • Your current regex [also allows `]]]]]]]]]]]]]` input or `^___[]][][`, and lowercase words](https://regex101.com/r/TUbN7k/3). Please explain what you actually are trying to achieve. – Wiktor Stribiżew Apr 26 '18 at 16:58
  • If my first and second name is "John Doe" I want to validate that input in that field must be with capitalized letter (not to be Johh doe, john Doe or john doe). – Ovakos Apr 26 '18 at 17:00
  • Then the basic regex would be `pattern="[A-Z][a-z]* [A-Z][a-z]*"`. – Wiktor Stribiżew Apr 26 '18 at 17:01
  • That is Wiktor, post it like an answer please. – Ovakos Apr 26 '18 at 17:03
  • Possible duplicate of [How to check if firstname's and lastname's first characters are uppercase?](https://stackoverflow.com/questions/49239808/how-to-check-if-firstnames-and-lastnames-first-characters-are-uppercase) – David Apr 26 '18 at 17:51

1 Answers1

0

The pattern="[A-zA-Z]{1,13}" pattern is parsed by the JS RegExp engine as ^(?:[A-zA-Z]{1,13})$. It matches a string that consists of 1 to 13 characters that are ASCII letters, and also [, \, ], ^, _, ` (see this answer for more details).

You plan to only validate strings that start with an uppercase ASCII letter followed with lowercase letters, then have a space, and then again an uppercase ASCII letter followed with lowercase letters.

Then, you may use

pattern="[A-Z][a-z]* [A-Z][a-z]*"

If you want to make the rule a bit less restricted, you may use [A-Z][a-zA-Z]*\s+[A-Z][a-zA-Z]* (this will also allow strings in ALLCAPS, and will allow any 1+ whitespace chars between two letter strings. If you plan to implement Unicode letter support, it will only be easy in latest Chrome versions (it will look like pattern="\p{Lu}\p{L}*\s+\p{Lu}\p{L}*"). However, it won't work in all other browsers that still do not support ECMA2018 standard.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • @ Wiktor, this pattern (pattern="[A-Z][a-z]* [A-Z][a-z]*") is giving me only 3 words in a string. – Ovakos May 02 '18 at 10:47
  • @Ovakos What do you mean by "giving" "3 words"? `/^(?:[A-Z][a-z]* [A-Z][a-z]*)$/` only matches a string that consists of exactly 2 ASCII capitalized words. – Wiktor Stribiżew May 02 '18 at 10:51
  • Sorry, I wrote fast and I didn't explain well. This (pattern="\p{Lu}\p{L}*\s+\p{Lu}\p{L}*") works for first and second name, and it is really good pattern. I have field "Adress" witch is in format: String String Number (or numbers) Comma String (My Street 30, NewYork). How can I implement the same thing here mate? – Ovakos May 02 '18 at 11:08
  • @Ovakos This is a different question then. I do not understand the requirements, but probably you want `pattern="\p{L}+\s+\p{L}+\s+\p{N}+,.*"`. No idea what you mean by "Number" and "String". When you plan to use regex, you match types of characters, so you must be very specific. – Wiktor Stribiżew May 02 '18 at 11:13
  • I will try to implement, and make a new question if it won't work :) Tnx – Ovakos May 02 '18 at 11:18