使用验证器jQuery验证Click事件上的表单 [英] Validate a form on Click event with validator jQuery

查看:97
本文介绍了使用验证器jQuery验证Click事件上的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery validate验证输入。
我的代码:

I am using jQuery validate for validate an input. My code:

 $('#button').click( function() {       
    $("#form").validate({              
        rules: {
            phone: {
                required: true,
                number: true,
                rangelength: [7, 14]
            }
        }
    }); 
});

HTML:

<form id="form" name="form" method="post" action="">
 <input id="phone" name="phone" class="required" type="text" />
 <div class="button" id="send_b">Send</div>
 </form>

此代码无效。如果我添加这行jQuery

This code is not working. If I add this line of jQuery

$("#form").submit();  

但我不想提交表单,所以我只想做点击验证。

inside the click event it works. But I don't want to submit the form so I want just to make the validation on click.

有人知道如何做到这一点吗?

Does anyone know how to do this?

推荐答案

.form() 手动触发验证立即(默认行为等待提交事件):

Just add .form() to manually trigger the validation immediately (the default behavior waits for a submit event):

$('#button').click( function() {       
  $("#form").validate({              
    rules: {
      phone: {
        required: true,
        number: true,
        rangelength: [7, 14]
      }
    }
  }).form(); 
});

这篇关于使用验证器jQuery验证Click事件上的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆