-2

This is the script i use for the date:

<script type="text/javascript">
var datefield=document.createElement("input")
datefield.setAttribute("type", "date")
if (datefield.type!="date"){ //if browser doesn't support input type="date", load files for jQuery UI Date Picker
    document.write('<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />\n')
    document.write('<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"><\/script>\n')
    document.write('<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"><\/script>\n') 
}
</script>

Alert is not working when the requirement is not fullfilled. Below is my code:

function validateForm() {


var x = document.forms["myForm"]["from"].value;
if (x == null || x == "") {
    alert("please choose the date");
    return false;
}
var y = document.forms["myForm"]["to"].value;
if (y == null || y == "") {
    alert("please choose the date");
    return false;
}

var date1 = new Date("x");
var date2 = new Date("y");

var diffDays = (date1 - date2);
var totalSeconds = diffDays / 1000;
var days = Math.floor(totalSeconds / 86400);
if(days < 7 )
{
    alert("please be more than 7 days");
    return false;
}

I have problem in getting the range between the dates. Any help will be appreciated.

June
  • 85
  • 1
  • 7

2 Answers2

1

You are passing the variables x and y into the Date object as strings. Remove the " around them.

var date1 = new Date(x);
var date2 = new Date(y);
turnip
  • 2,246
  • 5
  • 30
  • 58
1

change:

var date1 = new Date("x");
var date2 = new Date("y");

to:

var date1 = new Date(x);
var date2 = new Date(y);

Also see How can I tell if a browser supports <input type='date'> for more reliably check if the date type is supported.

Community
  • 1
  • 1