jm *_* li 4 javascript regex unicode validation
问题是我需要在客户端验证中将此类中文输入视为无效:
任何与任何中文字符和空格混合的英文字符总长度> = 10时,输入无效.
让我们说:"你的是你的你的你"或"你的你的你的你"(长度为10)无效.但是"你的是你的一个人"(长度为9)是可以的.
我使用Javascript进行客户端验证,使用Java进行服务器端.所以我认为在两者上应用正则表达式应该是完美的.
任何人都可以提供一些提示如何在正则表达式中编写规则?
来自Unicode的中文字符的完整范围是什么?,CJK unicode范围是:
Block Range Comment
--------------------------------------- ----------- ----------------------------------------------------
CJK Unified Ideographs 4E00-9FFF Common
CJK Unified Ideographs Extension A 3400-4DBF Rare
CJK Unified Ideographs Extension B 20000-2A6DF Rare, historic
CJK Unified Ideographs Extension C 2A700–2B73F Rare, historic
CJK Unified Ideographs Extension D 2B740–2B81F Uncommon, some in current use
CJK Unified Ideographs Extension E 2B820–2CEAF Rare, historic
CJK Compatibility Ideographs F900-FAFF Duplicates, unifiable variants, corporate characters
CJK Compatibility Ideographs Supplement 2F800-2FA1F Unifiable variants
CJK Symbols and Punctuation 3000-303F
Run Code Online (Sandbox Code Playgroud)
您可能希望允许来自Unicode块CJK Unified Ideographs和CJK Unified Ideographs Extension A的代码点.
此正则表达式将匹配0到9个空格,表意空间(U + 3000),AZ字母或这两个CJK块中的代码点.
/^[ A-Za-z\u3000\u3400-\u4DBF\u4E00-\u9FFF]{0,9}$/
Run Code Online (Sandbox Code Playgroud)
表意文字列于:
但是,您可以添加更多块.
function has10OrLessCJK(text) {
return /^[ A-Za-z\u3000\u3400-\u4DBF\u4E00-\u9FFF]{0,9}$/.test(text);
}
function checkValidation(value) {
var valid = document.getElementById("valid");
if (has10OrLessCJK(value)) {
valid.innerText = "Valid";
} else {
valid.innerText = "Invalid";
}
}
Run Code Online (Sandbox Code Playgroud)
<input type="text"
style="width:100%"
oninput="checkValidation(this.value)"
value="??a??a??a">
<div id="valid">
Valid
</div>
Run Code Online (Sandbox Code Playgroud)