validation.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. (function (Misago) {
  2. 'use strict';
  3. var EMAIL = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
  4. var USERNAME = new RegExp('^[0-9a-z]+$', 'i');
  5. // Validators namespace
  6. Misago.validators = {
  7. required: function() {
  8. return function(value) {
  9. if ($.trim(value).length === 0) {
  10. return gettext("This field is required.");
  11. }
  12. };
  13. },
  14. email: function(message) {
  15. return function(value) {
  16. if (!EMAIL.test(value)) {
  17. return message || gettext('Enter a valid email address.');
  18. }
  19. };
  20. },
  21. minLength: function(limit_value, message) {
  22. return function(value) {
  23. var returnMessage = '';
  24. var length = $.trim(value).length;
  25. if (length < limit_value) {
  26. if (message) {
  27. returnMessage = message(limit_value, length);
  28. } else {
  29. returnMessage = ngettext(
  30. "Ensure this value has at least %(limit_value)s character (it has %(show_value)s).",
  31. "Ensure this value has at least %(limit_value)s characters (it has %(show_value)s).",
  32. limit_value);
  33. }
  34. return interpolate(returnMessage, {
  35. limit_value: limit_value,
  36. show_value: length
  37. }, true);
  38. }
  39. };
  40. },
  41. maxLength: function(limit_value, message) {
  42. return function(value) {
  43. var returnMessage = '';
  44. var length = $.trim(value).length;
  45. if (length > limit_value) {
  46. if (message) {
  47. returnMessage = message(limit_value, length);
  48. } else {
  49. returnMessage = ngettext(
  50. "Ensure this value has at most %(limit_value)s character (it has %(show_value)s).",
  51. "Ensure this value has at most %(limit_value)s characters (it has %(show_value)s).",
  52. limit_value);
  53. }
  54. return interpolate(returnMessage, {
  55. limit_value: limit_value,
  56. show_value: length
  57. }, true);
  58. }
  59. };
  60. },
  61. usernameMinLength: function(settings) {
  62. var message = function(limit_value) {
  63. return ngettext(
  64. "Username must be at least %(limit_value)s character long.",
  65. "Username must be at least %(limit_value)s characters long.",
  66. limit_value);
  67. };
  68. return this.minLength(settings.username_length_min, message);
  69. },
  70. usernameMaxLength: function(settings) {
  71. var message = function(limit_value) {
  72. return ngettext(
  73. "Username cannot be longer than %(limit_value)s character.",
  74. "Username cannot be longer than %(limit_value)s characters.",
  75. limit_value);
  76. };
  77. return this.maxLength(settings.username_length_max, message);
  78. },
  79. usernameContent: function() {
  80. return function(value) {
  81. if (!USERNAME.test($.trim(value))) {
  82. return gettext("Username can only contain latin alphabet letters and digits.");
  83. }
  84. };
  85. },
  86. passwordMinLength: function(settings) {
  87. var message = function(limit_value) {
  88. return ngettext(
  89. "Valid password must be at least %(limit_value)s character long.",
  90. "Valid password must be at least %(limit_value)s characters long.",
  91. limit_value);
  92. };
  93. return this.minLength(settings.password_length_min, message);
  94. }
  95. };
  96. var validateField = function(value, validators) {
  97. var result = Misago.validators.required()(value);
  98. var errors = [];
  99. if (result) {
  100. return [result];
  101. } else {
  102. for (var i in validators) {
  103. result = validators[i](value);
  104. if (result) {
  105. errors.push(result);
  106. }
  107. }
  108. }
  109. return errors.length ? errors : true;
  110. };
  111. var validateForm = function(form) {
  112. var errors = {};
  113. var value = null;
  114. var isValid = true;
  115. for (var key in form.validation) {
  116. if (form.validation.hasOwnProperty(key)) {
  117. value = form[key]();
  118. errors[key] = validateField(form[key](), form.validation[key]);
  119. if (errors[key] !== true) {
  120. isValid = false;
  121. }
  122. }
  123. }
  124. form.errors = errors;
  125. return isValid;
  126. };
  127. var validate = function(form, name) {
  128. if (name) {
  129. return function(value) {
  130. var errors = null;
  131. if (typeof value !== 'undefined') {
  132. errors = validateField(value, form.validation[name]);
  133. if (errors) {
  134. if (!form.errors) {
  135. form.errors = {};
  136. }
  137. form.errors[name] = errors;
  138. }
  139. form[name](value);
  140. return form[name](value);
  141. } else {
  142. return form[name]();
  143. }
  144. };
  145. } else {
  146. return validateForm(form);
  147. }
  148. };
  149. Misago.addService('validate', {
  150. factory: function() {
  151. return validate;
  152. }
  153. });
  154. }(Misago.prototype));