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 ñ
.
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 ñ
.
6 Answers
Reset to default 18/^[ñ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
.
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)
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 ^^
You need to use a character class.
/[A-Za-z ñ]+/
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. – tchrist Commented Sep 10, 2011 at 19:03