PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/ozten/browserid
JavaScript | 101 lines | 68 code | 24 blank | 9 comment | 20 complexity | f0457c523c16cbdd773e99483f2c8dfd 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 passwordLength(password) {
  50. var valid = password && (password.length >= bid.PASSWORD_MIN_LENGTH && password.length <= bid.PASSWORD_MAX_LENGTH);
  51. if(!valid) {
  52. tooltip.showTooltip("#password_length");
  53. }
  54. return valid;
  55. }
  56. function validationPasswordExists(vpass) {
  57. var valid = !!vpass;
  58. if(!valid) {
  59. tooltip.showTooltip("#vpassword_required");
  60. }
  61. return valid;
  62. }
  63. function passwordAndValidationPassword(pass, vpass) {
  64. var valid = passwordExists(pass) && passwordLength(pass) && validationPasswordExists(vpass);
  65. if (valid && pass !== vpass) {
  66. valid = false;
  67. tooltip.showTooltip("#passwords_no_match");
  68. }
  69. return valid;
  70. }
  71. return {
  72. email: validateEmail,
  73. password: passwordExists,
  74. emailAndPassword: validateEmailAndPassword,
  75. passwordAndValidationPassword: passwordAndValidationPassword
  76. };
  77. }());