Developer forum

Forum » Templates » Validation of date-field

Validation of date-field

Lise T. Pedersen
Reply

Again, i'm struggling with validation for a form, using Forms for Datalists...

I have this form with a "date from" and "date to" field.

But how do I validate, that the dates is changed to something else than todays' date, before the form can be submitted?

Also I need a way to validate, if a selectbox has been used (= the value is something else than "nothing selected") - this last one could also be four radiobuttons, where one has to be selected, if that is better...

Hope someone will take the time to help me.

/Lise

 


Replies

 
Nicolai Høeg Pedersen
Reply

Hi Lise

An easy way would be using i.e. JQuery: http://jqueryvalidation.org/date-method/

Alternatively a couple of links and pointers.

To validate dates:

http://www.the-art-of-web.com/javascript/validate-date/

To check if something is selected in the select box:

if(document.getElementById("formElementId").selectedIndex>0){

}

To check if a group of radio buttons has a selected value, see this:

http://www.dyn-web.com/tutorials/forms/radio/get-selected.php

 
Lise T. Pedersen
Reply

Thanks for the links, Nicolai - but my date field is the type, where you pick the day, the month and the year, which you get with the input type "date" in forms for datalists - see attached file. 

And as I can see, your examples goes for a text field with a date written in it. And we like the calendar option with this date field - but without validation, it is useless.

And when I google myself, I also find a lot of validation methods for date fields - but not for this "set of select boxes" with day/month/year.

Can you help me with this? I am using the validation.js file in datamanagement/forms for my other validations, which you have helped me with earlier...

/Lise

 

 

 
Nicolai Høeg Pedersen
Reply

Hi Lise

The validation.js cannot figure out dates. So you have to add some more code.

Here is an example - it gets the values from each of the select boxes and creates a JS date object:

var theYear = document.getElementById("yearFieldId");
var theMonth = document.getElementById("monthFieldId");
var theDay = document.getElementById("dayFieldId");
var theInputDate = new Date(theYear, theMonth, theDay);

 
var today = new Date();
var checkDate = new Date(today);
checkDate.setDate(checkDate.getDate()+1); //This is todays date + one day. Calculate whatever needed.
 
if(theInputDate<checkDate ){
 alert('The date has to be tomorrow or after that');
}
 
Lise T. Pedersen
Reply

Hi again, Nicolai

Since last week, I have changed the input type=date fields with ordinary text fields again - found that the loading time for the date fields with calendar was much too long.

But the problem is that I really can't make the validation.js work - I am much to insecure about the javascript syntax when putting new validation elements into a existing file, and I simply need som help to do this.

I need the two checks:

The date fields (People has to write when they are going on vacation not later than 10 days before the first day):

Date from

Date to:

(1: a valid date 10 days from today, 2: a valid date coming after the first date)

The other check is that one of the values in a select box, except from "intet valgt", has to be selected, before the form can be submitted.

I have hopefully attached my validation_dk.js file here - have tried to make "something" with the select input, which is not working... please??

 

You must be logged in to post in the forum