Validate field for all language characters through REGEX

后端 未结 3 1614
隐瞒了意图╮
隐瞒了意图╮ 2021-02-10 01:10

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 R

3条回答
  •  天涯浪人
    2021-02-10 01:48

    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.

提交回复
热议问题