2+ (multiple) jQuery emails Verified text input

I have been working on this for several hours and cannot make it work. I need more than one email address in our customers section in the same input field.

Now I have (which is great for a single letter):

<script language="JavaScript"> $(document).ready(function(){ $("form#editclient").validate({ rules: { Contact: { required: true }, Clientname: { required: true }, ClientLogin: { required: true }, ClientPassword: { required: true }, email: { required: true, multiemail: true } }, messages: { Contact: { required: "Please enter your Contact Nmae." }, Clientname: { required: "Please enter your Client Name" }, ClientLogin: { required: "Please enter a login." }, ClientPassword: { required: "Please enter a password" }, email: { required: "Please enter an Email Address", multiemail: "You must enter a valid email, or comma separate multiple" } } }); }); </script> <input type="Text" id="email" name="Email" value="#Trim(Email)#" maxlength="60" class="inputText430 email required"> 

I found this that should make several validators work:

 jQuery.validator.addMethod( "multiemails", function (value, element) { if (this.optional(element)) // return true on optional element return true; var email = value.split(/[;,]+/); // split element by , and ; valid = true; for (var i in email) { value = email[i]; valid = valid && jQuery.validator.methods.email.call(this, $.trim(value), element); } return valid; }, jQuery.validator.messages.multiemail); 

But I can not get it to work correctly. Can someone please help me get the opportunity to scan 2 or more emails in one text box?

Thanks in advance.

(edited to add a multi-user form in the rules and messages) (edited using the solution below)

 <form> <input id='emailTest' name='emailTest' /> <input type='submit' /> </form> <script type="text/javascript"> jQuery.validator.addMethod( "multiemail", function (value, element) { var email = value.split(/[;,]+/); // split element by , and ; valid = true; for (var i in email) { value = email[i]; valid = valid && jQuery.validator.methods.email.call(this, $.trim(value), element); } return valid; }, jQuery.validator.messages.multiemail ); $("form").validate({ debug: true, rules: { emailTest: { multiemail: true } }, messages: { emailTest: { multiemail: "You must enter a valid email, or comma separate multiple" } }, submitHandler: function(form) { return false; } }); 
+6
source share
1 answer

I think there are some semantic errors in the code that lead to some things that you don't expect.

I managed to get your custom handler to work by matching it with a field by name and not by type - I'm not sure if overriding works in the plugin.

 rules: { emailTest: { multiemail: true } } 

Also in the handler, you had multiemails as an identifier and was later referred to as multiemail , but not sure how much of it was.

Here's the full working example: http://jsfiddle.net/jbergler/7jXn7/2/

+11
source

Source: https://habr.com/ru/post/950545/


All Articles