1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=555559
https://bugzilla.mozilla.org/show_bug.cgi?id=668817
https://bugzilla.mozilla.org/show_bug.cgi?id=854812
-->
<head>
<title>Test for <input type='email'> validity</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=555559">Mozilla Bug 555559</a>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=668817">Mozilla Bug 668817</a>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=854812">Mozilla Bug 854812</a>
<p id="display"></p>
<div id="content" style="display: none">
<form>
<input type='email' name='email' id='i' oninvalid="invalidEventHandler(event);">
<form>
</div>
<pre id="test">
<script type="application/javascript">
/** Test for <input type='email'> validity **/
var gInvalid = false;
function invalidEventHandler(e)
{
is(e.type, "invalid", "Invalid event type should be invalid");
gInvalid = true;
}
function checkValidEmailAddress(element)
{
gInvalid = false;
ok(!element.validity.typeMismatch && !element.validity.badInput,
"Element should not suffer from type mismatch or bad input (with value='"+element.value+"')");
ok(element.validity.valid, "Element should be valid");
ok(element.checkValidity(), "Element should be valid");
ok(!gInvalid, "The invalid event should not have been thrown");
is(element.validationMessage, '',
"Validation message should be the empty string");
ok(element.matches(":valid"), ":valid pseudo-class should apply");
}
const VALID = 0;
const TYPE_MISMATCH = 1 << 0;
const BAD_INPUT = 1 << 1;
function checkInvalidEmailAddress(element, failedValidityStates)
{
info("Checking " + element.value);
gInvalid = false;
var expectTypeMismatch = !!(failedValidityStates & TYPE_MISMATCH);
var expectBadInput = !!(failedValidityStates & BAD_INPUT);
ok(element.validity.typeMismatch == expectTypeMismatch,
"Element should " + (expectTypeMismatch ? "" : "not ") + "suffer from type mismatch (with value='"+element.value+"')");
ok(element.validity.badInput == expectBadInput,
"Element should " + (expectBadInput ? "" : "not ") + "suffer from bad input (with value='"+element.value+"')");
ok(!element.validity.valid, "Element should not be valid");
ok(!element.checkValidity(), "Element should not be valid");
ok(gInvalid, "The invalid event should have been thrown");
is(element.validationMessage, "Please enter an email address.",
"Validation message is not valid");
ok(element.matches(":invalid"), ":invalid pseudo-class should apply");
}
function testEmailAddress(aElement, aValue, aMultiple, aValidityFailures)
{
aElement.multiple = aMultiple;
aElement.value = aValue;
if (!aValidityFailures) {
checkValidEmailAddress(aElement);
} else {
checkInvalidEmailAddress(aElement, aValidityFailures);
}
}
var email = document.forms[0].elements[0];
// Simple values, checking the e-mail syntax validity.
var values = [
[ '' ], // The empty string shouldn't be considered as invalid.
[ 'foo@bar.com', VALID ],
[ ' foo@bar.com', VALID ],
[ 'foo@bar.com ', VALID ],
[ '\r\n foo@bar.com', VALID ],
[ 'foo@bar.com \n\r', VALID ],
[ '\n\n \r\rfoo@bar.com\n\n \r\r', VALID ],
[ '\n\r \n\rfoo@bar.com\n\r \n\r', VALID ],
[ 'tulip', TYPE_MISMATCH ],
// Some checks on the user part of the address.
[ '@bar.com', TYPE_MISMATCH ],
[ 'f\noo@bar.com', VALID ],
[ 'f\roo@bar.com', VALID ],
[ 'f\r\noo@bar.com', VALID ],
[ 'fü@foo.com', TYPE_MISMATCH ],
// Some checks for the domain part.
[ 'foo@bar', VALID ],
[ 'foo@b', VALID ],
[ 'foo@', TYPE_MISMATCH ],
[ 'foo@bar.', TYPE_MISMATCH ],
[ 'foo@foo.bar', VALID ],
[ 'foo@foo..bar', TYPE_MISMATCH ],
[ 'foo@.bar', TYPE_MISMATCH ],
[ 'foo@tulip.foo.bar', VALID ],
[ 'foo@tulip.foo-bar', VALID ],
[ 'foo@1.2', VALID ],
[ 'foo@127.0.0.1', VALID ],
[ 'foo@1.2.3', VALID ],
[ 'foo@b\nar.com', VALID ],
[ 'foo@b\rar.com', VALID ],
[ 'foo@b\r\nar.com', VALID ],
[ 'foo@.', TYPE_MISMATCH ],
[ 'foo@fü.com', VALID ],
[ 'foo@fu.cüm', VALID ],
[ 'thisUsernameIsLongerThanSixtyThreeCharactersInLengthRightAboutNow@mozilla.tld', VALID ],
// Long strings with UTF-8 in username.
[ 'this.is.email.should.be.longer.than.sixty.four.characters.föö@mözillä.tld', TYPE_MISMATCH ],
[ 'this-is-email-should-be-longer-than-sixty-four-characters-föö@mözillä.tld', TYPE_MISMATCH, true ],
// Long labels (labels greater than 63 chars long are not allowed).
[ 'foo@thislabelisexactly63characterssssssssssssssssssssssssssssssssss', VALID ],
[ 'foo@thislabelisexactly63characterssssssssssssssssssssssssssssssssss.com', VALID ],
[ 'foo@foo.thislabelisexactly63characterssssssssssssssssssssssssssssssssss.com', VALID ],
[ 'foo@foo.thislabelisexactly63characterssssssssssssssssssssssssssssssssss', VALID ],
[ 'foo@thislabelisexactly64charactersssssssssssssssssssssssssssssssssss', TYPE_MISMATCH | BAD_INPUT ],
[ 'foo@thislabelisexactly64charactersssssssssssssssssssssssssssssssssss.com', TYPE_MISMATCH | BAD_INPUT ],
[ 'foo@foo.thislabelisexactly64charactersssssssssssssssssssssssssssssssssss.com', TYPE_MISMATCH | BAD_INPUT ],
[ 'foo@foo.thislabelisexactly64charactersssssssssssssssssssssssssssssssssss', TYPE_MISMATCH | BAD_INPUT ],
// Long labels with UTF-8 (punycode encoding will increase the label to more than 63 chars).
[ 'foo@thisläbelisexäctly63charäcterssssssssssssssssssssssssssssssssss', TYPE_MISMATCH | BAD_INPUT ],
[ 'foo@thisläbelisexäctly63charäcterssssssssssssssssssssssssssssssssss.com', TYPE_MISMATCH | BAD_INPUT ],
[ 'foo@foo.thisläbelisexäctly63charäcterssssssssssssssssssssssssssssssssss.com', TYPE_MISMATCH | BAD_INPUT ],
[ 'foo@foo.thisläbelisexäctly63charäcterssssssssssssssssssssssssssssssssss', TYPE_MISMATCH | BAD_INPUT ],
// The domains labels (sub-domains or tld) can't start or finish with a '-'
[ 'foo@foo-bar', VALID ],
[ 'foo@-foo', TYPE_MISMATCH ],
[ 'foo@foo-.bar', TYPE_MISMATCH ],
[ 'foo@-.-', TYPE_MISMATCH ],
[ 'foo@fo-o.bar', VALID ],
[ 'foo@fo-o.-bar', TYPE_MISMATCH ],
[ 'foo@fo-o.bar-', TYPE_MISMATCH ],
[ 'foo@fo-o.-', TYPE_MISMATCH ],
[ 'foo@fo--o', VALID ],
];
// Multiple values, we don't check e-mail validity, only multiple stuff.
var multipleValues = [
[ 'foo@bar.com, foo@bar.com', VALID ],
[ 'foo@bar.com,foo@bar.com', VALID ],
[ 'foo@bar.com,foo@bar.com,foo@bar.com', VALID ],
[ ' foo@bar.com , foo@bar.com ', VALID ],
[ '\tfoo@bar.com\t,\tfoo@bar.com\t', VALID ],
[ '\rfoo@bar.com\r,\rfoo@bar.com\r', VALID ],
[ '\nfoo@bar.com\n,\nfoo@bar.com\n', VALID ],
[ '\ffoo@bar.com\f,\ffoo@bar.com\f', VALID ],
[ '\t foo@bar.com\r,\nfoo@bar.com\f', VALID ],
[ 'foo@b,ar.com,foo@bar.com', TYPE_MISMATCH ],
[ 'foo@bar.com,foo@bar.com,', TYPE_MISMATCH ],
[ ' foo@bar.com , foo@bar.com , ', TYPE_MISMATCH ],
[ ',foo@bar.com,foo@bar.com', TYPE_MISMATCH ],
[ ',foo@bar.com,foo@bar.com', TYPE_MISMATCH ],
[ 'foo@bar.com,,,foo@bar.com', TYPE_MISMATCH ],
[ 'foo@bar.com;foo@bar.com', TYPE_MISMATCH ],
[ '<foo@bar.com>, <foo@bar.com>', TYPE_MISMATCH ],
[ 'foo@bar, foo@bar.com', VALID ],
[ 'foo@bar.com, foo', TYPE_MISMATCH ],
[ 'foo, foo@bar.com', TYPE_MISMATCH ],
];
/* Additional username checks. */
var legalCharacters = "abcdefghijklmnopqrstuvwxyz";
legalCharacters += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
legalCharacters += "0123456789";
legalCharacters += "!#$%&'*+-/=?^_`{|}~.";
// Add all username legal characters individually to the list.
for (c of legalCharacters) {
values.push([c + "@bar.com", VALID]);
}
// Add the concatenation of all legal characters too.
values.push([legalCharacters + "@bar.com", VALID]);
// Add username illegal characters, the same way.
var illegalCharacters = "()<>[]:;@\\, \t";
for (c of illegalCharacters) {
values.push([illegalCharacters + "@bar.com", TYPE_MISMATCH]);
}
/* Additional domain checks. */
legalCharacters = "abcdefghijklmnopqrstuvwxyz";
legalCharacters += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
legalCharacters += "0123456789";
// Add domain legal characters (except '.' and '-' because they are special).
for (c of legalCharacters) {
values.push(["foo@foo.bar" + c, VALID]);
}
// Add the concatenation of all legal characters too.
values.push(["foo@bar." + legalCharacters, VALID]);
// Add domain illegal characters.
illegalCharacters = "()<>[]:;@\\,!#$%&'*+/=?^_`{|}~ \t";
for (c of illegalCharacters) {
values.push(['foo@foo.ba' + c + 'r', TYPE_MISMATCH]);
}
values.forEach(function([value, valid, todo]) {
if (todo === true) {
email.value = value;
todo_is(email.validity.valid, true, "value should be valid");
} else {
testEmailAddress(email, value, false, valid);
}
});
multipleValues.forEach(function([value, valid]) {
testEmailAddress(email, value, true, valid);
});
// Make sure setting multiple changes the value.
email.multiple = false;
email.value = "foo@bar.com, foo@bar.com";
checkInvalidEmailAddress(email, TYPE_MISMATCH);
email.multiple = true;
checkValidEmailAddress(email);
</script>
</pre>
</body>
</html>
|