1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Javascript validation - System Date

Discussion in 'JavaScript' started by sam20e, Oct 23, 2010.

Thread Status:
Not open for further replies.
  1. #1
    Guyz,

    I have 2 textboxes (PHP script) i need to validate them using Javascript. one textbox is to type date.. but the user should not be able to type a future date and press submit.

    i wrote a javascript validation :

    
    <script language="JavaScript">
            <!--
            function CompareDates(str1,str2)
            {
                var dt1  = parseInt(str1.substring(0,2),10);
                var mon1 = parseInt(str1.substring(3,5),10);
                var yr1  = parseInt(str1.substring(6,10),10);
                var dt2  = parseInt(str2.substring(0,2),10);
                var mon2 = parseInt(str2.substring(3,5),10);
                var yr2  = parseInt(str2.substring(6,10),10);
                var date1 = new Date(yr1, mon1, dt1);
                var date2 = new Date(yr2, mon2, dt2);
           
                if(date2 < date1)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            function validate()
            {
                var str2 = document.getElementById("date").value;
               
                var str1 = "23/10/2010";
               
               
                if (document.frm.date.value=="")
                {
                    alert("Please enter date");
                    document.frm.date.focus();
                    return false;
                }
                else if (document.frm.number.value=="")
                {
                    alert("Please enter number");
                    document.frm.number.focus();
                    return false;
                }           
                else if (eval(document.frm.number.value)<=0)
                {
                    alert("Please enter number positive");
                    document.frm.number.focus();
                    return false;
                }
                else if (CompareDates(str1,str2))
                {
                    alert("To date cannot be greater than from date");
                    document.frm.date.focus();       
                    return false;
                }
                else
                    return true;           
            }   
            //-->
            </script>
    
    Code (markup):

    This works perfectly but the problem is validation takes current date from :


     var str1 = "23/10/2010"; 
    Code (markup):
    i need that to be the system date so i dont have to change the str value everytime.. any help?

    Thanks in advance
     
    Last edited: Oct 23, 2010
    sam20e, Oct 23, 2010 IP
  2. _:codefan:_

    _:codefan:_ Active Member

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Just replace:
    var str1 = "23/10/2010";
    Code (markup):
    with:
    
    var date = new Date();
    var str1 = date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear();
    Code (markup):
     
    _:codefan:_, Oct 24, 2010 IP
  3. sam20e

    sam20e Member

    Messages:
    57
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    45
    #3
    you just made my day. its working like a charm. thanks alot codefan
     
    sam20e, Oct 24, 2010 IP
Thread Status:
Not open for further replies.