c# asp.net RangeValidator(范围验证)控件(11)
原创
©著作权归作者所有:来自51CTO博客作者shenzhoulong的原创作品,请联系作者获取转载授权,否则将追究法律责任
验证输入是否在一定范围,范围用MaximumValue(最大值)和MinimunVlaue(最小值)来确定,标准代码如下:
![](p_w_picpaths/editer/InBlock.gif)
<ASP:RangeValidator id=
"Vaidator_ID" Runat=
"Server" ![](p_w_picpaths/editer/InBlock.gif)
controlToValidate=
" 要验证的控件ID"![](p_w_picpaths/editer/InBlock.gif)
type=
"Integer"![](p_w_picpaths/editer/InBlock.gif)
MinimumValue=
"要验证的最小值"![](p_w_picpaths/editer/InBlock.gif)
MaximumValue=
"要验证最大值"![](p_w_picpaths/editer/InBlock.gif)
errorMessage=
" 错误信息"![](p_w_picpaths/editer/InBlock.gif)
Display=
"Static|Dymatic|None"![](p_w_picpaths/editer/InBlock.gif)
>
![](p_w_picpaths/editer/InBlock.gif)
占位符
![](p_w_picpaths/editer/InBlock.gif)
</ASP:RangeValidator>
用MinimumValue和MaximumValue来确定控件输入值得范围,用type来定义控件输入值的类型。
前台代码如下:
![](p_w_picpaths/editer/InBlock.gif)
form id=
"form1" runat=
"server">
![](p_w_picpaths/editer/InBlock.gif)
<div style=
"text-align:center">验证范围控件
![](p_w_picpaths/editer/InBlock.gif)
<hr style=
"width:50%"/>
![](p_w_picpaths/editer/InBlock.gif)
旅游登记信息<br />
![](p_w_picpaths/editer/InBlock.gif)
客户编号:
![](p_w_picpaths/editer/InBlock.gif)
<asp:TextBox ID=
"num_id" runat=
"server" BackColor=
"White"></asp:TextBox>
![](p_w_picpaths/editer/InBlock.gif)
<asp:RangeValidator ID=
"RangeValidator1" runat=
"server" ErrorMessage=
"编号为1~1000之间"![](p_w_picpaths/editer/InBlock.gif)
ControlToValidate=
"num_id" MaximumValue=
"1000" MinimumValue=
"1" Type=
"Integer"></asp:RangeValidator><br />
![](p_w_picpaths/editer/InBlock.gif)
旅游日期:
![](p_w_picpaths/editer/InBlock.gif)
<asp:TextBox ID=
"travel_date" runat=
"server"></asp:TextBox>
![](p_w_picpaths/editer/InBlock.gif)
<asp:RangeValidator runat=
"server" ID=
"RangeValidator2" ErrorMessage=
"旅游日期为2个月内" ![](p_w_picpaths/editer/InBlock.gif)
ControlToValidate=
"travel_date" Type=
"Date">
![](p_w_picpaths/editer/InBlock.gif)
</asp:RangeValidator><br/>
![](p_w_picpaths/editer/InBlock.gif)
<asp:Button ID=
"Button1" runat=
"server" Text=
"提交" OnClick=
"button_Click" /><br />
![](p_w_picpaths/editer/InBlock.gif)
<asp:Label ID=
"Label1" runat=
"server" Width=
"353px" ForeColor=
"Red"></asp:Label>
![](p_w_picpaths/editer/InBlock.gif)
</div>
![](p_w_picpaths/editer/InBlock.gif)
</form>
后台c#代码
protected void Page_Load(
object sender, EventArgs e)
![](p_w_picpaths/editer/InBlock.gif)
{
![](p_w_picpaths/editer/InBlock.gif)
RangeValidator2.MinimumValue = DateTime.Now.ToShortDateString();
![](p_w_picpaths/editer/InBlock.gif)
RangeValidator2.MaximumValue = DateTime.Now.AddMonths(2).ToShortDateString();
//这里是学习的重点,addmonths()为从目前加上几个月为止,也可以是年![](p_w_picpaths/editer/InBlock.gif)
}
protected void button_Click(
object sender, EventArgs e)
![](p_w_picpaths/editer/InBlock.gif)
{
//验证提示
if (Page.IsValid)
![](p_w_picpaths/editer/InBlock.gif)
Label1.Text =
"验证成功";
![](p_w_picpaths/editer/InBlock.gif)
}
效果图:
![](../p_w_upload/201004/201004241272122367165.jpg)
输入信息效果图:
![](file:///C:/Users/%E7%A5%9E%E8%88%9F%E9%BE%99/Desktop/%E8%8C%83%E5%9B%B4%E9%AA%8C%E8%AF%812.jpg)