bootstrapValidator.js 32 KB

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