Click here to Skip to main content
16,021,294 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i dont know y my code(date validation ) is not working i am newe to php , please help me , thanking in advance


PHP
<pre lang="PHP"><pre lang="xml"><!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( )
{
var format = /^\d{2}\/\d{2}\/\d{4}$/;
var date1= document.form1.textfield5.value;
valid = true;
if ( document.form1.textfield.value == "" ) {
alert ( "Please enter your User Name" );
valid = false;
return valid;}
if ( document.form1.textfield2.value == "" ) {
alert ( "Please enter password" );
valid = false;
return valid;}
if ( document.form1.textfield3.value != document.form1.textfield2.value ) {
alert ( "password mismatch" );
valid = false;
return valid;}

if(document.form1.textfield5.value == "")
{ valid=false;
alert("Enter a valid date");
return valid;
}
if ((!format.test(date1) )|| (!checkDate(format.exec(document.form1.textfield5))))
{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;
}

return valid;

}
</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<blockquote>
  <blockquote>
    <blockquote>
      <blockquote>
        <p>&nbsp;</p>
        <blockquote>
          <p>&nbsp;</p>
          <form id="form1" name="form1" method="post" enctype="multipart/form-data" action="test.php"  onsubmit="return validate_form( );" >
            <blockquote>
              <table width="200" height="113" border="1">
                <tr>
                  <td colspan="2">Username</td>
                  <td width="76"><input type="text" name="textfield" /></td>
                </tr>
                <tr>
                  <td colspan="2">Password</td>
                  <td><input type="text" name="textfield2" /></td>
                </tr>
                <tr>
                  <td colspan="2">Retype password </td>
                  <td><input type="text" name="textfield3" /></td>
                </tr>
                <tr>
                  <td colspan="2">email id </td>
                  <td><input type="text" name="textfield4" /></td>
                </tr>
                <tr>
                  <td colspan="2">dob(mm/dd/yyyy)</td>
                  <td><input type="text" name="textfield5" /></td>
                </tr>
              </table>
              <blockquote>
                <blockquote>
                  <p align="justify">
                    <input type="submit" name="Submit" value="Submit" />
                  </p>
                </blockquote>
              </blockquote>
            </blockquote>
          </form>
        </blockquote>
      </blockquote>
    </blockquote>
  </blockquote>
</blockquote>
</body>
</html>
Posted

var date1= document.form1.textfield5.value;
var month = parseInt(date1[1]);

Second line does not make sense. date1 is a date string value. How are you assuming it as an array and using an index to get month,date or year from it. If you want to use this array thing then split the date1 into an array first using their separator.

Try:
JavaScript
var dateBroken = date1.split("/");
//Now use this
// assuming it was mm/dd/yyyy
var month = dateBroken[0];
var day = dateBroken[1];
var year = dateBroken[2];
 
Share this answer
 
sandeep@ thanks buddy , but even after changing the code to
JavaScript
var dateBroken = date1.split("/");
//Now use this
// assuming it was mm/dd/yyyy
var month = dateBroken[0];
var day = dateBroken[1];
var year = dateBroken[2];
i get some error please get me relieved
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900