473,671 Members | 2,113 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 1899
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
3671
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 seems unnecessarily long. For some while, the following approach has been given here :- function ValidDate(y, m, d) { // m = 0..11 ; y m d integers, y!=0
14
13255
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 am expecting. Can anyone help please TIA Steve PS I'm new to javascript
11
8128
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
1572
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
8699
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/14/2006 and it shows in the field Feb 14 2006 after the onBlur I want it to leave the formatting of the date in the field as 2/14/2006 and not change the input to Feb 14 2006
2
6733
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 I' using the leave event. Right now I am creating a 'leave' sub for each of the fields. However, I'd like to simplify that and just call the name of a function and plug the field name as a variable and be done. In other words, I would like...
1
1903
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 will be displayed as mm/dd/yyyy format. i'm getting the from date value at textbox (FromDate) and to date value at another textbox(ToDate). i've to validate that the to date should not be lesser than that of from date validation must be check date...
8
1627
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 year var dtCh= "/"; var minYear=1900; var maxYear=2100; function isInteger(s){
5
3437
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 call it. It was suggested to be to have it run through an if statement and if the user enters an invalid date it will kick them to an error page. Here is the date validation code. // date validation using SimpleDateFormat // it will take a...
2
11370
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 that checks to see if a date entered by the user is a valid date in the second millenium. A skeleton of the program is in Dates.java. Open this program and save it to your directory. As indicated by the comments in the program, fill in the following: ...
0
8392
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8912
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8819
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8669
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7428
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4403
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2049
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1807
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.