bootstrapValidator.js 32 KB

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