I am looking for a Javascript regex to make sure the string contains only spaces, letters, and ñ — case insensitively.
I already tried: /^[A-Za-z _]*[A-Za-z][A-Za-z _]*$/
but it fails to accept ñ
.
/^[ñA-Za-z _]*[ñA-Za-z][ñA-Za-z _]*$/
and
/^[\u00F1A-Za-z _]*[\u00F1A-Za-z][\u00F1A-Za-z _]*$/
should work.
Javascript regex supports \u0000
through \uFFFF
.
[aábcdeéfghijklmnñoópqrstuúüvwxyzAÁBCDEÉFGHIJKLMNÑOÓPQRSTUÚÜVWXYZ]
for the Spanish alphabet, but that will only work if you NFC the data!!
If you simply want that caracter, insert it in the Regex, like [A-Za-zÑñ ]
. Otherwise use a Unicode-knowing Regex library for Javascript like http://xregexp.com/. Sadly JS Regexes don't support Unicode compliant character classes (like \p{L}
in C# regexes)
\p{L}
instead of just \pL
????? That's really dumb, because it triples the length of the specifier for the 7 most common cases. That's a Huffman failure. Somebody wasn't very smart. You need to make \pL
only a little bit harder to type than \w
. Otherwise people won’t use it. \p{L}
is a design bug.
\p{L}
is 5 chars and \pL is 3. Yeah, \pL would be better, but I DO think that supporting non-BMP chars and the \X would be MUCH more important :-)
With this it forces not to have spaces at the beginning but if later
/^[a-zA-Z\u00C0-\u00FF][a-zA-Z\u00C0-\u00FF\s]*$/
..for email you can use:
/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i
..for password you can use:
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}$/i
I hope it works for you like it did for me, good look..!
This worked out for me
/^[a-zA-ZáéñóúüÁÉÑÓÚÜ -]*$/
a shorter version from @tchrist answer ^^
This works for me, allow utf8 characters like ñóíú and spaces
const validationsLetters = /^[a-zA-Z\u00C0-\u00FF ]*$/;
[aábcdeéfghijklmnñoópqrstuúüvwxyzAÁBCDEÉFGHIJKLMNÑOÓPQRSTUÚÜVWXYZ]
and furthermore, that will only work if you first run the data through Unicode Normalization Form C for Canonical Composition, normally called NFC. Otherwise your ñ character might and sometimes shall comprimise two separate code points: a normal n followed by U+0303COMBINING TILDE
. Unicode characters can have multiple code point representations. You need to normalize.