Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Editing

Status
Not open for further replies.

santiago

Programmer
Aug 14, 2000
5
US
I have an text input field in my form that requires a valid date input (for birthdate).  How would I do a date validation of this field in Javascript?
 
Dear santiago,<br><br>Something like this:<br><br>&lt;script language=javascript&gt;<br>&lt;!--<br>function validateBD(){<br> <br>&nbsp;&nbsp;var sBD = document.form1.Birthday.value;<br>&nbsp;&nbsp;var dt = new Date(Date.parse(sBD));<br>&nbsp;&nbsp;if ( !isNaN(dt))<br>&nbsp;&nbsp;&nbsp;&nbsp;alert(&quot;Your Birthday is on &quot; + dt.toString());<br>&nbsp;&nbsp;else<br>&nbsp;&nbsp;&nbsp;&nbsp;alert(&quot;Not a valid date&quot;);<br>}<br>//--&gt;<br>&lt;/script&gt;<br>&lt;/HEAD&gt;<br>&lt;BODY&gt;<br>&lt;p&gt;Enter Birth Date&lt;/p&gt;<br>&lt;form name=&quot;form1&quot; onsubmit=&quot;validateBD()&quot;&gt;<br>&lt;input type=&quot;text&quot; name=&quot;Birthday&quot;&gt;<br>&lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;<br>&lt;/form&gt;<br>
 
Thanks for the example.<br><br>However,&nbsp;&nbsp;I do not believe it will solve my main problem.&nbsp;&nbsp;The example script accepts 15/35/1966 as a valid date.&nbsp;&nbsp;I need to be able to validate that the date entered is a proper date.
 
That's because the Date object adds the extra days and months to the time and comes up with April 4th 1967 as a valid time.<br><br>If you want to make sure the user knows how to enter a date correctly then you will have to parse and check the values using your own rules. This means you will have to account for leap year and the days of the month. That is why most sites use drop down boxes for selection of Month and Day etc., it is easier.<br><br>Good luck<br>-pete<br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top