控件输入限制总结

来源:互联网 发布:淘宝化妆品店推荐 编辑:程序博客网 时间:2024/05/17 21:44

 

  学生和机房完了之后,总是有一些小小的细节之类的东西需要注意,尤其是关于文本框等输入字符限制的问题,最近小编总结了一篇关于文本框限制的博客,此次总结来源于小伙伴们平时的辛苦成果,希望可以帮到你们!

 

     平时我们登录自己的账户,总会输入自己的用户名,自己的账号,卡号,密码等。有着许许多多的限制,说白了都是我们的ASCII码表。你还记得那一年你在三合班一起看的ASCII码表么,我还记得我们在501编了小故事。


ASCII表:


 

  

 1.只能有数字和字母组成:(密码长度不可以超过几位数)

Private Sub Password1_Change()Dim i As IntegerFor i = 1 To Len(Password1.Text)If Len(Password1.Text) >= 16 ThenPassword1.Text = ""MsgBox "密码不会超过16位,请重输", vbOKOnly + vbExclamation, "警告"ElseIf Password1.Text <> "" ThenIf ((Asc(Mid(Password1.Text, i, 1)) < 48) Or ((Asc(Mid(Password1.Text, i, 1)) > 57) And (Asc(Mid(Password1.Text, i, 1)) < 65)) Or ((Asc(Mid(Password1.Text, i, 1)) > 90) And (Asc(Mid(Password1.Text, i, 1)) < 97)) Or (Asc(Mid(Password1.Text, i, 1)) > 122)) ThenMsgBox "密码只能由数字和字母组成,请重输", vbOKOnly + vbExclamation, "警告"Password1.Text = ""End IfEnd IfNext iEnd Sub
2.只能输入数字和Backspace键
Private Sub txtSID_KeyPress(KeyAscii As Integer) If KeyAscii = 8 Then ElseIf KeyAscii < Asc(“0”) Or KeyAscii > Asc(“9”) Then KeyAscii = 0 End If End Sub
3.只能输入大小写英文字母和汉字(包括退格和回车)
Private Sub txtName_KeyPress(KeyAscii As Integer) If KeyAscii < 0 Or KeyAscii = 8 Or KeyAscii = 13 Then ElseIf Not Chr(KeyAscii) Like “[a-zA-Z]” Then KeyAscii = 0 End If End Sub
4.只能输入汉字
Private Sub txtDirector_KeyPress(KeyAscii As Integer) If KeyAscii < 0 Or KeyAscii = 8 Then Exit Sub KeyAscii = 0 ‘不能输入 End Sub
5.限制字符输入
Private Sub txtUserName_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case 8 Case Asc(“A”) To Asc(“Z”) Case Asc(“0”) To Asc(“9”) Case Asc(“a”) To Asc(“z”) Case Is < 0 Case Else KeyAscii = 0 End Select End Sub
Select Case KeyAscii    Case 8    '无变化,退格键不屏蔽    Case Asc("A") To Asc("z")    Case Asc("0") To Asc("9")    Case Is < 0Case Else ' 除了以上的 都屏蔽    KeyAscii = 0
6.输入的分数(成绩)只能是整型,输入的个数(分数大小)有限。
Private Sub txtResult_Change()Dim strItem(1 To 27) As StringDim m As IntegerDim t(30) As Stringm = m + 1t(m) = txtResultDim i As IntegerFor i = 1 To Len(txtResult.Text)    If Asc(Mid(txtResult.Text, i, 1)) < 48 Or Asc(Mid(txtResult.Text, i, 1)) > 58 Then       MsgBox "成绩只能是整型数字"       txtResult.Text = t(m - 1)       txtResult.SelStart = (m - 1)    End IfNext iIf m > 3 Then m = 1End IfIf Len(txtResult.Text) > 3 Then     MsgBox "分数不能超过3位"     txtResult.Text = t(m - 1)     txtResult.SelStart = (m - 1)End If
7.判断输入的是否是数字:
  If Not IsNumeric(Trim(txtSID.Text)) Then      MsgBox "请输入数字!", vbOKOnly + vbExclamation, "警告"      Exit Sub      txtSID.SetFocus  End If
如果小伙伴们还有其他想法,欢迎讨论。您的采纳就是我收到的最好的礼物!


原创粉丝点击