Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Setting user inputed date.

    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.

    Code:
    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);">

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Code:
    <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
    Last edited by Philip M; 11-18-2010 at 04:22 PM.

  • Users who have thanked Philip M for this post:

    dreadfate (11-18-2010)

  • #3
    New Coder
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Works perfectly thank you for helping me understand !

  • #4
    New Coder
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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???

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    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

  • #6
    New Coder
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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?



    Code:
    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.");
    }
    
    }

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    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.)

    Code:
    <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>
    Last edited by Philip M; 11-20-2010 at 10:01 AM.

  • Users who have thanked Philip M for this post:

    dreadfate (11-21-2010)

  • #8
    New Coder
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •