Skip to content

Instantly share code, notes, and snippets.

@marifrahman
Created December 12, 2014 04:02
Show Gist options
  • Save marifrahman/a3fd8e4c03eee3ada7ce to your computer and use it in GitHub Desktop.
Save marifrahman/a3fd8e4c03eee3ada7ce to your computer and use it in GitHub Desktop.
jQuery Multiple email address Validation // source http://jsbin.com/pomofoc
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="jQuery Multiple email address Validation" />
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.8/jquery.validate.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<form id="form_id">
<input id='email_address' name='email_address' />
<br/><input type='submit' />
</form>
<script id="jsbin-javascript">
$.validator.addMethod(
"multiemails",
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 && $.validator.methods.email.call(this, $.trim(value), element);
}
return valid;
},
$.validator.messages.multiemails
);
$("#form_id").validate({
rules: {
email_address: {required : true,multiemails: true}
},
messages: {
email_address: {
multiemails: "<br/>You must enter a valid email OR ';' separated multiple email address"
}
}
});
</script>
<script id="jsbin-source-html" type="text/html"><!DOCTYPE html>
<html>
<head>
<meta name="description" content="jQuery Multiple email address Validation" />
<script src="//code.jquery.com/jquery-2.1.1.min.js"><\/script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.8/jquery.validate.js"><\/script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<form id="form_id">
<input id='email_address' name='email_address' />
<br/><input type='submit' />
</form>
</body>
</html></script>
<script id="jsbin-source-javascript" type="text/javascript">$.validator.addMethod(
"multiemails",
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 && $.validator.methods.email.call(this, $.trim(value), element);
}
return valid;
},
$.validator.messages.multiemails
);
$("#form_id").validate({
rules: {
email_address: {required : true,multiemails: true}
},
messages: {
email_address: {
multiemails: "<br/>You must enter a valid email OR ';' separated multiple email address"
}
}
});</script></body>
</html>
$.validator.addMethod(
"multiemails",
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 && $.validator.methods.email.call(this, $.trim(value), element);
}
return valid;
},
$.validator.messages.multiemails
);
$("#form_id").validate({
rules: {
email_address: {required : true,multiemails: true}
},
messages: {
email_address: {
multiemails: "<br/>You must enter a valid email OR ';' separated multiple email address"
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment