jQuery验证addmethod总是返回false

时间:2014-03-11 11:29:57

标签: jquery asp.net jquery-validate

我正在使用jQuery 1.7.2和jQuery验证1.8.2我添加了ajax我检查响应所有正确的值也传递正确但仍然没有得到验证。这是我的代码

<script type="text/javascript">
$(document).ready(function () {
    var validator = $("#form1").validate({
      rules: {
        ctl00$ContentPlaceHolder$txCivilID: {
          required: true,
          rangelength: [12,12],
          digits: true,
          checkCivilID: true
        },
        ctl00$ContentPlaceHolder$txFirstName: {
          required: true
        },
        ctl00$ContentPlaceHolder$txLastName: {
          required: true
        },
        ctl00$ContentPlaceHolder$txMobile: {
          required: true,
          rangelength: [8,8],
          digits: true
        },
        ctl00$ContentPlaceHolder$dpUserType: {
          required: true
        }
      },
      messages: {
        ctl00$ContentPlaceHolder$txCivilID: {
          required: "Civil for is required!",
          rangelength: "Civil for should be {0} in length!",
          digits: "Civil for should be only numbers!"
        },
        ctl00$ContentPlaceHolder$txFirstName: "First Name is required!",
        ctl00$ContentPlaceHolder$txLastName: "Last Name is required!",
        ctl00$ContentPlaceHolder$txMobile: {
          required: "Mobile number is required!",
          rangelength: "Mobile number should be {0} in length!",
          digits: "Mobile number should be only numbers!"
        },
        ctl00$ContentPlaceHolder$dpUserType: "Select User Type !"
      },
      errorPlacement: function (error, element) {
        $('label[for="' + element.attr("id") + '"]').text(error.text());
      }
    });
    jQuery.validator.addMethod("checkCivilID", function (value, element) {
      var response = '';
      $.ajax({
        url: "CivilID.aspx/IsAlreadyAvailable",
        type: "POST",
        async: false,
        data: "{'civilID':'" + value + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
          return data.d == 0 ? true : false;
        }
      });
    }, "CivilID is Already Exists");
});
  </script>

我将addMethod更改为远程方法,现在我的代码如下所示,但仍然是相同的:(

 <script type="text/javascript">
$(document).ready(function () {
    var validator = $("#form1").validate({
      rules: {
        ctl00$ContentPlaceHolder$txCivilID: {
          required: true,
          rangelength: [12,12],
          digits: true,
          remote: function () {
            var value = $('#<%=txCivilID.ClientID%>').val();
            var r = {
              contentType: "application/json; charset=utf-8",
              data: JSON.stringify({ civilID: value }),
              type: "POST",
              dataType: "json",
              url: "CivilID.aspx/IsAlreadyAvailable",
              //async: false,
              dataFilter: function (data) { return $.parseJSON(data).d }
            };
            return r;
          }
        },
        ctl00$ContentPlaceHolder$txFirstName: {
          required: true
        },
        ctl00$ContentPlaceHolder$txLastName: {
          required: true
        },
        ctl00$ContentPlaceHolder$txMobile: {
          required: true,
          rangelength: [8,8],
          digits: true
        },
        ctl00$ContentPlaceHolder$dpUserType: {
          required: true
        }
      },
      messages: {
        ctl00$ContentPlaceHolder$txCivilID: {
          required: "Civil for is required!",
          rangelength: "Civil for should be {0} in length!",
          digits: "Civil for should be only numbers!",
          remote:"CivilID is Already Exists"
        },
        ctl00$ContentPlaceHolder$txFirstName: "First Name is required!",
        ctl00$ContentPlaceHolder$txLastName: "Last Name is required!",
        ctl00$ContentPlaceHolder$txMobile: {
          required: "Mobile number is required!",
          rangelength: "Mobile number should be {0} in length!",
          digits: "Mobile number should be only numbers!"
        },
        ctl00$ContentPlaceHolder$dpUserType: "Select User Type !"
      },
      errorPlacement: function (error, element) {
        $('label[for="' + element.attr("id") + '"]').text(error.text());
      }
    });

    /*jQuery.validator.addMethod("checkCivilID", function (value, element) {
      var response = '';
      $.ajax({
        url: "CivilID.aspx/IsAlreadyAvailable",
        type: "POST",
        async: true,
        data: "{'civilID':'" + value + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
          return data.d == 0 ? true : false;
        }
      });
    }, "CivilID is Already Exists");*/
});

 </script>

1 个答案:

答案 0 :(得分:0)

我解决了这个问题,因为人们告诉我使用远程方法而且我这样做但是它仍然给出了相同的问题。然后我开始更改Web服务的价值。这是第一次布尔然后我改为布尔但仍然是同样的问题,现在我使它成为字符串并返回值true和false。

Bingo它开始工作:D