ASP.NET中的输入验证
2007-12-16 15:30
344 查看
前些日子因为验证的问题而闹心,具体情况如下
1,使用javascript,在按钮按下时直接执行服务器端的代码去了,不会执行javascript;
2,使用验证控件,验证控件占页面空间,有时候不好排版,当有两个以上按钮时,如一个提交,一个取消,按下取消的那个按钮也会执行验证,而这是不需要的。
经过一段时间学习和摸索,以上问题都得以解决,现将所知的验证方法总结如下:
一,使用javascript验证
其实在asp.net也可以使用javascript验证的,方法也很简单,只是要在提交按钮增一个属性,如例1。
例1:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<%@ Page Language="C#" %>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<script runat="server">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
protected void Page_Load(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
btn.Attributes["onclick"] = "return check();";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
protected void btn_Click(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Response.Write("验证成功");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</script>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml" >
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<head runat="server">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<title>例一</title>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<script type="text/javascript">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
function check()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(document.form.tb.value == "")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
alert("不能为空");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
document.form.tb.focus();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</script>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</head>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<form id="form" runat="server">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<div>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<asp:TextBox runat="server" ID="tb" ></asp:TextBox>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<asp:Button runat="server" ID="btn" OnClick="btn_Click" Text="开始验证" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</div>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</form>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</html>
值得一提的这里的tb和btn是编译后的控件的name,而value也是编译后的控件的属性(<asp:textbox>没有value属性)。因空服务器控件会编译成相应的html控件。以下是部分对比:
<asp:Button > <input type=’submit’>
<asp:checkbox> <input type=’checkbox’>
<asp:checkboxlist> <input type=’checkbox’>
<asp:radiobutton> <input type=’radio’>
<asp:radiobuttonlist> <input type=’radio’>
<asp:image> <img>
<asp:imagebutton> <img>
<asp:hyperlink> <a>
<asp:linkbutton> <a>
<asp:dropdownlist> <select>
<asp:listbox> <select>
<asp:label> <span>
<asp:panel> <div>
所以使用javascript时要选择正确的属性。
二, 在服务器端验证
也挺简单的,如例2。
例2:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<%@ Page Language="C#" %>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<script runat="server">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
protected void btn_Click(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (tb.Text == "")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Response.Write("不能为空");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Response.End();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</script>
三, 使用验证控件
前面遇到的问题其实佷好解决,︿_︿。
其一:每一个验证控件都有一个display属性,属性值有:static:无论错误提示是否显示都占用网页空间。Dynamic:当错误提示不显示时不占网页空间,none:在ValidationSummary里显示。所以选择Dynamic可以解决一些版面的问题,但当错误显示时有时还是会破坏一些版面。
其二:<asp:Button>有一个属性CausesValidation,只要将它设为false就不会执行验证了。
1,验证控件有:
RequiredFieldValidator:验证输入是否为空
CompareValidator:可以比较两个控件的属性或一个控件与一个特定的值。
RangeValidator:验证目标控件的值是否在指定的范围内
RegularExpressionValidator:使用正则表达式,功能肯定不小
CustomValidator:自定义验证控件
ValidationSummary:列出所有没通过验证的控件的ErrorMessage值
验证控件可以验的的控件有:
HtmlInputText value
HtmlTextArea value
HtmlSelect value
HtmlInputFile value
TextBox value
ListBox SelectItem
DropDownList SelectItem
RadioButtonList SelectItem
2,正正则表达式:
^ 字符串的开始
$ 字符串的结束
[]符号中表示可以接受的字符,- 符号表示取值范围,[0-9]表示可以接受0-9中的任意数字,如果在[]符号中用^符号,表示反向接受
{}符号表示接受字符的个数。如[a-z]{0,}表示可以接受任意个小字母,[a-z]{1,5}表示可以接受1到5个小字字母,[a-z]{4}表示可以接受4个小字母。
/符号表示匹配特定含义的字符
. 符号表示任意一个字符
()符号表是将同一条件的表达式亿含起来
验证控件的内容较多,就不一一举例了,以后再使用中慢慢摸索了!
1,使用javascript,在按钮按下时直接执行服务器端的代码去了,不会执行javascript;
2,使用验证控件,验证控件占页面空间,有时候不好排版,当有两个以上按钮时,如一个提交,一个取消,按下取消的那个按钮也会执行验证,而这是不需要的。
经过一段时间学习和摸索,以上问题都得以解决,现将所知的验证方法总结如下:
一,使用javascript验证
其实在asp.net也可以使用javascript验证的,方法也很简单,只是要在提交按钮增一个属性,如例1。
例1:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<%@ Page Language="C#" %>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<script runat="server">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
protected void Page_Load(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
btn.Attributes["onclick"] = "return check();";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
protected void btn_Click(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Response.Write("验证成功");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</script>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml" >
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<head runat="server">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<title>例一</title>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<script type="text/javascript">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
function check()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(document.form.tb.value == "")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
alert("不能为空");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
document.form.tb.focus();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</script>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</head>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<form id="form" runat="server">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<div>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<asp:TextBox runat="server" ID="tb" ></asp:TextBox>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<asp:Button runat="server" ID="btn" OnClick="btn_Click" Text="开始验证" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</div>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</form>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</html>
值得一提的这里的tb和btn是编译后的控件的name,而value也是编译后的控件的属性(<asp:textbox>没有value属性)。因空服务器控件会编译成相应的html控件。以下是部分对比:
<asp:Button > <input type=’submit’>
<asp:checkbox> <input type=’checkbox’>
<asp:checkboxlist> <input type=’checkbox’>
<asp:radiobutton> <input type=’radio’>
<asp:radiobuttonlist> <input type=’radio’>
<asp:image> <img>
<asp:imagebutton> <img>
<asp:hyperlink> <a>
<asp:linkbutton> <a>
<asp:dropdownlist> <select>
<asp:listbox> <select>
<asp:label> <span>
<asp:panel> <div>
所以使用javascript时要选择正确的属性。
二, 在服务器端验证
也挺简单的,如例2。
例2:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<%@ Page Language="C#" %>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<script runat="server">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
protected void btn_Click(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (tb.Text == "")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Response.Write("不能为空");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Response.End();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</script>
三, 使用验证控件
前面遇到的问题其实佷好解决,︿_︿。
其一:每一个验证控件都有一个display属性,属性值有:static:无论错误提示是否显示都占用网页空间。Dynamic:当错误提示不显示时不占网页空间,none:在ValidationSummary里显示。所以选择Dynamic可以解决一些版面的问题,但当错误显示时有时还是会破坏一些版面。
其二:<asp:Button>有一个属性CausesValidation,只要将它设为false就不会执行验证了。
1,验证控件有:
RequiredFieldValidator:验证输入是否为空
CompareValidator:可以比较两个控件的属性或一个控件与一个特定的值。
RangeValidator:验证目标控件的值是否在指定的范围内
RegularExpressionValidator:使用正则表达式,功能肯定不小
CustomValidator:自定义验证控件
ValidationSummary:列出所有没通过验证的控件的ErrorMessage值
验证控件可以验的的控件有:
HtmlInputText value
HtmlTextArea value
HtmlSelect value
HtmlInputFile value
TextBox value
ListBox SelectItem
DropDownList SelectItem
RadioButtonList SelectItem
2,正正则表达式:
^ 字符串的开始
$ 字符串的结束
[]符号中表示可以接受的字符,- 符号表示取值范围,[0-9]表示可以接受0-9中的任意数字,如果在[]符号中用^符号,表示反向接受
{}符号表示接受字符的个数。如[a-z]{0,}表示可以接受任意个小字母,[a-z]{1,5}表示可以接受1到5个小字字母,[a-z]{4}表示可以接受4个小字母。
/符号表示匹配特定含义的字符
. 符号表示任意一个字符
()符号表是将同一条件的表达式亿含起来
验证控件的内容较多,就不一一举例了,以后再使用中慢慢摸索了!
相关文章推荐
- ASP.NET中对表单输入行有选择验证
- ASP.NET Web – 输入的有效性验证
- ASP.NET中服务器控件Button调用javascript代码运用正则表达式验证TextBox中输入的是否为正整数或正小数,符合要求则继续运行服务器端代码功能的实现
- ASP.NET 使用RegularExpressionValidator控件(数据输入格式验证)
- asp.net应用安全事项输入验证
- Asp.net RegularExpressionValidator 控件验证输入值验证输入值是否匹配正则表达式指定的模式
- ASP.NET c# textbox 正则表达式 文本框只允许输入数字(验证控件RegularExpressionValidator )
- 每日学习心得:CustomValidator验证控件验证用户输入的字符长度、Linq 多字段分组统计、ASP.NET后台弹出confirm对话框,然后点击确定,执行一段代码
- [ASP.NET-VII] ASP.NET 使用验证控件(RegularExpressionValidator)验证输入(TextBox)中的输入是否合法
- Asp.net如何验证TextBox控件只输入数字(五种情况分析)
- ASP.NET讲座(3)-输入验证控件
- ASP.NET讲座(3)-输入验证控件
- ASP.NET MVC 3里面客户端输入验证的改动
- ASP.NET学习手记:验证用户表单输入
- Asp.net MVC输入验证 Validation的用法
- ASP.NET动态生成输入页面,以及生成控件的大小控制,输入数据的验证等
- 每日学习心得:CustomValidator验证控件验证用户输入的字符长度、Linq 多字段分组统计、ASP.NET后台弹出confirm对话框,然后点击确定,执行一段代码
- 每日学习心得:CustomValidator验证控件验证用户输入的字符长度、Linq 多字段分组统计、ASP.NET后台弹出confirm对话框,然后点击确定,执行一段代码
- Asp.net如何验证TextBox控件只输入数字(五种情况分析)转帖
- ASP.NET - 在 ASP.NET 网页中验证用户输入