bootstrapValidator.js 35 KB

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