验证输入是否在一定范围,范围用
MaximumValue(最大值)和
MinimunVlaue(最小值)来确定,标准代码如下:
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
<ASP:RangeValidator id=
"Vaidator_ID" Runat=
"Server"
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
controlToValidate=
" 要验证的控件ID"
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
type=
"Integer"
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
MinimumValue=
"要验证的最小值"
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
MaximumValue=
"要验证最大值"
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
errorMessage=
" 错误信息"
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
Display=
"Static|Dymatic|None"
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
>
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
占位符
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
</ASP:RangeValidator>
用
MinimumValue和
MaximumValue来确定控件输入值得范围,用
type来定义控件输入值的类型。
前台代码如下:
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
form id=
"form1" runat=
"server">
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
<div style=
"text-align:center">验证范围控件
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
<hr style=
"width:50%"/>
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
旅游登记信息<br />
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
客户编号:
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
<asp:TextBox ID=
"num_id" runat=
"server" BackColor=
"White"></asp:TextBox>
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
<asp:RangeValidator ID=
"RangeValidator1" runat=
"server" ErrorMessage=
"编号为1~1000之间"
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
ControlToValidate=
"num_id" MaximumValue=
"1000" MinimumValue=
"1" Type=
"Integer"></asp:RangeValidator><br />
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
旅游日期:
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
<asp:TextBox ID=
"travel_date" runat=
"server"></asp:TextBox>
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
<asp:RangeValidator runat=
"server" ID=
"RangeValidator2" ErrorMessage=
"旅游日期为2个月内"
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
ControlToValidate=
"travel_date" Type=
"Date">
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
</asp:RangeValidator><br/>
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
<asp:Button ID=
"Button1" runat=
"server" Text=
"提交" OnClick=
"button_Click" /><br />
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
<asp:Label ID=
"Label1" runat=
"server" Width=
"353px" ForeColor=
"Red"></asp:Label>
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
</div>
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
</form>
后台c#代码
protected
void Page_Load(
object sender, EventArgs e)
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
{
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
RangeValidator2.MinimumValue = DateTime.Now.ToShortDateString();
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
RangeValidator2.MaximumValue = DateTime.Now.AddMonths(2).ToShortDateString();
//这里是学习的重点,addmonths()为从目前加上几个月为止,也可以是年
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
}
protected
void button_Click(
object sender, EventArgs e)
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
{
//验证提示
if (Page.IsValid)
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
Label1.Text =
"验证成功";
![InBlock.gif](http://blog.51cto.com/shenzhoulong/images/editer/InBlock.gif)
}
效果图:
输入信息效果图:
本文转自shenzhoulong 51CTO博客,原文链接:http://blog.51cto.com/shenzhoulong/304255,如需转载请自行联系原作者