bootstrapValidator.js 25 KB

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