Methods: Update email to use HTML5 regex, remove email2 method

The regex provided by the HTML5 spec allows email addresses without a top level
domain, like name@localhost. That makes the email2 method pointless.

Hopefully this is the last time the email methods ever has to be updated. At
least it will make bug triage easier: If you don't like the implementation,
report an issue against the HTML5 spec.

Fixes gh-828
Closes gh-753
Closes gh-739
This commit is contained in:
Jörn Zaefferer
2014-01-14 15:16:53 +01:00
parent d7a05b1c26
commit dd162ae360
3 changed files with 12 additions and 39 deletions

View File

@@ -1,4 +0,0 @@
// same as email, but TLD is optional
jQuery.validator.addMethod("email2", function(value, element, param) {
return this.optional(element) || /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)*(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);
}, jQuery.validator.messages.email);

View File

@@ -389,7 +389,7 @@ $.extend($.validator, {
}
//Add aria-invalid status for screen readers
$(element).attr("aria-invalid", !result);
if ( !this.numberOfInvalids() ) {
// Hide error containers on last error
this.toHide = this.toHide.add( this.containers );
@@ -1025,8 +1025,11 @@ $.extend($.validator, {
// http://jqueryvalidation.org/email-method/
email: function( value, element ) {
// contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
return this.optional(element) || /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i.test(value);
// From http://www.whatwg.org/specs/web-apps/current-work/multipage/states-of-the-type-attribute.html#e-mail-state-%28type=email%29
// 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);
},
// http://jqueryvalidation.org/url-method/

View File

@@ -70,46 +70,20 @@ test("email", function() {
ok( method( "bart+bart@tokbox.com" ), "Valid email" );
ok( method( "bart+bart@tokbox.travel" ), "Valid email" );
ok( method( "n@d.tld" ), "Valid email" );
ok( method( "ole@føtex.dk"), "Valid email" );
ok( method( "jörn@bassistance.de"), "Valid email" );
ok( method( "bla.blu@g.mail.com"), "Valid email" );
ok( method( "\"Scott Gonzalez\"@example.com" ), "Valid email" );
ok( method( "\"Scott González\"@example.com" ), "Valid email" );
ok( method( "\"name.\"@domain.tld" ), "Valid email" ); // valid without top label
ok( method( "\"name,\"@domain.tld" ), "Valid email" ); // valid without top label
ok( method( "\"name;\"@domain.tld" ), "Valid email" ); // valid without top label
ok( method( "name@domain" ), "Valid email" );
ok( method( "name.@domain.tld" ), "Valid email" );
ok(!method( "ole@føtex.dk"), "Invalid email" );
ok(!method( "jörn@bassistance.de"), "Invalid email" );
ok(!method( "name" ), "Invalid email" );
ok(!method( "test@test-.com" ), "Invalid email" );
ok(!method( "name@website.a" ), "Invalid email" );
ok(!method( "name@" ), "Invalid email" );
ok(!method( "name@domain" ), "Invalid email" );
ok(!method( "name.@domain.tld" ), "Invalid email" );
ok(!method( "name,@domain.tld" ), "Invalid email" );
ok(!method( "name;@domain.tld" ), "Invalid email" );
ok(!method( "name;@domain.tld." ), "Invalid email" );
});
test("email2 (tld optional)", function() {
var method = methodTest("email2");
ok( method( "name@domain.tld" ), "Valid email" );
ok( method( "name@domain.tl" ), "Valid email" );
ok( method( "bart+bart@tokbox.com" ), "Valid email" );
ok( method( "bart+bart@tokbox.travel" ), "Valid email" );
ok( method( "n@d.tld" ), "Valid email" );
ok( method( "ole@føtex.dk"), "Valid email" );
ok( method( "jörn@bassistance.de"), "Valid email" );
ok( method( "bla.blu@g.mail.com"), "Valid email" );
ok( method( "\"Scott Gonzalez\"@example.com" ), "Valid email" );
ok( method( "\"Scott González\"@example.com" ), "Valid email" );
ok( method( "\"name.\"@domain.tld" ), "Valid email" ); // valid without top label
ok( method( "\"name,\"@domain.tld" ), "Valid email" ); // valid without top label
ok( method( "\"name;\"@domain.tld" ), "Valid email" ); // valid without top label
ok(!method( "name" ), "Invalid email" );
ok(!method( "name@" ), "Invalid email" );
ok( method( "name@domain" ), "Invalid email" );
ok(!method( "name.@domain.tld" ), "Invalid email" );
ok(!method( "name,@domain.tld" ), "Invalid email" );
ok(!method( "name;@domain.tld" ), "Invalid email" );
});
test("number", function() {
var method = methodTest("number");
ok( method( "123" ), "Valid number" );