Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery validation plugin multiple email addresses

I have the following jQuery Validation Plugin:

<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>

By value emails people must can give multiple emailaddresses like [email protected];[email protected] or [email protected],[email protected]

When you do this, the validation plugin gives the error: Je moet een geldig e-mailadres van je vriend(in) invullen.

How can I solve this problem?

like image 610
Arthur Avatar asked Apr 27 '12 14:04

Arthur


People also ask

How do I verify multiple email addresses?

You just parse the string into an array of string, each with a single email address. Split the string on your delimiter, get the array of string back and then enumerate the array, sending each email address to your code that validates the address.


1 Answers

Here's a custom made function added with the help of the 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
);

Here's a jsFiddle that shows you how to use the multiemail custom method:

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

like image 55
Leniel Maccaferri Avatar answered Oct 17 '22 01:10

Leniel Maccaferri