test_input_email.html 9.1 KB

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