473,511 Members | 16,282 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

date validation

13 New Member
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
3 1894
Dormilich
8,658 Recognized Expert Moderator Expert
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
kavinhuh
13 New Member
@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
8,658 Recognized Expert Moderator Expert
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

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

Similar topics

30
3638
by: Dr John Stockton | last post by:
It has appeared that ancient sources give a method for Numeric Date Validation that involves numerous tests to determine month length; versions are often posted by incomers here. That sort of code...
14
13237
by: Steve Wright | last post by:
I am trying to validate if an entry on a form is a date. I have adapted code I found here http://www.codingforums.com/archive/index.php/t-14325 as below but I can't seem the get the results that I...
11
8108
by: Diego | last post by:
Hi all a quick question: how can I validate a date in asp.net (2.0) with c#? I didn't find a quick anwer. Thanks, Diego.
3
1568
by: LDD | last post by:
Are there any built in time and/or date validation methods that one can make use of in .NET? thanks folks LDD
3
8692
by: pmarisole | last post by:
The following javascript code gives me the date validation that I need except after the correct date is entered into the field, it puts the date in the wrong format EXAMPLE: User enters...
2
6729
by: John Smith | last post by:
Hello, I have a VB.NET application with a Windows form that have several textboxes fields where I have dates entered. I would like to do a date validation check after the the field is updated, so...
1
1895
by: jsredhar | last post by:
Dear Experts, I have two text boxes in my form (f1) i'm prompting the users to select the date from callender which is embeded within the from. when the users Pick the date from the calender.it...
8
1622
pradeepjain
by: pradeepjain | last post by:
<script language = "Javascript"> /** * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/) */ // Declaring valid date character, minimum year and maximum...
5
3424
Stang02GT
by: Stang02GT | last post by:
I have been asked to validate a date on our web-page so that people cannot enter dates like 14/1/08 or 2/30/06. I have found code that will do exactly what i need it to do, but i am not sure how to...
2
11337
by: mshroom12 | last post by:
I am having trouble with the following project on hand. I use Eclipse to do my work in Java. This is what I'm supposed to complete. Date Validation In this exercise you will write a program...
0
7245
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7144
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7512
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5671
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5069
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
449
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.