...

View Full Version : Setting user inputed date.



dreadfate
11-18-2010, 03:35 PM
I am trying to make the date that is entered by the user within the pickDate text box be +2 days ahead of the current date.



function reserveDate(){
var mydate = new Date();
var theyear = mydate.getFullYear();
var themonth = mydate.getMonth()+1;
var thetoday = mydate.getDate()+2;
if (document.forms[0].pickDate.value <= thetoday)
window.alert("Reservations need to be two days in advance.");
}
input type="text" name="pickDate" onblur="return reserveDate(this.value);">

Philip M
11-18-2010, 05:20 PM
<script type = "text/javascript">

var reserveDate = new Date(2010,10,20); // date passed from date picker 20th November 2010
// Months in Javascript are 0-11
var mydate = new Date();
mydate.setDate(mydate.getDate()+2);

if (reserveDate < mydate) {
window.alert("Reservations need to be at least two days in advance.");
}

alert((mydate.getMonth()+1) + "/" + mydate.getDate() + "/" + mydate.getYear()); // USA format
alert(mydate.getDate() + "/" + (mydate.getMonth()+1) + "/" + mydate.getYear()); // UK format

</script>

Quizmaster: In nature, what invertebrate has a name which literally means "one hundred feet"?
Contestant: Giraffe

dreadfate
11-18-2010, 06:32 PM
Works perfectly thank you for helping me understand !

dreadfate
11-19-2010, 01:18 PM
var reserveDate = new Date(2010,10,20);


Actually this only worked for yesterday. I need to have a constant current date so the input field has to be constantly ahead of the current date. The date picker just sets the date is there a way to set the variable so that it constantly retrieves the current date???

Philip M
11-19-2010, 01:25 PM
var reserveDate = new Date(2010,10,20); // date passed from date picker 20th November 2010

This was only an example. You must pass the date chosen by the date picker to the function. As I do not know the date format as generated by your datepicker I cannot do any better.

You may have to reformat the chosen date in YYYY-MM-DD format.
(Months in Javascript are 0-11, so November is month 10).

var mydate = new Date(); // TODAY
mydate.setDate(mydate.getDate()+2); // TODAY PLUS 2 DAYS

dreadfate
11-20-2010, 09:51 AM
This seems to work for me but only for the indicated format mm/dd/yy is there a way to allow this to work with multiple formats? For the date fields, use a regular expression pattern that allows users to enter a one- or two-digit month, one- or two-digit date and a two- or four-digit year. Users should be able to enter any of the following date formats: 11-2-10, 1-25-2010, or 01/25/2010.

I have the regular expression to do this, is there a way to apply that to this function to allow multiple formats?





function reserveDate(){
var reserveDate = new Date(document.forms[0].pickDate.value);
var mydate = new Date();
mydate.setDate(mydate.getDate()+2);

if (reserveDate < mydate) {
window.alert("Reservations need to be at least two days in advance.");
}

}

Philip M
11-20-2010, 10:46 AM
I thought you said it was a date picker, not just a simple textbox into which the user enters a date. That requires validation to check that the date is in the proper format, but is also a valid date (not 43rd February or -3rd July.)


<html>
<head>

<script type = "text/javascript">

function checkValidDate(which) {
which = which.replace(/[-:,\.]/g,"/"); // replace non-/ delimiters by
which = which.replace(/[^0-9\/]/,"");
var dt = which.split("/");
var yr = dt[2];
if (!yr) {yr = 9999}
if (yr.length == 2) {yr = parseInt(yr) + 2000}
if ((yr < 2010) || ( yr > 2020)) {
alert ("Invalid Year or Incorrect Date Format!");
document.getElementById('dateIn').value = ""; // clear the field
setTimeout("document.getElementById('dateIn').focus()", 25); // and refocus on it
return false;
}
var mm = dt[0]-1;
var mmx = dt[0];
var dd = dt[1];
var nd = new Date();
nd.setFullYear(yr,mm,dd); // YYYY,MM(0-11),DD
var ndmm = nd.getMonth();
if (ndmm != mm) {
alert (mmx + "/" + dd + "/" + yr + " is NOT a Valid Date!");
document.getElementById("dateIn").value = ""; // clear the field
setTimeout("document.getElementById('dateIn').focus()", 25); // and refocus on it
return false;
}
else {
alert (mmx + "/" + dd + "/" + yr + " is a Valid Date!"); // for testing
}

var reserveDate = new Date(yr,mm,dd);
var mydate = new Date();
mydate.setDate(mydate.getDate()+2); // 2 days ahead of today
if (reserveDate < mydate) {
window.alert("Reservations need to be at least two days in advance.");
document.getElementById("dateIn").value = ""; // clear the field
setTimeout("document.getElementById('dateIn').focus()", 25); // and refocus on it
}

}

</script>

</head>

<body>

ENTER A DATE at least two days ahead of today's date (USA format MM/DD/YYYY) <input type = "text" id = "dateIn" onchange = checkValidDate(this.value)>

</body>
</html>

dreadfate
11-21-2010, 04:58 PM
Yeah the "pickDate" was my field name. Probably a poor choice. But that code does exactly what I needed. Thank you very much you are the man. :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum