PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/resources/static/shared/validation.js

https://github.com/tmd10470/browserid
JavaScript | 100 lines | 66 code | 24 blank | 10 comment | 19 complexity | 102605e7e959785183b8d7a160e56db4 MD5 | raw file
  1. /*globals BrowserID: true */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. BrowserID.Validation = (function() {
  6. var bid = BrowserID,
  7. tooltip = bid.Tooltip;
  8. bid.verifyEmail = function(address) {
  9. // Original gotten from http://blog.gerv.net/2011/05/html5_email_address_regexp/
  10. // changed the requirement that there must be a ldh-str because BrowserID
  11. // is only used on internet based networks.
  12. var parts = address.split("@");
  13. return /^[\w.!#$%&'*+\-/=?\^`{|}~]+@[a-z\d-]+(\.[a-z\d-]+)+$/i.test(address)
  14. // total address allwed to be 254 bytes long
  15. && address.length <= 254
  16. // local side only allowed to be 64 bytes long
  17. && parts[0] && parts[0].length <= 64
  18. // domain side allowed to be up to 253 bytes long
  19. && parts[1] && parts[1].length <= 253;
  20. };
  21. function validateEmail(email) {
  22. var valid = false;
  23. if (!email) {
  24. tooltip.showTooltip("#email_required");
  25. }
  26. else if (!bid.verifyEmail(email)) {
  27. tooltip.showTooltip("#email_format");
  28. }
  29. else {
  30. valid = true;
  31. }
  32. return valid;
  33. }
  34. function validateEmailAndPassword(email, password) {
  35. var valid = validateEmail(email);
  36. if (valid) {
  37. valid = passwordExists(password);
  38. }
  39. return valid;
  40. }
  41. function passwordExists(password) {
  42. var valid = !!password;
  43. if (!valid) {
  44. tooltip.showTooltip("#password_required");
  45. }
  46. return valid;
  47. }
  48. function passwordLength(password) {
  49. var valid = password && (password.length >= 8 && password.length <= 80);
  50. if(!valid) {
  51. tooltip.showTooltip("#password_length");
  52. }
  53. return valid;
  54. }
  55. function validationPasswordExists(vpass) {
  56. var valid = !!vpass;
  57. if(!valid) {
  58. tooltip.showTooltip("#vpassword_required");
  59. }
  60. return valid;
  61. }
  62. function passwordAndValidationPassword(pass, vpass) {
  63. var valid = passwordExists(pass) && passwordLength(pass) && validationPasswordExists(vpass);
  64. if (valid && pass !== vpass) {
  65. valid = false;
  66. tooltip.showTooltip("#passwords_no_match");
  67. }
  68. return valid;
  69. }
  70. return {
  71. email: validateEmail,
  72. password: passwordExists,
  73. emailAndPassword: validateEmailAndPassword,
  74. passwordAndValidationPassword: passwordAndValidationPassword
  75. };
  76. }());