程序設計中我們時常需要檢測用戶輸入是否正確,特別是姓名,地址等等是不是輸入的漢字。那麼,如何判斷一個字符是不是漢字呢?其實在asp中至少有兩種方式
一、直接將某字符用asc轉為ascii碼,如果是英文,他應該是0-127的範圍,而漢字則是一個比較大的數字,所以可以使用以下代碼來判斷:
ifabs(asc(whichchar))>127then
response.writewhichchar&是一個漢字
else
response.writewhichchar&不是一個漢字
endif
二、漢字的unicode編碼範圍是4e00-9fa5,所以使用正則表達試就可以判斷一個漢字是不是漢字了。
setregexpobj=newregexp
regexpobj.pattern=^[/u4e00-/u9fa5]+$
regcheck=regexpobj.test(whichchar)
setregexpobj=nothing
ifregcheckthen
response.writewhichchar&是漢字
else
resposne.writewhichchar&不是漢字
endif