用户名限制输入特殊字符

在SQL查询语句中要避免用户名中有特殊符号,像“=”,“‘”,等等,这些都会干扰查询甚至会报错。

以限制”‘“为例,我想了两种方法:

一、利用Like

Private Sub txtUserID_Change() '限制输入非法字符 dim bJudge as boolean bJudge = txtUserID Like "*'*" If bJudge Then MsgBox "请不要输入非法字符!", vbOKOnly + vbExclamation, "警告" End If End Sub

二、利用split()

'避免出现用户名中有"'"干扰sql查询语句出错 dim strA() as string strA = Split(txtUsername.Text, "'") '如果含有"'"就会把字符串截开,赋值到数组 If UBound(strA) <> 0 Then MsgBox "用户名中有非法字符,请重新输入!", vbOKOnly + vbExclamation, "警告" Exit Sub End If


补充一个:

三、利用inStr()

Private Sub txtUserName_Change() If InStr(txtUserName.Text, "'") Then MsgBox "用户名中有非法字符!", vbOKOnly + vbExclamation, "警告" End If End Sub


你可能感兴趣的:(用户名限制输入特殊字符)