0

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)?

2 Answers 2

2

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]+$
Sign up to request clarification or add additional context in comments.

2 Comments

You should also remove that last pipe character because it creates an empty (though harmless) character match between | and ).
@mart1n, I removed that. Thank you.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.