date validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kavinhuh
    New Member
    • Feb 2012
    • 13

    date validation

    hi all i know i am wrong in the date validation codes , please correct me , i am new to java script >>
    Code:
    <?php
    //include_once "con_1.php";
    /*$warn="";
         if (isset($_POST['email'])) 
    	{ $email=$_POST['email']; 
         if (preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/",$email))
    	{}
    	 else 
    		{
    	 $warn="please enter a valid email id";
    }
    }*/
    
    ?>
    
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <script type="text/javascript">
    <!-- Form Validation -->
    function validate_form()
    {
    valid = true; 
    var format = /^\d{2}\/\d{2}\/\d{4}$/;
    var date1= document.logform.dob.value;
    if ( document.logform.usname.value == "" ) { 
    alert ( "Please enter your User Name" ); 
    valid = false;
    }
    if ( document.logform.pass1.value == "" ) { 
    alert ( "Please enter your User Name" ); 
    valid = false;
    }
    if ( document.logform.pass2.value == "" ) { 
    alert ( "Please enter your User Name" ); 
    valid = false;
    }
    if ( document.logform.pass1.value != document.logform.pass2.value ) { 
    alert ( " password mismatch" ); 
    valid = false;
    }
    if(document.logform.dob.value == "")
    { alert("sa");
    }
    if ((!format.test(date1) )|| (!checkDate(format.exec(document.logform.dob))))
    {alert("Please enter a date in the format mm/dd/yyyy");
    }
    var month = parseInt(date1[1]);
    var date= parseInt(date1[2]);
    var year= parseInt(date1[3]);
    if (month<1 || month>12 || (day<30 || day>31)) valid=false;
    
    switch (month) {
    
    case 4: case 6: case 9: case 11:
    
    if (day > 30) 
    { valid= false;
    
    break;
    }
    case 2:
    if((year%400 ==0) || (year%100 != 0 && year%4 == 0) and (date>29)) 
    { valid= false;
    break;
    }
    else
    if(date>28)
    { valid= false;
    break;
    }
    default:
    
    if (day > 31) 
    {
    valid=false;
    break;
    }
    } 
    var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth()+1;
    var yyyy = today.getFullYear();
    
    if (year<1 || year>yyyy) valid= false;
    
    return valid;
    }
    
    <!-- Form Validation -->
    </script>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <style type="text/css">
    <!--
    #Layer1 {
    	position:absolute;
    	left:44px;
    	top:31px;
    	width:130px;
    	height:22px;
    	z-index:1;
    }
    -->
    </style>
    </head>
    
    <body>
    
    <form action="" method="post" enctype="multipart/form-data" name="logform" onsubmit="return validate_form();">
    <table cellpadding="3" cellspacing="2" border="0">
      <tr>
        <td width="30%">username</td>
        <td><input name="usname" type="text" id="usname" /></td>
      </tr>
      <tr>
        <td>password</td>
        <td colspan="2"><input name="pass1" type="text" id="pass1" /></td>
      </tr>
      <tr>
        <td>re-type password</td>
        <td colspan="2"><input name="pass2" type="text" id="pass2" /></td>
      </tr>
      <tr>
        <td>email id</td>
        <td colspan="2"><input name="email" type="text" id="email" /><marquee> <?php echo $warn?></marquee></td>
      </tr>
      <tr>
        <td>dob</td>
        <td><input name="dob" type="text" id="dob" /></td>
      </tr>
      <tr>
        <td align="center" colspan="2"><input type="submit" name="Submit" value="Submit" /></td>
      </tr>
      <select name="yourselection">
        <option value="">--Select--</option>
        <?php
     /*$msql = mysql_query("SELECT * FROM role");
     while($m_row = mysql_fetch_array($msql))        
            echo("<option value = '" . $m_row['role'] . "'>" . $m_row['role'] . "</option>");*/
        ?>
    </select>
    </body>
    </html>
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    there is a syntax error which would be listed in the Error Console. and line 48-50 do not make sense when compared to line 46.

    additionally, your switch() statement is highly susceptible to execute checks you do not intend.

    tip: the most simple leap year test I know:
    Code:
    function checkDay(day, month, year)
    {
        return (new Date(year, month-1, day).getMonth() == month-1);
    //  equivalent to
    //  return (new Date(year, month-1, day).getDate() == day);
    }

    Comment

    • kavinhuh
      New Member
      • Feb 2012
      • 13

      #3
      Originally posted by Dormilich
      there is a syntax error which would be listed in the Error Console. and line 48-50 do not make sense when compared to line 46.

      additionally, your switch() statement is highly susceptible to execute checks you do not intend.

      tip: the most simple leap year test I know:
      Code:
      function checkDay(day, month, year)
      {
          return (new Date(year, month-1, day).getMonth() == month-1);
      //  equivalent to
      //  return (new Date(year, month-1, day).getDate() == day);
      }
      how should i break the date which is entered into the text field into date , month year
      and wat does new date().getmonth () function does

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        and wat does new date().getmonth () function does
        see Description of the Date object

        how should i break the date which is entered into the text field
        consider the string you get. how could you break that up into the desired parts?

        Comment

        Working...