在2个文本框之间验证 - jquery

时间:2009-05-08 10:59:14

标签: jquery validation datetime

我如何使用jquery验证两个日期文本框。我遇到了这段代码。我如何输入文本框参数:

$.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}.' );

开始日期必须大于结束日期。

1 个答案:

答案 0 :(得分:1)

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>