jQuery 验证无法正常工作

2024-01-03

我需要在单击 div 按钮时触发此验证。如果验证成功,我希望它提醒“很好”。

不幸的是,尽管会触发验证并检查所有规则,但如果必填字段为空,则不会出错。不过,任何其他检查都会出错。

最后,如果我正确填写字段并单击按钮,则不会发生任何情况。

JS:

$(document).ready(function() {

// Submit prompted password reset
$("#passwordResetButton").click(function() {

    $("#passwordResetForm").validate({  
        rules: {
            password: { required: true, minlength: 8, maxlength: 40, remote: { url: "/ajax/isPasswordOK", data: { product: function() { return $("#password").val();  } } } },
            confirmPassword: { required: true, equalTo: "#password" },
        }, 
        messages: { 
            password: { required: "Please enter a password", minlength: "Password must be at least 8 characters long", remote: "Password should contain: <li>At least one upper case character <li>At least one lower case character <li>At least one number <li>And may not contain any of the following: \\;-\")(&*='|$" },
            confirmPassword: { required: "Please confirm your password", equalTo: "The passwords do not match" },
          }, 
        onkeyup: false, //turn off auto validate whilst typing
        onblur: true,
        afterValidation: function() {
            alert('is good');
        }
    });
});
});

HTML:

<form id="passwordResetForm" style="width: 480px; margin: auto;">
        <div class="row"><p class="lead">Let's reset that password</p></div>
        <div class="row">
            <div class="small-8 large-4 columns"><label>New Password:</label></div>
            <div class="small-12 large-6 columns"><input name="password" id="password" type="password" size="20"/></div>
        </div>
        <div class="row">
            <div class="small-8 large-4 columns"><label>Confirm Password:</label></div>
            <div class="small-12 large-6 columns"><input name="confirmPassword" id="confirmPassword" type="password" size="20"/></div>
        </div>
        <div class="row">
            <div class="large-offset-10 small-3 large-1 columns">
                <div id="passwordResetButton"  class="small button">Submit</div>
            </div>
        </div>
    </form>

您的代码存在一些问题。

1) .validate() is the 初始化插件的方法,而不是测试表单的方法。换句话说,不要将其放在click处理程序,否则当您需要它时它不会准备好。放.validate()在 DOM 就绪事件处理程序内部,因此在创建页面时会触发一次,然后表单立即准备好进行验证。

2) 没有这样的选项onblur对于这个插件。它被称为onfocusout并且该选项已默认激活。将此类选项设置为true无效 http://jqueryvalidation.org/validate#onfocusout并且可能会破坏某些东西,因此只需将其完全排除在初始化之外即可。

3)没有这样的选项/函数调用afterValidation:。你不能只是“发明”回调函数......jQuery 插件必须有这些东西已经专门内置于其中 http://docs.jquery.com/Plugins/Validation/validate#toptions。如果您想在有效表单上触发某些内容,请使用提供的submitHandler:回调函数。

submitHandler: function (form) {
    alert('is good');
    return false;
}

4) 用一个<input type="submit" /> or a <button type="submit">Submit</button>用于表单的提交。这样,插件就可以自动捕获点击事件并按设计处理提交。

5)小注意事项:对于您的自定义消息,只需使用{0}并且规则的参数将自动插入。这种代码更容易维护。

minlength: "Password must be at least {0} characters long",

工作演示:http://jsfiddle.net/J9N3g/ http://jsfiddle.net/J9N3g/

我建议您在此处查看完整文档:http://docs.jquery.com/Plugins/Validation http://docs.jquery.com/Plugins/Validation

jQuery:

$(document).ready(function () {

    $("#passwordResetForm").validate({
        rules: {
            password: {
                required: true,
                minlength: 8,
                maxlength: 40,
                remote: {
                    url: "/ajax/isPasswordOK",
                    data: {
                        product: function () {
                            return $("#password").val();
                        }
                    }
                }
            },
            confirmPassword: {
                required: true,
                equalTo: "#password"
            },
        },
        messages: {
            password: {
                required: "Please enter a password",
                minlength: "Password must be at least {0} characters long",
                remote: "Password should contain: <li>At least one upper case character <li>At least one lower case character <li>At least one number <li>And may not contain any of the following: \\;-\")(&*='|$"
            },
            confirmPassword: {
                required: "Please confirm your password",
                equalTo: "The passwords do not match"
            },
        },
        onkeyup: false, //turn off auto validate whilst typing
        submitHandler: function (form) {
            alert('is good');
            return false;
        }
    });

});

HTML:

<form id="passwordResetForm" style="width: 480px; margin: auto;">
    <div class="row">
        <p class="lead">Let's reset that password</p>
    </div>
    <div class="row">
        <div class="small-8 large-4 columns">
            <label>New Password:</label>
        </div>
        <div class="small-12 large-6 columns">
            <input name="password" id="password" type="password" size="20" />
        </div>
    </div>
    <div class="row">
        <div class="small-8 large-4 columns">
            <label>Confirm Password:</label>
        </div>
        <div class="small-12 large-6 columns">
            <input name="confirmPassword" id="confirmPassword" type="password" size="20" />
        </div>
    </div>
    <div class="row">
        <div class="large-offset-10 small-3 large-1 columns">
            <input type="submit" id="passwordResetButton" class="small button" />
        </div>
    </div>
</form>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

jQuery 验证无法正常工作 的相关文章

随机推荐

  • 使用 T-SQL 查询 XML 字段

    如何使用 T SQL 查询 XML 数据中的多个节点并将结果输出到单个逗号分隔的字符串 例如 我想获取以下 XML 中所有目的地名称的列表 如 德国 法国 英国 意大利 西班牙 葡萄牙
  • 单击时突出显示文本框内容

    我有动态生成的用户表单 其中包含标签 复选框和文本框 单击时是否可以选择文本框的内容 这是我用来创建文本框的方法 Set NewTextBox MainFrame Controls Add Forms TextBox 1 With NewT
  • 在asp.net core 2中流式传输视频文件

    我想使用asp net core在浏览器中播放视频 在 html 中我有
  • 抓取大量带有 url 的 Google Scholar 页面

    我正在尝试使用 BeautifulSoup 从 Google 学者的作者那里获取所有出版物的完整作者列表 由于作者的主页只有每篇论文的作者列表 因此我必须打开论文的链接才能获取完整列表 结果 我每隔几次尝试就会遇到验证码 有没有办法避免验证
  • 工具栏中的后退按钮不起作用

    我只有 Activity 它是 ActionBarActivity 类的子级 在方法中我设置OnCreate支持工具栏 为此 我重写了 OnOptionsItemSelected 因此当我按下后退按钮时执行了一些操作 代码如下所示 Acti
  • 为什么 ThreadLocalRandom 的实现如此奇怪?

    这个问题涉及到实施ThreadLocalRandom在 OpenJDK 版本 1 8 0 中 ThreadLocalRandom提供每线程随机数生成器 没有 Random 带来的同步开销 最明显的实现 IMO 是这样的 它似乎保留了向后兼容
  • IE11 上的 XSLT 处理?

    IE11 中的 XSLT 处理发生了什么 在IE8 9 10上 您可以使用 if window ActiveXObject var xslt new ActiveXObject Msxml2 XSLTemplate 在 Chrome Fir
  • 尝试使用 ctypes 调用 wincred api

    我正在尝试使用 ctypes API 读取 Windows 凭证库 但我不确定如何将函数结果转换回可用的 ctypes Structure import ctypes class CREDENTIALS ctypes Structure f
  • Joshua Bloch 在《Effective Java》中解释了枚举类型

    请看这个link http kulferhat blogspot in 2014 08 ej 30 use enum instead of int constants html 关于枚举 布洛赫先生说 Java 枚举类型是通过公共静态最终字
  • uint32 和 uint32_t 之间的区别[重复]

    这个问题在这里已经有答案了 可能的重复 不同整数类型之间的区别 https stackoverflow com questions 11786113 difference between different integer types 有什
  • 带有特殊字符的 ASP.NET MVC 身份电子邮件/用户名

    当通过以下方式注册帐户时Web API带有诸如 电子邮件受保护 cdn cgi l email protection Fiddler 返回跟随错误 请注意 电子邮件也用于用户名 因此两个字段是相同的 但在 MVC 本身上注册时它可以工作 E
  • 如何将 WindowState 从桌面快捷方式传递到 WPF 应用程序?

    如何从桌面快捷方式控制 WPF 主窗口的初始 WindowState 正常 最小化 最大化 快捷方式属性对话框的 运行 组合框让我可以在 正常窗口 最小化 和 最大化 之间进行选择 但 WPF 应用程序似乎完全忽略了此选项 对于 WinFo
  • 统计每月有条件的记录数

    我有一张桌子 我们打电话给他们吧SUMMARYDATA NIP NAME DEPARTMENT STATUSIN STATUSOUT LATECOME A1 ARIA BB 2020 01 21 08 06 23 2020 01 21 11
  • EF 5 Enable-Migrations:在程序集中找不到上下文类型

    我有 4 个项目 Toombu Entities all models are there Toombu DataAccess Mapping Repository and ToombuContext Toombu Logique Logi
  • 当搜索中使用多个单词时,如何在 Lucene.net 中执行 AND 搜索?

    我正在研究 Lucene net 尝试了解如何在我的应用程序中实现它 我有以下代码 Add 2 documents var doc1 new Document var doc2 new Document doc1 Add new Field
  • “LIKE”运算符适用于 MS Access,但不适用于 ADO

    我正在尝试使用带有星号的 Like 来过滤记录 它在使用 Access 2010 返回许多记录时有效 我很困惑为什么它与 ADO 一起使用时什么也不返回 该代码包含多个表和列 因此为了排除故障 我做了一个简单的查询 这是代码 strsql
  • 将 Jpeg 更改为渐进式 Jpeg 图像 [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我想从基线 Jpeg 图像转换为渐进式 Jpeg 图像 我的磁盘中存储了 10 000 张图像 我尝试
  • 如何混合两个图像?

    我的视图中有两个图像 我需要混合这两个图像 但问题是无法得到完美的融合 图像混合代码 CGSize newSize CGSizeMake backGroundImage frame size width backGroundImage fr
  • NHibernate Future 对象图许多查询

    给定一个使用 Future 调用的多级对象图 var Dads db Session Query
  • jQuery 验证无法正常工作

    我需要在单击 div 按钮时触发此验证 如果验证成功 我希望它提醒 很好 不幸的是 尽管会触发验证并检查所有规则 但如果必填字段为空 则不会出错 不过 任何其他检查都会出错 最后 如果我正确填写字段并单击按钮 则不会发生任何情况 JS do