求教怎么限制只能输入汉字
Private Sub PoText_KeyPress(KeyAscii As Integer)
If KeyAscii > = -20319 And KeyAscii <= -3652 Or KeyAscii = 8 Then
Else
KeyAscii = 0
End If
End Sub
我是这样做的
但在中文输入法下一样可以输入数字和字母
我应该怎么做才能让它只输入汉字?
[解决办法]
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
[解决办法]
正则:
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
[解决办法]
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
[解决办法]
匹配中文字符的正则表达式: [\u4e00-\u9fa5]
评注:匹配中文还真是个头疼的事,有了这个表达式就好办了
匹配双字节字符(包括汉字在内):[^\x00-\xff]
评注:可以用来计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)
用正则表达式限制只能输入全角字符:/[^\uFF00-\uFFFF]/g