How do I ensure that there are no special characters in the name except an apostrophe…Eg:- I can allow O’Connor but not John “Doe” ? Please help.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Here’s a working example of how you’d use a regular expression to check for a valid name given the conditions you laid out:
The
/^[A-Za-z'\s]+$/regular expression allows uppercase English, lowercase English, apostrophes, and spaces. (It’s similar to the one proposed by Afiefh, with the addition of the \s to allow spaces, and the + instead of the * to prevent blank names from passing the test.)As David Dorward suggests above, if you really are using this to test names, you might consider relaxing the rule to accommodate international names by changing line 6 to this:
This allows unicode characters, apostrophes, and hyphens.