views:

1765

answers:

4

i have a textbox adn a datepicker next to it and i am using asp.net and the user can enter the date as well select the date from datepicker.

how would you validate if the date is entered correct?

  <script type="text/javascript"> 
        $(document).ready(function () { 
            $('#<%=StartDate.ClientID%>').datepicker({ showOn: 'button', 
                    buttonImage: '../images/Calendar.png', 
                    buttonImageOnly: true, onSelect: function () { }, 
                    onClose: function () { $(this).focus(); } 
            }); 
        }); 
    </script> 
+1  A: 
Doc Hoffiday
i know i have option to use asp.net control but i wanted to try using jquery validation.
Abu Hamzah
i took the javascript produced by the compare validator and wrapped a custom method around it
Doc Hoffiday
you could also use 'remote' and on the server try to convert it to a date which will also only work if it's a valid date
Doc Hoffiday
@Doc, getting this error: Microsoft JScript runtime error: '$.validator' is null or not an object
Abu Hamzah
do you have references to both jquery and jquery validation ? (http://ajax.microsoft.com/ajax/jquery.validate/1.5.5/jquery.validate.min.js)
Doc Hoffiday
oops i was missing the validation js.
Abu Hamzah
@doc, how can i make seperate function (validaiton of date) so i can use on my other pages. - thanks.
Abu Hamzah
just put the '$.validator.addMethod("truedate"...' code in a separate .js file and reference it on our other pages
Doc Hoffiday
A: 

Since you are already using jquery, you could check out http://bassistance.de/jquery-plugins/jquery-plugin-validation/ which has great flexibility for validations..

Rabbott
i am using bassistance.de for my form validation but i dont find any example for date validation, if you find please redirect me.
Abu Hamzah
it has a date validation, but it only checks the format of the string, so "30/30/2008" would be a valid date
Doc Hoffiday
@Doc: so there is no real way to check date? or am i left with only option to use asp.net validatin controls?
Abu Hamzah
+3  A: 

I use this handler for validate input:

   onClose: function(dateText, inst) {
        try {
            $.datepicker.parseDate('dd/mm/yy', dateText);
        } catch (e) {
            alert(e);
        };

Hope, it'll be useful for somebody

katrin
A: 

I'm using a mix of Katrin & Doc Holiday's approach, using the datepicker control's parseDate utility method in a custom validator I've called isDate.

Just change the format argument in parseDate to the appropriate value (reference: http://docs.jquery.com/UI/Datepicker/parseDate).

    $(document).ready(function()
    {
        $.validator.addMethod('isDate', function(value, element)
        {
            var isDate = false;
            try
            {
                $.datepicker.parseDate('dd/mm/yy', value);
                isDate = true;
            }
            catch (e)
            {

            }
            return isDate;
        });

        $("#form1").validate();

        $("#dateToValidate").rules("add", { isDate: true, messages: {isDate: "Date to Validate is invalid."} });
    });

It's probably not best practice for the validator to be referencing a UI control but what the heck. :)

Trawler