jQuery验证插件多个电子邮件地址

时间:2012-04-27 14:10:12

标签: jquery email jquery-validate

我有以下jQuery Validation插件:

<script type="text/javascript" src="/requires/js/jquery.min.js"></script>
<script type="text/javascript" src="/requires/js/jquery.validate.js"></script>
<script type="text/javascript"> 
    $(document).ready(function() {
        $("#form").validate({
            rules: {
                name: {
                    required: true,
                    minlength: 2
                },
                email: {
                    required: true,
                    email: true
                },
                emails: {
                    required: true,
                    email: true
                }
            },
            messages: {
                name: {
                    required: "Je bent vergeten om je naam in te vullen.",
                    minlength: "Je bent vergeten om je naam in te vullen."
                },
                email: {
                    required: "Je bent vergeten om je emailadres in te vullen.",
                    email: "Het opgegeven emailadres is niet geldig."
                },
                emails: {
                    required: "Je bent vergeten om het emailadres van je vriend(in) in te vullen.",
                    email: "Je moet een geldig e-mailadres van je vriend(in) invullen."
                }
            }
        });
    }); 
</script>

<form id="form" method="post">
Jouw naam: <input type="text" name="name"><br />
Jouw e-mailadres: <input type="text" name="email"><br />
Het e-mailadres van je vriend(in): <input type="text" name="emails"><br />
<input type="submit" value="Verstuur">
</form>

通过超值电子邮件,人们必须提供多个电子邮件,例如test@test.com; test2@test.com或test @ test.com,test @ test2.com

执行此操作时,验证插件会显示错误:Je moet een geldig e-mailadres van je vriend(in)invullen。

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:46)

这是在addMethod

的帮助下添加的自定义功能
jQuery.validator.addMethod(
    "multiemail",
     function(value, element) {
         if (this.optional(element)) // return true on optional element 
             return true;
         var emails = value.split(/[;,]+/); // split element by , and ;
         valid = true;
         for (var i in emails) {
             value = emails[i];
             valid = valid &&
                     jQuery.validator.methods.email.call(this, $.trim(value), element);
         }
         return valid;
     },

    jQuery.validator.messages.email
);

这是一个jsFiddle,向您展示如何使用multiemail自定义方法:

http://jsfiddle.net/leniel/xFphm/7/

答案 1 :(得分:0)

自定义功能,内置消息

<script>    
jQuery.validator.addMethod("custom_method_one", function (value, element) {
    if (this.optional(element)) {
    return true;
    }

    var emails = value.split(','),
    valid = true;

    for (var i = 0, limit = emails.length; i < limit; i++) {
    value = emails[i];
    valid = valid && jQuery.validator.methods.email.call(this, value, element);
    }

    return valid;
    }, "Invalid email format: please use a comma to separate multiple email addresses.");

$('#form').validate({
 "emails": {  required:true,custom_method_one: true }   
})
</script>

<form method="post" id="form" name="form" action="">
 <input type="text" name="emails" id="emails" placeholder="(eg:abc@incipio.com,xyz@ncpo.com,....)"/>

</form>