bootstrapValidator.js 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600
  1. /**
  2. * BootstrapValidator (https://github.com/nghuuphuoc/bootstrapvalidator)
  3. *
  4. * A jQuery plugin to validate form fields. Use with Bootstrap 3
  5. *
  6. * @version v0.3.3
  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. this.STATUS_NOT_VALIDATED = 'NOT_VALIDATED';
  21. this.STATUS_VALIDATING = 'VALIDATING';
  22. this.STATUS_INVALID = 'INVALID';
  23. this.STATUS_VALID = 'VALID';
  24. };
  25. // The default options
  26. BootstrapValidator.DEFAULT_OPTIONS = {
  27. // The form CSS class
  28. elementClass: 'bootstrap-validator-form',
  29. // Default invalid message
  30. message: 'This value is not valid',
  31. // Shows ok/error/loading icons based on the field validity.
  32. // This feature requires Bootstrap v3.1.0 or later (http://getbootstrap.com/css/#forms-control-validation).
  33. // Since Bootstrap doesn't provide any methods to know its version, this option cannot be on/off automatically.
  34. // In other word, to use this feature you have to upgrade your Bootstrap to v3.1.0 or later.
  35. //
  36. // Examples:
  37. // - Use Glyphicons icons:
  38. // feedbackIcons: {
  39. // valid: 'glyphicon glyphicon-ok',
  40. // invalid: 'glyphicon glyphicon-remove',
  41. // validating: 'glyphicon glyphicon-refresh'
  42. // }
  43. // - Use FontAwesome icons:
  44. // feedbackIcons: {
  45. // valid: 'fa fa-check',
  46. // invalid: 'fa fa-times',
  47. // validating: 'fa fa-refresh'
  48. // }
  49. feedbackIcons: {
  50. valid: null,
  51. invalid: null,
  52. validating: null
  53. },
  54. // The submit buttons selector
  55. // These buttons will be disabled to prevent the valid form from multiple submissions
  56. submitButtons: 'button[type="submit"]',
  57. // The custom submit handler
  58. // It will prevent the form from the default submission
  59. //
  60. // submitHandler: function(validator, form) {
  61. // - validator is the BootstrapValidator instance
  62. // - form is the jQuery object present the current form
  63. // }
  64. submitHandler: null,
  65. // Live validating option
  66. // Can be one of 3 values:
  67. // - enabled: The plugin validates fields as soon as they are changed
  68. // - disabled: Disable the live validating. The error messages are only shown after the form is submitted
  69. // - submitted: The live validating is enabled after the form is submitted
  70. live: 'enabled',
  71. // Map the field name with validator rules
  72. fields: null
  73. };
  74. BootstrapValidator.prototype = {
  75. constructor: BootstrapValidator,
  76. /**
  77. * Init form
  78. */
  79. _init: function() {
  80. if (this.options.fields == null) {
  81. return;
  82. }
  83. var that = this;
  84. this.$form
  85. // Disable client side validation in HTML 5
  86. .attr('novalidate', 'novalidate')
  87. .addClass(this.options.elementClass)
  88. // Disable the default submission first
  89. .on('submit.bootstrapValidator', function(e) {
  90. e.preventDefault();
  91. that.validate();
  92. })
  93. .find(this.options.submitButtons)
  94. .on('click', function() {
  95. that.$submitButton = $(this);
  96. });
  97. for (var field in this.options.fields) {
  98. this._initField(field);
  99. }
  100. this._setLiveValidating();
  101. },
  102. /**
  103. * Init field
  104. *
  105. * @param {String} field The field name
  106. */
  107. _initField: function(field) {
  108. if (this.options.fields[field] == null || this.options.fields[field].validators == null) {
  109. return;
  110. }
  111. this.dfds[field] = {};
  112. this.results[field] = {};
  113. var fields = this.getFieldElements(field);
  114. // We don't need to validate non-existing fields
  115. if (fields == null) {
  116. delete this.options.fields[field];
  117. delete this.dfds[field];
  118. return;
  119. }
  120. fields.attr('data-bv-field', field);
  121. // Create help block elements for showing the error messages
  122. var $field = $(fields[0]),
  123. $parent = $field.parents('.form-group'),
  124. // Allow user to indicate where the error messages are shown
  125. $message = this.options.fields[field].container ? $parent.find(this.options.fields[field].container) : this._getMessageContainer($field);
  126. $field.data('bootstrapValidator.messageContainer', $message);
  127. for (var validatorName in this.options.fields[field].validators) {
  128. if (!$.fn.bootstrapValidator.validators[validatorName]) {
  129. delete this.options.fields[field].validators[validatorName];
  130. continue;
  131. }
  132. this.results[field][validatorName] = this.STATUS_NOT_VALIDATED;
  133. $('<small/>')
  134. .css('display', 'none')
  135. .attr('data-bv-validator', validatorName)
  136. .html(this.options.fields[field].validators[validatorName].message || this.options.message)
  137. .addClass('help-block')
  138. .appendTo($message);
  139. }
  140. // Prepare the feedback icons
  141. // Available from Bootstrap 3.1 (http://getbootstrap.com/css/#forms-control-validation)
  142. if (this.options.feedbackIcons
  143. && this.options.feedbackIcons.validating && this.options.feedbackIcons.invalid && this.options.feedbackIcons.valid)
  144. {
  145. $parent.addClass('has-feedback');
  146. var $icon = $('<i/>').css('display', 'none').addClass('form-control-feedback').attr('data-bv-field', field).insertAfter($(fields[fields.length - 1]));
  147. // The feedback icon does not render correctly if there is no label
  148. // https://github.com/twbs/bootstrap/issues/12873
  149. if ($parent.find('label').length == 0) {
  150. $icon.css('top', 0);
  151. }
  152. }
  153. if (this.options.fields[field]['enabled'] == null) {
  154. this.options.fields[field]['enabled'] = true;
  155. }
  156. // Whenever the user change the field value, mark it as not validated yet
  157. var that = this,
  158. type = fields.attr('type'),
  159. event = ('radio' == type || 'checkbox' == type || 'file' == type || 'SELECT' == fields[0].tagName) ? 'change' : 'keyup';
  160. fields.on(event + '.bootstrapValidator', function() {
  161. that.updateStatus($field, that.STATUS_NOT_VALIDATED, null);
  162. });
  163. },
  164. /**
  165. * Get the element to place the error messages
  166. *
  167. * @param {jQuery} $field The field element
  168. * @returns {jQuery}
  169. */
  170. _getMessageContainer: function($field) {
  171. var $parent = $field.parent();
  172. if ($parent.hasClass('form-group')) {
  173. return $parent;
  174. }
  175. var cssClasses = $parent.attr('class');
  176. if (!cssClasses) {
  177. return this._getMessageContainer($parent);
  178. }
  179. cssClasses = cssClasses.split(' ');
  180. var n = cssClasses.length;
  181. for (var i = 0; i < n; i++) {
  182. if (/^col-(xs|sm|md|lg)-\d+$/.test(cssClasses[i]) || /^col-(xs|sm|md|lg)-offset-\d+$/.test(cssClasses[i])) {
  183. return $parent;
  184. }
  185. }
  186. return this._getMessageContainer($parent);
  187. },
  188. /**
  189. * Enable live validating
  190. */
  191. _setLiveValidating: function() {
  192. if ('enabled' == this.options.live) {
  193. var that = this;
  194. for (var field in this.options.fields) {
  195. (function(f) {
  196. var fields = that.getFieldElements(f);
  197. if (fields) {
  198. var type = fields.attr('type'),
  199. event = ('radio' == type || 'checkbox' == type || 'file' == type || 'SELECT' == fields[0].tagName) ? 'change' : 'keyup';
  200. fields.on(event + '.bootstrapValidator', function() {
  201. that.validateField(f);
  202. });
  203. }
  204. })(field);
  205. }
  206. }
  207. },
  208. /**
  209. * Disable/Enable submit buttons
  210. *
  211. * @param {Boolean} disabled
  212. */
  213. _disableSubmitButtons: function(disabled) {
  214. if (!disabled) {
  215. this.$form.find(this.options.submitButtons).removeAttr('disabled');
  216. } else if (this.options.live != 'disabled') {
  217. // Don't disable if the live validating mode is disabled
  218. this.$form.find(this.options.submitButtons).attr('disabled', 'disabled');
  219. }
  220. },
  221. /**
  222. * Called when all validations are completed
  223. */
  224. _submit: function() {
  225. if (!this.isValid()) {
  226. if ('submitted' == this.options.live) {
  227. this.options.live = 'enabled';
  228. this._setLiveValidating();
  229. }
  230. // Focus to the first invalid field
  231. if (this.invalidField) {
  232. this.getFieldElements(this.invalidField).focus();
  233. }
  234. return;
  235. }
  236. this._disableSubmitButtons(true);
  237. // Call the custom submission if enabled
  238. if (this.options.submitHandler && 'function' == typeof this.options.submitHandler) {
  239. // Turn off the submit handler, so user can call form.submit() inside their submitHandler method
  240. this.$form.off('submit.bootstrapValidator');
  241. this.options.submitHandler.call(this, this, this.$form, this.$submitButton);
  242. } else {
  243. // Submit form
  244. this.$form.off('submit.bootstrapValidator').submit();
  245. }
  246. },
  247. // --- Public methods ---
  248. /**
  249. * Retrieve the field elements by given name
  250. *
  251. * @param {String} field The field name
  252. * @returns {null|jQuery[]}
  253. */
  254. getFieldElements: function(field) {
  255. var fields = this.$form.find(this.options.fields[field].selector || '[name="' + field + '"]');
  256. return (fields.length == 0) ? null : fields;
  257. },
  258. /**
  259. * Validate the form
  260. *
  261. * @return {BootstrapValidator}
  262. */
  263. validate: function() {
  264. if (!this.options.fields) {
  265. return this;
  266. }
  267. this._disableSubmitButtons(true);
  268. for (var field in this.options.fields) {
  269. this.validateField(field);
  270. }
  271. this._submit();
  272. return this;
  273. },
  274. /**
  275. * Validate given field
  276. *
  277. * @param {String} field The field name
  278. */
  279. validateField: function(field) {
  280. if (!this.options.fields[field]['enabled']) {
  281. return;
  282. }
  283. var that = this,
  284. fields = this.getFieldElements(field),
  285. $field = $(fields[0]),
  286. validators = this.options.fields[field].validators,
  287. validatorName,
  288. validateResult;
  289. // We don't need to validate disabled field
  290. if (fields.length == 1 && fields.is(':disabled')) {
  291. delete this.options.fields[field];
  292. delete this.dfds[field];
  293. return;
  294. }
  295. for (validatorName in validators) {
  296. if (this.dfds[field][validatorName]) {
  297. this.dfds[field][validatorName].reject();
  298. }
  299. // Don't validate field if it is already done
  300. if (this.results[field][validatorName] == this.STATUS_VALID || this.results[field][validatorName] == this.STATUS_INVALID) {
  301. continue;
  302. }
  303. this.results[field][validatorName] = this.STATUS_VALIDATING;
  304. validateResult = $.fn.bootstrapValidator.validators[validatorName].validate(this, $field, validators[validatorName]);
  305. if ('object' == typeof validateResult) {
  306. this.updateStatus($field, this.STATUS_VALIDATING, validatorName);
  307. this.dfds[field][validatorName] = validateResult;
  308. validateResult.done(function(isValid, v) {
  309. // v is validator name
  310. delete that.dfds[field][v];
  311. that.updateStatus($field, isValid ? that.STATUS_VALID : that.STATUS_INVALID, v);
  312. if (isValid && 'disabled' == that.options.live) {
  313. that._submit();
  314. }
  315. });
  316. } else if ('boolean' == typeof validateResult) {
  317. this.updateStatus($field, validateResult ? this.STATUS_VALID : this.STATUS_INVALID, validatorName);
  318. }
  319. }
  320. },
  321. /**
  322. * Check the form validity
  323. *
  324. * @returns {Boolean}
  325. */
  326. isValid: function() {
  327. var field, validatorName;
  328. for (field in this.results) {
  329. if (this.options.fields[field] == null || !this.options.fields[field]['enabled']) {
  330. continue;
  331. }
  332. for (validatorName in this.results[field]) {
  333. if (this.results[field][validatorName] == this.STATUS_NOT_VALIDATED || this.results[field][validatorName] == this.STATUS_VALIDATING) {
  334. return false;
  335. }
  336. if (this.results[field][validatorName] == this.STATUS_INVALID) {
  337. this.invalidField = field;
  338. return false;
  339. }
  340. }
  341. }
  342. return true;
  343. },
  344. /**
  345. * Update field status
  346. *
  347. * @param {String|jQuery} field The field name or field element
  348. * @param {String} status The status
  349. * Can be 'NOT_VALIDATED', 'VALIDATING', 'INVALID' or 'VALID'
  350. * @param {String|null} validatorName The validator name. If null, the method updates validity result for all validators
  351. * @return {BootstrapValidator}
  352. */
  353. updateStatus: function(field, status, validatorName) {
  354. var $field = ('string' == typeof field) ? this.getFieldElements(field) : field,
  355. that = this,
  356. field = $field.attr('data-bv-field'),
  357. $parent = $field.parents('.form-group'),
  358. $message = $field.data('bootstrapValidator.messageContainer'),
  359. $errors = $message.find('.help-block[data-bv-validator]'),
  360. $icon = $parent.find('.form-control-feedback[data-bv-field="' + field + '"]');
  361. // Update status
  362. if (validatorName) {
  363. this.results[field][validatorName] = status;
  364. } else {
  365. for (var v in this.options.fields[field].validators) {
  366. this.results[field][v] = status;
  367. }
  368. }
  369. // Show/hide error elements and feedback icons
  370. switch (status) {
  371. case this.STATUS_VALIDATING:
  372. this._disableSubmitButtons(true);
  373. $parent.removeClass('has-success').removeClass('has-error');
  374. // TODO: Show validating message
  375. validatorName ? $errors.filter('.help-block[data-bv-validator="' + validatorName + '"]').hide() : $errors.hide();
  376. if ($icon) {
  377. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.invalid).addClass(this.options.feedbackIcons.validating).show();
  378. }
  379. break;
  380. case this.STATUS_INVALID:
  381. this._disableSubmitButtons(true);
  382. $parent.removeClass('has-success').addClass('has-error');
  383. validatorName ? $errors.filter('[data-bv-validator="' + validatorName + '"]').show() : $errors.show();
  384. if ($icon) {
  385. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.validating).addClass(this.options.feedbackIcons.invalid).show();
  386. }
  387. break;
  388. case this.STATUS_VALID:
  389. validatorName ? $errors.filter('[data-bv-validator="' + validatorName + '"]').hide() : $errors.hide();
  390. // If the field is valid
  391. if ($errors.filter(function() {
  392. var display = $(this).css('display'), v = $(this).attr('data-bv-validator');
  393. return ('block' == display) || (that.results[field][v] != that.STATUS_VALID);
  394. }).length == 0
  395. ) {
  396. this._disableSubmitButtons(false);
  397. $parent.removeClass('has-error').addClass('has-success');
  398. if ($icon) {
  399. $icon.removeClass(this.options.feedbackIcons.invalid).removeClass(this.options.feedbackIcons.validating).addClass(this.options.feedbackIcons.valid).show();
  400. }
  401. }
  402. break;
  403. case this.STATUS_NOT_VALIDATED:
  404. default:
  405. this._disableSubmitButtons(false);
  406. $parent.removeClass('has-success').removeClass('has-error');
  407. validatorName ? $errors.filter('.help-block[data-bv-validator="' + validatorName + '"]').hide() : $errors.hide();
  408. if ($icon) {
  409. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.invalid).removeClass(this.options.feedbackIcons.validating).hide();
  410. }
  411. break;
  412. }
  413. return this;
  414. },
  415. // Useful APIs which aren't used internally
  416. /**
  417. * Reset the form
  418. *
  419. * @param {Boolean} resetFormData Reset current form data
  420. * @return {BootstrapValidator}
  421. */
  422. resetForm: function(resetFormData) {
  423. var field, $field, type;
  424. for (field in this.options.fields) {
  425. this.dfds[field] = {};
  426. this.results[field] = {};
  427. $field = this.getFieldElements(field);
  428. // Mark field as not validated yet
  429. this.updateStatus($field, this.STATUS_NOT_VALIDATED, null);
  430. if (resetFormData) {
  431. type = $field.attr('type');
  432. ('radio' == type || 'checkbox' == type) ? $field.removeAttr('checked').removeAttr('selected') : $field.val('');
  433. }
  434. }
  435. this.invalidField = null;
  436. this.$submitButton = null;
  437. // Enable submit buttons
  438. this._disableSubmitButtons(false);
  439. return this;
  440. },
  441. /**
  442. * Enable/Disable all validators to given field
  443. *
  444. * @param {String} field The field name
  445. * @param {Boolean} enabled Enable/Disable field validators
  446. * @return {BootstrapValidator}
  447. */
  448. enableFieldValidators: function(field, enabled) {
  449. this.options.fields[field]['enabled'] = enabled;
  450. this.updateStatus(field, this.STATUS_NOT_VALIDATED, null);
  451. return this;
  452. }
  453. };
  454. // Plugin definition
  455. $.fn.bootstrapValidator = function(options) {
  456. return this.each(function() {
  457. var $this = $(this), data = $this.data('bootstrapValidator');
  458. if (!data) {
  459. $this.data('bootstrapValidator', (data = new BootstrapValidator(this, options)));
  460. }
  461. if ('string' == typeof options) {
  462. data[options]();
  463. }
  464. });
  465. };
  466. // Available validators
  467. $.fn.bootstrapValidator.validators = {};
  468. $.fn.bootstrapValidator.Constructor = BootstrapValidator;
  469. }(window.jQuery));
  470. ;(function($) {
  471. $.fn.bootstrapValidator.validators.base64 = {
  472. /**
  473. * Return true if the input value is a base 64 encoded string.
  474. *
  475. * @param {BootstrapValidator} validator The validator plugin instance
  476. * @param {jQuery} $field Field element
  477. * @param {Object} options Can consist of the following keys:
  478. * - message: The invalid message
  479. * @returns {Boolean}
  480. */
  481. validate: function(validator, $field, options) {
  482. var value = $field.val();
  483. if (value == '') {
  484. return true;
  485. }
  486. return /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$/.test(value);
  487. }
  488. };
  489. }(window.jQuery));
  490. ;(function($) {
  491. $.fn.bootstrapValidator.validators.between = {
  492. /**
  493. * Return true if the input value is between (strictly or not) two given numbers
  494. *
  495. * @param {BootstrapValidator} validator The validator plugin instance
  496. * @param {jQuery} $field Field element
  497. * @param {Object} options Can consist of the following keys:
  498. * - min
  499. * - max
  500. * - inclusive [optional]: Can be true or false. Default is true
  501. * - message: The invalid message
  502. * @returns {Boolean}
  503. */
  504. validate: function(validator, $field, options) {
  505. var value = $field.val();
  506. if (value == '') {
  507. return true;
  508. }
  509. value = parseFloat(value);
  510. return (options.inclusive === true)
  511. ? (value > options.min && value < options.max)
  512. : (value >= options.min && value <= options.max);
  513. }
  514. };
  515. }(window.jQuery));
  516. ;(function($) {
  517. $.fn.bootstrapValidator.validators.callback = {
  518. /**
  519. * Return result from the callback method
  520. *
  521. * @param {BootstrapValidator} validator The validator plugin instance
  522. * @param {jQuery} $field Field element
  523. * @param {Object} options Can consist of the following keys:
  524. * - callback: The callback method that passes 2 parameters:
  525. * callback: function(fieldValue, validator) {
  526. * // fieldValue is the value of field
  527. * // validator is instance of BootstrapValidator
  528. * }
  529. * - message: The invalid message
  530. * @returns {Boolean|Deferred}
  531. */
  532. validate: function(validator, $field, options) {
  533. var value = $field.val();
  534. if (options.callback && 'function' == typeof options.callback) {
  535. var dfd = new $.Deferred();
  536. dfd.resolve(options.callback.call(this, value, validator), 'callback');
  537. return dfd;
  538. }
  539. return true;
  540. }
  541. };
  542. }(window.jQuery));
  543. ;(function($) {
  544. $.fn.bootstrapValidator.validators.choice = {
  545. /**
  546. * Check if the number of checked boxes are less or more than a given number
  547. *
  548. * @param {BootstrapValidator} validator The validator plugin instance
  549. * @param {jQuery} $field Field element
  550. * @param {Object} options Consists of following keys:
  551. * - min
  552. * - max
  553. * At least one of two keys is required
  554. * @returns {Boolean}
  555. */
  556. validate: function(validator, $field, options) {
  557. var numChoices = validator
  558. .getFieldElements($field.attr('data-bv-field'))
  559. .filter(':checked')
  560. .length;
  561. if ((options.min && numChoices < options.min) || (options.max && numChoices > options.max)) {
  562. return false;
  563. }
  564. return true;
  565. }
  566. };
  567. }(window.jQuery));
  568. ;(function($) {
  569. $.fn.bootstrapValidator.validators.creditCard = {
  570. /**
  571. * Return true if the input value is valid credit card number
  572. * Based on https://gist.github.com/DiegoSalazar/4075533
  573. *
  574. * @param {BootstrapValidator} validator The validator plugin instance
  575. * @param {jQuery} $field Field element
  576. * @param {Object} options Can consist of the following key:
  577. * - message: The invalid message
  578. * @returns {Boolean}
  579. */
  580. validate: function(validator, $field, options) {
  581. var value = $field.val();
  582. if (value == '') {
  583. return true;
  584. }
  585. // Accept only digits, dashes or spaces
  586. if (/[^0-9-\s]+/.test(value)) {
  587. return false;
  588. }
  589. value = value.replace(/\D/g, '');
  590. // Validate the check sum
  591. // The Luhn Algorithm
  592. // http://en.wikipedia.org/wiki/Luhn
  593. var check = 0, digit = 0, even = false, length = value.length;
  594. for (var n = length - 1; n >= 0; n--) {
  595. digit = parseInt(value.charAt(n), 10);
  596. if (even) {
  597. if ((digit *= 2) > 9) {
  598. digit -= 9;
  599. }
  600. }
  601. check += digit;
  602. even = !even;
  603. }
  604. if ((check % 10) != 0) {
  605. return false;
  606. }
  607. // Validate the card number based on prefix (IIN ranges) and length
  608. var cards = {
  609. AMERICAN_EXPRESS: {
  610. length: [15],
  611. prefix: ['34', '37']
  612. },
  613. DINERS_CLUB: {
  614. length: [14],
  615. prefix: ['300', '301', '302', '303', '304', '305', '36']
  616. },
  617. DINERS_CLUB_US: {
  618. length: [16],
  619. prefix: ['54', '55']
  620. },
  621. DISCOVER: {
  622. length: [16],
  623. prefix: ['6011', '622126', '622127', '622128', '622129', '62213',
  624. '62214', '62215', '62216', '62217', '62218', '62219',
  625. '6222', '6223', '6224', '6225', '6226', '6227', '6228',
  626. '62290', '62291', '622920', '622921', '622922', '622923',
  627. '622924', '622925', '644', '645', '646', '647', '648',
  628. '649', '65']
  629. },
  630. JCB: {
  631. length: [16],
  632. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  633. },
  634. LASER: {
  635. length: [16, 17, 18, 19],
  636. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  637. },
  638. MAESTRO: {
  639. length: [12, 13, 14, 15, 16, 17, 18, 19],
  640. prefix: ['5018', '5020', '5038', '6304', '6759', '6761', '6762', '6763', '6764', '6765', '6766']
  641. },
  642. MASTERCARD: {
  643. length: [16],
  644. prefix: ['51', '52', '53', '54', '55']
  645. },
  646. SOLO: {
  647. length: [16, 18, 19],
  648. prefix: ['6334', '6767']
  649. },
  650. UNIONPAY: {
  651. length: [16, 17, 18, 19],
  652. prefix: ['622126', '622127', '622128', '622129', '62213', '62214',
  653. '62215', '62216', '62217', '62218', '62219', '6222', '6223',
  654. '6224', '6225', '6226', '6227', '6228', '62290', '62291',
  655. '622920', '622921', '622922', '622923', '622924', '622925']
  656. },
  657. VISA: {
  658. length: [16],
  659. prefix: ['4']
  660. }
  661. };
  662. var type, i;
  663. for (type in cards) {
  664. for (i in cards[type]['prefix']) {
  665. if (value.substr(0, cards[type]['prefix'][i].length) == cards[type]['prefix'][i] // Check the prefix
  666. && cards[type]['length'].indexOf(value.length) != -1) // and length
  667. {
  668. return true;
  669. }
  670. }
  671. }
  672. return false;
  673. }
  674. };
  675. }(window.jQuery));
  676. ;(function($) {
  677. $.fn.bootstrapValidator.validators.cvv = {
  678. /**
  679. * Return true if the input value is a valid CVV number.
  680. *
  681. * @param {BootstrapValidator} validator The validator plugin instance
  682. * @param {jQuery} $field Field element
  683. * @param {Object} options Can consist of the following keys:
  684. * - creditCardField: The credit card number field. It can be null
  685. * - message: The invalid message
  686. * @returns {Boolean}
  687. */
  688. validate: function(validator, $field, options) {
  689. var value = $field.val();
  690. if (value == '') {
  691. return true;
  692. }
  693. if (!/^[0-9]{3,4}$/.test(value)) {
  694. return false;
  695. }
  696. if (!options.creditCardField) {
  697. return true;
  698. }
  699. // Get the credit card number
  700. var creditCard = validator.getFieldElements(options.creditCardField).val();
  701. if (creditCard == '') {
  702. return true;
  703. }
  704. // Supported credit card types
  705. var cards = {
  706. AMERICAN_EXPRESS: {
  707. length: [15],
  708. prefix: ['34', '37']
  709. },
  710. DINERS_CLUB: {
  711. length: [14],
  712. prefix: ['300', '301', '302', '303', '304', '305', '36']
  713. },
  714. DINERS_CLUB_US: {
  715. length: [16],
  716. prefix: ['54', '55']
  717. },
  718. DISCOVER: {
  719. length: [16],
  720. prefix: ['6011', '622126', '622127', '622128', '622129', '62213',
  721. '62214', '62215', '62216', '62217', '62218', '62219',
  722. '6222', '6223', '6224', '6225', '6226', '6227', '6228',
  723. '62290', '62291', '622920', '622921', '622922', '622923',
  724. '622924', '622925', '644', '645', '646', '647', '648',
  725. '649', '65']
  726. },
  727. JCB: {
  728. length: [16],
  729. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  730. },
  731. LASER: {
  732. length: [16, 17, 18, 19],
  733. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  734. },
  735. MAESTRO: {
  736. length: [12, 13, 14, 15, 16, 17, 18, 19],
  737. prefix: ['5018', '5020', '5038', '6304', '6759', '6761', '6762', '6763', '6764', '6765', '6766']
  738. },
  739. MASTERCARD: {
  740. length: [16],
  741. prefix: ['51', '52', '53', '54', '55']
  742. },
  743. SOLO: {
  744. length: [16, 18, 19],
  745. prefix: ['6334', '6767']
  746. },
  747. UNIONPAY: {
  748. length: [16, 17, 18, 19],
  749. prefix: ['622126', '622127', '622128', '622129', '62213', '62214',
  750. '62215', '62216', '62217', '62218', '62219', '6222', '6223',
  751. '6224', '6225', '6226', '6227', '6228', '62290', '62291',
  752. '622920', '622921', '622922', '622923', '622924', '622925']
  753. },
  754. VISA: {
  755. length: [16],
  756. prefix: ['4']
  757. }
  758. };
  759. var type, i, creditCardType = null;
  760. for (type in cards) {
  761. for (i in cards[type]['prefix']) {
  762. if (creditCard.substr(0, cards[type]['prefix'][i].length) == cards[type]['prefix'][i] // Check the prefix
  763. && cards[type]['length'].indexOf(creditCard.length) != -1) // and length
  764. {
  765. creditCardType = type;
  766. break;
  767. }
  768. }
  769. }
  770. return (creditCardType == null)
  771. ? false
  772. : (('AMERICAN_EXPRESS' == creditCardType) ? (value.length == 4) : (value.length == 3));
  773. }
  774. };
  775. }(window.jQuery));
  776. ;(function($) {
  777. $.fn.bootstrapValidator.validators.date = {
  778. /**
  779. * Return true if the input value is valid date
  780. *
  781. * @param {BootstrapValidator} validator The validator plugin instance
  782. * @param {jQuery} $field Field element
  783. * @param {Object} options Can consist of the following keys:
  784. * - format: The date format. Default is MM/DD/YYYY
  785. * Support the following formats:
  786. * YYYY/DD/MM
  787. * YYYY/DD/MM h:m A
  788. * YYYY/MM/DD
  789. * YYYY/MM/DD h:m A
  790. *
  791. * YYYY-DD-MM
  792. * YYYY-DD-MM h:m A
  793. * YYYY-MM-DD
  794. * YYYY-MM-DD h:m A
  795. *
  796. * MM/DD/YYYY
  797. * MM/DD/YYYY h:m A
  798. * DD/MM/YYYY
  799. * DD/MM/YYYY h:m A
  800. *
  801. * MM-DD-YYYY
  802. * MM-DD-YYYY h:m A
  803. * DD-MM-YYYY
  804. * DD-MM-YYYY h:m A
  805. * - message: The invalid message
  806. * @returns {Boolean}
  807. */
  808. validate: function(validator, $field, options) {
  809. var value = $field.val();
  810. if (value == '') {
  811. return true;
  812. }
  813. // Determine the separator
  814. options.format = options.format || 'MM/DD/YYYY';
  815. var separator = (options.format.indexOf('/') != -1)
  816. ? '/'
  817. : ((options.format.indexOf('-') != -1) ? '-' : null);
  818. if (separator == null) {
  819. return false;
  820. }
  821. var month, day, year, minutes = null, hours = null, matches;
  822. switch (true) {
  823. case (separator == '/' && (matches = value.match(/^(\d{4})\/(\d{1,2})\/(\d{1,2})$/i)) && options.format == 'YYYY/DD/MM'):
  824. case (separator == '-' && (matches = value.match(/^(\d{4})-(\d{1,2})-(\d{1,2})$/i)) && options.format == 'YYYY-DD-MM'):
  825. year = matches[1]; day = matches[2]; month = matches[3];
  826. break;
  827. case (separator == '/' && (matches = value.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/i)) && options.format == 'DD/MM/YYYY'):
  828. case (separator == '-' && (matches = value.match(/^(\d{1,2})-(\d{1,2})-(\d{4})$/i)) && options.format == 'DD-MM-YYYY'):
  829. day = matches[1]; month = matches[2]; year = matches[3];
  830. break;
  831. case (separator == '/' && (matches = value.match(/^(\d{4})\/(\d{1,2})\/(\d{1,2})$/i)) && options.format == 'YYYY/MM/DD'):
  832. case (separator == '-' && (matches = value.match(/^(\d{4})-(\d{1,2})-(\d{1,2})$/i)) && options.format == 'YYYY-MM-DD'):
  833. year = matches[1]; month = matches[2]; day = matches[3];
  834. break;
  835. case (separator == '/' && (matches = value.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/i)) && options.format == 'MM/DD/YYYY'):
  836. case (separator == '-' && (matches = value.match(/^(\d{1,2})-(\d{1,2})-(\d{4})$/i)) && options.format == 'MM-DD-YYYY'):
  837. month = matches[1]; day = matches[2]; year = matches[3];
  838. break;
  839. case (separator == '/' && (matches = value.match(/^(\d{4})\/(\d{1,2})\/(\d{1,2})\s+(\d{1,2}):(\d{1,2})\s+(AM|PM)$/i)) && options.format == 'YYYY/DD/MM h:m A'):
  840. case (separator == '-' && (matches = value.match(/^(\d{4})-(\d{1,2})-(\d{1,2})\s+(\d{1,2}):(\d{1,2})\s+(AM|PM)$/i)) && options.format == 'YYYY-DD-MM h:m A'):
  841. year = matches[1]; day = matches[2]; month = matches[3]; hours = matches[4]; minutes = matches[5];
  842. break;
  843. case (separator == '/' && (matches = value.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})\s+(\d{1,2}):(\d{1,2})\s+(AM|PM)$/i)) && options.format == 'DD/MM/YYYY h:m A'):
  844. case (separator == '-' && (matches = value.match(/^(\d{1,2})-(\d{1,2})-(\d{4})\s+(\d{1,2}):(\d{1,2})\s+(AM|PM)$/i)) && options.format == 'DD-MM-YYYY h:m A'):
  845. day = matches[1]; month = matches[2]; year = matches[3]; hours = matches[4]; minutes = matches[5];
  846. break;
  847. case (separator == '/' && (matches = value.match(/^(\d{4})\/(\d{1,2})\/(\d{1,2})\s+(\d{1,2}):(\d{1,2})\s+(AM|PM)$/i)) && options.format == 'YYYY/MM/DD h:m A'):
  848. case (separator == '-' && (matches = value.match(/^(\d{4})-(\d{1,2})-(\d{1,2})\s+(\d{1,2}):(\d{1,2})\s+(AM|PM)$/i)) && options.format == 'YYYY-MM-DD h:m A'):
  849. year = matches[1]; month = matches[2]; day = matches[3]; hours = matches[4]; minutes = matches[5];
  850. break;
  851. case (separator == '/' && (matches = value.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})\s+(\d{1,2}):(\d{1,2})\s+(AM|PM)$/i)) && options.format == 'MM/DD/YYYY h:m A'):
  852. case (separator == '-' && (matches = value.match(/^(\d{1,2})-(\d{1,2})-(\d{4})\s+(\d{1,2}):(\d{1,2})\s+(AM|PM)$/i)) && options.format == 'MM-DD-YYYY h:m A'):
  853. month = matches[1]; day = matches[2]; year = matches[3]; hours = matches[4]; minutes = matches[5];
  854. break;
  855. default:
  856. return false;
  857. }
  858. // Validate hours and minutes
  859. if (hours && minutes) {
  860. hours = parseInt(hours, 10);
  861. minutes = parseInt(minutes, 10);
  862. if (hours < 1 || hours > 12 || minutes < 0 || minutes > 59) {
  863. return false;
  864. }
  865. }
  866. // Validate day, month, and year
  867. day = parseInt(day, 10);
  868. month = parseInt(month, 10);
  869. year = parseInt(year, 10);
  870. if (year < 1000 || year > 9999 || month == 0 || month > 12) {
  871. return false;
  872. }
  873. var numDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  874. // Update the number of days in Feb of leap year
  875. if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
  876. numDays[1] = 29;
  877. }
  878. // Check the day
  879. return (day > 0 && day <= numDays[month - 1]);
  880. }
  881. };
  882. }(window.jQuery));
  883. ;(function($) {
  884. $.fn.bootstrapValidator.validators.different = {
  885. /**
  886. * Return true if the input value is different with given field's value
  887. *
  888. * @param {BootstrapValidator} validator The validator plugin instance
  889. * @param {jQuery} $field Field element
  890. * @param {Object} options Consists of the following key:
  891. * - field: The name of field that will be used to compare with current one
  892. * @returns {Boolean}
  893. */
  894. validate: function(validator, $field, options) {
  895. var value = $field.val();
  896. if (value == '') {
  897. return true;
  898. }
  899. var compareWith = validator.getFieldElements(options.field);
  900. if (compareWith == null) {
  901. return true;
  902. }
  903. if (value != compareWith.val()) {
  904. validator.updateStatus(compareWith, validator.STATUS_VALID, 'different');
  905. return true;
  906. } else {
  907. return false;
  908. }
  909. }
  910. };
  911. }(window.jQuery));
  912. ;(function($) {
  913. $.fn.bootstrapValidator.validators.digits = {
  914. /**
  915. * Return true if the input value contains digits only
  916. *
  917. * @param {BootstrapValidator} validator Validate plugin instance
  918. * @param {jQuery} $field Field element
  919. * @param {Object} options
  920. * @returns {Boolean}
  921. */
  922. validate: function(validator, $field, options) {
  923. var value = $field.val();
  924. if (value == '') {
  925. return true;
  926. }
  927. return /^\d+$/.test(value);
  928. }
  929. }
  930. }(window.jQuery));
  931. ;(function($) {
  932. $.fn.bootstrapValidator.validators.emailAddress = {
  933. /**
  934. * Return true if and only if the input value is a valid email address
  935. *
  936. * @param {BootstrapValidator} validator Validate plugin instance
  937. * @param {jQuery} $field Field element
  938. * @param {Object} options
  939. * @returns {Boolean}
  940. */
  941. validate: function(validator, $field, options) {
  942. var value = $field.val();
  943. if (value == '') {
  944. return true;
  945. }
  946. // Email address regular expression
  947. // http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
  948. 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,}))$/;
  949. return emailRegExp.test(value);
  950. }
  951. }
  952. }(window.jQuery));
  953. ;(function($) {
  954. $.fn.bootstrapValidator.validators.greaterThan = {
  955. /**
  956. * Return true if the input value is greater than or equals to given number
  957. *
  958. * @param {BootstrapValidator} validator Validate plugin instance
  959. * @param {jQuery} $field Field element
  960. * @param {Object} options Can consist of the following keys:
  961. * - value: The number used to compare to
  962. * - inclusive [optional]: Can be true or false. Default is true
  963. * - message: The invalid message
  964. * @returns {Boolean}
  965. */
  966. validate: function(validator, $field, options) {
  967. var value = $field.val();
  968. if (value == '') {
  969. return true;
  970. }
  971. value = parseFloat(value);
  972. return (options.inclusive === true) ? (value > options.value) : (value >= options.value);
  973. }
  974. }
  975. }(window.jQuery));
  976. ;(function($) {
  977. $.fn.bootstrapValidator.validators.hexColor = {
  978. /**
  979. * Return true if the input value is a valid hex color
  980. *
  981. * @param {BootstrapValidator} validator The validator plugin instance
  982. * @param {jQuery} $field Field element
  983. * @param {Object} options Can consist of the following keys:
  984. * - message: The invalid message
  985. * @returns {Boolean}
  986. */
  987. validate: function(validator, $field, options) {
  988. var value = $field.val();
  989. if (value == '') {
  990. return true;
  991. }
  992. return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(value);
  993. }
  994. };
  995. }(window.jQuery));
  996. ;(function($) {
  997. $.fn.bootstrapValidator.validators.identical = {
  998. /**
  999. * Check if input value equals to value of particular one
  1000. *
  1001. * @param {BootstrapValidator} validator The validator plugin instance
  1002. * @param {jQuery} $field Field element
  1003. * @param {Object} options Consists of the following key:
  1004. * - field: The name of field that will be used to compare with current one
  1005. * @returns {Boolean}
  1006. */
  1007. validate: function(validator, $field, options) {
  1008. var value = $field.val();
  1009. if (value == '') {
  1010. return true;
  1011. }
  1012. var compareWith = validator.getFieldElements(options.field);
  1013. if (compareWith == null) {
  1014. return true;
  1015. }
  1016. if (value == compareWith.val()) {
  1017. validator.updateStatus(compareWith, validator.STATUS_VALID, 'identical');
  1018. return true;
  1019. } else {
  1020. return false;
  1021. }
  1022. }
  1023. };
  1024. }(window.jQuery));
  1025. ;(function($) {
  1026. $.fn.bootstrapValidator.validators.ip = {
  1027. /**
  1028. * Return true if the input value is a IP address.
  1029. *
  1030. * @param {BootstrapValidator} validator The validator plugin instance
  1031. * @param {jQuery} $field Field element
  1032. * @param {Object} options Can consist of the following keys:
  1033. * - ipv4: Enable IPv4 validator, default to true
  1034. * - ipv6: Enable IPv6 validator, default to true
  1035. * - message: The invalid message
  1036. * @returns {Boolean}
  1037. */
  1038. validate: function(validator, $field, options) {
  1039. var value = $field.val();
  1040. if (value == '') {
  1041. return true;
  1042. }
  1043. options = $.extend({}, { ipv4: true, ipv6: true }, options);
  1044. if (options.ipv4) {
  1045. return /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(value);
  1046. } else if (options.ipv6) {
  1047. return /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/.test(str);
  1048. }
  1049. return false;
  1050. }
  1051. };
  1052. }(window.jQuery));
  1053. ;(function($) {
  1054. $.fn.bootstrapValidator.validators.isbn = {
  1055. /**
  1056. * Return true if the input value is a valid ISBN 10 or ISBN 13 number
  1057. *
  1058. * @param {BootstrapValidator} validator The validator plugin instance
  1059. * @param {jQuery} $field Field element
  1060. * @param {Object} options Can consist of the following keys:
  1061. * - message: The invalid message
  1062. * @returns {Boolean}
  1063. */
  1064. validate: function(validator, $field, options) {
  1065. var value = $field.val();
  1066. if (value == '') {
  1067. return true;
  1068. }
  1069. // Replace all special characters except digits and X
  1070. value = value.replace(/[^\dX]/gi, '');
  1071. var chars = value.split(''),
  1072. sum = 0,
  1073. checksum;
  1074. // See http://en.wikipedia.org/wiki/International_Standard_Book_Number
  1075. switch (chars.length) {
  1076. // ISBN 10
  1077. case 10:
  1078. sum = 0;
  1079. for (var i = 0; i < 9; i++) {
  1080. sum += ((10 - i) * parseInt(chars[i]));
  1081. }
  1082. checksum = 11 - (sum % 11);
  1083. if (checksum == 11) {
  1084. checksum = 0;
  1085. } else if (checksum == 10) {
  1086. checksum = 'X';
  1087. }
  1088. return (checksum == chars[9]);
  1089. // ISBN 13
  1090. case 13:
  1091. sum = 0;
  1092. for (var i = 0; i < 12; i++) {
  1093. sum += ((i % 2 == 0) ? parseInt(chars[i]) : (parseInt(chars[i]) * 3));
  1094. }
  1095. checksum = 10 - (sum % 10);
  1096. if (checksum == 10) {
  1097. checksum = '0';
  1098. }
  1099. return (checksum == chars[12]);
  1100. default:
  1101. return false;
  1102. }
  1103. }
  1104. };
  1105. }(window.jQuery));
  1106. ;(function($) {
  1107. $.fn.bootstrapValidator.validators.lessThan = {
  1108. /**
  1109. * Return true if the input value is less than or equal to given number
  1110. *
  1111. * @param {BootstrapValidator} validator The validator plugin instance
  1112. * @param {jQuery} $field Field element
  1113. * @param {Object} options Can consist of the following keys:
  1114. * - value: The number used to compare to
  1115. * - inclusive [optional]: Can be true or false. Default is true
  1116. * - message: The invalid message
  1117. * @returns {Boolean}
  1118. */
  1119. validate: function(validator, $field, options) {
  1120. var value = $field.val();
  1121. if (value == '') {
  1122. return true;
  1123. }
  1124. value = parseFloat(value);
  1125. return (options.inclusive === true) ? (value < options.value) : (value <= options.value);
  1126. }
  1127. };
  1128. }(window.jQuery));
  1129. ;(function($) {
  1130. $.fn.bootstrapValidator.validators.mac = {
  1131. /**
  1132. * Return true if the input value is a MAC address.
  1133. *
  1134. * @param {BootstrapValidator} validator The validator plugin instance
  1135. * @param {jQuery} $field Field element
  1136. * @param {Object} options Can consist of the following keys:
  1137. * - message: The invalid message
  1138. * @returns {Boolean}
  1139. */
  1140. validate: function(validator, $field, options) {
  1141. var value = $field.val();
  1142. if (value == '') {
  1143. return true;
  1144. }
  1145. return /^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$/.test(value);
  1146. }
  1147. };
  1148. }(window.jQuery));
  1149. ;(function($) {
  1150. $.fn.bootstrapValidator.validators.notEmpty = {
  1151. /**
  1152. * Check if input value is empty or not
  1153. *
  1154. * @param {BootstrapValidator} validator The validator plugin instance
  1155. * @param {jQuery} $field Field element
  1156. * @param {Object} options
  1157. * @returns {Boolean}
  1158. */
  1159. validate: function(validator, $field, options) {
  1160. var type = $field.attr('type');
  1161. if ('radio' == type || 'checkbox' == type) {
  1162. return validator
  1163. .getFieldElements($field.attr('data-bv-field'))
  1164. .filter(':checked')
  1165. .length > 0;
  1166. }
  1167. return $.trim($field.val()) != '';
  1168. }
  1169. };
  1170. }(window.jQuery));
  1171. ;(function($) {
  1172. $.fn.bootstrapValidator.validators.phone = {
  1173. /**
  1174. * Return true if the input value contains a valid US phone number only
  1175. *
  1176. * @param {BootstrapValidator} validator Validate plugin instance
  1177. * @param {jQuery} $field Field element
  1178. * @param {Object} options Consist of key:
  1179. * - country: The ISO 3166 country code
  1180. *
  1181. * Currently it only supports United State (US) country
  1182. * @returns {Boolean}
  1183. */
  1184. validate: function(validator, $field, options) {
  1185. var value = $field.val();
  1186. if (value == '') {
  1187. return true;
  1188. }
  1189. options.country = options.country || 'US';
  1190. switch (options.country.toUpperCase()) {
  1191. case 'US':
  1192. default:
  1193. value = value.replace(/\(|\)|\s+/g, '');
  1194. return (/^(?:1\-?)?(\d{3})[\-\.]?(\d{3})[\-\.]?(\d{4})$/).test(value);
  1195. }
  1196. }
  1197. }
  1198. }(window.jQuery));
  1199. ;(function($) {
  1200. $.fn.bootstrapValidator.validators.regexp = {
  1201. /**
  1202. * Check if the element value matches given regular expression
  1203. *
  1204. * @param {BootstrapValidator} validator The validator plugin instance
  1205. * @param {jQuery} $field Field element
  1206. * @param {Object} options Consists of the following key:
  1207. * - regexp: The regular expression you need to check
  1208. * @returns {Boolean}
  1209. */
  1210. validate: function(validator, $field, options) {
  1211. var value = $field.val();
  1212. if (value == '') {
  1213. return true;
  1214. }
  1215. return options.regexp.test(value);
  1216. }
  1217. };
  1218. }(window.jQuery));
  1219. ;(function($) {
  1220. $.fn.bootstrapValidator.validators.remote = {
  1221. /**
  1222. * Request a remote server to check the input value
  1223. *
  1224. * @param {BootstrapValidator} validator Plugin instance
  1225. * @param {jQuery} $field Field element
  1226. * @param {Object} options Can consist of the following keys:
  1227. * - url
  1228. * - data [optional]: By default, it will take the value
  1229. * {
  1230. * <fieldName>: <fieldValue>
  1231. * }
  1232. * - message: The invalid message
  1233. * @returns {Boolean|Deferred}
  1234. */
  1235. validate: function(validator, $field, options) {
  1236. var value = $field.val();
  1237. if (value == '') {
  1238. return true;
  1239. }
  1240. var name = $field.attr('data-bv-field'), data = options.data;
  1241. if (data == null) {
  1242. data = {};
  1243. }
  1244. // Support dynamic data
  1245. if ('function' == typeof data) {
  1246. data = data.call(this, validator);
  1247. }
  1248. data[name] = value;
  1249. var dfd = new $.Deferred();
  1250. var xhr = $.ajax({
  1251. type: 'POST',
  1252. url: options.url,
  1253. dataType: 'json',
  1254. data: data
  1255. });
  1256. xhr.then(function(response) {
  1257. dfd.resolve(response.valid === true || response.valid === 'true', 'remote');
  1258. });
  1259. dfd.fail(function() {
  1260. xhr.abort();
  1261. });
  1262. return dfd;
  1263. }
  1264. };
  1265. }(window.jQuery));
  1266. ;(function($) {
  1267. $.fn.bootstrapValidator.validators.step = {
  1268. /**
  1269. * Return true if the input value is valid step one
  1270. *
  1271. * @param {BootstrapValidator} validator The validator plugin instance
  1272. * @param {jQuery} $field Field element
  1273. * @param {Object} options Can consist of the following keys:
  1274. * - baseValue: The base value
  1275. * - step: The step
  1276. * - message: The invalid message
  1277. * @returns {Boolean}
  1278. */
  1279. validate: function(validator, $field, options) {
  1280. var value = $field.val();
  1281. if (value == '') {
  1282. return true;
  1283. }
  1284. options = $.extend({}, { baseValue: 0, step: 1 }, options);
  1285. value = parseFloat(value);
  1286. if (isNaN(value) || !isFinite(value)) {
  1287. return false;
  1288. }
  1289. var round = function(x, precision) {
  1290. var m = Math.pow(10, precision);
  1291. x = x * m;
  1292. var sign = (x > 0) | -(x < 0),
  1293. isHalf = (x % 1 === 0.5 * sign);
  1294. if (isHalf) {
  1295. return (Math.floor(x) + (sign > 0)) / m;
  1296. } else {
  1297. return Math.round(x) / m;
  1298. }
  1299. },
  1300. floatMod = function(x, y) {
  1301. if (y == 0.0) {
  1302. return 1.0;
  1303. }
  1304. var dotX = (x + '').split('.'),
  1305. dotY = (y + '').split('.'),
  1306. precision = ((dotX.length == 1) ? 0 : dotX[1].length) + ((dotY.length == 1) ? 0 : dotY[1].length);
  1307. return round(x - y * Math.floor(x / y), precision);
  1308. };
  1309. var mod = floatMod(value - options.baseValue, options.step);
  1310. return (mod == 0.0 || mod == options.step);
  1311. }
  1312. };
  1313. }(window.jQuery));
  1314. ;(function($) {
  1315. $.fn.bootstrapValidator.validators.stringLength = {
  1316. /**
  1317. * Check if the length of element value is less or more than given number
  1318. *
  1319. * @param {BootstrapValidator} validator The validator plugin instance
  1320. * @param {jQuery} $field Field element
  1321. * @param {Object} options Consists of following keys:
  1322. * - min
  1323. * - max
  1324. * At least one of two keys is required
  1325. * @returns {Boolean}
  1326. */
  1327. validate: function(validator, $field, options) {
  1328. var value = $field.val();
  1329. if (value == '') {
  1330. return true;
  1331. }
  1332. var length = $.trim(value).length;
  1333. if ((options.min && length < options.min) || (options.max && length > options.max)) {
  1334. return false;
  1335. }
  1336. return true;
  1337. }
  1338. };
  1339. }(window.jQuery));
  1340. ;(function($) {
  1341. $.fn.bootstrapValidator.validators.uri = {
  1342. /**
  1343. * Return true if the input value is a valid URL
  1344. *
  1345. * @param {BootstrapValidator} validator The validator plugin instance
  1346. * @param {jQuery} $field Field element
  1347. * @param {Object} options
  1348. * @returns {Boolean}
  1349. */
  1350. validate: function(validator, $field, options) {
  1351. var value = $field.val();
  1352. if (value == '') {
  1353. return true;
  1354. }
  1355. // Credit to https://gist.github.com/dperini/729294
  1356. //
  1357. // Regular Expression for URL validation
  1358. //
  1359. // Author: Diego Perini
  1360. // Updated: 2010/12/05
  1361. //
  1362. // the regular expression composed & commented
  1363. // could be easily tweaked for RFC compliance,
  1364. // it was expressly modified to fit & satisfy
  1365. // these test for an URL shortener:
  1366. //
  1367. // http://mathiasbynens.be/demo/url-regex
  1368. //
  1369. // Notes on possible differences from a standard/generic validation:
  1370. //
  1371. // - utf-8 char class take in consideration the full Unicode range
  1372. // - TLDs have been made mandatory so single names like "localhost" fails
  1373. // - protocols have been restricted to ftp, http and https only as requested
  1374. //
  1375. // Changes:
  1376. //
  1377. // - IP address dotted notation validation, range: 1.0.0.0 - 223.255.255.255
  1378. // first and last IP address of each class is considered invalid
  1379. // (since they are broadcast/network addresses)
  1380. //
  1381. // - Added exclusion of private, reserved and/or local networks ranges
  1382. //
  1383. // Compressed one-line versions:
  1384. //
  1385. // Javascript version
  1386. //
  1387. // /^(?:(?: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
  1388. //
  1389. // PHP version
  1390. //
  1391. // _^(?:(?: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
  1392. var urlExp = new RegExp(
  1393. "^" +
  1394. // protocol identifier
  1395. "(?:(?:https?|ftp)://)" +
  1396. // user:pass authentication
  1397. "(?:\\S+(?::\\S*)?@)?" +
  1398. "(?:" +
  1399. // IP address exclusion
  1400. // private & local networks
  1401. "(?!10(?:\\.\\d{1,3}){3})" +
  1402. "(?!127(?:\\.\\d{1,3}){3})" +
  1403. "(?!169\\.254(?:\\.\\d{1,3}){2})" +
  1404. "(?!192\\.168(?:\\.\\d{1,3}){2})" +
  1405. "(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})" +
  1406. // IP address dotted notation octets
  1407. // excludes loopback network 0.0.0.0
  1408. // excludes reserved space >= 224.0.0.0
  1409. // excludes network & broacast addresses
  1410. // (first & last IP address of each class)
  1411. "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
  1412. "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
  1413. "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
  1414. "|" +
  1415. // host name
  1416. "(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)" +
  1417. // domain name
  1418. "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*" +
  1419. // TLD identifier
  1420. "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" +
  1421. ")" +
  1422. // port number
  1423. "(?::\\d{2,5})?" +
  1424. // resource path
  1425. "(?:/[^\\s]*)?" +
  1426. "$", "i"
  1427. );
  1428. return urlExp.test(value);
  1429. }
  1430. };
  1431. }(window.jQuery));
  1432. ;(function($) {
  1433. $.fn.bootstrapValidator.validators.zipCode = {
  1434. /**
  1435. * Return true if and only if the input value is a valid country zip code
  1436. *
  1437. * @param {BootstrapValidator} validator The validator plugin instance
  1438. * @param {jQuery} $field Field element
  1439. * @param {Object} options Consist of key:
  1440. * - country: The ISO 3166 country code
  1441. *
  1442. * Currently it supports the following countries:
  1443. * - US (United State)
  1444. * - DK (Denmark)
  1445. * - SE (Sweden)
  1446. *
  1447. * @returns {Boolean}
  1448. */
  1449. validate: function(validator, $field, options) {
  1450. var value = $field.val();
  1451. if (value == '' || !options.country) {
  1452. return true;
  1453. }
  1454. options.country = options.country || 'US';
  1455. switch (options.country.toUpperCase()) {
  1456. case 'DK':
  1457. return /^(DK(-|\s)?)?\d{4}$/i.test(value);
  1458. case 'SE':
  1459. return /^(S-)?\d{3}\s?\d{2}$/i.test(value);
  1460. case 'US':
  1461. default:
  1462. return /^\d{5}([\-]\d{4})?$/.test(value);
  1463. }
  1464. }
  1465. };
  1466. }(window.jQuery));