您的位置:程序门 -> vb -> 基础类



求教怎么限制只能输入汉字


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


求教怎么限制只能输入汉字
发表于:2007-06-29 00:38:19 楼主
private   sub   potext_keypress(keyascii   as   integer)

  if   keyascii   > =   -20319   and   keyascii   <=   -3652   or   keyascii   =   8   then
      else
              keyascii   =   0
        end   if
       
end   sub

我是这样做的
但在中文输入法下一样可以输入数字和字母
我应该怎么做才能让它只输入汉字?
发表于:2007-06-29 08:00:371楼 得分:0
private   sub   text1_validate(cancel   as   boolean)
if   len(text1.text)   *   2   <>   lenb(strconv(text1.text,   vbfromunicode))   then
msgbox   "please   input   chinese   characters "
cancel   =   true
end   if
end   sub
发表于:2007-06-29 08:17:122楼 得分:0
你是说连全角数字和字母也禁止?
private   sub   potext_keypress(keyascii   as   integer)

    if   keyascii   >   0   then   keyascii   =   0

    select   case   keyascii
            case   -23632   to   -23623,   -23615   to   -23590,-23583   to   -23558  
                keyascii   =   0
    end   select

end   sub
发表于:2007-06-29 09:09:083楼 得分:0
学习一下
发表于:2007-06-29 10:31:144楼 得分:0
正则:

option   explicit

'引用   microsoft   vbscript   regular   expressions
dim   stext   as   string

function   bmatching(s   as   string,   p   as   string)   as   boolean
'参数:s要匹配的字符串,p正则表达式
on   error   goto   100
        dim   myreg   as   regexp
        set   myreg   =   new   regexp
       
        bmatching   =   false
        myreg.ignorecase   =   true
        myreg.pattern   =   p
        bmatching   =   myreg.test(s)
        exit   function
100:
        bmatching   =   false
end   function

private   sub   text2_change()
        dim   p   as   string
        p   =   "^[\u4e00-\u9fa5]{0,}$ "
        if   bmatching(text2,   p)   then
                stext   =   text2
        else
                text2   =   stext
                text2.selstart   =   len(text2)
        end   if
end   sub


发表于:2007-06-29 10:48:325楼 得分:0
to   of123
是的,我想把全角的数字和字母也禁止
现在还是不行
有没有办法?
发表于:2007-06-29 11:32:526楼 得分:0

private   sub   text1_change()
        dim   i   as   long
        for   i   =   0   to   25
                text1.text   =   replace(text1.text,   chr(i   +   65),   " ",   ,   ,   vbtextcompare)
        next
end   sub
发表于:2007-06-29 12:09:227楼 得分:0
匹配中文字符的正则表达式:   [\u4e00-\u9fa5]
评注:匹配中文还真是个头疼的事,有了这个表达式就好办了

匹配双字节字符(包括汉字在内):[^\x00-\xff]
评注:可以用来计算字符串的长度(一个双字节字符长度计2,ascii字符计1)

用正则表达式限制只能输入全角字符:/[^\uff00-\uffff]/g


快速检索

最新资讯
热门点击