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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    New Coder
    Join Date
    Dec 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Verify Date function

    I'm trying to verify todays date against my input date, using a check value function, but for some reason when it comes to a day over 9 like 12/10/2009 or 12/20/2009 looks like is only recognizing the first number 12/1/2009 and 12/2/2009 and getting my alert back as to choose a future date, this is my code, any help would be greatly appreciated:


    var currentTime = new Date()
    var month = currentTime.getMonth() + 1
    var day = currentTime.getDate()
    var year = currentTime.getFullYear()
    function checkValues()
    {
    if (owt8.dayr.value <=month+'/'+day+'/'+year )
    {
    alert("Please enter a future date!");
    return false;
    }
    owt8.submit();
    alert("Your request has been submited")
    window.close()
    }

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,253
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    Try this:-


    Code:
    <script type = "text/javascript">
    
    function checkFutureDate() {
    var end_year = 2009;
    var end_month = 11;   // months are 0-11
    var end_day = 4;
    
    var now = new Date().getTime();
    var d = new Date();
    d.setFullYear(end_year, end_month, end_day);  // YYYY,MM(0-11),DD
    var selectedDate = d.getTime();  // today or after
    if (selectedDate <= now) {  // valid after today's date
    //if (selectedDate < now) {  // valid on today's date or after
    alert ("Date must be (on or) after today's date!");
    return false;
    }
    alert ("Date is valid");
    return true;
    }
    
    checkFutureDate();
    
    </script>

    "In the beginner's mind there are many possibilities, but in the expert's mind there are few” - Shunryu Suzuki (Japanese Zen priest, ?-1971)

  • #3
    New Coder
    Join Date
    Dec 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I forgot to mention, my variable is "owt8.dayr.value", this is the value I'm verifying, this is a form where when the date is input, will not allow to select current date or before, the logic I have is if date is less than or equal to current date, then will give me the alert, else will submit the form.

    Thanks for your help

  • #4
    New Coder
    Join Date
    Dec 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I guess my problem is that is not in a variable maybe?

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Code:
    if (new Date(owt8.dayr.value) <= new Date())
    {
        alert("Please enter a future date!");
        return false;
    }
    You may want to validate first the input if it is a valid date.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #6
    New Coder
    Join Date
    Dec 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yeah, it returns the right date, I manually input it

  • #7
    New Coder
    Join Date
    Dec 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I actually have a little javascript calendar to format my time, but even if manually input still gives me the error.

  • #8
    New Coder
    Join Date
    Dec 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    var currentTime = new Date()
    var month = currentTime.getMonth() + 1
    var day = currentTime.getDate()
    var year = currentTime.getFullYear()
    function checkValues()
    {
    if (owt8.tb1.options[0].selected == true)
    {
    alert("Please select your current start shift time!");
    return false;
    }
    else if (owt8.tb2.options[0].selected == true)
    {
    alert("Please select your current end shift time!");
    return false;
    }
    else if (owt8.dayr.value == "")
    {
    alert("Please Enter the date you are requesting!");
    return false;
    }
    else if (new Date(owt8.dayr.value) <=new Date()) )
    {
    alert("Please enter a future date!");
    return false;
    }
    owt8.submit();
    alert("Your request has been submited")
    window.close()
    }

  • #9
    New Coder
    Join Date
    Dec 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    this is the original:
    var currentTime = new Date()
    var month = currentTime.getMonth() + 1
    var day = currentTime.getDate()
    var year = currentTime.getFullYear()
    function checkValues()
    {
    if (owt8.tb1.options[0].selected == true)
    {
    alert("Please select your current start shift time!");
    return false;
    }
    else if (owt8.tb2.options[0].selected == true)
    {
    alert("Please select your current end shift time!");
    return false;
    }
    else if (owt8.dayr.value == "")
    {
    alert("Please Enter the date you are requesting!");
    return false;
    }
    else if (owt8.dayr.value <=month+'/'+day+'/'+year )
    {
    alert("Please enter a future date!");
    return false;
    }
    owt8.submit();
    alert("Your request has been submited")
    window.close()
    }

  • #10
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Did you not see that the browser throws an error?

    You have an extra )
    Code:
    else if (new Date(owt8.dayr.value) <=new Date()) )
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #11
    New Coder
    Join Date
    Dec 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Wow, programming is a test of patience, and attention, that WORKED, you are the man!!

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,253
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    Be aware that JavaScript form validation only provides convenience for users, not security. This means that JavaScript should be used as an "enhancement", not as a requirement. So your form should not be dependent on JavaScript alone to perform your validation. Instead, whatever server-side language you use to process the form (PERL, ASP, PHP, etc.) should also perform the same validation. Otherwise, people will be able to bypass your validation (and even possibly inject malicious code) simply by disabling JavaScript.

  • #13
    New Coder
    Join Date
    Dec 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    It's me again, same code, but now I'm trying to authenticate the and copare a fix date, same code, but with a new if statement, somthing like this:

    else if (new Date(owt8.dayr.value) >=Date (1/30/2010))
    {
    alert("No entries available on or after 1/30/2010!");
    return false;


    for some reason I believe 1/30/2010 is not been recognized as a date, my variable are

    var currentTime = new Date()
    var month = currentTime.getMonth() + 1
    var day = currentTime.getDate()
    var year = currentTime.getFullYear();


    Thank you in advance

  • #14
    New Coder
    Join Date
    Dec 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    here is the complete code

    var currentTime = new Date()
    var month = currentTime.getMonth() + 1
    var day = currentTime.getDate()
    var year = currentTime.getFullYear();
    function checkValues()
    {
    if (new Date(owt8.dayr.value) <=new Date())
    {
    alert("Please enter a future date!");
    return false;
    }
    else if (new Date(owt8.dayr.value) >=Date (1/30/2010))
    {
    alert("No entries available on or after 1/30/2010!");
    return false;
    }
    owt8.submit();
    alert("Your request has been submited")
    window.close()
    }

  • #15
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,253
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    Code:
    var futureDate = new Date(2010,0,30);  // 30th January 2010
    ......
    else if (new Date(owt8.dayr.value) >= futureDate) {
    alert("No entries available on or after 1/30/2010!");
    return false;
    }


    "Kuku kaki kakak kakak ku kayak kuku kaki kakek kakek ku" is Indonesian for "My sister's toenails look like my grandfather's."


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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