使用 JQuery Validate() 问题验证 .NET 2.0 表单

发布于 2024-08-23 16:29:04 字数 3534 浏览 1 评论 0原文

我正在尝试使用 JQuery Validate() 插件验证 .NET 2.0 中的表单,但它不起作用。我知道我的 Jquery 代码正在工作,因为我设置了清除 focus() 上文本字段的默认文本的函数,并且它按预期工作。

form.validate() 方法似乎从未被调用,因为我在自定义验证方法中调用了alert(),该方法在单独的 HTML 测试页面中工作,但在集成到 .NET 页面中时从未触发。

这可能是因为整个页面被视为 .NET 中的表单吗?如果我想覆盖默认的提交操作并添加一些客户端验证,我该怎么做或者我只能验证后面代码中的表单?谢谢。

我的脚本:

<script src="js/jquery.validate.min.js" type="text/javascript"></script>
<script src="js/additional-methods.js" type="text/javascript"></script>
<script src="js/jquery.maskedinput.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function() {

        jQuery.validator.addMethod("checkfields", function(value, element) {

            if ($("#from_date").val() == "" && $("#to_date").val() == "" && $("#last_name").val() == "") {
                return false;
            }
        }, "Please enter either a last name, from date or to date.");

        $("input.from_date").mask("99/99/9999");
        $("input.to_date").mask("99/99/9999");

        $("#searchForm").validate({
            errorElement: "span",
            errorContainer: $("#error"),
            errorPlacement: function(error, element) {
                error.appendTo(document.getElementById("error"));
            },
            success: function(label) {
                $("#searchForm").submit();
            },
            rules: {
                last_name: "checkfields"
            }
        });
        $("#from_date").focus(function() {
            if ($("#from_date").val() == "mm/dd/yyyy") {
                $("#from_date").val("");
            }
        });
        $("#from_date").blur(function() {
            if ($("#from_date").val() == "") {
                $("#from_date").val("mm/dd/yyyy");
            }
        });
        $("#to_date").focus(function() {
            if ($("#to_date").val() == "mm/dd/yyyy") {
                $("#to_date").val("");
            }
        });
        $("#to_date").blur(function() {
            if ($("#to_date").val() == "") {
                $("#to_date").val("mm/dd/yyyy");
            }
        });
    });
</script>

和形式:

<form action="search.spax" id="searchForm" name="searchForm" method="post">

  <div id="error" style="display:none;">
      <span></span>.<br clear="all"/>
    </div>
  <table width="520" border="0" cellspacing="0" cellpadding="0" class="obit_form">
     <tr align="left">
      <th><label for="last_name">Last Name</label></th>
      <th></th>
      <th><label for="from_date">From</label></th>
      <th><label for="to_date">To</label></th>
      <th></th>
     </tr>
     <tr align="left">

      <td><input type="text" id="last_name" class="required date" name="last_name" value="" style="width:135px;" /></td>
      <td>and/or</td>
      <td><input type="text" id="from_date" class="required date" name="from_date" value="mm/dd/yyyy" style="width:75px;" /></td>
      <td><input type="text" id="to_date" class="required date" name="to_date" value="mm/dd/yyyy" style="width:75px;" /></td>

      <td><input type="submit alt="Search" title="Search" value="Search" name="submitButton" id="Submit2" /></td>
     </tr>
   </table>       
</form>

I am trying to validate a form in .NET 2.0 using the JQuery Validate() plug-in and it’s not working. I know my Jquery code is working as I set up functions to clear the default text of a text field on focus() and that works as expected.

The form.validate() method never seems to get called as I have alert() calls in a custom validation method that work in a separate HTML test page but never fire when integrated into the .NET page.

Could this be because the whole page is treated like a form in .NET? If I want to override the default submit action and add some client side validation, how can I do this or can I only validate the form in the code behind? Thanks.

my script:

<script src="js/jquery.validate.min.js" type="text/javascript"></script>
<script src="js/additional-methods.js" type="text/javascript"></script>
<script src="js/jquery.maskedinput.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function() {

        jQuery.validator.addMethod("checkfields", function(value, element) {

            if ($("#from_date").val() == "" && $("#to_date").val() == "" && $("#last_name").val() == "") {
                return false;
            }
        }, "Please enter either a last name, from date or to date.");

        $("input.from_date").mask("99/99/9999");
        $("input.to_date").mask("99/99/9999");

        $("#searchForm").validate({
            errorElement: "span",
            errorContainer: $("#error"),
            errorPlacement: function(error, element) {
                error.appendTo(document.getElementById("error"));
            },
            success: function(label) {
                $("#searchForm").submit();
            },
            rules: {
                last_name: "checkfields"
            }
        });
        $("#from_date").focus(function() {
            if ($("#from_date").val() == "mm/dd/yyyy") {
                $("#from_date").val("");
            }
        });
        $("#from_date").blur(function() {
            if ($("#from_date").val() == "") {
                $("#from_date").val("mm/dd/yyyy");
            }
        });
        $("#to_date").focus(function() {
            if ($("#to_date").val() == "mm/dd/yyyy") {
                $("#to_date").val("");
            }
        });
        $("#to_date").blur(function() {
            if ($("#to_date").val() == "") {
                $("#to_date").val("mm/dd/yyyy");
            }
        });
    });
</script>

And the form:

<form action="search.spax" id="searchForm" name="searchForm" method="post">

  <div id="error" style="display:none;">
      <span></span>.<br clear="all"/>
    </div>
  <table width="520" border="0" cellspacing="0" cellpadding="0" class="obit_form">
     <tr align="left">
      <th><label for="last_name">Last Name</label></th>
      <th></th>
      <th><label for="from_date">From</label></th>
      <th><label for="to_date">To</label></th>
      <th></th>
     </tr>
     <tr align="left">

      <td><input type="text" id="last_name" class="required date" name="last_name" value="" style="width:135px;" /></td>
      <td>and/or</td>
      <td><input type="text" id="from_date" class="required date" name="from_date" value="mm/dd/yyyy" style="width:75px;" /></td>
      <td><input type="text" id="to_date" class="required date" name="to_date" value="mm/dd/yyyy" style="width:75px;" /></td>

      <td><input type="submit alt="Search" title="Search" value="Search" name="submitButton" id="Submit2" /></td>
     </tr>
   </table>       
</form>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你在做这样的事吗?

$(document).ready(function() {

 $('#aspnetForm').validate({
 rules: {
        ctl00$chMain$tbLoginEmail: {
            required: true,
            email: true
        },
...

我知道硬编码 id 并不理想,但你明白了。

You're doing something like this?

$(document).ready(function() {

 $('#aspnetForm').validate({
 rules: {
        ctl00$chMain$tbLoginEmail: {
            required: true,
            email: true
        },
...

I know hard-coding the id isn't ideal, but you get the picture.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文