简体   繁体   English

对动态生成的表单使用jQuery表单验证equalTo规则

[英]use jQuery form validation equalTo rule for dynamically generated forms

I have n forms on one page. 我在一页上有n个表格。 Each one has two email address fields (normal field and verification). 每个都有两个电子邮件地址字段(普通字段和验证)。 What I want to do is to set a equalTo rule for the two fields. 我要为两个字段设置一个equalTo规则。 What I am doing now is this: 我现在正在做的是这样的:

$(".contactform").validate({
   onsubmit: true,
   onkeydown: false,
   onkeyup: false,
   onfocusin: false,
   onfocusout: false,
   onchange: false,
   onclick: false,
      rules: {
         email_veri: {
            equalTo: ".email"
         }
      }
});

But this is obviously not working as the id of email and email_veri is different in every form. 但这显然不起作用,因为每种形式的email和email_veri的ID都不同。 I read that it is possible to set a validation rule directly on a field but I didn't find out how. 我读到可以直接在字段上设置验证规则,但我没有找到具体方法。 I hope someone can help. 我希望有人能帮帮忙。

This may be useful, from "jQuery Validation with multiple (mostly) identical forms" at http://www.epalla.com/2009/12/jquery-validation-with-multiple-forms/ 这可能很有用,它来自http://www.epalla.com/2009/12/jquery-validation-with-multiple-forms/上的 “具有多个(大部分)相同表单的jQuery验证”

Their answer is to validate each form separately: 他们的答案是分别验证每种形式:

$(".question_form").each(function() {
    $(this).validate(
        rules: {
            email_veri: {
                equalTo: ".email"
        }
    );
});

Hiya Created a very simple demo for you: http://jsfiddle.net/bY5P6/ Hiya为您创建了一个非常简单的演示http : //jsfiddle.net/bY5P6/

SO if you will type different email and email verification field then you will get equal to message validation. 因此,如果您键入不同的电子邮件和电子邮件验证字段,那么您将获得equal to消息验证相同的效果。

further documentation for it: http://docs.jquery.com/Plugins/Validation/Methods/equalTo 进一步的文档: http : //docs.jquery.com/Plugins/Validation/Methods/equalTo

or for multiple forms please see here: Using JQuery Validate Plugin to validate multiple form fields with identical names 或用于多个表单,请参见此处: 使用JQuery验证插件来验证具有相同名称的多个表单字段

In case you are working with multiple forms please make sure quote : 如果您使用多种表格,请确保引用

validation requires field names to be unique except for radio buttons and check boxes. 验证要求字段名称必须唯一,但单选按钮和复选框除外。

:) ! :)!

Jquery Code jQuery代码

//================================= AZIONE FORM
$('#submit').click(function() {
    $('#mailer').submit();
    return false;
});

//================================= VALIDAZIONE FORM
$('#mailer').validate({
    focusInvalid: false,
    debug: true,
    rules: {
        name: {
            required: true
        },
        email_address: {
            required: true,
            email: true
        },
        email_address_veri: {
            required: true,
            email: true,
            equalTo: ".email"
        },
        request: {
            required: true
        }
    },
    messages: {
        name: {
            required: 'name required'
        },
        email_address: {
            required: 'email required',
            email: 'email address is not valid'
        },
        email_address_veri: {
            required: 'email required',
            email: 'email address verification is not valid',
            equalTo: "Please enter the same email as above"
        },
        request: {
            required: 'request required'
        }
    }
});​

HTML 的HTML

<!doctype html>
<html class="no-js" lang="en">
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>

        <form action="#" method="post" id="mailer" autocomplete="off">
            <label>
                <span>name</span>
                <input type="text" name="name" required="required" value="" />
            </label><br /><br />
            <label>
                <span>email</span>
                <input type="email" name="email_address" class="email"  value="" />
            </label><br /><br />
            <label>
                <span>email Verification</span>
                <input type="email" name="email_address_veri"  value="" />
            </label><br /><br />
            <label>
                <span>request</span>
                <textarea name="request" required="required"></textarea>
            </label><br /><br />
            <a href="javascript:;" id="submit">submit</a>

        </form>

    </body>
</html>
​

Output 输出量

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM