tags:

views:

383

answers:

10

I need to validate a date/time field on a webpage but want it to do it without reloading the page and would like 'instant' feedback for the users.

What's the best/easiest solution. BTW: easiest scores 65% of total points

Edit: What if best was 65% of total points?

+2  A: 

Check out this javascript date validation function.

It uses javascript, regular expressions and the 'onblur' event of a text input.

David HAust
A: 

A simple javascript method that reads what's in the input field on submit and validates it. If it's not valid, return false so that the form is not submitted to the server.

... onSubmit="return validateForm();" ...

Make sure you validate on the server side too, it's easy to bypass javascript validation.

GoodEnough
+3  A: 

If you would like to use JavaScript then it has built in date validation functions. However, if you do not want to go the JavaScript route, you could change the UI to dropdown controls which would limit the users ability to enter invalid data. You would still need to check server side to ensure nobody submits Feb 30th.

AdamSane
A: 

If you're using ASP.NET, it has validator controls that you can point to textboxes which you can then use to validate proper date/time formats.

Jon Limjap
A: 

There are a couple of date widgets available out in the aether. Then you can allow only valid input.

Matthew Schinckel
A: 

Looks like there's a great video about the ASP.NET AJAX Control Toolkit provides the MaskedEdit control and the MaskedEditValidator control that works great. Not easy for beginners but VERY good and instant feedback.

Thanks for all the answers though!

asp.net

Unfortunately I can't accept this answer.

Keng
+1  A: 

I would recommend using drop-downs for dates, as indicated above. I can't really think of any reason not to--you want the user to choose from pre-defined data, not give you something unique that you can't anticipate.

You can avoid February 30 with a little bit of Javascript (make the days field populate dynamically based on the month).

Brian Warshaw
+2  A: 

@David H. Aust

Using onblur for validation is problematic, because some folks use the enter key, not the mouse, to submit a form. Using onblur and the form's onsubmit event in conjunction could be a better solution. Back when I did JS validation for forms a lot more, I would run against keyup events. This gave the user instant feedback on whether or not their entry was correct. You can (and I did) also put checks in place so that the user doesn't receive an "incorrect" message until they've left the field (since you shouldn't tell them they're incorrect if they aren't done yet).

Brian Warshaw
+1  A: 

@Brian Warshaw
That is a really good point you make about not forgetting the users who navigate via the keyboard (uh, me).

Thanks for bringing our attention to that.

David HAust
A: 

I've used this small bit of js code in a few projects, it'll do date quickly and easily along with a few others.

Link

Galbrezu