tags:

views:

59

answers:

4

I need to do some basic validation on a date, things like validating that the date is greater than today, validating that the date is real (i.e. "99/99/9999"), etc. Basically, I need to emulate what .NET does with DateTime.TryParse.

Is there anything like that in jQuery? Or would I be better off making an AJAX call to validate the date in C#?

A: 

You could use the RangeValidator or jquery.validation.

Yuriy Faktorovich
+1  A: 

validating date ranges

http://jqueryui.com/demos/datepicker/ you could use this.. it's a decent tool with lots of settings

Avien
A: 

A way around this issue is to use a jQuery UI Datepicker for the input. Of course you might want to validate the date on the server when it's posted, too.

SquidScareMe
I actually am using that plugin, but I don't see any validation settings on the site. Do you know how to validate with that?
Steven
Hi Steve. There is no validation because any date returned from the datepicker is assumed to be valid. Just make sure the user cannot directly type into the input field that uses the datepicker.
SquidScareMe
A: 

You could certainly create a JavaScript validator that would apply a regex to the field. Don't think you'd even really need JQuery for it. However, you will need to perform similar validation on the server-side anyway, because JavaScript can be disabled at the client, or your HTML can be edited client-side to remove the validation call from the form element.

A regex like the following would catch most "insane" dates, e.g. 99/99/9999, but is still a bit naive as it doesn't properly validate leap years or 30/31 day months; it'll accept 2/31/2009 for instance.

"^(1[0-2]|0?\d)/(3[0-1]|[1-2]\d|0?\d)/(19|20)\d{2}$"

KeithS