0

I got a problem with a javascript regex.

It doesn't stop numbers and "!" character while it should. It works for the others special characters, and of course I have debuged it to see if it really does the test, etc... and it does. It return true even if there is numbers in my String.

This is the RegExp :

new RegExp("^[^\\- ][A-Za-z\u00C00-\u017F\\- ]+"); // \u00C00-\u017F = À-S(latin)

It is to validate a first name in a form.

The problem is weird because I have exactly the same regex on the server side in java, and it works perfectly.

Thanks for your answers :)

3
  • 3
    Can you elaborate on what you're trying to do here? Commented Aug 29, 2012 at 12:20
  • @pimvdb: makes no difference. There's no ending anchor so it will almost always validate. Commented Aug 29, 2012 at 12:22
  • Yes. It is to validate a first name in a form. Commented Aug 29, 2012 at 12:22

2 Answers 2

6

You probably have some typo:

\u00C00-\u017F

C00 hex would be LARGER THAN 17F hex. But \u catches four characters only, see comment below.

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

3 Comments

+1 Good catch. The first four digits are bound to the \u but the 0 is not. So the range is really 0-\u017f which includes digits .
It is not that. I have try without it and it is still the same problem.
wow! It was that indeed. Rhaa, it was so stupid >< Thanks a lot !
0

You should escape (for the javascript string) the regex \u

new RegExp("^[^\\- ][A-Za-z\\u00C0-\\u017F\\- ]+");

Also, if you are trying to match agains accented letters the range is (regex) [\u00C0-\u00FF]

3 Comments

The problem doesn't come form the unicode range. I did it without them yesterday and the problem was already here.
So you already had an error and made the code more complicated without fixing it before? Can you please show the statement where you do the test?
It works no, with the unicode corrected. I gess there was another errors before which made the same problem.

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.