Reputation: 17576
I want to validate a text box to ensure only characters and Unicode characters can be added to it. For example, the following would be valid:
Gräfelfing
Gießen
München
But these should be invalid:
Gräfelfing123
Gießen!@#$
München123#@#
I know how to validate for characters using validator method:
jQuery.validator.addMethod("lettersonly", function(value, element) {
return this.optional(element) || /^[a-zA-Z]+$/i.test(jQuery.trim(value));
}, "Letters only please");
But how can I add Unicode character validation to this? Please help, thanks in advance.
* UPDATE *
I tried a solution with the help of this question - Regular expression to match non-English characters.
But it is not working:
jQuery.validator.addMethod("lettersonly", function(value, element) {
return this.optional(element) || /^[a-zA-Z\u0000-\u0080]+$/i.test(jQuery.trim(value));
}, "Letters only please");
Upvotes: 1
Views: 4759
Reputation: 22421
Javascript lacks built-in character classe for "all latin-based letters with diactrics", so you must decide yourself what you really want to accept.
For example:
Those all are "non-Englih (or, more correctly: non-Latin) letters" after all.
After you've decided:
Example
To add diactrics to list of allowed chars in the check in your code, you can use /^[a-zA-Z\u0080-\u024F]+$/i
That will cover
This should cover most of used diactrics and works will all examples you've provided.
Mix and match as you want.
Upvotes: 2