lua quick-x 区分 中文和中文符号

来源:互联网 发布:发型设计软件绿色版 编辑:程序博客网 时间:2024/06/10 02:04

最近在做用户名验证,有个要求就是 用户只能输入汉字  那么 汉字的标点符号 就必须排除出去了

lua 代码 如下


网上了解到  中文的ASC码 区间是   0xe4b880 - 0xe9bfbf 也就是228 184 128 -- 233 191 191

function M:checkWord(str)
    if #str > 0 and #str/3%1 == 0 then
        --判断是否全部为中文
        for i=1,#str,3 do
            local tmp = string.byte(str, i)
            print(" ----   ----",tmp)--230
            if tmp >= 240 or tmp < 224 then
                return false
            end
        end
        --判断中文中是否有中文标点符号
        for i=1,#str,3 do
            local tmp1 = string.byte(str, i)
            local tmp2 = string.byte(str, i+1)
            local tmp3 = string.byte(str, i+2)
            print("mp1,mp2,mp3",tmp1,tmp2,tmp3)
            --228 184 128 -- 233 191 191
            if tmp1 < 228 or tmp1 > 233 then
                return false
            elseif tmp1 == 228 then
                if tmp2 < 184 then
                    return false
                elseif tmp2 == 184 then
                    if tmp3 < 128 then
                        return false
                    end
                end
            elseif tmp1 == 233 then
                if tmp2 > 191 then
                    return false
                elseif tmp2 == 191 then
                    if tmp3 >191 then
                        return false
                    end
                end            
            end
        end
        return true
    end
    return false
end

0 0
原创粉丝点击