/resources/static/shared/validation.js

https://github.com/fetep/browserid · JavaScript · 130 lines · 65 code · 24 blank · 41 comment · 18 complexity · dba3d883ef425423d431e0d9c189f595 MD5 · raw file

  1. /*globals BrowserID: true */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is Mozilla BrowserID.
  16. *
  17. * The Initial Developer of the Original Code is Mozilla.
  18. * Portions created by the Initial Developer are Copyright (C) 2011
  19. * the Initial Developer. All Rights Reserved.
  20. *
  21. * Contributor(s):
  22. *
  23. * Alternatively, the contents of this file may be used under the terms of
  24. * either the GNU General Public License Version 2 or later (the "GPL"), or
  25. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26. * in which case the provisions of the GPL or the LGPL are applicable instead
  27. * of those above. If you wish to allow use of your version of this file only
  28. * under the terms of either the GPL or the LGPL, and not to allow others to
  29. * use your version of this file under the terms of the MPL, indicate your
  30. * decision by deleting the provisions above and replace them with the notice
  31. * and other provisions required by the GPL or the LGPL. If you do not delete
  32. * the provisions above, a recipient may use your version of this file under
  33. * the terms of any one of the MPL, the GPL or the LGPL.
  34. *
  35. * ***** END LICENSE BLOCK ***** */
  36. BrowserID.Validation = (function() {
  37. var bid = BrowserID,
  38. tooltip = bid.Tooltip;
  39. bid.verifyEmail = function(address) {
  40. // Original gotten from http://blog.gerv.net/2011/05/html5_email_address_regexp/
  41. // changed the requirement that there must be a ldh-str because BrowserID
  42. // is only used on internet based networks.
  43. var parts = address.split("@");
  44. return /^[\w.!#$%&'*+\-/=?\^`{|}~]+@[a-z\d-]+(\.[a-z\d-]+)+$/i.test(address)
  45. // total address allwed to be 254 bytes long
  46. && address.length <= 254
  47. // local side only allowed to be 64 bytes long
  48. && parts[0] && parts[0].length <= 64
  49. // domain side allowed to be up to 253 bytes long
  50. && parts[1] && parts[1].length <= 253;
  51. };
  52. function validateEmail(email) {
  53. var valid = false;
  54. if (!email) {
  55. tooltip.showTooltip("#email_required");
  56. }
  57. else if (!bid.verifyEmail(email)) {
  58. tooltip.showTooltip("#email_format");
  59. }
  60. else {
  61. valid = true;
  62. }
  63. return valid;
  64. }
  65. function validateEmailAndPassword(email, password) {
  66. var valid = validateEmail(email);
  67. if (valid) {
  68. valid = passwordExists(password);
  69. }
  70. return valid;
  71. }
  72. function passwordExists(password) {
  73. var valid = !!password;
  74. if (!valid) {
  75. tooltip.showTooltip("#password_required");
  76. }
  77. return valid;
  78. }
  79. function passwordLength(password) {
  80. var valid = password && (password.length >= 8);
  81. if(!valid) {
  82. tooltip.showTooltip("#password_too_short");
  83. }
  84. return valid;
  85. }
  86. function validationPasswordExists(vpass) {
  87. var valid = !!vpass;
  88. if(!valid) {
  89. tooltip.showTooltip("#vpassword_required");
  90. }
  91. return valid;
  92. }
  93. function passwordAndValidationPassword(pass, vpass) {
  94. var valid = passwordExists(pass) && passwordLength(pass) && validationPasswordExists(vpass);
  95. if (valid && pass !== vpass) {
  96. valid = false;
  97. tooltip.showTooltip("#passwords_no_match");
  98. }
  99. return valid;
  100. }
  101. return {
  102. email: validateEmail,
  103. emailAndPassword: validateEmailAndPassword,
  104. passwordAndValidationPassword: passwordAndValidationPassword
  105. };
  106. }());