简体   繁体   中英

Javascript regex for validating name without accented characters

I'm using a simple Javascript regular expression to validate people's names. However, I do not want to allow a user to enter accented characters like ã , ä , õ , ö , etc. I am aware that these can actually be valid characters in a name, but for my exercise, I need to be able to filter them out.

My current regex:

^[a-zA-Z]+('|-|.|)[a-zA-Z\s]+$

This matches accented characters as well. How do I modify this regex so that it doesn't match names with accents (or any Unicode character, for that matter)?

Remove . , because . match any character. That could cause matching accented character.

^[a-zA-Z]+('|-)[a-zA-Z\s]+$

or escape . if you mean literal dot.

^[a-zA-Z]+('|-|\.)[a-zA-Z\s]+$

To match a single character it's better to use a class [...] , not a group with alternatives (...|...) :

^[a-zA-Z]+['.-][a-zA-Z\s]+$

Note that this is not 100% accurate, since it validates things like foo.bar etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM