bootstrapValidator.js 27 KB

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