VB 在TEXT文本框中只允许输入数字0~9 如何实现
发布网友
发布时间:2022-05-09 20:23
我来回答
共4个回答
热心网友
时间:2023-10-17 16:44
'手机号码文本框失去焦点时判断合法性
Private Sub txtMobile_Validate(Cancel As Boolean)
If Not IsMobile(txtMobile.Text) And Len(txtMobile.Text) > 0 Then
MsgBox "手机号码不合法,请重新输入!"
Cancel = True
txtMobile.SetFocus
End If
End Sub
'电子邮箱文本框失去焦点时判断合法性
Private Sub txtEmail_Validate(Cancel As Boolean)
If Not IsValidEmail(txtEmail.Text) And Len(txtEmail.Text) > 0 Then
MsgBox "电子邮箱不合法,请重新输入!"
Cancel = True
txtEmail.SetFocus
End If
End Sub
'**************************************************
'函数名:IsMobile
'作 用:判断是否手机号码
'参 数:ParNumber----要检查的手机号码
'返回值:True ----手机号码合法
' False ----手机号码不合法
'**************************************************
Public Function IsMobile(ByVal ParNumber As String) As Boolean
'外地手机加0就是12位
If Len(ParNumber) <> 11 And Len(ParNumber) <> 12 Then
IsMobile = False
Exit Function
End If
'130~139、150~159、180~189
If Left(ParNumber, 2) = "13" Or Left(ParNumber, 3) = "013" Or Left(ParNumber, 2) = "15" Or Left(ParNumber, 3) = "015" Or Left(ParNumber, 2) = "18" Or Left(ParNumber, 3) = "018" Then
IsMobile = True
Else
IsMobile = False
End If
End Function
'**************************************************
'函数名:IsValidEmail
'作 用:检查Email地址合法性
'参 数:Email ----要检查的Email地址
'返回值:True ----Email地址合法
' False ----Email地址不合法
'**************************************************
Public Function IsValidEmail(ByVal Email As String) As Boolean
Dim names, Name, i, c
IsValidEmail = True
names = Split(Email, "@")
If UBound(names) <> 1 Then
IsValidEmail = False
Exit Function
End If
For Each Name In names
If Len(Name) <= 0 Then
IsValidEmail = False
Exit Function
End If
For i = 1 To Len(Name)
c = LCase(Mid(Name, i, 1))
If InStr("abcdefghijklmnopqrstuvwxyz_-.", c) <= 0 And Not IsNumeric(c) Then
IsValidEmail = False
Exit Function
End If
Next
If Left(Name, 1) = "." Or Right(Name, 1) = "." Then
IsValidEmail = False
Exit Function
End If
Next
If InStr(names(1), ".") <= 0 Then
IsValidEmail = False
Exit Function
End If
i = Len(names(1)) - InStrRev(names(1), ".")
If i <> 2 And i <> 3 And i <> 4 Then
IsValidEmail = False
Exit Function
End If
If InStr(Email, "..") > 0 Then
IsValidEmail = False
End If
End Function
热心网友
时间:2023-10-17 16:45
看看合不合你意吧,随便做的,测试了合你的要求:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 8 Or KeyAscii = 13 Then Exit Sub
If KeyAscii > vbKey9 Or KeyAscii < vbKey0 Then KeyAscii = 0
If Len(Text1.Text) >= 11 Then KeyAscii = 0
End Sub
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
Dim Num As String
'130、131、132、133、153、155、156
'134、135、136、137、138、139、150、151 157、158、159
If KeyCode = 13 Then
Num = Text1.Text
Print Len(Num)
If Len(Num) <> 11 Then GoTo wring
If Left(Num, 2) <> "13" And Left(Num, 2) <> "15" Then GoTo wring
End If
Exit Sub
wring: MsgBox "对不起,您输入的手机号码不正确,请重新输入!!", vbOKOnly, "号码输入错误"
End Sub
Private Sub Text2_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
If InStr(1, Text2.Text, "@") < 1 Then GoTo wring
If InStr(1, Text2.Text, ".") < 1 Then GoTo wring
End If
Exit Sub
wring: MsgBox "对不起,您输入的邮件地址不正确,请重新输入", vbOKOnly, "邮箱输入错误"
End Sub
热心网友
时间:2023-10-17 16:45
Private Sub Form_Load()
Text1.MaxLength = 11
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
aa = "1234567890"
If KeyAscii = 8 Then Exit Sub
If InStr(aa, Chr(KeyAscii)) = 0 Then KeyAscii = 0
End Sub
Private Sub Text1_LostFocus()
If Len(Text1) <> 11 Then
MsgBox "对不起,您输入的手机号码不正确,请重新输入!", , "注意"
Text1.SetFocus
End If
End Sub
热心网友
时间:2023-10-17 16:46
不提供直接源码,给你提供些函数吧
IsNumeric 判断数字
Instr 判断字符串包含某些字符串
Len 字符串长度