views:

1403

answers:

1

how i do validate two date textboxes using jquery. i came across this snippet of code. how do i enter the textbox parameters:

$.validator.addMethod('greaterThan', function(value, element, param) { return ( IsNaN( value ) && IsNaN( $(param).val() ) ) || ( value > $(param).val() ); }, 'Must be greater than {0}.' );

$.validator.addMethod('lesserThan', function(value, element, param) { return ( IsNaN( value ) && IsNaN( $(param).val() ) ) || ( value < $(param).val() ); }, 'Must be lesser than {0}.' );

the start date has to be greater than end date.

+1  A: 

Grab DateJS

http://www.datejs.com/

  <form>
   <input type="text" id="startdate" />
   <input type="text" id="enddate" />
</form>
<button id="checkdates">Check Dates</button>

<div id="msg"></div>

<script>
    $(function() {
        $("#msg").text('');

        $("#checkdates").click(function(event) {
            var startdate = Date.parse($("#startdate").val());
            if (!startdate) { $("#msg").append("Start Date is not a valid date<br/>"); }

            var enddate = Date.parse($("#enddate ").val());
            if (!enddate) { $("#msg").append("End Date is not a valid date<br/>"); }

            if (startdate && enddate) {
              if (startdate.compareTo(enddate) <= 0) {
                  $("#msg").append("Start date must be greater than end date");
              }   
            }
            event.preventDefault();
            return false;
        });
    });
</script>
Chad Grant
it's not throwing any error. where do i add the function validateDates()?and what if i want to display the error in label format instead of alert [messagebox]?
fuz3d
there are many ways to do it, but I dont have much info other than what you posted in your question, this code works ... but I can't read your mind
Chad Grant
you've given the validation on the button click event. but i'd like the error label to be displayed as soon as the user enters the wrong date/the end date is lesser than the start date.
fuz3d
i'd have thought that the code which posted in the question would have been easier to implement had i known how to modify it accordingly - ie, to get the values of the textbox and compare and validate.
fuz3d