Date validations

Last post 05-17-2008 4:26 AM by kieboy. 8 replies.

Sort Posts:

  • Date validations

    05-17-2008, 1:18 AM

    Hi All,

    I am generating some ree ports and displaying result on aspx pages.

    My requirement is that there will be two date from date and to date, now these dates should not be greater than todays date and from date should always be lesser than to date.

    Does anybody know how to get it done using validation controls.

    Any help would be appreciated.

    Regards.

  • Re: Date validations

    05-17-2008, 2:01 AM
    Answer
    • Loading...
    • SolaimalaiVT
    • Joined on 06-12-2007, 7:00 AM
    • India
    • Posts 73

     Hi,

    <asp:TextBox runat="Server" ID="txtFormDate"></asp:TextBox>

    <asp:CompareValidator runat="Server" ID="cvFormDate" ErrorMessage="From Date Must be Greater Than Today Date" ControlToValidate="txtFormDate" Operator="GreaterThan" Type="Date"></asp:CompareValidator>

    <asp:TextBox runat="Server" ID="txtToDate"></asp:TextBox>

    <asp:CompareValidator runat="Server" ID="cvToDate" ErrorMessage="To Date Must be Greater Then From Date" ControlToCompare="txtFormDate" ControlToValidate="txtToDate" Operator="GreaterThan" Type="Date"></asp:CompareValidator>

    </div>

     Add this in the Page Load

     cvFormDate.ValueToCompare = System.DateTime.Now.ToShortDateString();

     This is Working for me.

    Check this out

    Don't Forgot to Mark it as ANSWER if it is Helpful for you

    With Regards,
    Solaimalai V.T
  • Re: Date validations

    05-17-2008, 2:15 AM

    Hi,

    For comparing 2 dates u have to use compareTo function of datetime object.

    First u have to cast the string value to a object...

    DateTime firstDate = DateTime.Parse("5/17/2008");
    DateTime secondDate = DateTime.Parse("5/17/2008");
    DateTime todaysDate =   DateTime.Parse(DateTime.Now.ToShortDateString());

     if (firstDate .CompareTo(todaysDate ) >= 0 ||  secondDate .CompareTo(todaysDate ) >= 0 )
            {
                Response.Write("To date and from date should be greater than todays date");

            }
            else if (firstDate .CompareTo(todaysDate ) >= 0 )
            {
                Response.Write("From date should be less than second date");

            }
            else
            {
               // Ur code

            }

     

     

    Hope this helps U.... 

  • Re: Date validations

    05-17-2008, 2:16 AM

    in addition to the above solution; dont forget to set the "ValidationGroup" Property to same Group (for Ex., ValidateDateSelection) for 2 CompareValidators, 2 TextBoxes and the Button where you are initiating the report and using these dates...

    hope it helps./. 

    Thanx,
    [KaushaL] || BloG || Profile

  • Re: Date validations

    05-17-2008, 3:10 AM

    Hi,

    Thanks for sparing some time for me.

    I tried and i got the following error

    The value '17-May-2008' of the ValueToCompare property of 'cvFormDate' cannot be converted to type 'Date'.

    Do i have set date to certain format in regional settings or is there any other problem.

    Kindly reply me asap.

    Regards.

  • Re: Date validations

    05-17-2008, 3:15 AM
    • Loading...
    • SolaimalaiVT
    • Joined on 06-12-2007, 7:00 AM
    • India
    • Posts 73

    Hi,

       Please Enter the date in the Formate of mm/dd/yyyy

     Eg: 05/17/2008 This is the formate to enter the date

    17-May-2008 will not convert into date formate

    Don't Forgot to Mark it as ANSWER if it is helpful for you

     

    With Regards,
    Solaimalai V.T
  • Re: Date validations

    05-17-2008, 3:21 AM

    Hi,

    Error comes up the moment i run the application i.e., while the page is loading.

  • Re: Date validations

    05-17-2008, 4:00 AM
    • Loading...
    • SolaimalaiVT
    • Joined on 06-12-2007, 7:00 AM
    • India
    • Posts 73

    Please post the code in Pageload i wil help you

    With Regards,
    Solaimalai V.T
  • Re: Date validations

    05-17-2008, 4:26 AM
    • Loading...
    • kieboy
    • Joined on 09-06-2004, 12:47 AM
    • Philippines/Saudi Arabia
    • Posts 119

    It will be a lot easier if you use the calendar control to lessen the typo error or format issues. Anyway you can try this:

    <asp:Calendar ID="calFrom" runat="server" OnSelectionChanged="calFrom_SelectionChanged" OnDayRender="calDataBind" />
                <asp:TextBox ID="txtFrom" runat="server" />
                <br />
                <asp:Calendar ID="calTo" runat="server" OnSelectionChanged="calTo_SelectionChanged" OnDayRender="calDataBind" />
                <asp:TextBox ID="txtTo" runat="server" />
                <br />
                <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="txtFrom" ControlToValidate="txtTo" ErrorMessage="Date from should be earlier than date to" Operator="GreaterThan" />
                <br />
                <asp:Button ID="btnValidate" runat="server"  Text="Validate!" />

    Code behind:

    protected void calFrom_SelectionChanged(object sender, EventArgs e) {

        txtFrom.Text = calFrom.SelectedDate.ToShortDateString();

    }

    protected void calTo_SelectionChanged(object sender, EventArgs e) {

        txtTo.Text = calTo.SelectedDate.ToShortDateString();

    }

    protected void calDataBind(object sender, DayRenderEventArgs e) {

        if (e.Day.Date > DateTime.Now) {

            e.Day.IsSelectable = false;

        }

    }

    Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
Page 1 of 1 (9 items)