views:

30

answers:

2

I've got an input field in a form that needs a date value. I'm using jQuery UI's datepicker for the calendar and I've set a range for it. However, the user can override that by typing a different date in the input field. How can I specify a date range for the field with jQuery validation? All I see with jQuery is that you can only specify that the field be a date type. Can I create min and max values that work with dates?

+1  A: 

UPDATED

in responce to comment...

i can imagine many ways of doing this task, such as observing the keypress event, OR by checking like this startDate < myDate && myDate < endDate OR by doing regex etc..!

but the very simple and efficent way is by simply using the <input/> readonly property, so by having a sinple input like this:

<input type="text" name="dateRange" readonly="readonly" maxlength="10" />

you are sure no one can put date, except if they are using the datepicker!

that's all! ;-)


look like what exactly do minDate and maxDate

demo: http://jqueryui.com/demos/datepicker/min-max.html

$("#datepicker").datepicker({minDate: -20, maxDate: '+1M +10D'});

minDate: Set a minimum selectable date via a Date object or as a string in the current dateFormat, or a number of days from today (e.g. +7) or a string of values and periods ('y' for years, 'm' for months, 'w' for weeks, 'd' for days, e.g. '-1y -1m'), or null for no limit.


maxDate: Set a maximum selectable date via a Date object or as a string in the current dateFormat, or a number of days from today (e.g. +7) or a string of values and periods ('y' for years, 'm' for months, 'w' for weeks, 'd' for days, e.g. '+1m +1w'), or null for no limit.

aSeptik
I've got this in my datepicker, but unless I'm doing something wrong, the user has the ability to override that by typing directly in the input field itself. Thats what I'm trying to set a range on.
now i have understood! ;-) see the updates!
aSeptik
A: 

You just need to add a setting to Datepicker to make it so they can't edit the text field:

$( ".selector" ).datepicker({ constrainInput: true });

jQuery UI Documentation

S Pangborn
That doesn't work. constrainInput still allows valid characters in the input field. That means I can change the year.