views:

23

answers:

1

I have created a add email method (jquery) to validate a multiple emails for recipient text box. it's working fine when prototype.js is not declared on the page. To get rid of the $ conflict i also incorporated the $ noconflict() method measure measure. The other field validations are working in this scenario, except the receipient email validation field. AS per my finding "jQuery.validator.methods.email.call(this, value, element)" line no 50 of the page is not working and hence the method is not firing . I need to call the prototype.js as well. Please see the following code for a clearer understanding.......Thanks in advance.

Please see the code below: Multi Email Validation
var JQ = jQuery.noConflict();
JQ(document).ready(function() {

  // Handler for .ready() called.
JQ("#email-form").validate({
            rules : {
                email : {
                    required : true,
                                    email : true
                },
                            recipientEmail : {
                    multiemail: true,
                                    required : true
                            //      email : true
                                    }
            },
                    messages: {
                email: {
                    required: "Please enter your email address.",
                                    email: "Please enter a valid email address"
                 },
                             recipientEmail: {
                                    multiemail: "One or more of your recipient email addresses needs correction.",
                                    required: "Please enter the recipient's email address."
                                    //email: "Please enter a valid email address"
                }
            }
        }); 
});

JQ.validator.addMethod("multiemail", function(value, element) { if (this.optional(element)) // return true on optional element return true; // var emails = value.split( new RegExp( "\s*,\s*", "gi" ) ); var emails = value.split( new RegExp( "\s*,\s*", "gi" ) );

       valid = true; 
               maxEmaillength = emails.length;
       for(var i in emails) 
            {
         value = emails[i]; 
         valid = valid && jQuery.validator.methods.email.call(this, value, element);
                     // Maximum email length validation
                       if(maxEmaillength > 5)
                               {
                                       JQ('label.error:first').html("Please enter only 5 mail IDs at a time");
                                       JQ('label.error:first').css(display, block);                            
                                       setTimeout(alert("Please enter only 5 mail IDs at a time"), 5);
                            }       
        } 
        return valid; 
                    }, 'One or more email addresses are invalid');

</head>
<body>
<form action="" method="get" name="email-form" id="email-form">
<label for="email">email</label>
<input type="text" name="email" id="email" style="width:200px" />
<br />
<label for="recipientEmail">Recipient Email</label>
<input type="text" name="recipientEmail" id="recipientEmail" style="width:500px" /><br />

<input type="submit" name="Submit" id="Submit" value="Submit" />
</form>
</body>
</html>
A: 

I have just change the approach a little bit, as jQuery.validator.methods.email.call(this, value, element) was not working in the previous custom method. Although i could not find the exact reason, why that was not working with prototype.js and what the exact solution for that problem. But the following code snippet is working as desired. Just replace that previous jquery custom email method with the following one.

function validateEmail(field) { var regex=/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}\b/i; return (regex.test(field)) ? true : false; }

JQ.validator.addMethod("multiemail", function(value, element) { var result = value.split(","); for(var i = 0;i < result.length;i++) if(!validateEmail(result[i]) || result.length > 5) return false;
return true;

},'One or more email addresses are invalid');

Want to be there
I have tested at my end. It's working. Please give your suggestion for improvement if any.
Want to be there