What is JotForm?
JotForm is a free online form builder which helps you create online forms without writing a single line of code. No sign-up required.

At JotForm, we want to make sure that you’re getting the online form builder help that you need. Our friendly customer support team is available 24/7.

We believe that if one user has a question, there could be more users who may have the same question. This is why many of our support forum threads are public and available to be searched and viewed. If you’d like help immediately, feel free to search for a similar question, or submit your question or concern.


  • Profile Image

    How do I validate an inputted Date to insure it is between two other Dates?

    Asked by ButchJotForm on June 22, 2015 at 08:10 AM

    I am trying to 'not allow' the entry of a Birthdate that does not imply an Age (as of a particular Date) that does not fall within a range of ages.  For example,

    As of a particular Date (in this case May 24, 2015), if someone enters an Age-Category (which would be a Required field) of:

    18-and-Over:  Then the entered Birthdate must be on/before May 23, 1997... or the Birthdate is to be considered invalid.

    13-to-17:  Then the entered Birthdate must be on/after May 24, 1997 AND on/before May 23, 2002... or the Birthdate is to be considered invalid.

    0-to-12:  Then the entered Birthdate must be on/after May 24, 2002... or the Birthdate is to be considered invalid.

     

    The Invalid-Date situation should present an appropriate Warning... with specifics.

    Page URL:
    http://form.jotformpro.com/form/51695989432977

    validate required field Birthdate date calculations
  • Profile Image

    Answered by Ben on June 22, 2015 at 10:28 AM

    This is a bit tricky, but should be possible to do. The way we do it is by adding 5 Form Calculation widgets to our form that would help us find out:

    1. if date is higher/older than 12
    2. if it is less than 13 years
    3. or more than 17
    4. and if the date is less than 18 years

    Next to this we need two date fields that we can use to allow the date to be selected and checked against and a dropdown or radio option to select the segment of years as you have placed above (0-12, 13-17 and 18 and older).

    Now once we add that on the form, we will also need to do some mathematical conditions / checks to make sure that the dates match just right.

    First off, this is the form that I put together quickly for you to see and test out: http://form.jotformpro.com/form/51723572973967 and these are the steps on how to make it.

    1. Checking if the date is for years more than 12

    The way we check for the years is by adding the date field to the Form Calculation widget and then subtracting the required days from it. The days that we are subtracting are the ones that we know are required - in this case days in 12 years.

    This is calculated with 12 * 365 + 3 extra days that we have every 4 years.

    Right away I must say that this is obviously not 100% perfect, since the years could happen in such fashion that there are only 2 extra days available by calendar (for example if someone opens the form right before 29th of February, but it would mean that one day could possibly be skipped, while otherwise the solution works as it should.

    Now to get back to Form Calculation, this is how it looks after adding that math:

    As you can see what we did was to subtract that from the current date field - this field is set as read only and hidden from the form so that it can not be changed, so that we can do the math properly.

    2. Check if the date is 'younger' than 13

    For this we do a similar math, where we check the same field, minus 13 years of 365 days and again + 3 extra days like so:

    3. Calculations for over 17 are in order and we do that by subtracting 17 years of the 365 days + 4 extra days.

    4. We do the same for 18 years, just changing the number of years:

    OK, so we now have these form Calculations all set up and ready to use.

    5. What we should do now, is to hide them from the form as well so that the end user filling out the form does not see them and what they do.

    To do this we should click on each Form Calculation and then click on the little gear wheel in the right top part of it and select Hide Field option from the menu shown to us.

    6. Its time to add error messages. I have used the Text field:

    And just added the errors for younger and older than specific years like so:

    One Text field holds "Older than 12!", the other "Younger than 13!", the third one holds "Older than 17!" and the last one holds "Younger than 18!".

    7. Capture the date users select in the form calculation as a number.

    To do this, just add one more form calculation widget to your form and add that field into it and that is it:

    8. Conditions

    OK, so everything is as we want it to be so all we need is that final piece of the puzzle that connects it all together - and that are conditions.

    More specifically we will need show / hide field conditions.

    This is how they look:

    Basically what we did with them was to manually add the tag / field name of the form calculation that captures the birthday (date selected by the user). You can see how to get this info here: How to find Field IDs & Names

    In the case above, that is {myBirthday14}.

    As you can now see for under 12 years, we have compared if the form calculation that gives us the numeric representation of today - 12 years is less than the number in the {myBirthday14} field and if so, we know that we have an older person.

    The reason why is because today is presented as 20 (just as an example), when we take 12 it is 8. If we end up with anything larger than 8 it means that the person is younger than 12. In the same time if someone gets 0 it would mean that they are 20 years old - way too old for a 12 year old I believe :)

    We just follow the same logic to create all the other conditions and voila - that is it :)

    I hope that the instructions were clear, but if not feel free to let us know and we would be happy to answer any questions that you might have.

    In the same time I would like to recommend the following guides, as they might be of help:

    How to Add a Widget to your Form

    Smart Forms Using Conditional Logic

    How to Find The OR operator in conditions?

    How to Clone an Existing Form from a URL (if you wish to clone the form above to your account)