-2

I'm making a form in HTML that has a name field that I'd like to validate with the HTML pattern attribute.

I would like to allow any letters (including with accents) as well as - . , ' and space. (added in edit to clarify)

Here was my attempt:

Nome e Cognome: <input type="text" name="nome" required pattern="^[\p{L}\p{M} \-\.',]*$" placeholder="Mario Rossi">

However, when I input invalid data like John" Doe? the form doesn't throw an error. Why?

sonicseamus
  • 1
  • 1
  • 2

1 Answers1

0

Your Regex doesn't look quite right, but without knowing your exact requirements it's hard to write the one you want!

Try this: /^[a-zÀ-ÿ ,.'-]+$/i It will match first and last names, and allow for only the symbols commonly found in names.

For this to work, you also need to put it in a <form> (of bind it to a button). For example:

<form action="/submit-name/">
    Nome e Cognome: <input type="text" name="nome" required pattern="^[A-Za-zÀ-ÿ ,.'-]+$"   placeholder="Mario Rossi">
    <input type="submit">
</form>
Alicia Sykes
  • 5,997
  • 7
  • 36
  • 64
  • he wants to include accents – Joshua Duxbury Jun 21 '17 at 09:28
  • Okay, thanks! Could someone explain why the Unicode tags I used don't work/aren't the best way to do this? One [thing I read](http://www.regular-expressions.info/unicode.html#prop) said that accented characters can be coded as two separate code points so I was trying to cover all my bases. Also, why the `+` in place of the `*`? – sonicseamus Jun 21 '17 at 09:42
  • Your right, and allowing for accents sometimes gets tricky. See this question: https://stackoverflow.com/questions/20690499/concrete-javascript-regex-for-accented-characters-diacritics. You could also do something like this: `/^[a-z\u00E0-\u00FC]+$/i` – Alicia Sykes Jun 21 '17 at 09:51