4个回答
展开全部
char[] charstr={'!','@','#','$','%','^','&','*','(',')','<','>','?',':','"','{','}'};// 非法字符数组(有几个就写几个用单引号引上‘’用,点隔开)
char[] textstr = TextBox1.Text.ToString().Trim().ToCharArray();//文本框输入的值
int temp = 0;
for (int i = 0; i < charstr.Length; i++)
{
for (int j = 0; j < textstr.Length; j++)
{
if (charstr[i] == textstr[j])
{
temp++;
}
}
}
if (temp > 0)
{
Response.Write("<script>alert('您输入了非法字符,请重新输入!')</script>");
}
试试看
char[] textstr = TextBox1.Text.ToString().Trim().ToCharArray();//文本框输入的值
int temp = 0;
for (int i = 0; i < charstr.Length; i++)
{
for (int j = 0; j < textstr.Length; j++)
{
if (charstr[i] == textstr[j])
{
temp++;
}
}
}
if (temp > 0)
{
Response.Write("<script>alert('您输入了非法字符,请重新输入!')</script>");
}
试试看
展开全部
用js表达式
例子
Public Function bTxtFmat(ByVal strTxt As String, ByVal iTxtType As Integer, ByVal bNullAllow As Boolean, ByVal strCaption As String) As Boolean
'txt=text.text
'txttype=1,2,3(1=数字型,2=字符型,3=汉字,4=数字和字符)
'nullallow=是否允许为空,true=可以为空。
'caption=显示文本框内容的标题
Dim i, k, m As Integer
If bNullAllow = False Then
If Len(strTxt) = 0 Then
MsgBox("" & strCaption & "输入项中内容不允许为空,请重新输入!", MsgBoxStyle.Exclamation, "系统警告")
bTxtFmat = False
Exit Function
End If
End If
k = Len(strTxt)
Select Case iTxtType
Case 1
For i = 1 To k
m = Asc(Right(Left(strTxt, i), 1))
If m < 48 Or m > 57 Then
MsgBox("" & strCaption & "输入项中只允许输入数字", MsgBoxStyle.Exclamation, "系统警告")
bTxtFmat = False
Exit Function
End If
Next
Case 2
For i = 1 To k
m = Asc(Right(Left(strTxt, i), 1))
If m < 65 Or (m > 91 And m < 97) Or m > 123 Then
MsgBox("" & strCaption & "输入项中只允许输入字符", MsgBoxStyle.Exclamation, "系统警告")
bTxtFmat = False
Exit Function
End If
Next
Case 3
For i = 1 To k
m = Asc(Right(Left(strTxt, i), 1))
If m >= 0 Then '汉字首位为1,按有符号计算全部为负数
MsgBox("" & strCaption & "输入项中只允许输入汉字!", MsgBoxStyle.Exclamation, "系统警告")
bTxtFmat = False
Exit Function
End If
Next
Case 4
For i = 1 To k
m = Asc(Right(Left(strTxt, i), 1))
If m < 0 Then '汉字首位为1,按有符号计算全部为负数
MsgBox("" & strCaption & "输入项中不允许输入汉字!", MsgBoxStyle.Exclamation, "系统警告")
bTxtFmat = False
Exit Function
End If
Next
End Select
bTxtFmat = True
End Function
用正则表达式限制用户输入汉字:
using System.Text.RegularExpressions;
......
string str;
......
bool yn=Regex.IsMatch(str,@"^[\u4e00-\u9fa5]+$");
if(yn==true)
{
MessageBox.Show("y"); //汉字
}
else
{
MessageBox.Show("n"); //不是汉字
}
例子
Public Function bTxtFmat(ByVal strTxt As String, ByVal iTxtType As Integer, ByVal bNullAllow As Boolean, ByVal strCaption As String) As Boolean
'txt=text.text
'txttype=1,2,3(1=数字型,2=字符型,3=汉字,4=数字和字符)
'nullallow=是否允许为空,true=可以为空。
'caption=显示文本框内容的标题
Dim i, k, m As Integer
If bNullAllow = False Then
If Len(strTxt) = 0 Then
MsgBox("" & strCaption & "输入项中内容不允许为空,请重新输入!", MsgBoxStyle.Exclamation, "系统警告")
bTxtFmat = False
Exit Function
End If
End If
k = Len(strTxt)
Select Case iTxtType
Case 1
For i = 1 To k
m = Asc(Right(Left(strTxt, i), 1))
If m < 48 Or m > 57 Then
MsgBox("" & strCaption & "输入项中只允许输入数字", MsgBoxStyle.Exclamation, "系统警告")
bTxtFmat = False
Exit Function
End If
Next
Case 2
For i = 1 To k
m = Asc(Right(Left(strTxt, i), 1))
If m < 65 Or (m > 91 And m < 97) Or m > 123 Then
MsgBox("" & strCaption & "输入项中只允许输入字符", MsgBoxStyle.Exclamation, "系统警告")
bTxtFmat = False
Exit Function
End If
Next
Case 3
For i = 1 To k
m = Asc(Right(Left(strTxt, i), 1))
If m >= 0 Then '汉字首位为1,按有符号计算全部为负数
MsgBox("" & strCaption & "输入项中只允许输入汉字!", MsgBoxStyle.Exclamation, "系统警告")
bTxtFmat = False
Exit Function
End If
Next
Case 4
For i = 1 To k
m = Asc(Right(Left(strTxt, i), 1))
If m < 0 Then '汉字首位为1,按有符号计算全部为负数
MsgBox("" & strCaption & "输入项中不允许输入汉字!", MsgBoxStyle.Exclamation, "系统警告")
bTxtFmat = False
Exit Function
End If
Next
End Select
bTxtFmat = True
End Function
用正则表达式限制用户输入汉字:
using System.Text.RegularExpressions;
......
string str;
......
bool yn=Regex.IsMatch(str,@"^[\u4e00-\u9fa5]+$");
if(yn==true)
{
MessageBox.Show("y"); //汉字
}
else
{
MessageBox.Show("n"); //不是汉字
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
正则表达式判断
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
js
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询