0

I am building a RegEx that is almost complete, but I can not get it to check for digits (0 - 9): So for example: Jones-Parry is valid but Jones-Parry1 is not. The regex at present looks like this:

^([\\w\\s,'\\-ÀÈÌÒÙàèìòùÁÉÍÓÚÝáéíóúýÂÊÎÔÛâêîôûÃÑÕãñõÄËÏÖÜŸäëïöüŸçÇŒœßØøÅ寿ÞþÐð]){0,80}$

I have tried using \d and [0-9] but to no avail. All else is working with the regex aside from the numbers. It validates special characters etc.

Any pointers greatly appreciated!

1
  • Oh, you've just EXCLUDED most slavic languages (Eastern and Central Europe), non latin, and possibly, many more. Bad idea. Commented Dec 1, 2014 at 10:15

1 Answer 1

1

The problem is \w expands to A-Za-z0-9_, which includes digits 0-9. This explains why strings with digit pass your test.

You may want to specify A-Za-z_ directly instead of \w in your regex. It will fix your problem.

As georg has pointed out in the comment, your regex is very weak, since aside from the length requirement, it only checks whether it does not contain any character outside your allowed character set. A string with only spaces, or a string with only punctuation would pass the test.

Anyway, I doubt validating name is a good idea in general. Many assumptions programmers make about name are wrong. Depending on your requirement, you can give user a field for display name, where user can type anything in, and another field for username, where you only allow a strict set of characters.

Sign up to request clarification or add additional context in comments.

1 Comment

A simple regex like [...]{80} is especially bad for validation, because it also "validates" ---, ,,,, and similar kinds of stuff.

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.