gpt4 book ai didi

jquery - 如何在使用表单向导时运行 jquery validate()?

转载 作者:行者123 更新时间:2023-12-03 23:06:16 24 4
gpt4 key购买 nike

我正在使用jQuery Form Wizard 3.0.4 plugin多步骤注册过程。它使用内置的 jQuery 验证器插件,在一步一步进行时工作正常。 编辑:这意味着我已经在使用validationEnabled:true、formOptions和validationOptions,并且这些正在工作。我需要在常规功能之外运行相同的验证。

问题是我需要运行验证器并在两个点手动显示错误。对于我有的特殊领域,并且在 AJAX 提交之前。我尝试过以下方法,但没有任何作用:

$("#registrationForm").validate();  

表单向导脚本 (jquery.form.wizard-3.0.4.js) 似乎在执行下一步时执行此操作:

this.element.validate().focusInvalid();

所以我尝试了这个,但也没有任何作用:

$("#registrationForm").element.validate().focusInvalid();

有什么想法吗?

  1. 如何运行与点击下一步时发生的相同的验证?
  2. 如何调用验证并显示特定字段错误的函数?

最佳答案

我认为下面的示例代码可能适合您,当单击 id="validate_form"的按钮或元素时会触发验证。这基本上是当用户单击“下一步”时运行的代码(在插件中)。

    $(function(){
$("#validate_form").click(function(){ // when the button is clicked...
var wizard = $("#demoForm"); // cache the form element selector
if(!wizard.valid()){ // validate the form
wizard.validate().focusInvalid(); //focus the invalid fields
}

})
})

如果您只需要验证一个输入字段,那么您可以使用以下代码(在本例中单击 id="validate_email"的按钮):

    $(function(){
$("#validate_email").click(function(){ // when the button is clicked...
var wizard = $("#demoForm"); // cache the form element selector
if(!wizard.validate().element( "#myemail" )){ // validate the input field
wizard.validate().focusInvalid(); // focus it if it was invalid
}

})
})

希望这有帮助。

/一月

关于jquery - 如何在使用表单向导时运行 jquery validate()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4762603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com