bootstrapValidator.js 27 KB

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