Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1357,13 +1357,28 @@ $.extend( $.validator, {
},

// https://jqueryvalidation.org/email-method/
email: function( value, element ) {
email: function( value, element, no_multiple ) {

// From https://html.spec.whatwg.org/multipage/forms.html#valid-e-mail-address
// Retrieved 2014-01-14
// If you have a problem with this implementation, report a bug against the above spec
// Or use custom methods to implement your own email validation
return this.optional( element ) || /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test( value );
if ( this.optional( element ) ) {
return true;
}
if ( !element.multiple || !no_multiple ) {
return /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test( value );
} else {

var emails = value.split( /[,]+/ );
var valid = true;

for ( var i in emails ) {
value = emails[ i ];
valid = valid && $.validator.methods.email.call( this, $.trim( value ), element, false );
}
return valid;
}
},

// https://jqueryvalidation.org/url-method/
Expand Down
3 changes: 3 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,9 @@ <h3></h3>
<input name="year"/>
<button name="submitForm27" value="someValue" type="submit"><span>Submit</span></button>
</form>
<form id="testForm28">
<input type="email" id="email1" name="email1" value="name1@domain.tld,name2@domain.tld" multiple="true">
</form>
<form id="_contenteditableForm">
<div name="first_name" id="first_name" contenteditable placeholder="First Name"></div>
<br>
Expand Down
14 changes: 13 additions & 1 deletion test/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ QUnit.test( "url2 (tld optional)", function( assert ) {
} );

QUnit.test( "email", function( assert ) {
var method = methodTest( "email" );
var method = methodTest( "email" ),
v;
assert.ok( method( "name@domain.tld" ), "Valid email" );
assert.ok( method( "name@domain.tl" ), "Valid email" );
assert.ok( method( "bart+bart@tokbox.com" ), "Valid email" );
Expand All @@ -119,6 +120,17 @@ QUnit.test( "email", function( assert ) {
assert.ok( !method( "name,@domain.tld" ), "Invalid email" );
assert.ok( !method( "name;@domain.tld" ), "Invalid email" );
assert.ok( !method( "name;@domain.tld." ), "Invalid email" );

v = jQuery( "#testForm28" ).validate();
method = function( value, param ) {
return $.validator.methods.email.call( v, value, $( "#email1" )[ 0 ], param );
};

assert.ok( !method( "name1@domain.tld,name2@domain.tld", false ), "Invalid single email" );
assert.ok( method( "name1@domain.tld,name2@domain.tld", true ), "Valid multiple emails" );
assert.ok( method( "name1@domain.tld, name2@domain.tld", true ), "Valid multiple emails" );
assert.ok( !method( "name1@domain.tld;name2@domain.tld", true ), "Invalid multiple emails due to separator" );
assert.ok( !method( "name1@domain.tld;name2", true ), "Invalid multiple emails" );
} );

QUnit.test( "number", function( assert ) {
Expand Down