Results 1 to 6 of 6

Thread: Using the Web Control RangeValidator in Asp.NET

  1. #1
    Join Date
    Jan 2011
    Posts
    40

    Using the Web Control RangeValidator in Asp.NET

    I am newbie to use the Asp.Net software and I have recently joined a course to learn about it. But still I don't think that certain things of an Asp.Net would be covered in my course so it would be better to keep on learning things online as well. Today I have created this thread to know that how can I use the Web Control RangeValidator in Asp.NET? No matter which software I create by using this beautiful asp but validation would be much more important, so lets start by it.

  2. #2
    Join Date
    May 2009
    Posts
    539

    Re: Using the Web Control RangeValidator in Asp.NET

    The RangeValidator is a control that allows the developer to ensure that the content entered by a user in a TexBox, is within a certain range of numbers (integer or the decimal point), strings, or dates, simply by specifying the minimum and maximum this range. Here's an example of use:

    Code:
    <%@ Page Language="C#" Debug="true"%> 
    <script runat="server"> 
    
    private void btn_onClick(object sender, EventArgs e) 
    { 
    if(Page.IsValid) 
    { 
    lbl.Text = "Valore inserito: "+txt.Text; 
    } 
    } 
    
    </script> 
    <html> 
    <body> 
    <form runat="server"> 
    <asp:TextBox id="txt" runat="server" /> 
    <asp:RangeValidator id="rv" 
    ControlToValidate="txt" 
    MaximumValue="10" 
    MinimumValue="0" 
    Type="Integer" 
    ErrorMessage="Il valore inserito non รจ corretto !" 
    runat="server" /> 
    <br /> 
    <asp:Button id="btn" 
    onClick="btn_onClick" 
    Text="Controlla" 
    runat="server" /> 
    <br /> 
    <asp:Label id="lbl" runat="server" /> 
    </form> 
    </body> 
    </html> 
    Convenient is not it?

  3. #3
    Join Date
    May 2009
    Posts
    529

    Re: Using the Web Control RangeValidator in Asp.NET

    In line with my posts on validating a CheckBoxList and validating a checkbox , here's a post that explains how to use controls and RangeValidator CompareValidator to ensure that the user may appreciate a number, it is between such and such a value so ...

    Validation of an integer:

    Code:
     <Asp: label runat = "server" AssociatedControlID = "tbInteger" Text = "integer"    />  
      <Asp: TextBox runat = "server" ID = "tbInteger"    /> 
      <Asp: CompareValidator runat = "server" ControlToValidate = "tbInteger"  
          Type = "Integer" Operator = "DataTypeCheck" Text = "*"    />
    Verifies that the number is between captures and int.MinValue Int.MaxValue.

    Validation of a point number:

    Code:
      <Asp: Label runat = "server" AssociatedControlID = "tbDouble" Text = "point number"    /> 
      <Asp: TextBox ID = "tbDouble" runat = "server"    /> 
      <Asp: CompareValidator runat = "server" ControlToValidate = "tbDouble"  
          Operator = "DataTypeCheck" Text = "*" Type = "Double"    />

  4. #4
    Join Date
    May 2009
    Posts
    637

    Re: Using the Web Control RangeValidator in Asp.NET

    You can also put self as value, in this case, culture is the configuration of the browser.

    Validating a date:

    Code:
     <Asp: Label runat = "server" AssociatedControlID = "tbDate" Text = "Date"    /> 
      <Asp: TextBox ID = "tbDate" runat = "server"    /> 
      <Asp: CompareValidator runat = "server" ControlToValidate = "tbDate" Operator = "DataTypeCheck" Text = "*" type = "date"    />

    Again the date format will be following the current culture. Remember to use the control of the Ajax toolkits Calendar , it will be much more enjoyable for the user.

    Validation of a value relative to another fixed value:

    As its name suggests, the CompareValidator can also verify that a value is lower than another ...

    Code:
     <Asp: Label runat = "server" AssociatedControlID = "tbCompare" Text = "Comparison"    /> 
      <Asp: TextBox ID = "tbCompare" runat = "server"    /> 
      <Asp: CompareValidator runat = "server" ControlToValidate = "tbCompare" ValueToCompare = "5" Operator = "LessThan" Text = "*" Type = "Integer"     />

  5. #5
    Join Date
    May 2009
    Posts
    543

    Re: Using the Web Control RangeValidator in Asp.NET

    Operators are available LessThan, LessThanEqual, NotEqual, Equal, GreaterThan, GreaterThanEqual. You can do the same thing with the type Date, Double, String. In the case of a comparison with a String it will String.Compare method to be used for most crops is in alphabetical order is taken into account.

    Validation of a value through an interval of fixed value: Using this time the RangeValidator:
    Code:
     <Asp: Label runat = "server" AssociatedControlID = "tbCompare2" Text = "Comparison 2"    /> 
      <Asp: TextBox ID = "tbCompare2" runat = "server"    /> 
      <Asp: RangeValidator runat = "server" ControlToValidate = "tbCompare2" MinimumValue = "01/01/2007" MaximumValue = "31/12/2007" Type = "Date" Text = "*"    />
    Again we can test dates, integers, floats, and strings, comparisons will be made following today's culture.

    Validating a value against another value defined by the user

    When you use a CompareValidator with the property ValueToCompare you can replace property ControlToCompare to leave the choice to the user on what value it should look. That's what we use when we want to verify that two passwords are identical.
    Code:
     <Asp: Label runat = "server" AssociatedControlID = "tbCompare" Text = "Comparison"    /> 
      <Asp: TextBox ID = "tbCompare" runat = "server"    /> 
      <Asp: TextBox ID = "tbCompare2" runat = "server"    /> 
      <Asp: CompareValidator runat = "server" ControlToValidate = "tbCompare" ControlToCompare = "tbCompare2" Operator = "LessThan" Text = "*" Type = "Integer"     />

  6. #6
    Join Date
    May 2009
    Posts
    511

    Re: Using the Web Control RangeValidator in Asp.NET

    These validators do not test the presence of value, only that the value is correct. To mandate a value you must use a RequiredFieldValidator. ASP.net default does not check the values, it is imperative to verify that the page is valid before any action:

    Code:
      Page.Validate ("validationGroupName"); 
      if (Page.IsValid) 
      {  
          / / Values are OK 
      }
    Then you can safely make a XXX.Parse to retrieve the correct value. Finally, to reiterate, the validators are not compatible with the UpdatePanel, but I have already explained how to make the validators compatible with UpdatePanel .

Similar Threads

  1. Replies: 7
    Last Post: 12-06-2013, 10:16 AM
  2. Replies: 3
    Last Post: 24-01-2012, 11:14 PM
  3. Replies: 4
    Last Post: 04-05-2011, 11:21 AM
  4. What is Media Access Control and Logical Link Control
    By Pakhi in forum Networking & Security
    Replies: 4
    Last Post: 10-01-2011, 11:53 AM
  5. What are the differences between flexgrid control and dbgrid control?
    By Chandrakant81 in forum Software Development
    Replies: 4
    Last Post: 27-02-2009, 06:28 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,618,201.64675 seconds with 17 queries