yii.validation.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**
  2. * Yii validation module.
  3. *
  4. * This JavaScript module provides the validation methods for the built-in validators.
  5. *
  6. * @link http://www.yiiframework.com/
  7. * @copyright Copyright (c) 2008 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. * @author Qiang Xue <[email protected]>
  10. * @since 2.0
  11. */
  12. yii.validation = (function ($) {
  13. var isEmpty = function (value, trim) {
  14. return value === null || value === undefined || value == []
  15. || value === '' || trim && $.trim(value) === '';
  16. };
  17. var addMessage = function (messages, message, value) {
  18. messages.push(message.replace(/\{value\}/g, value));
  19. };
  20. return {
  21. required: function (value, messages, options) {
  22. var valid = false;
  23. if (options.requiredValue === undefined) {
  24. if (options.strict && value !== undefined || !options.strict && !isEmpty(value, true)) {
  25. valid = true;
  26. }
  27. } else if (!options.strict && value == options.requiredValue || options.strict && value === options.requiredValue) {
  28. valid = true;
  29. }
  30. if (!valid) {
  31. addMessage(messages, options.message, value);
  32. }
  33. },
  34. boolean: function (value, messages, options) {
  35. if (options.skipOnEmpty && isEmpty(value)) {
  36. return;
  37. }
  38. var valid = !options.strict && (value == options.trueValue || value == options.falseValue)
  39. || options.strict && (value === options.trueValue || value === options.falseValue);
  40. if (!valid) {
  41. addMessage(messages, options.message, value);
  42. }
  43. },
  44. string: function (value, messages, options) {
  45. if (options.skipOnEmpty && isEmpty(value)) {
  46. return;
  47. }
  48. if (typeof value !== 'string') {
  49. addMessage(messages, options.message, value);
  50. return;
  51. }
  52. if (options.min !== undefined && value.length < options.min) {
  53. addMessage(messages, options.tooShort, value);
  54. }
  55. if (options.max !== undefined && value.length > options.max) {
  56. addMessage(messages, options.tooLong, value);
  57. }
  58. if (options.is !== undefined && value.length != options.is) {
  59. addMessage(messages, options.is, value);
  60. }
  61. },
  62. number: function (value, messages, options) {
  63. if (options.skipOnEmpty && isEmpty(value)) {
  64. return;
  65. }
  66. if (typeof value === 'string' && !value.match(options.pattern)) {
  67. addMessage(messages, options.message, value);
  68. return;
  69. }
  70. if (options.min !== undefined && value < options.min) {
  71. addMessage(messages, options.tooSmall, value);
  72. }
  73. if (options.max !== undefined && value > options.max) {
  74. addMessage(messages, options.tooBig, value);
  75. }
  76. },
  77. range: function (value, messages, options) {
  78. if (options.skipOnEmpty && isEmpty(value)) {
  79. return;
  80. }
  81. var valid = !options.not && $.inArray(value, options.range) > -1
  82. || options.not && $.inArray(value, options.range) == -1;
  83. if (!valid) {
  84. addMessage(messages, options.message, value);
  85. }
  86. },
  87. regularExpression: function (value, messages, options) {
  88. if (options.skipOnEmpty && isEmpty(value)) {
  89. return;
  90. }
  91. if (!options.not && !value.match(options.pattern) || options.not && value.match(options.pattern)) {
  92. addMessage(messages, options.message, value);
  93. }
  94. },
  95. email: function (value, messages, options) {
  96. if (options.skipOnEmpty && isEmpty(value)) {
  97. return;
  98. }
  99. var valid = true;
  100. if (options.enableIDN) {
  101. var regexp = /^(.*<?)(.*)@(.*)(>?)$/,
  102. matches = regexp.exec(value);
  103. if (matches === null) {
  104. valid = false;
  105. } else {
  106. value = matches[1] + punycode.toASCII(matches[2]) + '@' + punycode.toASCII(matches[3]) + matches[4];
  107. }
  108. }
  109. if (!valid || !(value.match(options.pattern) || (options.allowName && value.match(options.fullPattern)))) {
  110. addMessage(messages, options.message, value);
  111. }
  112. },
  113. url: function (value, messages, options) {
  114. if (options.skipOnEmpty && isEmpty(value)) {
  115. return;
  116. }
  117. if (options.defaultScheme && !value.match(/:\/\//)) {
  118. value = options.defaultScheme + '://' + value;
  119. }
  120. var valid = true;
  121. if (options.enableIDN) {
  122. var regexp = /^([^:]+):\/\/([^\/]+)(.*)$/,
  123. matches = regexp.exec(value);
  124. if (matches === null) {
  125. valid = false;
  126. } else {
  127. value = matches[1] + '://' + punycode.toASCII(matches[2]) + matches[3];
  128. }
  129. }
  130. if (!valid || !value.match(options.pattern)) {
  131. addMessage(messages, options.message, value);
  132. }
  133. },
  134. captcha: function (value, messages, options) {
  135. if (options.skipOnEmpty && isEmpty(value)) {
  136. return;
  137. }
  138. // CAPTCHA may be updated via AJAX and the updated hash is stored in body data
  139. var hash = $('body').data(options.hashKey);
  140. if (hash == null) {
  141. hash = options.hash;
  142. } else {
  143. hash = hash[options.caseSensitive ? 0 : 1];
  144. }
  145. var v = options.caseSensitive ? value : value.toLowerCase();
  146. for (var i = v.length - 1, h = 0; i >= 0; --i) {
  147. h += v.charCodeAt(i);
  148. }
  149. if (h != hash) {
  150. addMessage(messages, options.message, value);
  151. }
  152. },
  153. compare: function (value, messages, options) {
  154. if (options.skipOnEmpty && isEmpty(value)) {
  155. return;
  156. }
  157. var compareValue, valid = true;
  158. if (options.compareAttribute === undefined) {
  159. compareValue = options.compareValue;
  160. } else {
  161. compareValue = $('#' + options.compareAttribute).val();
  162. }
  163. switch (options.operator) {
  164. case '==':
  165. valid = value == compareValue;
  166. break;
  167. case '===':
  168. valid = value === compareValue;
  169. break;
  170. case '!=':
  171. valid = value != compareValue;
  172. break;
  173. case '!==':
  174. valid = value !== compareValue;
  175. break;
  176. case '>':
  177. valid = value > compareValue;
  178. break;
  179. case '>=':
  180. valid = value >= compareValue;
  181. break;
  182. case '<':
  183. valid = value < compareValue;
  184. break;
  185. case '<=':
  186. valid = value <= compareValue;
  187. break;
  188. default:
  189. valid = false;
  190. break;
  191. }
  192. if (!valid) {
  193. addMessage(messages, options.message, value);
  194. }
  195. }
  196. };
  197. })(jQuery);