5

フィールドが空であることを検証する必要があります。ただし、英語と外国語の文字 (UTF-8) は許可する必要がありますが、特殊文字は許可しないでください。私は正規表現が苦手です。したがって、これに関するヘルプは素晴らしいでしょう...

4

3 に答える 3

6

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.

于 2012-12-13T08:02:24.197 に答える
4

「やれよ」って言えばよかっ/^\w+$/.test(word)たのに…。

JavaScript 正規表現での Unicode サポート (または不足) の現在の状態については、この回答を参照してください。

彼が提案するライブラリを使用することができますが、これは遅いかもしれませんし、サーバーの助けを借りることもできます (遅いかもしれません)。

于 2012-12-13T08:00:38.647 に答える