Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Add multiple attribute to email method. #2074

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/core.js
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
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
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