How to use ASP.net RangeValidator controls

Source: Internet
Author: User

The RangeValidator control is used to detect whether the value of a form field is between the specified minimum and maximum values. When you use this control, you must set 5 properties:

Q controltovalidate--The ID of the form field that is validated.

Q text--The error message that is displayed when validation fails.

Q minimumvalue--The minimum value of the validation range.

Q maximumvalue--The maximum value of the validation range.

Q type--The type of comparison performed. Possible values are string, Integer, Double, date, and currency

<%@ Page language= "C #"%>
2
3 <script runat= "Server" >
4 protected void Page_Load (object sender, EventArgs e)
5 {
6 rangevalidator1.minimumvalue = DateTime.Now.ToShortDateString ();
7 Rangevalidator1.maximumvalue = DateTime.Now.AddDays (14). ToShortDateString ();
8}
9
protected void Calendar1_SelectionChanged (object sender, EventArgs e)
11 {
TextBox1.Text = Calendar1.SelectedDate.ToShortDateString ();
13}
14
protected void Button1_Click (object sender, EventArgs e)
16 {
if (Page.IsValid)
18 {
Label1.Text = "You are set to arrive on:" + TextBox1.Text.ToString ();
20}
21}
</script>
23
<title>date Validation check</title>

<body>
<form id= "Form1" runat= "Server" >
Arrival Date:
<asp Tutorial: TextBox id= "TextBox1" runat= "Server" ></asp:TextBox>&nbsp;
<asp:rangevalidator id= "RangeValidator1" runat= "Server" text= "You must only select a date within the next two weeks. "
Controltovalidate= "TextBox1" type= "Date" ></asp:rangevalidator><br/>
<br/>
Select your arrival date:<br/>
<asp:calendar id= "Calendar1" runat= "Server" onselectionchanged= "calendar1_selectionchanged" ></asp: Calendar>
Panax Notoginseng &nbsp;
<br/>
<asp:button id= "Button1" runat= "Server" text= "button" onclick= "Button1_Click"/>
<br/>
<br/>
<asp:label id= "Label1" runat= "Server" ></asp:Label>
</form>
</body>

The RangeValidator control primarily defines the range of values entered. Because sometimes we require a certain range of values to be entered, so we use RangeValidator to judge.
In the following example, we'll introduce the RangeValidator control in the future.
Please see the validata5.aspx program content:

<!--source file: Formweb Page Introduction validate5.aspx-->
<%@ Page Clienttarget=downlevel%>

<center>
<title>rangevalidator Control Demo </title>

<script language= "VB" runat= "Server" >
Sub Button1Click (sender as Object, E as EventArgs)
If (Page.IsValid) Then
Lbloutput.text = "The result is correct!"
Else
Lbloutput.text = "The result is incorrect!"
End If
End Sub
Sub lstoperatorselectedindexchanged (sender as Object, E as EventArgs)
Rangeval.type = Lsttype.selectedindex
Rangeval.validate
End Sub
</script>

<body>

<p>
<form runat= "Server" >
<table bgcolor= "#eeeeee" cellpadding=10>
&LT;TR valign= "Top" >
<td>
<asp:textbox Selected id= "Txtcomp" runat= "Server"/>
</td>
<td>
<asp:dropdownlist id= "Lsttype" onselectedindexchanged= "lstoperatorselectedindexchanged" runat=server>
<asp:listitem Selected value= "String" >String</asp:ListItem>
<asp:listitem value= "Integer" >Integer</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:textbox id= "txtmin" runat= "Server"/>
</td>
<td>
<asp:textbox id= "Txtmax" runat= "Server"/><p>
<asp:button text= "Verify" id= "Button1" onclick= "Button1Click" runat= "Server"/>
</td>
</tr>
</table>
<asp:rangevalidator id= "Rangeval" type= "String" controltovalidate= "Txtcomp" maximumcontrol= "TxtMax" Minimumcontrol= "Txtmin" runat= "Server"/>
<br>
<asp:label id= "Lbloutput" font-name= "Verdana" font-size= "10pt" runat= "Server"/>
</form>
</body>
</center>


When we enter three text boxes to verify the value, the maximum, and the minimum value, and then press the Validation button, the page will display the result of the judgment

Do not forget to set the Type property when using the RangeValidator control. The value of the Type property defaults to a string comparison performed by the String,rangevalidator control to determine whether the value is between the minimum and the maximum value.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.