Skip to content Skip to sidebar Skip to footer

How To Validate Three Separate Fields As A Date

I am trying to validate a date that is separated into three fields (year, month, and day, of course). I cannot change these fields, as much as I'd like to. I am using the jQuery Va

Solution 1:

Quote OP:

"Even validating them individually would suffice."

Your code is broken because none of the fields in your HTML markup contain a name attribute. A unique name is mandatory in order for this plugin to operate. It's how it keeps track of the input elements.

See: http://jqueryvalidation.org/reference/#markup-recommendations

It's not shown in your markup, but all relevant input elements must also be contained within <form></form> tags. This plugin will not work otherwise.


Since you already have the date saved into an hidden field, you can also just validate the hidden field.

<inputtype="hidden" name="myDate" />

Activate validation on hidden fields by using the ignore: [] option.

Then as long as this hidden field has a unique name attribute, you can apply validation rules the same as you would on any other field.

Use the errorPlacement callback to precisely place the message attached to the hidden field.

Solution 2:

You can merge this three fields to one (hidden input), and validate this input

var day, month, year;

day= $('.day').val();
month= $('.month').val();
year= $('.year').val();

$('.hidden-date-input').val([day, year, month].join('-'));

Post a Comment for "How To Validate Three Separate Fields As A Date"