PageRenderTime 59ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/resources/static/common/js/validation.js

https://github.com/mozilla/persona
JavaScript | 135 lines | 92 code | 31 blank | 12 comment | 25 complexity | fc52f064aeb9d60573037d13f0501c5a MD5 | raw file
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. BrowserID.Validation = (function() {
  5. var bid = BrowserID,
  6. tooltip = bid.Tooltip;
  7. bid.verifyEmail = function(address) {
  8. if (typeof(address) !== "string")
  9. return false;
  10. // Original gotten from http://blog.gerv.net/2011/05/html5_email_address_regexp/
  11. // changed the requirement that there must be a ldh-str because BrowserID
  12. // is only used on internet based networks.
  13. var parts = address.split("@");
  14. return (/^[\w.!#$%&'*+\-\/=?\^`{|}~]+@[a-z\d\-]+(\.[a-z\d\-]+)+$/i).test(address)
  15. // total address allwed to be 254 bytes long
  16. && address.length <= 254
  17. // local side only allowed to be 64 bytes long
  18. && parts[0] && parts[0].length <= 64
  19. // domain side allowed to be up to 253 bytes long
  20. && parts[1] && parts[1].length <= 253;
  21. };
  22. function validateEmail(email) {
  23. var valid = false;
  24. if (!email) {
  25. tooltip.showTooltip("#email_required");
  26. }
  27. else if (!bid.verifyEmail(email)) {
  28. tooltip.showTooltip("#email_format");
  29. }
  30. else {
  31. valid = true;
  32. }
  33. return valid;
  34. }
  35. function validateEmailAndPassword(email, password) {
  36. var valid = validateEmail(email);
  37. if (valid) {
  38. valid = passwordExists(password);
  39. }
  40. return valid;
  41. }
  42. function passwordExists(password) {
  43. var valid = !!password;
  44. if (!valid) {
  45. tooltip.showTooltip("#password_required");
  46. }
  47. return valid;
  48. }
  49. function authenticationPassword(password) {
  50. var valid = passwordExists(password)
  51. && passwordLengthForAuthenticationPassword(password);
  52. return valid;
  53. }
  54. function passwordLengthForAuthenticationPassword(password) {
  55. var valid = password
  56. && (password.length >= bid.PASSWORD_MIN_LENGTH
  57. && password.length <= bid.PASSWORD_MAX_LENGTH);
  58. if (!valid) {
  59. // If the authentication password is out of range, don't even bother to
  60. // send the password to the backend. Fail quickly without wasting
  61. // electrons.
  62. tooltip.showTooltip("#cannot_authenticate");
  63. }
  64. return valid;
  65. }
  66. function passwordLengthForNewPassword(password) {
  67. var valid = password
  68. && (password.length >= bid.PASSWORD_MIN_LENGTH
  69. && password.length <= bid.PASSWORD_MAX_LENGTH);
  70. if (!valid) {
  71. tooltip.showTooltip("#password_length");
  72. }
  73. return valid;
  74. }
  75. function validationPasswordExists(vpass) {
  76. var valid = !!vpass;
  77. if (!valid) {
  78. tooltip.showTooltip("#vpassword_required");
  79. }
  80. return valid;
  81. }
  82. function newPassword(pass) {
  83. var valid = passwordExists(pass)
  84. && passwordLengthForNewPassword(pass);
  85. return valid;
  86. }
  87. function passwordAndValidationPassword(pass, vpass) {
  88. var valid = passwordExists(pass)
  89. && passwordLengthForNewPassword(pass)
  90. && validationPasswordExists(vpass);
  91. if (valid && pass !== vpass) {
  92. valid = false;
  93. tooltip.showTooltip("#passwords_no_match");
  94. }
  95. return valid;
  96. }
  97. return {
  98. email: validateEmail,
  99. password: authenticationPassword,
  100. emailAndPassword: validateEmailAndPassword,
  101. newPassword: newPassword,
  102. passwordAndValidationPassword: passwordAndValidationPassword
  103. };
  104. }());