0

I have built a form with jqueryui-date picker - basically if the end date is less than or equal to start time it needs to display a message saying it must be greater than the start time before allowing the user to submit the form. Cannot see where i am going wrong.

Code Below on Submit

protected void btnSubmit_Click(object sender, EventArgs e)
    {

        DateTime startDate = Convert.ToDateTime(txtStartDate.Text + " " + ddlTime.SelectedValue);
        DateTime endDate = Convert.ToDateTime(txtEndDate.Text + " " + ddlTime2.SelectedValue);

        if (startDate >= DateTime.Now)
        {
            if (endDate <= startDate)
            {
                usrComment.Visible = true;
                //usrComment.Text = "Return time needs to be greater than pickup time IF same day";
                usrComment.Text = "Date =" + startDate + "Date 2 =" + endDate;
            }
            else
            {
                if (Page.IsValid)
                {
                    string EmailServer = WebConfigurationManager.AppSettings["Email.Server"];
                    int ServerPort = Int32.Parse(WebConfigurationManager.AppSettings["Email.ServerPort"]);
                    string EmailServerUser = (WebConfigurationManager.AppSettings["Email.UserName"]);
                    string EmailServerPass = (WebConfigurationManager.AppSettings["Email.Password"]);

                    string EmailFrom = (WebConfigurationManager.AppSettings["Email.From"]);
                    string EmailTo = (WebConfigurationManager.AppSettings["Email.To"]);
                    string EmailToUser = txtEmail.Text;
                    string EmailSubject = "Quote Form submitted";

                    ****.****.*****.Email m = new ****.****.Helpers.Email(EmailServer, ServerPort, EmailServerUser, EmailServerPass);

                    StringBuilder html = new StringBuilder();
                    html.AppendLine("<ul>");
                    html.AppendLine("<li>" + lblName.Text + ": " + txtName.Text + "</li>");
                    html.AppendLine("<li>" + lblEmail.Text + ": " + txtEmail.Text + "</li>");
                    html.AppendLine("<li>" + lblPhone.Text + ": " + txtPhone.Text + "</li>");
                    html.AppendLine("<li>" + lblVehicleType.Text + ": " + ddlVehicleType.SelectedValue + "</li>");
                    html.AppendLine("<li>" + lblPickupDate.Text + ": " + txtStartDate.Text + "</li>");
                    html.AppendLine("<li>" + ddlTime.SelectedValue + "</li>");
                    html.AppendLine("<li>" + lblReturnDate.Text + ": " + txtEndDate.Text + "</li>");
                    html.AppendLine("<li>" + ddlTime2.SelectedValue + "</li>");
                    html.AppendLine("</ul>");

                    m.SendHTMLEmail(EmailFrom, EmailTo, EmailSubject, html.ToString());

                    //Response.Redirect("/contact-us/quote-form-submitted.aspx");
                }
                usrComment.Text = "SUBMIT IT NOW!!!!!";
            }
        }
    }

jQuery for the date picker

$(function () {

    function getDiff() {
        var from = $(".start").val();
        var till = $(".fin").val();
        var c = from.split("/");
        beg = new Date(c[2], c[1] - 1, c[0]);
        var d = till.split("/");
        en = new Date(d[2], d[1] - 1, d[0]);
        var rest = (en - beg) / 86400000;
        var txt = rest == 0 ? "" : rest + " days"
        $("#res").text(txt);
    }

    $(".start").datepicker({
        changeMonth: false,
        changeYear: false,
        showAnim: "fadeIn",
        gotoCurrent: true,
        minDate: 0, //change this to +3 to start 3 days from now
        dateFormat: "dd/mm/yy",
        onSelect: function (dateText, inst) {
            $(".fin").val(dateText);
            $(".fin").datepicker("option", "minDate", dateText);
            getDiff();
        }
    });

    $(".fin").datepicker({
        dateFormat: "dd/mm/yy",
        changeMonth: true,
        changeYear: true,
        showAnim: "fadeIn",
        onSelect: getDiff
    });

    //Disabling Copy, Paste, Cut
    $('.start').bind('paste', function (e) {
        e.preventDefault();
        //alert("You cannot paste text into this textbox!");
        window.alert = function () { };
    });

    $('.fin').bind('paste', function (e) {
        e.preventDefault();
        //alert("You cannot paste text into this textbox!");
        window.alert = function () { };
    });

});

So if you have a pickup date of 17/09/2013 and pickup time as 08:00 and the same for return date and time it should alert you with the message and if the return date is greater than or equal to start time the return pickup time needs to be greater than 08:00 if that makes sense?

7
  • 1
    You should consider reading up on validation techniques in ASP.NET before proceeding further. This is just one simple validation; if you need twenty in the same form, you're going to be in a world of hurt without better techniques. Commented Sep 17, 2013 at 16:13
  • @JonLaMarr it's mot alerting the usrComment om submit. I may just have to comment out the code and go through it line by line. Commented Sep 17, 2013 at 16:15
  • 1
    You can certainly validate on the client side, but since you cannot trust input from a browser, you still need to validate on the server side. msdn.microsoft.com/en-us/library/a0z2h4sw(VS.100).aspx Commented Sep 17, 2013 at 16:18
  • 1
    @c-sharpnewbie well maybe this criteria is never met: if (startDate >= DateTime.Now) considering the start date you gave in your example was in the past... and you have no else. Commented Sep 17, 2013 at 16:20
  • 1
    @c-sharpnewbie ok good.... I would have posted as an answer you know... Commented Sep 18, 2013 at 9:07

2 Answers 2

1

It would be nice to have a useful, constructive comment. It doesn't matter if this is the "right way" to do it. I believe this is what you're trying to do. I've just added an else to the initial if statement to inform the user to choose a Start date later than now and altered the text of the other else statement to inform the user to pick a return date later than the start date.

protected void btnSubmit_Click(object sender, EventArgs e)
    {

        DateTime startDate = Convert.ToDateTime(txtStartDate.Text + " " + ddlTime.SelectedValue);
        DateTime endDate = Convert.ToDateTime(txtEndDate.Text + " " + ddlTime2.SelectedValue);

        if (startDate >= DateTime.Now)
        {
            if (endDate <= startDate)
            {
                usrComment.Visible = true;
                usrComment.Text = "Please enter a Return date later than " + startDate;
            }
            else
            {
                if (Page.IsValid)
                {
                    string EmailServer = WebConfigurationManager.AppSettings["Email.Server"];
                    int ServerPort = Int32.Parse(WebConfigurationManager.AppSettings["Email.ServerPort"]);
                    string EmailServerUser = (WebConfigurationManager.AppSettings["Email.UserName"]);
                    string EmailServerPass = (WebConfigurationManager.AppSettings["Email.Password"]);

                    string EmailFrom = (WebConfigurationManager.AppSettings["Email.From"]);
                    string EmailTo = (WebConfigurationManager.AppSettings["Email.To"]);
                    string EmailToUser = txtEmail.Text;
                    string EmailSubject = "Quote Form submitted";

                    ****.****.*****.Email m = new ****.****.Helpers.Email(EmailServer, ServerPort, EmailServerUser, EmailServerPass);

                    StringBuilder html = new StringBuilder();
                    html.AppendLine("<ul>");
                    html.AppendLine("<li>" + lblName.Text + ": " + txtName.Text + "</li>");
                    html.AppendLine("<li>" + lblEmail.Text + ": " + txtEmail.Text + "</li>");
                    html.AppendLine("<li>" + lblPhone.Text + ": " + txtPhone.Text + "</li>");
                    html.AppendLine("<li>" + lblVehicleType.Text + ": " + ddlVehicleType.SelectedValue + "</li>");
                    html.AppendLine("<li>" + lblPickupDate.Text + ": " + txtStartDate.Text + "</li>");
                    html.AppendLine("<li>" + ddlTime.SelectedValue + "</li>");
                    html.AppendLine("<li>" + lblReturnDate.Text + ": " + txtEndDate.Text + "</li>");
                    html.AppendLine("<li>" + ddlTime2.SelectedValue + "</li>");
                    html.AppendLine("</ul>");

                    m.SendHTMLEmail(EmailFrom, EmailTo, EmailSubject, html.ToString());

                    //Response.Redirect("/contact-us/quote-form-submitted.aspx");
                }
                usrComment.Text = "SUBMIT IT NOW!!!!!";
            }
        }
        else
        {
            usrComment.Visible = true;
            usrComment.Text = "Please enter a Start date later than " + DateTime.Now;
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

I suggest you not to use any kind of custom function. JquerUI-date picker have inbuilt functionality for comparing end date with start date.

Please try this

http://jqueryui.com/datepicker/#date-range

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.