PageRenderTime 332ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/browserid/static/dialog/resources/validation.js

https://github.com/catlee/browserid
JavaScript | 122 lines | 61 code | 23 blank | 38 comment | 13 complexity | 6c2b1b22a9826e23b0d21f9e7bf1cd48 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. // 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. return /^[\w.!#$%&'*+\-/=?\^`{|}~]+@[a-z0-9-]+(\.[a-z0-9-]+)+$/.test(address);
  44. };
  45. function validateEmail(email) {
  46. var valid = false;
  47. if (!email) {
  48. tooltip.showTooltip("#email_required");
  49. }
  50. else if (!bid.verifyEmail(email)) {
  51. tooltip.showTooltip("#email_format");
  52. }
  53. else {
  54. valid = true;
  55. }
  56. return valid;
  57. }
  58. function validateEmailAndPassword(email, password) {
  59. var valid = validateEmail(email);
  60. if (valid) {
  61. valid = passwordExists(password);
  62. }
  63. return valid;
  64. }
  65. function passwordExists(password) {
  66. var valid = !!password;
  67. if (!valid) {
  68. tooltip.showTooltip("#password_required");
  69. }
  70. return valid;
  71. }
  72. function passwordLength(password) {
  73. var valid = password && (password.length >= 8);
  74. if(!valid) {
  75. tooltip.showTooltip("#password_too_short");
  76. }
  77. return valid;
  78. }
  79. function validationPasswordExists(vpass) {
  80. var valid = !!vpass;
  81. if(!valid) {
  82. tooltip.showTooltip("#vpassword_required");
  83. }
  84. return valid;
  85. }
  86. function passwordAndValidationPassword(pass, vpass) {
  87. var valid = passwordExists(pass) && passwordLength(pass) && validationPasswordExists(vpass);
  88. if (valid && pass !== vpass) {
  89. valid = false;
  90. tooltip.showTooltip("#passwords_no_match");
  91. }
  92. return valid;
  93. }
  94. return {
  95. email: validateEmail,
  96. emailAndPassword: validateEmailAndPassword,
  97. passwordAndValidationPassword: passwordAndValidationPassword
  98. };
  99. }());