bootstrapValidator.js 29 KB

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