bootstrapValidator.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. /**
  2. * BootstrapValidator v0.1.1 (http://github.com/nghuuphuoc/bootstrapvalidator)
  3. *
  4. * A jQuery plugin to validate form fields. Use with Bootstrap 3
  5. *
  6. * @author Nguyen Huu Phuoc <phuoc@huuphuoc.me>
  7. * @copyright (c) 2013 Nguyen Huu Phuoc
  8. * @license MIT
  9. */
  10. (function($) {
  11. var BootstrapValidator = function(form, options) {
  12. this.$form = $(form);
  13. this.options = $.extend({}, BootstrapValidator.DEFAULT_OPTIONS, options);
  14. this.invalidFields = {};
  15. this.xhrRequests = {};
  16. this.numPendingRequests = null;
  17. this.formSubmited = false;
  18. this._init();
  19. };
  20. // The default options
  21. BootstrapValidator.DEFAULT_OPTIONS = {
  22. // The form CSS class
  23. elementClass: 'bootstrap-validator-form',
  24. // Default invalid message
  25. message: 'This value is not valid',
  26. // The submit buttons selector
  27. // These buttons will be disabled when the form input are invalid
  28. submitButtons: 'button[type="submit"]',
  29. // Map the field name with validator rules
  30. fields: null
  31. };
  32. BootstrapValidator.prototype = {
  33. constructor: BootstrapValidator,
  34. /**
  35. * Init form
  36. */
  37. _init: function() {
  38. if (this.options.fields == null) {
  39. return;
  40. }
  41. var that = this;
  42. this.$form
  43. // Disable client side validation in HTML 5
  44. .attr('novalidate', 'novalidate')
  45. .addClass(this.options.elementClass)
  46. .on('submit', function(e) {
  47. that.formSubmited = true;
  48. if (that.options.fields) {
  49. for (var field in that.options.fields) {
  50. if (that.numPendingRequests > 0 || that.numPendingRequests == null) {
  51. // Check if the field is valid
  52. var $field = that.getFieldElement(field);
  53. if ($field.data('bootstrapValidator.isValid') !== true) {
  54. that.validateField(field);
  55. }
  56. }
  57. }
  58. if (!that.isValid()) {
  59. that.$form.find(that.options.submitButtons).attr('disabled', 'disabled');
  60. e.preventDefault();
  61. }
  62. }
  63. });
  64. for (var field in this.options.fields) {
  65. this._initField(field);
  66. }
  67. },
  68. /**
  69. * Init field
  70. *
  71. * @param {String} field The field name
  72. */
  73. _initField: function(field) {
  74. if (this.options.fields[field] == null || this.options.fields[field].validators == null) {
  75. return;
  76. }
  77. var $field = this.getFieldElement(field);
  78. if (null == $field) {
  79. return;
  80. }
  81. // Create a help block element for showing the error
  82. var that = this,
  83. $parent = $field.parents('.form-group'),
  84. helpBlock = $parent.find('.help-block');
  85. if (helpBlock.length == 0) {
  86. var $small = $('<small/>').addClass('help-block').appendTo($parent);
  87. $field.data('bootstrapValidator.error', $small);
  88. // Calculate the number of columns of the label/field element
  89. // Then set offset to the help block element
  90. var label, cssClasses, offset;
  91. if (label = $parent.find('label').get(0)) {
  92. cssClasses = $(label).attr('class').split(' ');
  93. for (var i = 0; i < cssClasses.length; i++) {
  94. if (cssClasses[i].substr(0, 7) == 'col-lg-') {
  95. offset = cssClasses[i].substr(7);
  96. break;
  97. }
  98. }
  99. } else {
  100. cssClasses = $parent.children().eq(0).attr('class').split(' ');
  101. for (var i = 0; i < cssClasses.length; i++) {
  102. if (cssClasses[i].substr(0, 14) == 'col-lg-offset-') {
  103. offset = cssClasses[i].substr(14);
  104. break;
  105. }
  106. }
  107. }
  108. if (offset) {
  109. $small.addClass('col-lg-offset-' + offset).addClass('col-lg-' + parseInt(12 - offset));
  110. }
  111. } else {
  112. $field.data('bootstrapValidator.error', helpBlock.eq(0));
  113. }
  114. var type = $field.attr('type'),
  115. event = ('checkbox' == type || 'radio' == type || 'SELECT' == $field.get(0).tagName) ? 'change' : 'keyup';
  116. $field.on(event, function() {
  117. that.formSubmited = false;
  118. that.validateField(field);
  119. });
  120. },
  121. /**
  122. * Get field element
  123. *
  124. * @param {String} field The field name
  125. * @returns {jQuery}
  126. */
  127. getFieldElement: function(field) {
  128. var fields = this.$form.find('[name="' + field + '"]');
  129. return (fields.length == 0) ? null : $(fields[0]);
  130. },
  131. /**
  132. * Validate given field
  133. *
  134. * @param {String} field The field name
  135. */
  136. validateField: function(field) {
  137. var $field = this.getFieldElement(field);
  138. if (null == $field) {
  139. // Return if cannot find the field with given name
  140. return;
  141. }
  142. var that = this,
  143. validators = that.options.fields[field].validators;
  144. for (var validatorName in validators) {
  145. if (!$.fn.bootstrapValidator.validators[validatorName]) {
  146. continue;
  147. }
  148. var isValid = $.fn.bootstrapValidator.validators[validatorName].validate(that, $field, validators[validatorName]);
  149. if (isValid === false) {
  150. that.showError($field, validatorName);
  151. break;
  152. } else if (isValid === true) {
  153. that.removeError($field);
  154. }
  155. }
  156. },
  157. /**
  158. * Show field error
  159. *
  160. * @param {jQuery} $field The field element
  161. * @param {String} validatorName
  162. */
  163. showError: function($field, validatorName) {
  164. var field = $field.attr('name'),
  165. validator = this.options.fields[field].validators[validatorName],
  166. message = validator.message || this.options.message,
  167. $parent = $field.parents('.form-group');
  168. this.invalidFields[field] = true;
  169. // Add has-error class to parent element
  170. $parent.removeClass('has-success').addClass('has-error');
  171. $field.data('bootstrapValidator.error').html(message).show();
  172. this.$form.find(this.options.submitButtons).attr('disabled', 'disabled');
  173. },
  174. /**
  175. * Remove error from given field
  176. *
  177. * @param {jQuery} $field The field element
  178. */
  179. removeError: function($field) {
  180. delete this.invalidFields[$field.attr('name')];
  181. $field.parents('.form-group').removeClass('has-error').addClass('has-success');
  182. $field.data('bootstrapValidator.error').hide();
  183. this.$form.find(this.options.submitButtons).removeAttr('disabled');
  184. },
  185. /**
  186. * Start remote checking
  187. *
  188. * @param {jQuery} $field The field element
  189. * @param {String} validatorName
  190. * @param {XMLHttpRequest} xhr
  191. */
  192. startRequest: function($field, validatorName, xhr) {
  193. var field = $field.attr('name');
  194. $field.data('bootstrapValidator.isValid', false);
  195. this.$form.find(this.options.submitButtons).attr('disabled', 'disabled');
  196. if(this.numPendingRequests == null){
  197. this.numPendingRequests = 0;
  198. }
  199. this.numPendingRequests++;
  200. // Abort the previous request
  201. if (!this.xhrRequests[field]) {
  202. this.xhrRequests[field] = {};
  203. }
  204. if (this.xhrRequests[field][validatorName]) {
  205. this.numPendingRequests--;
  206. this.xhrRequests[field][validatorName].abort();
  207. }
  208. this.xhrRequests[field][validatorName] = xhr;
  209. },
  210. /**
  211. * Complete remote checking
  212. *
  213. * @param {jQuery} $field The field element
  214. * @param {String} validatorName
  215. * @param {boolean} isValid
  216. */
  217. completeRequest: function($field, validatorName, isValid) {
  218. if (isValid === false) {
  219. this.showError($field, validatorName);
  220. } else if (isValid === true) {
  221. this.removeError($field);
  222. $field.data('bootstrapValidator.isValid', true);
  223. }
  224. var field = $field.attr('name');
  225. delete this.xhrRequests[field][validatorName];
  226. this.numPendingRequests--;
  227. if (this.numPendingRequests <= 0) {
  228. this.numPendingRequests = 0;
  229. if (this.formSubmited) {
  230. this.$form.submit();
  231. }
  232. }
  233. },
  234. /**
  235. * Check the form validity
  236. *
  237. * @returns {boolean}
  238. */
  239. isValid: function() {
  240. if (this.numPendingRequests > 0) {
  241. return false;
  242. }
  243. for (var field in this.invalidFields) {
  244. if (this.invalidFields[field]) {
  245. return false;
  246. }
  247. }
  248. return true;
  249. }
  250. };
  251. // Plugin definition
  252. $.fn.bootstrapValidator = function(options) {
  253. return this.each(function() {
  254. var $this = $(this), data = $this.data('bootstrapValidator');
  255. if (!data) {
  256. $this.data('bootstrapValidator', (data = new BootstrapValidator(this, options)));
  257. }
  258. });
  259. };
  260. // Available validators
  261. $.fn.bootstrapValidator.validators = {};
  262. $.fn.bootstrapValidator.Constructor = BootstrapValidator;
  263. }(window.jQuery));
  264. ;(function($) {
  265. $.fn.bootstrapValidator.validators.between = {
  266. /**
  267. * Return true if the input value is between (strictly or not) two given numbers
  268. *
  269. * @param {BootstrapValidator} validator The validator plugin instance
  270. * @param {jQuery} $field Field element
  271. * @param {Object} options Can consist of the following keys:
  272. * - min
  273. * - max
  274. * - inclusive [optional]: Can be true or false. Default is true
  275. * - message: The invalid message
  276. * @returns {boolean}
  277. */
  278. validate: function(validator, $field, options) {
  279. var value = parseFloat($field.val());
  280. return (options.inclusive === true)
  281. ? (value > options.min && value < options.max)
  282. : (value >= options.min && value <= options.max);
  283. }
  284. };
  285. }(window.jQuery));
  286. ;(function($) {
  287. $.fn.bootstrapValidator.validators.digits = {
  288. /**
  289. * Return true if the input value contains digits only
  290. *
  291. * @param {BootstrapValidator} validator Validate plugin instance
  292. * @param {jQuery} $field Field element
  293. * @param {Object} options
  294. * @returns {boolean}
  295. */
  296. validate: function(validator, $field, options) {
  297. return /^\d+$/.test($field.val());
  298. }
  299. }
  300. }(window.jQuery));
  301. ;(function($) {
  302. $.fn.bootstrapValidator.validators.emailAddress = {
  303. /**
  304. * Return true if and only if the input value is a valid email address
  305. *
  306. * @param {BootstrapValidator} validator Validate plugin instance
  307. * @param {jQuery} $field Field element
  308. * @param {Object} options
  309. * @returns {boolean}
  310. */
  311. validate: function(validator, $field, options) {
  312. var value = $field.val(),
  313. // Email address regular expression
  314. // http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
  315. emailRegExp = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  316. return emailRegExp.test(value);
  317. }
  318. }
  319. }(window.jQuery));
  320. ;(function($) {
  321. $.fn.bootstrapValidator.validators.greaterThan = {
  322. /**
  323. * Return true if the input value is greater than or equals to given number
  324. *
  325. * @param {BootstrapValidator} validator Validate plugin instance
  326. * @param {jQuery} $field Field element
  327. * @param {Object} options Can consist of the following keys:
  328. * - value: The number used to compare to
  329. * - inclusive [optional]: Can be true or false. Default is true
  330. * - message: The invalid message
  331. * @returns {boolean}
  332. */
  333. validate: function(validator, $field, options) {
  334. var value = parseFloat($field.val());
  335. return (options.inclusive === true) ? (value > options.value) : (value >= options.value);
  336. }
  337. }
  338. }(window.jQuery));
  339. ;(function($) {
  340. $.fn.bootstrapValidator.validators.hexColor = {
  341. /**
  342. * Return true if the input value is a valid hex color
  343. *
  344. * @param {BootstrapValidator} validator The validator plugin instance
  345. * @param {jQuery} $field Field element
  346. * @param {Object} options Can consist of the following keys:
  347. * - message: The invalid message
  348. * @returns {boolean}
  349. */
  350. validate: function(validator, $field, options) {
  351. var value = $field.val();
  352. return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(value);
  353. }
  354. };
  355. }(window.jQuery));
  356. ;(function($) {
  357. $.fn.bootstrapValidator.validators.identical = {
  358. /**
  359. * Check if input value equals to value of particular one
  360. *
  361. * @param {BootstrapValidator} validator The validator plugin instance
  362. * @param {jQuery} $field Field element
  363. * @param {Object} options Consists of the following key:
  364. * - field: The name of field that will be used to compare with current one
  365. * @returns {boolean}
  366. */
  367. validate: function(validator, $field, options) {
  368. var value = $field.val(),
  369. $compareWith = validator.getFieldElement(options.field);
  370. if ($compareWith && value == $compareWith.val()) {
  371. validator.removeError($compareWith);
  372. return true;
  373. } else {
  374. return false;
  375. }
  376. }
  377. };
  378. }(window.jQuery));
  379. ;(function($) {
  380. $.fn.bootstrapValidator.validators.lessThan = {
  381. /**
  382. * Return true if the input value is less than or equal to given number
  383. *
  384. * @param {BootstrapValidator} validator The validator plugin instance
  385. * @param {jQuery} $field Field element
  386. * @param {Object} options Can consist of the following keys:
  387. * - value: The number used to compare to
  388. * - inclusive [optional]: Can be true or false. Default is true
  389. * - message: The invalid message
  390. * @returns {boolean}
  391. */
  392. validate: function(validator, $field, options) {
  393. var value = parseFloat($field.val());
  394. return (options.inclusive === true) ? (value < options.value) : (value <= options.value);
  395. }
  396. };
  397. }(window.jQuery));
  398. ;(function($) {
  399. $.fn.bootstrapValidator.validators.notEmpty = {
  400. /**
  401. * Check if input value is empty or not
  402. *
  403. * @param {BootstrapValidator} validator The validator plugin instance
  404. * @param {jQuery} $field Field element
  405. * @param {Object} options
  406. * @returns {boolean}
  407. */
  408. validate: function(validator, $field, options) {
  409. var type = $field.attr('type');
  410. return ('checkbox' == type || 'radio' == type) ? $field.is(':checked') : ($.trim($field.val()) != '');
  411. }
  412. };
  413. }(window.jQuery));
  414. ;(function($) {
  415. $.fn.bootstrapValidator.validators.regexp = {
  416. /**
  417. * Check if the element value matches given regular expression
  418. *
  419. * @param {BootstrapValidator} validator The validator plugin instance
  420. * @param {jQuery} $field Field element
  421. * @param {Object} options Consists of the following key:
  422. * - regexp: The regular expression you need to check
  423. * @returns {boolean}
  424. */
  425. validate: function(validator, $field, options) {
  426. var value = $field.val();
  427. return value.match(options.regexp);
  428. }
  429. };
  430. }(window.jQuery));
  431. ;(function($) {
  432. $.fn.bootstrapValidator.validators.remote = {
  433. /**
  434. * Request a remote server to check the input value
  435. *
  436. * @param {BootstrapValidator} validator Plugin instance
  437. * @param {jQuery} $field Field element
  438. * @param {Object} options Can consist of the following keys:
  439. * - url
  440. * - data [optional]: By default, it will take the value
  441. * {
  442. * <fieldName>: <fieldValue>
  443. * }
  444. * - message: The invalid message
  445. * @returns {string}
  446. */
  447. validate: function(validator, $field, options) {
  448. var value = $field.val(), name = $field.attr('name'), data = options.data;
  449. if (data == null) {
  450. data = {};
  451. data[name] = value;
  452. }
  453. var xhr = $.ajax({
  454. type: 'POST',
  455. url: options.url,
  456. dataType: 'json',
  457. data: data
  458. }).success(function(response) {
  459. var isValid = response.valid === true || response.valid === 'true';
  460. validator.completeRequest($field, 'remote', isValid);
  461. });
  462. validator.startRequest($field, 'remote', xhr);
  463. return 'pending';
  464. }
  465. };
  466. }(window.jQuery));
  467. ;(function($) {
  468. $.fn.bootstrapValidator.validators.stringLength = {
  469. /**
  470. * Check if the length of element value is less or more than given number
  471. *
  472. * @param {BootstrapValidator} validator The validator plugin instance
  473. * @param {jQuery} $field Field element
  474. * @param {Object} options Consists of following keys:
  475. * - min
  476. * - max
  477. * At least one of two keys is required
  478. * @returns {boolean}
  479. */
  480. validate: function(validator, $field, options) {
  481. var value = $.trim($field.val()), length = value.length;
  482. if ((options.min && length < options.min) || (options.max && length > options.max)) {
  483. return false;
  484. }
  485. return true;
  486. }
  487. };
  488. }(window.jQuery));
  489. ;(function($) {
  490. $.fn.bootstrapValidator.validators.uri = {
  491. /**
  492. * Return true if the input value is a valid URL
  493. *
  494. * @param {BootstrapValidator} validator The validator plugin instance
  495. * @param {jQuery} $field Field element
  496. * @param {Object} options
  497. * @returns {boolean}
  498. */
  499. validate: function(validator, $field, options) {
  500. // Credit to https://gist.github.com/dperini/729294
  501. //
  502. // Regular Expression for URL validation
  503. //
  504. // Author: Diego Perini
  505. // Updated: 2010/12/05
  506. //
  507. // the regular expression composed & commented
  508. // could be easily tweaked for RFC compliance,
  509. // it was expressly modified to fit & satisfy
  510. // these test for an URL shortener:
  511. //
  512. // http://mathiasbynens.be/demo/url-regex
  513. //
  514. // Notes on possible differences from a standard/generic validation:
  515. //
  516. // - utf-8 char class take in consideration the full Unicode range
  517. // - TLDs have been made mandatory so single names like "localhost" fails
  518. // - protocols have been restricted to ftp, http and https only as requested
  519. //
  520. // Changes:
  521. //
  522. // - IP address dotted notation validation, range: 1.0.0.0 - 223.255.255.255
  523. // first and last IP address of each class is considered invalid
  524. // (since they are broadcast/network addresses)
  525. //
  526. // - Added exclusion of private, reserved and/or local networks ranges
  527. //
  528. // Compressed one-line versions:
  529. //
  530. // Javascript version
  531. //
  532. // /^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$/i
  533. //
  534. // PHP version
  535. //
  536. // _^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:/[^\s]*)?$_iuS
  537. var urlExp = new RegExp(
  538. "^" +
  539. // protocol identifier
  540. "(?:(?:https?|ftp)://)" +
  541. // user:pass authentication
  542. "(?:\\S+(?::\\S*)?@)?" +
  543. "(?:" +
  544. // IP address exclusion
  545. // private & local networks
  546. "(?!10(?:\\.\\d{1,3}){3})" +
  547. "(?!127(?:\\.\\d{1,3}){3})" +
  548. "(?!169\\.254(?:\\.\\d{1,3}){2})" +
  549. "(?!192\\.168(?:\\.\\d{1,3}){2})" +
  550. "(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})" +
  551. // IP address dotted notation octets
  552. // excludes loopback network 0.0.0.0
  553. // excludes reserved space >= 224.0.0.0
  554. // excludes network & broacast addresses
  555. // (first & last IP address of each class)
  556. "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
  557. "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
  558. "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
  559. "|" +
  560. // host name
  561. "(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)" +
  562. // domain name
  563. "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*" +
  564. // TLD identifier
  565. "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" +
  566. ")" +
  567. // port number
  568. "(?::\\d{2,5})?" +
  569. // resource path
  570. "(?:/[^\\s]*)?" +
  571. "$", "i"
  572. );
  573. return urlExp.test($field.val());
  574. }
  575. };
  576. }(window.jQuery));
  577. ;(function($) {
  578. $.fn.bootstrapValidator.validators.usZipCode = {
  579. /**
  580. * Return true if and only if the input value is a valid US zip code
  581. *
  582. * @param {BootstrapValidator} validator The validator plugin instance
  583. * @param {jQuery} $field Field element
  584. * @param {Object} options
  585. * @returns {boolean}
  586. */
  587. validate: function(validateInstance, $field, options) {
  588. var value = $field.val();
  589. return /^\d{5}([\-]\d{4})?$/.test(value);
  590. }
  591. };
  592. }(window.jQuery));