bootstrapValidator.js 30 KB

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