I have a HTML Form which needs to be submitted to an order page so a user can order a personalized product. I have everything working except for the date validation for the form. I need to be able to check the Date of Birth they are entering is a valid date. That means I need to check for Leap Years so they can't enter 29th Feb on a non leap and I also need to check for the month so they cannot enter more than 30 for months where there is no 31.

What I need to be able to do is check "day1, month1, year1" as one date and then "day2. month2, year2" as another date and if either one of them return invalid(false) I need to display an alert to tell them to correct it when the Submit button is pushed. I don't want the form to submit unless both dates are valid.

Can somebody please help me modify the above javascript and form to accommodate this requirement?

Thanks.

Denk
—
2012-10-12T16:44:09Z —
#2

The way I used to do it in the past was as follows:

Create a date based on the selected values (year, month, day)

Check if the DAY of the newly created date is the same as the selected day and if the MONTH of the newly created date is the same as the selected month (keep in mind: zero-based!)

If they are, then the date is validIf they aren't, then the date is invalid

I'm explaining it in words, because that way you can try it for yourself.

CBResources
—
2012-10-16T03:04:03Z —
#3

Sorry Denk but I'm not sure that actually solves my problem.

I don't see how your solution would tell me if 29th February 2009 was a valid date? Or if 31st November was a valid date? The function I have got does just that, I just need to know how to run it on BOTH date fields before the form submits rather than just running it on one of them.

Any other ideas?

Denk
—
2012-10-16T08:12:05Z —
#4

Hi CBResources

I made an example, based on your HTML:

I have changed the way you populate your "day" and "month", using PHP (in your example, the day and month options didn't have a value and it was easier for me to write it this way

So what have I changed in your HTML?- The way day and month options are created- Added the attribute, onsubmit on the form element. ==> onsubmit="return validateForm()"

Then I created the JavaScript function(s), which you need to validate it

So in "validateForm", I just get the values and pass them onto the "checkDate" function.The "checkDate" function creates a new Date object and then checks if the selected DAY and the selected MONTH match with the day and month of the newly created Date object.

Thanks for your reply, but as you can see above - we already resolved that question I didn't understand the function properly but denk has kindly given me an example so now that is working. What I am trying to do now is add in an alert somewhere so the user can get some feedback on why the form doesn't submit if they do enter an invalid date. At the moment, all it does is stay on the page without any feedback to the user. Do you know where in the above code I would place the alert so the user can be told they have entered an invalid date?

felgall
—
2012-10-17T06:18:26Z —
#8

CBResources said:

What I am trying to do now is add in an alert somewhere so the user can get some feedback on why the form doesn't submit if they do enter an invalid date.

Don't use an alert for client messages - alerts are intended for debugging and display additional options in some browsers - such as an option to turn off JavaScript for the current page.

You should be inserting the message into the HTML in the page itself - the easiest way is to use innerHTML.

CBResources
—
2012-10-18T04:03:08Z —
#9

I see...

Well I got the error message to work with an alert using the following javascript code:

In regards to your error message:- You should create an empty error-div somewhere on your page

<div id="form_error" style="display:none"></div>

Change the alert to add a message to "form_error" and to make it appear. Complete JavaScript-code below (untested though). Read it first and see if you understand what is written. If you don't, then just ask what's troubling you