0

in jquery i try to validate my page.I have to filds txtStartDate and txtEndDate.i want to validate start date is lower than end date.How this condition append in given code.Bcz txtPageName and txtTitle is already validate.Thanks.

                 var v = $("#form1").validate({
                    ignore: ':hidden',
                    rules: {
                        : { required: true },
                        txtTitle: { required: true }


                    },
                    messages: {
                        txtPageName: "<br/>Please enter a Page Name",
                        txtTitle: "<br/>Please enter a Title"
                    }
                });
2
  • Possible duplicate: stackoverflow.com/questions/833997/… Commented Dec 30, 2011 at 5:16
  • @SKS thanks for link.But as above link i try and got a error in console jQuery.validate is undefined.Any way thanks for a link. Commented Dec 30, 2011 at 5:33

1 Answer 1

3

You can need a custom rule for doing this. See the below code: isAfterStartDate checks is start date if less than end date and returns a boolean. This is used by the custom validation rule to validate the txtEndDate.

var isAfterStartDate = function(startDateStr, endDateStr) {
            var inDate = new Date(startDateStr),
                eDate = new Date(endDateStr);

            if(inDate < eDate) {
                return false;
            }

        };
jQuery.validator.addMethod("isAfterStartDate", function(value, element) {

        return isAfterStartDate($('#txtStartDate').val(), value);
    }, "End date should be after start date");

 var v = $("#form1").validate({
                ignore: ':hidden',
                rules: {
                    txtPageName: { required: true },
                    txtTitle: { required: true },
                    txtStartDate : {required: true}
                    txtEndDate: {
                                  required: true,
                                  isAfterStartDate: true
                                 }


                },
                messages: {
                    txtPageName: "<br/>Please enter a Page Name",
                    txtTitle: "<br/>Please enter a Title"
                }
            });

This works assuming the date is format "dd/mm/yyyy".

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot.But i Got a error jQuery.validator is undefined.what is my mistake?
Just asking, have you included jQuery validation plugin in your page?
Ya i include jQuery validation plugin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.