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

How do I Verify Date Format - mm/dd/yyyy - using Javascript

P: 67
Hi,

I have a registration form that uses the javascript validation code I have posted below. I am trying to verify that the user enters a birthdate in the format 01/11/1999

Anyone know how I can incorporate a date format code into my existing code?

Thanks for any help - Jerry

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. <!--
  3. function validate_form ( )
  4. {
  5. valid = true;
  6. if ( document.registration.firstname.value == "")
  7. {
  8. alert ( "Please fill in the 'First Name' box." );
  9. document.registration.firstname.focus();
  10. return false;
  11. }
  12. if ( document.registration.birthdate.value == "")
  13. {
  14. alert ( "Please fill in the 'Birthdate' box." );
  15. document.registration.birthdate.focus();
  16. return false;
  17. }
  18. return valid;
  19. }
  20. //-->
  21. </script>
Nov 6 '08 #1
Share this Question
Share on Google+
5 Replies


RamananKalirajan
100+
P: 607
Hello Sir,

Are u using any calendar to get the date value from the user on the user must enter the date in the input in the date format as u have specified.


Regards
Ramanan Kalirajan
Nov 6 '08 #2

P: 67
Hi,

I am asking the user to enter their birthdate and would prefer if they would enter the date the way I wish.

For instance, I have a form and it says:
Birthdate(please use format mm/dd/yyyy): "text box"

I would like a box to pop up just like my other javascript that says "Please enter a date in the following format "mm/dd/yyyy" if the user fails to do so.

Is there a code I can implement into the code I already have(posted above)?

thanks
Nov 6 '08 #3

acoder
Expert Mod 15k+
P: 16,027
Probably the best method would be to use a regular expression to pattern match the input. Off the top of my head, something like
Expand|Select|Wrap|Line Numbers
  1. /^\d{2}\/\d{2}\/\d{4}$/
Of course, this only matches the format, but doesn't validate that the date is an actual valid one.
Nov 6 '08 #4

P: 67
Thank you for your post.

I am not sure how to implement this code you speak of into my script. Any ideas?

Thanks
Nov 7 '08 #5

acoder
Expert Mod 15k+
P: 16,027
You can use the RegExp object or use the string methods for matching.

Some links:
http://www.regular-expressions.info/javascript.html
https://developer.mozilla.org/en/Cor...ar_Expressions
https://developer.mozilla.org/En/Cor...Objects/RegExp
Hope those help. If you get stuck, post your code.
Nov 7 '08 #6

Post your reply

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