Question

I have this page and I am trying to confirm a date that's from an input text field vs the current date. If the date from the input field is later then the current date, the confirm box needs to come up to confirm that they are putting down the right date. So the 'OK' button needs to finish submitting the data and the 'Cancel' button needs to return them back to the AJAX form.

if($('sdate' > curdate)) {
    confirm("The date you chose " + curdate + "is in the future OK to continue");
}

I also wanted the 'sdate' to appear in the confirm box instead of curdate, but whenever I try that the form no longer appears.

Was it helpful?

Solution 4

I was able to get it to work with this

var d=$('sdate').value;

if(d > curdate)) {

  var dates confirm("The date you chose " + d + "is in the future OK to continue");
  if(!dates) return false;
}

OTHER TIPS

Instead of

$('sdate' > curdate)

you probably wanted

$('sdate').val() > curdate

that is a comparison of the dates (as strings but it should work if the dates are well formed which is the case for an input of type date or an input of type text with a standard plugin).

Note that you don't use the returned value from the confirm call. You would usually do something like

if ($('sdate').val() > curdate) {
    if (confirm("someText")) {
        // do something
    }
}

You need to do this:

var date1 = new Date($('#sdate').val());
var date2 = new Date(curdate);
if (date1 > date2 ) {
    confirm("The date you chose " + curdate + "is in the future OK to continue");
}
  • Element not fully wrapped. It should be like this $('#sdate')
  • '#' for the id missing. Not sure, if its a id or class. If class, add a '.' in place of #

In your current script, the confirm() call doesn't actually do anything.

What ever you're trying to do it will need to incorporate the following in some form or another.

if(confirm("The date you chose " + curdate + "is in the future OK to continue"))
    //Do stuff

Goin' by what you've posted and the answer posted by @dystroy you're probably looking for

if($("#sdate").val() > curdate && confirm("The date you chose " + curdate + "is in the future OK to continue")
    //Do stuff

In the above script both conditions are present in the one conditional. As per @dystroy's answer, I've included the .val() call. The other change is that it has been presumed that you're looking for an element with id="sdate", so the selector include the id symbol #

Edit (after OP's comment) - Updated based on this stackoverflow post

If you're trying to prevent a form submission then you should attach the function to the form in a manner similar to this

<form name="myForm" onsubmit="return validateMyForm();"> 

Once you've bound the event you could update your code to something like

if($("#sdate").val() > curdate && confirm("The date you chose " + curdate + "is in the future OK to continue")
    return true;
else
    return false;

This should only submit the form if the confirm returns true and the date inequality is met.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top