bootstrapValidator.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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.different = {
  305. /**
  306. * Return true if the input value is different with given field's value
  307. *
  308. * @param {BootstrapValidator} validator The validator plugin instance
  309. * @param {jQuery} $field Field element
  310. * @param {Object} options Consists of the following key:
  311. * - field: The name of field that will be used to compare with current one
  312. * @returns {boolean}
  313. */
  314. validate: function(validator, $field, options) {
  315. var value = $field.val(),
  316. $compareWith = validator.getFieldElement(options.field);
  317. if ($compareWith && value != $compareWith.val()) {
  318. validator.removeError($compareWith);
  319. return true;
  320. } else {
  321. return false;
  322. }
  323. }
  324. };
  325. }(window.jQuery));
  326. ;(function($) {
  327. $.fn.bootstrapValidator.validators.digits = {
  328. /**
  329. * Return true if the input value contains digits only
  330. *
  331. * @param {BootstrapValidator} validator Validate plugin instance
  332. * @param {jQuery} $field Field element
  333. * @param {Object} options
  334. * @returns {boolean}
  335. */
  336. validate: function(validator, $field, options) {
  337. return /^\d+$/.test($field.val());
  338. }
  339. }
  340. }(window.jQuery));
  341. ;(function($) {
  342. $.fn.bootstrapValidator.validators.emailAddress = {
  343. /**
  344. * Return true if and only if the input value is a valid email address
  345. *
  346. * @param {BootstrapValidator} validator Validate plugin instance
  347. * @param {jQuery} $field Field element
  348. * @param {Object} options
  349. * @returns {boolean}
  350. */
  351. validate: function(validator, $field, options) {
  352. var value = $field.val(),
  353. // Email address regular expression
  354. // http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
  355. 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,}))$/;
  356. return emailRegExp.test(value);
  357. }
  358. }
  359. }(window.jQuery));
  360. ;(function($) {
  361. $.fn.bootstrapValidator.validators.greaterThan = {
  362. /**
  363. * Return true if the input value is greater than or equals to given number
  364. *
  365. * @param {BootstrapValidator} validator Validate plugin instance
  366. * @param {jQuery} $field Field element
  367. * @param {Object} options Can consist of the following keys:
  368. * - value: The number used to compare to
  369. * - inclusive [optional]: Can be true or false. Default is true
  370. * - message: The invalid message
  371. * @returns {boolean}
  372. */
  373. validate: function(validator, $field, options) {
  374. var value = parseFloat($field.val());
  375. return (options.inclusive === true) ? (value > options.value) : (value >= options.value);
  376. }
  377. }
  378. }(window.jQuery));
  379. ;(function($) {
  380. $.fn.bootstrapValidator.validators.hexColor = {
  381. /**
  382. * Return true if the input value is a valid hex color
  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. * - message: The invalid message
  388. * @returns {boolean}
  389. */
  390. validate: function(validator, $field, options) {
  391. var value = $field.val();
  392. return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(value);
  393. }
  394. };
  395. }(window.jQuery));
  396. ;(function($) {
  397. $.fn.bootstrapValidator.validators.identical = {
  398. /**
  399. * Check if input value equals to value of particular one
  400. *
  401. * @param {BootstrapValidator} validator The validator plugin instance
  402. * @param {jQuery} $field Field element
  403. * @param {Object} options Consists of the following key:
  404. * - field: The name of field that will be used to compare with current one
  405. * @returns {boolean}
  406. */
  407. validate: function(validator, $field, options) {
  408. var value = $field.val(),
  409. $compareWith = validator.getFieldElement(options.field);
  410. if ($compareWith && value == $compareWith.val()) {
  411. validator.removeError($compareWith);
  412. return true;
  413. } else {
  414. return false;
  415. }
  416. }
  417. };
  418. }(window.jQuery));
  419. ;(function($) {
  420. $.fn.bootstrapValidator.validators.lessThan = {
  421. /**
  422. * Return true if the input value is less than or equal to given number
  423. *
  424. * @param {BootstrapValidator} validator The validator plugin instance
  425. * @param {jQuery} $field Field element
  426. * @param {Object} options Can consist of the following keys:
  427. * - value: The number used to compare to
  428. * - inclusive [optional]: Can be true or false. Default is true
  429. * - message: The invalid message
  430. * @returns {boolean}
  431. */
  432. validate: function(validator, $field, options) {
  433. var value = parseFloat($field.val());
  434. return (options.inclusive === true) ? (value < options.value) : (value <= options.value);
  435. }
  436. };
  437. }(window.jQuery));
  438. ;(function($) {
  439. $.fn.bootstrapValidator.validators.notEmpty = {
  440. /**
  441. * Check if input value is empty or not
  442. *
  443. * @param {BootstrapValidator} validator The validator plugin instance
  444. * @param {jQuery} $field Field element
  445. * @param {Object} options
  446. * @returns {boolean}
  447. */
  448. validate: function(validator, $field, options) {
  449. var type = $field.attr('type');
  450. return ('checkbox' == type || 'radio' == type) ? $field.is(':checked') : ($.trim($field.val()) != '');
  451. }
  452. };
  453. }(window.jQuery));
  454. ;(function($) {
  455. $.fn.bootstrapValidator.validators.regexp = {
  456. /**
  457. * Check if the element value matches given regular expression
  458. *
  459. * @param {BootstrapValidator} validator The validator plugin instance
  460. * @param {jQuery} $field Field element
  461. * @param {Object} options Consists of the following key:
  462. * - regexp: The regular expression you need to check
  463. * @returns {boolean}
  464. */
  465. validate: function(validator, $field, options) {
  466. var value = $field.val();
  467. return value.match(options.regexp);
  468. }
  469. };
  470. }(window.jQuery));
  471. ;(function($) {
  472. $.fn.bootstrapValidator.validators.remote = {
  473. /**
  474. * Request a remote server to check the input value
  475. *
  476. * @param {BootstrapValidator} validator Plugin instance
  477. * @param {jQuery} $field Field element
  478. * @param {Object} options Can consist of the following keys:
  479. * - url
  480. * - data [optional]: By default, it will take the value
  481. * {
  482. * <fieldName>: <fieldValue>
  483. * }
  484. * - message: The invalid message
  485. * @returns {string}
  486. */
  487. validate: function(validator, $field, options) {
  488. var value = $field.val(), name = $field.attr('name'), data = options.data;
  489. if (data == null) {
  490. data = {};
  491. data[name] = value;
  492. }
  493. var xhr = $.ajax({
  494. type: 'POST',
  495. url: options.url,
  496. dataType: 'json',
  497. data: data
  498. }).success(function(response) {
  499. var isValid = response.valid === true || response.valid === 'true';
  500. validator.completeRequest($field, 'remote', isValid);
  501. });
  502. validator.startRequest($field, 'remote', xhr);
  503. return 'pending';
  504. }
  505. };
  506. }(window.jQuery));
  507. ;(function($) {
  508. $.fn.bootstrapValidator.validators.stringLength = {
  509. /**
  510. * Check if the length of element value is less or more than given number
  511. *
  512. * @param {BootstrapValidator} validator The validator plugin instance
  513. * @param {jQuery} $field Field element
  514. * @param {Object} options Consists of following keys:
  515. * - min
  516. * - max
  517. * At least one of two keys is required
  518. * @returns {boolean}
  519. */
  520. validate: function(validator, $field, options) {
  521. var value = $.trim($field.val()), length = value.length;
  522. if ((options.min && length < options.min) || (options.max && length > options.max)) {
  523. return false;
  524. }
  525. return true;
  526. }
  527. };
  528. }(window.jQuery));
  529. ;(function($) {
  530. $.fn.bootstrapValidator.validators.uri = {
  531. /**
  532. * Return true if the input value is a valid URL
  533. *
  534. * @param {BootstrapValidator} validator The validator plugin instance
  535. * @param {jQuery} $field Field element
  536. * @param {Object} options
  537. * @returns {boolean}
  538. */
  539. validate: function(validator, $field, options) {
  540. // Credit to https://gist.github.com/dperini/729294
  541. //
  542. // Regular Expression for URL validation
  543. //
  544. // Author: Diego Perini
  545. // Updated: 2010/12/05
  546. //
  547. // the regular expression composed & commented
  548. // could be easily tweaked for RFC compliance,
  549. // it was expressly modified to fit & satisfy
  550. // these test for an URL shortener:
  551. //
  552. // http://mathiasbynens.be/demo/url-regex
  553. //
  554. // Notes on possible differences from a standard/generic validation:
  555. //
  556. // - utf-8 char class take in consideration the full Unicode range
  557. // - TLDs have been made mandatory so single names like "localhost" fails
  558. // - protocols have been restricted to ftp, http and https only as requested
  559. //
  560. // Changes:
  561. //
  562. // - IP address dotted notation validation, range: 1.0.0.0 - 223.255.255.255
  563. // first and last IP address of each class is considered invalid
  564. // (since they are broadcast/network addresses)
  565. //
  566. // - Added exclusion of private, reserved and/or local networks ranges
  567. //
  568. // Compressed one-line versions:
  569. //
  570. // Javascript version
  571. //
  572. // /^(?:(?: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
  573. //
  574. // PHP version
  575. //
  576. // _^(?:(?: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
  577. var urlExp = new RegExp(
  578. "^" +
  579. // protocol identifier
  580. "(?:(?:https?|ftp)://)" +
  581. // user:pass authentication
  582. "(?:\\S+(?::\\S*)?@)?" +
  583. "(?:" +
  584. // IP address exclusion
  585. // private & local networks
  586. "(?!10(?:\\.\\d{1,3}){3})" +
  587. "(?!127(?:\\.\\d{1,3}){3})" +
  588. "(?!169\\.254(?:\\.\\d{1,3}){2})" +
  589. "(?!192\\.168(?:\\.\\d{1,3}){2})" +
  590. "(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})" +
  591. // IP address dotted notation octets
  592. // excludes loopback network 0.0.0.0
  593. // excludes reserved space >= 224.0.0.0
  594. // excludes network & broacast addresses
  595. // (first & last IP address of each class)
  596. "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
  597. "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
  598. "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
  599. "|" +
  600. // host name
  601. "(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)" +
  602. // domain name
  603. "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*" +
  604. // TLD identifier
  605. "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" +
  606. ")" +
  607. // port number
  608. "(?::\\d{2,5})?" +
  609. // resource path
  610. "(?:/[^\\s]*)?" +
  611. "$", "i"
  612. );
  613. return urlExp.test($field.val());
  614. }
  615. };
  616. }(window.jQuery));
  617. ;(function($) {
  618. $.fn.bootstrapValidator.validators.usZipCode = {
  619. /**
  620. * Return true if and only if the input value is a valid US zip code
  621. *
  622. * @param {BootstrapValidator} validator The validator plugin instance
  623. * @param {jQuery} $field Field element
  624. * @param {Object} options
  625. * @returns {boolean}
  626. */
  627. validate: function(validateInstance, $field, options) {
  628. var value = $field.val();
  629. return /^\d{5}([\-]\d{4})?$/.test(value);
  630. }
  631. };
  632. }(window.jQuery));