bootstrapValidator.js 30 KB

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