asp.net 2.0中的几个常用验证控件,很好用,下面帖一些示例代码
<form id="form1" runat="server">
<div>
<font color="blue">验证控件RequireFieldValidator:</font>
<br>
<asp:Label ID="Label1" Text='我的名字:' runat=server/>
<asp:TextBox ID='tb' runat="server" Width = 100px ></asp:TextBox>
<asp:Button ID='mybtn' Text='验证' runat='server'/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
ControlToValidate='tb'
runat="server"
ErrorMessage="请输入姓名"
SetFocusOnError="true">
</asp:RequiredFieldValidator>
</div>
<br />
<div>
<font color="blue">验证控件CompareValidator:</font>
<br>
密 码: <asp:TextBox ID='psw' runat="server" Width=100px TextMode=Password></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2"
ControlToValidate='psw'
runat="server"
ErrorMessage="请输入密码"
SetFocusOnError="true">
</asp:RequiredFieldValidator>
<br>
再次输入:<asp:TextBox ID='repsw' runat=server Width=100px TextMode=Password></asp:TextBox>
<asp:CompareValidator runat=server
id='CompareValidator1'
ControlToValidate='psw'
ControlToCompare='repsw'
ErrorMessage='前后输入不一致'
SetFocusOnError="true">
</asp:CompareValidator>
<br/>
<asp:Button runat=server Text='提交' id='btn'/>
</div>
<br />
<div>
<font color="blue">验证控件RangeValidator :</font>
<br />
数值:<asp:TextBox ID='tb1' runat=server></asp:TextBox>
<asp:RangeValidator id='RangeValidator1'
runat=server
ControlToValidate='tb1'
Type=Integer
MinimumValue=0
MaximumValue=255
SetFocusOnError=true
ErrorMessage='请输入0-255数值'>
</asp:RangeValidator>
<br />
<asp:Button ID="Button1" runat=server Text='提交' />
</div>
<br />
<div>
<font color="blue">验证控件RegularExpressionValidator :</font>
<br />
数值:<asp:TextBox ID='tb2' runat=server></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat=server
ControlToValidate='tb2'
ValidationExpression='[a-zA-Z]'
SetFocusOnError=true
ErrorMessage='请输入英文'>
</asp:RegularExpressionValidator >
<br />
<asp:Button ID="Button2" runat=server Text='提交' />
</div>
<br />
<div>
<font color="blue">验证控件ValidationSummary :</font>
<br />
您的ID:<asp:TextBox ID='tb3' runat=server></asp:TextBox>
<asp:RequiredFieldValidator ID="RegularExpressionValidator2" runat=server
ControlToValidate='tb3'
SetFocusOnError=true
ErrorMessage='请输入您的ID'>
</asp:RequiredFieldValidator>
<br />
<asp:Button ID="Button4" runat=server Text='提交' />
<br>
<div>
<asp:ValidationSummary ID="regValidationSummary" runat="server"
HeaderText="输入错误" DisplayMode="BulletList" />
</div>
</div>
<br />
<div>
<font color="blue">验证控件CustomValidator :</font>
<br />
<asp:DropDownList ID="list" runat="server">
<asp:ListItem Text="请选择" Value="0"></asp:ListItem>
<asp:ListItem Text="Item1" Value="a"></asp:ListItem>
<asp:ListItem Text="Item2" Value="b"></asp:ListItem>
</asp:DropDownList>
<asp:CustomValidator ID="CustomValidator1" runat=server
ControlToValidate='list'
ErrorMessage='请选择列表'
SetFocusOnError=true
Display="dynamic"
OnServerValidate="CustomValidator1_ServerValidate">
</asp:CustomValidator>
<br />
<asp:Button ID="Button3" runat=server Text='提交' />
<br />
</div>
</form>
<form id="form1" runat="server">
<div>
<font color="blue">验证控件RequireFieldValidator:</font>
<br>
<asp:Label ID="Label1" Text='我的名字:' runat=server/>
<asp:TextBox ID='tb' runat="server" Width = 100px ></asp:TextBox>
<asp:Button ID='mybtn' Text='验证' runat='server'/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
ControlToValidate='tb'
runat="server"
ErrorMessage="请输入姓名"
SetFocusOnError="true">
</asp:RequiredFieldValidator>
</div>
<br />
<div>
<font color="blue">验证控件CompareValidator:</font>
<br>
密 码: <asp:TextBox ID='psw' runat="server" Width=100px TextMode=Password></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2"
ControlToValidate='psw'
runat="server"
ErrorMessage="请输入密码"
SetFocusOnError="true">
</asp:RequiredFieldValidator>
<br>
再次输入:<asp:TextBox ID='repsw' runat=server Width=100px TextMode=Password></asp:TextBox>
<asp:CompareValidator runat=server
id='CompareValidator1'
ControlToValidate='psw'
ControlToCompare='repsw'
ErrorMessage='前后输入不一致'
SetFocusOnError="true">
</asp:CompareValidator>
<br/>
<asp:Button runat=server Text='提交' id='btn'/>
</div>
<br />
<div>
<font color="blue">验证控件RangeValidator :</font>
<br />
数值:<asp:TextBox ID='tb1' runat=server></asp:TextBox>
<asp:RangeValidator id='RangeValidator1'
runat=server
ControlToValidate='tb1'
Type=Integer
MinimumValue=0
MaximumValue=255
SetFocusOnError=true
ErrorMessage='请输入0-255数值'>
</asp:RangeValidator>
<br />
<asp:Button ID="Button1" runat=server Text='提交' />
</div>
<br />
<div>
<font color="blue">验证控件RegularExpressionValidator :</font>
<br />
数值:<asp:TextBox ID='tb2' runat=server></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat=server
ControlToValidate='tb2'
ValidationExpression='[a-zA-Z]'
SetFocusOnError=true
ErrorMessage='请输入英文'>
</asp:RegularExpressionValidator >
<br />
<asp:Button ID="Button2" runat=server Text='提交' />
</div>
<br />
<div>
<font color="blue">验证控件ValidationSummary :</font>
<br />
您的ID:<asp:TextBox ID='tb3' runat=server></asp:TextBox>
<asp:RequiredFieldValidator ID="RegularExpressionValidator2" runat=server
ControlToValidate='tb3'
SetFocusOnError=true
ErrorMessage='请输入您的ID'>
</asp:RequiredFieldValidator>
<br />
<asp:Button ID="Button4" runat=server Text='提交' />
<br>
<div>
<asp:ValidationSummary ID="regValidationSummary" runat="server"
HeaderText="输入错误" DisplayMode="BulletList" />
</div>
</div>
<br />
<div>
<font color="blue">验证控件CustomValidator :</font>
<br />
<asp:DropDownList ID="list" runat="server">
<asp:ListItem Text="请选择" Value="0"></asp:ListItem>
<asp:ListItem Text="Item1" Value="a"></asp:ListItem>
<asp:ListItem Text="Item2" Value="b"></asp:ListItem>
</asp:DropDownList>
<asp:CustomValidator ID="CustomValidator1" runat=server
ControlToValidate='list'
ErrorMessage='请选择列表'
SetFocusOnError=true
Display="dynamic"
OnServerValidate="CustomValidator1_ServerValidate">
</asp:CustomValidator>
<br />
<asp:Button ID="Button3" runat=server Text='提交' />
<br />
</div>
</form>