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

前端之家收集整理的这篇文章主要介绍了jQuery验证插件多个电子邮件地址前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下jQuery验证插件
<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>

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

当你这样做时,验证插件会出现错误:Je。。。。。。。。。。。。。。。。。

如何解决这个问题?

解决方法

这是在 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,它显示了如何使用多邮箱自定义方法

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

原文链接:https://www.f2er.com/jquery/181779.html

猜你在找的jQuery相关文章