[英]How to validate form based on input type=text/radio/checkbox using jQuery Validation Plugin
I have a multi step form with dynamically generated fields that I need to validate using jQuery, this is the form . 我有一个包含动态生成字段的多步骤表单,需要使用jQuery进行验证,这就是表单 。 How I can achieve this ?
我该如何实现?
// next step
$('.form-horizontal .btn-next').on('click', function() {
$("#multiphase").validate();
$('input[type="text"]').each(function(){
$(this).rule('add', {
required: true,
messages:{
required: "This field is required"
}
})
})
});
First of all, you can validate elements using "name"
attribute. 首先,您可以使用
"name"
属性来验证元素。 you can show below example: 您可以显示以下示例:
<script type="text/javascript">
$("#pseudoForm").validate({
onfocusout:true,
rules:{
first_name:"required",
last_name:"required"
}
});
</script>
<!-- whatever -->
<div id="pseudoForm">
<input type="text" name="first_name"/>
<input type="text" name="last_name"/>
</div>
also you can see reference as this link LINK 您也可以看到此链接的参考链接LINK
You should map the input fields to an object containing the rules for the validation plugin. 您应该将输入字段映射到包含验证插件规则的对象。 I assume you're using this: http://jqueryvalidation.org/
我假设您正在使用此: http : //jqueryvalidation.org/
Here's the general idea: 这是一般的想法:
var fields = {};
$('form').find(':input').each(function() {
fields[this.name] = "required";
});
$('form').validate({
rules: fields
});
Of course you have to configure the plugin a bit more, but this is how you will handle the dynamic fields at least. 当然,您必须多配置一些插件,但这至少是您处理动态字段的方式。 An alternative would be to generate the rules server-side.
一种替代方法是在服务器端生成规则。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.