yii.validation.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 <qiang.xue@gmail.com>
  10. * @since 2.0
  11. */
  12. yii.validation = (function ($) {
  13. var pub = {
  14. isEmpty: function (value) {
  15. return value === null || value === undefined || ($.isArray(value) && value.length === 0) || value === '';
  16. },
  17. addMessage: function (messages, message, value) {
  18. messages.push(message.replace(/\{value\}/g, value));
  19. },
  20. required: function (value, messages, options) {
  21. var valid = false;
  22. if (options.requiredValue === undefined) {
  23. var isString = typeof value == 'string' || value instanceof String;
  24. if (options.strict && value !== undefined || !options.strict && !pub.isEmpty(isString ? $.trim(value) : value)) {
  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. pub.addMessage(messages, options.message, value);
  32. }
  33. },
  34. // "boolean" is a reserved keyword in older versions of ES so it's quoted for IE < 9 support
  35. 'boolean': function (value, messages, options) {
  36. if (options.skipOnEmpty && pub.isEmpty(value)) {
  37. return;
  38. }
  39. var valid = !options.strict && (value == options.trueValue || value == options.falseValue)
  40. || options.strict && (value === options.trueValue || value === options.falseValue);
  41. if (!valid) {
  42. pub.addMessage(messages, options.message, value);
  43. }
  44. },
  45. string: function (value, messages, options) {
  46. if (options.skipOnEmpty && pub.isEmpty(value)) {
  47. return;
  48. }
  49. if (typeof value !== 'string') {
  50. pub.addMessage(messages, options.message, value);
  51. return;
  52. }
  53. if (options.is !== undefined && value.length != options.is) {
  54. pub.addMessage(messages, options.notEqual, value);
  55. return;
  56. }
  57. if (options.min !== undefined && value.length < options.min) {
  58. pub.addMessage(messages, options.tooShort, value);
  59. }
  60. if (options.max !== undefined && value.length > options.max) {
  61. pub.addMessage(messages, options.tooLong, value);
  62. }
  63. },
  64. file: function (attribute, messages, options) {
  65. var files = getUploadedFiles(attribute, messages, options);
  66. $.each(files, function (i, file) {
  67. validateFile(file, messages, options);
  68. });
  69. },
  70. image: function (attribute, messages, options, deferredList) {
  71. var files = getUploadedFiles(attribute, messages, options);
  72. $.each(files, function (i, file) {
  73. validateFile(file, messages, options);
  74. // Skip image validation if FileReader API is not available
  75. if (typeof FileReader === "undefined") {
  76. return;
  77. }
  78. var deferred = $.Deferred();
  79. pub.validateImage(file, messages, options, deferred, new FileReader(), new Image());
  80. deferredList.push(deferred);
  81. });
  82. },
  83. validateImage: function (file, messages, options, deferred, fileReader, image) {
  84. image.onload = function() {
  85. validateImageSize(file, image, messages, options);
  86. deferred.resolve();
  87. };
  88. image.onerror = function () {
  89. messages.push(options.notImage.replace(/\{file\}/g, file.name));
  90. deferred.resolve();
  91. };
  92. fileReader.onload = function () {
  93. image.src = this.result;
  94. };
  95. // Resolve deferred if there was error while reading data
  96. fileReader.onerror = function () {
  97. deferred.resolve();
  98. };
  99. fileReader.readAsDataURL(file);
  100. },
  101. number: function (value, messages, options) {
  102. if (options.skipOnEmpty && pub.isEmpty(value)) {
  103. return;
  104. }
  105. if (typeof value === 'string' && !options.pattern.test(value)) {
  106. pub.addMessage(messages, options.message, value);
  107. return;
  108. }
  109. if (options.min !== undefined && value < options.min) {
  110. pub.addMessage(messages, options.tooSmall, value);
  111. }
  112. if (options.max !== undefined && value > options.max) {
  113. pub.addMessage(messages, options.tooBig, value);
  114. }
  115. },
  116. range: function (value, messages, options) {
  117. if (options.skipOnEmpty && pub.isEmpty(value)) {
  118. return;
  119. }
  120. if (!options.allowArray && $.isArray(value)) {
  121. pub.addMessage(messages, options.message, value);
  122. return;
  123. }
  124. var inArray = true;
  125. $.each($.isArray(value) ? value : [value], function (i, v) {
  126. if ($.inArray(v, options.range) == -1) {
  127. inArray = false;
  128. return false;
  129. } else {
  130. return true;
  131. }
  132. });
  133. if (options.not === undefined) {
  134. options.not = false;
  135. }
  136. if (options.not === inArray) {
  137. pub.addMessage(messages, options.message, value);
  138. }
  139. },
  140. regularExpression: function (value, messages, options) {
  141. if (options.skipOnEmpty && pub.isEmpty(value)) {
  142. return;
  143. }
  144. if (!options.not && !options.pattern.test(value) || options.not && options.pattern.test(value)) {
  145. pub.addMessage(messages, options.message, value);
  146. }
  147. },
  148. email: function (value, messages, options) {
  149. if (options.skipOnEmpty && pub.isEmpty(value)) {
  150. return;
  151. }
  152. var valid = true,
  153. regexp = /^((?:"?([^"]*)"?\s)?)(?:\s+)?(?:(<?)((.+)@([^>]+))(>?))$/,
  154. matches = regexp.exec(value);
  155. if (matches === null) {
  156. valid = false;
  157. } else {
  158. var localPart = matches[5],
  159. domain = matches[6];
  160. if (options.enableIDN) {
  161. localPart = punycode.toASCII(localPart);
  162. domain = punycode.toASCII(domain);
  163. value = matches[1] + matches[3] + localPart + '@' + domain + matches[7];
  164. }
  165. if (localPart.length > 64) {
  166. valid = false;
  167. } else if ((localPart + '@' + domain).length > 254) {
  168. valid = false;
  169. } else {
  170. valid = options.pattern.test(value) || (options.allowName && options.fullPattern.test(value));
  171. }
  172. }
  173. if (!valid) {
  174. pub.addMessage(messages, options.message, value);
  175. }
  176. },
  177. url: function (value, messages, options) {
  178. if (options.skipOnEmpty && pub.isEmpty(value)) {
  179. return;
  180. }
  181. if (options.defaultScheme && !/:\/\//.test(value)) {
  182. value = options.defaultScheme + '://' + value;
  183. }
  184. var valid = true;
  185. if (options.enableIDN) {
  186. var matches = /^([^:]+):\/\/([^\/]+)(.*)$/.exec(value);
  187. if (matches === null) {
  188. valid = false;
  189. } else {
  190. value = matches[1] + '://' + punycode.toASCII(matches[2]) + matches[3];
  191. }
  192. }
  193. if (!valid || !options.pattern.test(value)) {
  194. pub.addMessage(messages, options.message, value);
  195. }
  196. },
  197. trim: function ($form, attribute, options) {
  198. var $input = $form.find(attribute.input);
  199. var value = $input.val();
  200. if (!options.skipOnEmpty || !pub.isEmpty(value)) {
  201. value = $.trim(value);
  202. $input.val(value);
  203. }
  204. return value;
  205. },
  206. captcha: function (value, messages, options) {
  207. if (options.skipOnEmpty && pub.isEmpty(value)) {
  208. return;
  209. }
  210. // CAPTCHA may be updated via AJAX and the updated hash is stored in body data
  211. var hash = $('body').data(options.hashKey);
  212. hash = hash == null ? options.hash : hash[options.caseSensitive ? 0 : 1];
  213. var v = options.caseSensitive ? value : value.toLowerCase();
  214. for (var i = v.length - 1, h = 0; i >= 0; --i) {
  215. h += v.charCodeAt(i);
  216. }
  217. if (h != hash) {
  218. pub.addMessage(messages, options.message, value);
  219. }
  220. },
  221. compare: function (value, messages, options) {
  222. if (options.skipOnEmpty && pub.isEmpty(value)) {
  223. return;
  224. }
  225. var compareValue,
  226. valid = true;
  227. if (options.compareAttribute === undefined) {
  228. compareValue = options.compareValue;
  229. } else {
  230. compareValue = $('#' + options.compareAttribute).val();
  231. }
  232. if (options.type === 'number') {
  233. value = parseFloat(value);
  234. compareValue = parseFloat(compareValue);
  235. }
  236. switch (options.operator) {
  237. case '==':
  238. valid = value == compareValue;
  239. break;
  240. case '===':
  241. valid = value === compareValue;
  242. break;
  243. case '!=':
  244. valid = value != compareValue;
  245. break;
  246. case '!==':
  247. valid = value !== compareValue;
  248. break;
  249. case '>':
  250. valid = value > compareValue;
  251. break;
  252. case '>=':
  253. valid = value >= compareValue;
  254. break;
  255. case '<':
  256. valid = value < compareValue;
  257. break;
  258. case '<=':
  259. valid = value <= compareValue;
  260. break;
  261. default:
  262. valid = false;
  263. break;
  264. }
  265. if (!valid) {
  266. pub.addMessage(messages, options.message, value);
  267. }
  268. },
  269. ip: function (value, messages, options) {
  270. if (options.skipOnEmpty && pub.isEmpty(value)) {
  271. return;
  272. }
  273. var negation = null,
  274. cidr = null,
  275. matches = new RegExp(options.ipParsePattern).exec(value);
  276. if (matches) {
  277. negation = matches[1] || null;
  278. value = matches[2];
  279. cidr = matches[4] || null;
  280. }
  281. if (options.subnet === true && cidr === null) {
  282. pub.addMessage(messages, options.messages.noSubnet, value);
  283. return;
  284. }
  285. if (options.subnet === false && cidr !== null) {
  286. pub.addMessage(messages, options.messages.hasSubnet, value);
  287. return;
  288. }
  289. if (options.negation === false && negation !== null) {
  290. pub.addMessage(messages, options.messages.message, value);
  291. return;
  292. }
  293. var ipVersion = value.indexOf(':') === -1 ? 4 : 6;
  294. if (ipVersion == 6) {
  295. if (!(new RegExp(options.ipv6Pattern)).test(value)) {
  296. pub.addMessage(messages, options.messages.message, value);
  297. }
  298. if (!options.ipv6) {
  299. pub.addMessage(messages, options.messages.ipv6NotAllowed, value);
  300. }
  301. } else {
  302. if (!(new RegExp(options.ipv4Pattern)).test(value)) {
  303. pub.addMessage(messages, options.messages.message, value);
  304. }
  305. if (!options.ipv4) {
  306. pub.addMessage(messages, options.messages.ipv4NotAllowed, value);
  307. }
  308. }
  309. }
  310. };
  311. function getUploadedFiles(attribute, messages, options) {
  312. // Skip validation if File API is not available
  313. if (typeof File === "undefined") {
  314. return [];
  315. }
  316. var files = $(attribute.input, attribute.$form).get(0).files;
  317. if (!files) {
  318. messages.push(options.message);
  319. return [];
  320. }
  321. if (files.length === 0) {
  322. if (!options.skipOnEmpty) {
  323. messages.push(options.uploadRequired);
  324. }
  325. return [];
  326. }
  327. if (options.maxFiles && options.maxFiles < files.length) {
  328. messages.push(options.tooMany);
  329. return [];
  330. }
  331. return files;
  332. }
  333. function validateFile(file, messages, options) {
  334. if (options.extensions && options.extensions.length > 0) {
  335. var index = file.name.lastIndexOf('.');
  336. var ext = !~index ? '' : file.name.substr(index + 1, file.name.length).toLowerCase();
  337. if (!~options.extensions.indexOf(ext)) {
  338. messages.push(options.wrongExtension.replace(/\{file\}/g, file.name));
  339. }
  340. }
  341. if (options.mimeTypes && options.mimeTypes.length > 0) {
  342. if (!validateMimeType(options.mimeTypes, file.type)) {
  343. messages.push(options.wrongMimeType.replace(/\{file\}/g, file.name));
  344. }
  345. }
  346. if (options.maxSize && options.maxSize < file.size) {
  347. messages.push(options.tooBig.replace(/\{file\}/g, file.name));
  348. }
  349. if (options.minSize && options.minSize > file.size) {
  350. messages.push(options.tooSmall.replace(/\{file\}/g, file.name));
  351. }
  352. }
  353. function validateMimeType(mimeTypes, fileType) {
  354. for (var i = 0, len = mimeTypes.length; i < len; i++) {
  355. if (new RegExp(mimeTypes[i]).test(fileType)) {
  356. return true;
  357. }
  358. }
  359. return false;
  360. }
  361. function validateImageSize(file, image, messages, options) {
  362. if (options.minWidth && image.width < options.minWidth) {
  363. messages.push(options.underWidth.replace(/\{file\}/g, file.name));
  364. }
  365. if (options.maxWidth && image.width > options.maxWidth) {
  366. messages.push(options.overWidth.replace(/\{file\}/g, file.name));
  367. }
  368. if (options.minHeight && image.height < options.minHeight) {
  369. messages.push(options.underHeight.replace(/\{file\}/g, file.name));
  370. }
  371. if (options.maxHeight && image.height > options.maxHeight) {
  372. messages.push(options.overHeight.replace(/\{file\}/g, file.name));
  373. }
  374. }
  375. return pub;
  376. })(jQuery);