bootstrapValidator.js 30 KB

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