Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate field for all language characters through REGEX

i need to validate a field for empty. But it should allow English and the Foreign languages characters(UTF-8) but not the special characters. I'm not good at Regex. So any help on this would be great...

like image 643
Stranger Avatar asked Dec 13 '12 07:12

Stranger


1 Answers

If you want to support a wide range of languages, you'll have to work by excluding only the characters you don't want, since specifying all of the ranges you do want will be difficult.

You'll need to look at the list of Unicode blocks and or the character database to identify the blocks you want to exclude (like, for instance, U+0000 through U+001F. This Wikipedia article may also help.

Then use a regular expression with character classes to look for what you want to exclude.

For example, this will check for the U+0000 through U+001F and the U+007F characters (obviously you'll be excluding more than just these):

if (/[\u0000-\u001F\u007F]/.exec(theString)) {
    // Contains at least one invalid character
}

The [] identify a "character class" (list and/or range of characters to look for). That particular one says look for \u0000 through \u001F (inclusive) as well as \u007F.

like image 70
T.J. Crowder Avatar answered Nov 14 '22 22:11

T.J. Crowder