By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,481 Members | 1,211 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,481 IT Pros & Developers. It's quick & easy.

date validation

P: 13
hi all i know i am wrong in the date validation codes , please correct me , i am new to java script >>
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. //include_once "con_1.php";
  3. /*$warn="";
  4.      if (isset($_POST['email'])) 
  5.     { $email=$_POST['email']; 
  6.      if (preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/",$email))
  7.     {}
  8.      else 
  9.         {
  10.      $warn="please enter a valid email id";
  11. }
  12. }*/
  13.  
  14. ?>
  15.  
  16.  
  17. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  18. <html xmlns="http://www.w3.org/1999/xhtml">
  19. <script type="text/javascript">
  20. <!-- Form Validation -->
  21. function validate_form()
  22. {
  23. valid = true; 
  24. var format = /^\d{2}\/\d{2}\/\d{4}$/;
  25. var date1= document.logform.dob.value;
  26. if ( document.logform.usname.value == "" ) { 
  27. alert ( "Please enter your User Name" ); 
  28. valid = false;
  29. }
  30. if ( document.logform.pass1.value == "" ) { 
  31. alert ( "Please enter your User Name" ); 
  32. valid = false;
  33. }
  34. if ( document.logform.pass2.value == "" ) { 
  35. alert ( "Please enter your User Name" ); 
  36. valid = false;
  37. }
  38. if ( document.logform.pass1.value != document.logform.pass2.value ) { 
  39. alert ( " password mismatch" ); 
  40. valid = false;
  41. }
  42. if(document.logform.dob.value == "")
  43. { alert("sa");
  44. }
  45. if ((!format.test(date1) )|| (!checkDate(format.exec(document.logform.dob))))
  46. {alert("Please enter a date in the format mm/dd/yyyy");
  47. }
  48. var month = parseInt(date1[1]);
  49. var date= parseInt(date1[2]);
  50. var year= parseInt(date1[3]);
  51. if (month<1 || month>12 || (day<30 || day>31)) valid=false;
  52.  
  53. switch (month) {
  54.  
  55. case 4: case 6: case 9: case 11:
  56.  
  57. if (day > 30) 
  58. { valid= false;
  59.  
  60. break;
  61. }
  62. case 2:
  63. if((year%400 ==0) || (year%100 != 0 && year%4 == 0) and (date>29)) 
  64. { valid= false;
  65. break;
  66. }
  67. else
  68. if(date>28)
  69. { valid= false;
  70. break;
  71. }
  72. default:
  73.  
  74. if (day > 31) 
  75. {
  76. valid=false;
  77. break;
  78. }
  79. var today = new Date();
  80. var dd = today.getDate();
  81. var mm = today.getMonth()+1;
  82. var yyyy = today.getFullYear();
  83.  
  84. if (year<1 || year>yyyy) valid= false;
  85.  
  86. return valid;
  87. }
  88.  
  89. <!-- Form Validation -->
  90. </script>
  91. <head>
  92. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  93. <title>Untitled Document</title>
  94. <style type="text/css">
  95. <!--
  96. #Layer1 {
  97.     position:absolute;
  98.     left:44px;
  99.     top:31px;
  100.     width:130px;
  101.     height:22px;
  102.     z-index:1;
  103. }
  104. -->
  105. </style>
  106. </head>
  107.  
  108. <body>
  109.  
  110. <form action="" method="post" enctype="multipart/form-data" name="logform" onsubmit="return validate_form();">
  111. <table cellpadding="3" cellspacing="2" border="0">
  112.   <tr>
  113.     <td width="30%">username</td>
  114.     <td><input name="usname" type="text" id="usname" /></td>
  115.   </tr>
  116.   <tr>
  117.     <td>password</td>
  118.     <td colspan="2"><input name="pass1" type="text" id="pass1" /></td>
  119.   </tr>
  120.   <tr>
  121.     <td>re-type password</td>
  122.     <td colspan="2"><input name="pass2" type="text" id="pass2" /></td>
  123.   </tr>
  124.   <tr>
  125.     <td>email id</td>
  126.     <td colspan="2"><input name="email" type="text" id="email" /><marquee> <?php echo $warn?></marquee></td>
  127.   </tr>
  128.   <tr>
  129.     <td>dob</td>
  130.     <td><input name="dob" type="text" id="dob" /></td>
  131.   </tr>
  132.   <tr>
  133.     <td align="center" colspan="2"><input type="submit" name="Submit" value="Submit" /></td>
  134.   </tr>
  135.   <select name="yourselection">
  136.     <option value="">--Select--</option>
  137.     <?php
  138.  /*$msql = mysql_query("SELECT * FROM role");
  139.  while($m_row = mysql_fetch_array($msql))        
  140.         echo("<option value = '" . $m_row['role'] . "'>" . $m_row['role'] . "</option>");*/
  141.     ?>
  142. </select>
  143. </body>
  144. </html>
  145.  
May 12 '12 #1
Share this Question
Share on Google+
3 Replies


Dormilich
Expert Mod 5K+
P: 8,639
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:
Expand|Select|Wrap|Line Numbers
  1. function checkDay(day, month, year)
  2. {
  3.     return (new Date(year, month-1, day).getMonth() == month-1);
  4. //  equivalent to
  5. //  return (new Date(year, month-1, day).getDate() == day);
  6. }
May 12 '12 #2

P: 13
@Dormilich
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
May 12 '12 #3

Dormilich
Expert Mod 5K+
P: 8,639
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?
May 12 '12 #4

Post your reply

Sign in to post your reply or Sign up for a free account.