bootstrapValidator.js 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  1. /**
  2. * BootstrapValidator (http://bootstrapvalidator.com)
  3. *
  4. * The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3
  5. *
  6. * @author http://twitter.com/nghuuphuoc
  7. * @copyright (c) 2013 - 2014 Nguyen Huu Phuoc
  8. * @license MIT
  9. */
  10. (function($) {
  11. var BootstrapValidator = function(form, options) {
  12. this.$form = $(form);
  13. this.options = $.extend({}, BootstrapValidator.DEFAULT_OPTIONS, options);
  14. this.$invalidFields = $([]); // Array of invalid fields
  15. this.$submitButton = null; // The submit button which is clicked to submit form
  16. // Validating status
  17. this.STATUS_NOT_VALIDATED = 'NOT_VALIDATED';
  18. this.STATUS_VALIDATING = 'VALIDATING';
  19. this.STATUS_INVALID = 'INVALID';
  20. this.STATUS_VALID = 'VALID';
  21. // Determine the event that is fired when user change the field value
  22. // Most modern browsers supports input event except IE 7, 8.
  23. // IE 9 supports input event but the event is still not fired if I press the backspace key.
  24. // Get IE version
  25. // https://gist.github.com/padolsey/527683/#comment-7595
  26. var ieVersion = (function() {
  27. var v = 3, div = document.createElement('div'), a = div.all || [];
  28. while (div.innerHTML = '<!--[if gt IE '+(++v)+']><br><![endif]-->', a[0]);
  29. return v > 4 ? v : !v;
  30. }());
  31. var el = document.createElement('div');
  32. this._changeEvent = (ieVersion === 9 || !('oninput' in el)) ? 'keyup' : 'input';
  33. // The flag to indicate that the form is ready to submit when a remote/callback validator returns
  34. this._submitIfValid = null;
  35. // Field elements
  36. this._cacheFields = {};
  37. this._init();
  38. };
  39. // The default options
  40. BootstrapValidator.DEFAULT_OPTIONS = {
  41. // The form CSS class
  42. elementClass: 'bv-form',
  43. // Default invalid message
  44. message: 'This value is not valid',
  45. // The error messages container
  46. // It can be:
  47. // * 'tooltip' if you want to use Bootstrap tooltip to show error messages
  48. // * 'popover' if you want to use Bootstrap popover to show error messages
  49. // * a CSS selector indicating the container
  50. //
  51. // In the first two cases, since the tooltip/popover should be small enough, the plugin only shows only one error message
  52. // You also can define the message container for particular field
  53. container: null,
  54. // The field will not be live validated if its length is less than this number of characters
  55. threshold: null,
  56. // Indicate fields which won't be validated
  57. // By default, the plugin will not validate the following kind of fields:
  58. // - disabled
  59. // - hidden
  60. // - invisible
  61. //
  62. // The setting consists of jQuery filters. Accept 3 formats:
  63. // - A string. Use a comma to separate filter
  64. // - An array. Each element is a filter
  65. // - An array. Each element can be a callback function
  66. // function($field, validator) {
  67. // $field is jQuery object representing the field element
  68. // validator is the BootstrapValidator instance
  69. // return true or false;
  70. // }
  71. //
  72. // The 3 following settings are equivalent:
  73. //
  74. // 1) ':disabled, :hidden, :not(:visible)'
  75. // 2) [':disabled', ':hidden', ':not(:visible)']
  76. // 3) [':disabled', ':hidden', function($field) {
  77. // return !$field.is(':visible');
  78. // }]
  79. excluded: [':disabled', ':hidden', ':not(:visible)'],
  80. // Shows ok/error/loading icons based on the field validity.
  81. // This feature requires Bootstrap v3.1.0 or later (http://getbootstrap.com/css/#forms-control-validation).
  82. // Since Bootstrap doesn't provide any methods to know its version, this option cannot be on/off automatically.
  83. // In other word, to use this feature you have to upgrade your Bootstrap to v3.1.0 or later.
  84. //
  85. // Examples:
  86. // - Use Glyphicons icons:
  87. // feedbackIcons: {
  88. // valid: 'glyphicon glyphicon-ok',
  89. // invalid: 'glyphicon glyphicon-remove',
  90. // validating: 'glyphicon glyphicon-refresh'
  91. // }
  92. // - Use FontAwesome icons:
  93. // feedbackIcons: {
  94. // valid: 'fa fa-check',
  95. // invalid: 'fa fa-times',
  96. // validating: 'fa fa-refresh'
  97. // }
  98. feedbackIcons: {
  99. valid: null,
  100. invalid: null,
  101. validating: null
  102. },
  103. // The submit buttons selector
  104. // These buttons will be disabled to prevent the valid form from multiple submissions
  105. submitButtons: '[type="submit"]',
  106. // The custom submit handler
  107. // It will prevent the form from the default submission
  108. //
  109. // submitHandler: function(validator, form) {
  110. // - validator is the BootstrapValidator instance
  111. // - form is the jQuery object presenting the current form
  112. // }
  113. submitHandler: null,
  114. // Live validating option
  115. // Can be one of 3 values:
  116. // - enabled: The plugin validates fields as soon as they are changed
  117. // - disabled: Disable the live validating. The error messages are only shown after the form is submitted
  118. // - submitted: The live validating is enabled after the form is submitted
  119. live: 'enabled',
  120. // Map the field name with validator rules
  121. fields: {}
  122. };
  123. BootstrapValidator.prototype = {
  124. constructor: BootstrapValidator,
  125. /**
  126. * Init form
  127. */
  128. _init: function() {
  129. var that = this,
  130. options = {
  131. excluded: this.$form.attr('data-bv-excluded'),
  132. trigger: this.$form.attr('data-bv-trigger'),
  133. message: this.$form.attr('data-bv-message'),
  134. container: this.$form.attr('data-bv-container'),
  135. submitButtons: this.$form.attr('data-bv-submitbuttons'),
  136. threshold: this.$form.attr('data-bv-threshold'),
  137. live: this.$form.attr('data-bv-live'),
  138. fields: {},
  139. feedbackIcons: {
  140. valid: this.$form.attr('data-bv-feedbackicons-valid'),
  141. invalid: this.$form.attr('data-bv-feedbackicons-invalid'),
  142. validating: this.$form.attr('data-bv-feedbackicons-validating')
  143. }
  144. };
  145. this.$form
  146. // Disable client side validation in HTML 5
  147. .attr('novalidate', 'novalidate')
  148. .addClass(this.options.elementClass)
  149. // Disable the default submission first
  150. .on('submit.bv', function(e) {
  151. e.preventDefault();
  152. that.validate();
  153. })
  154. .on('click.bv', this.options.submitButtons, function() {
  155. that.$submitButton = $(this);
  156. // The user just click the submit button
  157. that._submitIfValid = true;
  158. })
  159. // Find all fields which have either "name" or "data-bv-field" attribute
  160. .find('[name], [data-bv-field]')
  161. .each(function() {
  162. var $field = $(this);
  163. if (that._isExcluded($field)) {
  164. return;
  165. }
  166. var field = $field.attr('name') || $field.attr('data-bv-field'),
  167. opts = that._parseOptions($field);
  168. if (opts) {
  169. $field.attr('data-bv-field', field);
  170. options.fields[field] = $.extend({}, opts, options.fields[field]);
  171. }
  172. });
  173. this.options = $.extend(true, this.options, options);
  174. for (var field in this.options.fields) {
  175. this._initField(field);
  176. }
  177. this.$form.trigger($.Event('init.form.bv'), {
  178. options: this.options
  179. });
  180. },
  181. /**
  182. * Parse the validator options from HTML attributes
  183. *
  184. * @param {jQuery} $field The field element
  185. * @returns {Object}
  186. */
  187. _parseOptions: function($field) {
  188. var field = $field.attr('name') || $field.attr('data-bv-field'),
  189. validators = {},
  190. validator,
  191. v, // Validator name
  192. enabled,
  193. optionName,
  194. optionValue,
  195. html5AttrName,
  196. html5AttrMap;
  197. for (v in $.fn.bootstrapValidator.validators) {
  198. validator = $.fn.bootstrapValidator.validators[v];
  199. enabled = $field.attr('data-bv-' + v.toLowerCase()) + '';
  200. html5AttrMap = ('function' == typeof validator.enableByHtml5) ? validator.enableByHtml5($field) : null;
  201. if ((html5AttrMap && enabled != 'false')
  202. || (html5AttrMap !== true && ('' == enabled || 'true' == enabled)))
  203. {
  204. // Try to parse the options via attributes
  205. validator.html5Attributes = validator.html5Attributes || { message: 'message' };
  206. validators[v] = $.extend({}, html5AttrMap == true ? {} : html5AttrMap, validators[v]);
  207. for (html5AttrName in validator.html5Attributes) {
  208. optionName = validator.html5Attributes[html5AttrName];
  209. optionValue = $field.attr('data-bv-' + v.toLowerCase() + '-' + html5AttrName);
  210. if (optionValue) {
  211. if ('true' == optionValue) {
  212. optionValue = true;
  213. } else if ('false' == optionValue) {
  214. optionValue = false;
  215. }
  216. validators[v][optionName] = optionValue;
  217. }
  218. }
  219. }
  220. }
  221. var opts = {
  222. feedbackIcons: $field.attr('data-bv-feedbackicons'),
  223. trigger: $field.attr('data-bv-trigger'),
  224. message: $field.attr('data-bv-message'),
  225. container: $field.attr('data-bv-container'),
  226. selector: $field.attr('data-bv-selector'),
  227. threshold: $field.attr('data-bv-threshold'),
  228. validators: validators
  229. },
  230. emptyOptions = $.isEmptyObject(opts), // Check if the field options are set using HTML attributes
  231. emptyValidators = $.isEmptyObject(validators); // Check if the field validators are set using HTML attributes
  232. if (!emptyValidators || (!emptyOptions && this.options.fields[field])) {
  233. opts.validators = validators;
  234. return opts;
  235. } else {
  236. return null;
  237. }
  238. },
  239. /**
  240. * Init field
  241. *
  242. * @param {String|jQuery} field The field name or field element
  243. */
  244. _initField: function(field) {
  245. var fields = $([]);
  246. switch (typeof field) {
  247. case 'object':
  248. fields = field;
  249. field = field.attr('data-bv-field');
  250. break;
  251. case 'string':
  252. fields = this.getFieldElements(field);
  253. fields.attr('data-bv-field', field);
  254. break;
  255. default:
  256. break;
  257. }
  258. if (this.options.fields[field] == null || this.options.fields[field].validators == null) {
  259. return;
  260. }
  261. // We don't need to validate non-existing fields
  262. if (fields.length == 0) {
  263. delete this.options.fields[field];
  264. return;
  265. }
  266. for (var validatorName in this.options.fields[field].validators) {
  267. if (!$.fn.bootstrapValidator.validators[validatorName]) {
  268. delete this.options.fields[field].validators[validatorName];
  269. }
  270. }
  271. if (this.options.fields[field]['enabled'] == null) {
  272. this.options.fields[field]['enabled'] = true;
  273. }
  274. var that = this,
  275. total = fields.length,
  276. type = fields.attr('type'),
  277. updateAll = (total == 1) || ('radio' == type) || ('checkbox' == type);
  278. for (var i = 0; i < total; i++) {
  279. var $field = fields.eq(i),
  280. $parent = $field.parents('.form-group'),
  281. // Allow user to indicate where the error messages are shown
  282. container = this.options.fields[field].container || this.options.container,
  283. $message = (container && container != 'tooltip' && container != 'popover') ? $(container) : this._getMessageContainer($field);
  284. if (container && container != 'tooltip' && container != 'popover') {
  285. $message.addClass('has-error');
  286. }
  287. // Remove all error messages and feedback icons
  288. $message.find('.help-block[data-bv-validator][data-bv-for="' + field + '"]').remove();
  289. $parent.find('i[data-bv-icon-for="' + field + '"]').remove();
  290. // Whenever the user change the field value, mark it as not validated yet
  291. var event = ('radio' == type || 'checkbox' == type || 'file' == type || 'SELECT' == $field.get(0).tagName) ? 'change' : this._changeEvent;
  292. $field.off(event + '.update.bv').on(event + '.update.bv', function() {
  293. // Reset the flag
  294. that._submitIfValid = false;
  295. that.updateStatus($(this), that.STATUS_NOT_VALIDATED);
  296. });
  297. // Create help block elements for showing the error messages
  298. $field.data('bv.messages', $message);
  299. for (var validatorName in this.options.fields[field].validators) {
  300. $field.data('bv.result.' + validatorName, this.STATUS_NOT_VALIDATED);
  301. if (!updateAll || i == total - 1) {
  302. $('<small/>')
  303. .css('display', 'none')
  304. .addClass('help-block')
  305. .attr('data-bv-validator', validatorName)
  306. .attr('data-bv-for', field)
  307. .attr('data-bv-result', this.STATUS_NOT_VALIDATED)
  308. .html(this.options.fields[field].validators[validatorName].message || this.options.fields[field].message || this.options.message)
  309. .appendTo($message);
  310. }
  311. }
  312. // Prepare the feedback icons
  313. // Available from Bootstrap 3.1 (http://getbootstrap.com/css/#forms-control-validation)
  314. if (this.options.fields[field].feedbackIcons !== false && this.options.fields[field].feedbackIcons !== 'false'
  315. && this.options.feedbackIcons
  316. && this.options.feedbackIcons.validating && this.options.feedbackIcons.invalid && this.options.feedbackIcons.valid
  317. && (!updateAll || i == total - 1))
  318. {
  319. $parent.removeClass('has-success').removeClass('has-error').addClass('has-feedback');
  320. var $icon = $('<i/>')
  321. .css('display', 'none')
  322. .addClass('form-control-feedback')
  323. .attr('data-bv-icon-for', field)
  324. // Place it after the label containing the checkbox/radio
  325. // so when clicking the icon, it doesn't effect to the checkbox/radio element
  326. .insertAfter(('checkbox' == type || 'radio' == type) ? $field.parent() : $field);
  327. // The feedback icon does not render correctly if there is no label
  328. // https://github.com/twbs/bootstrap/issues/12873
  329. if ($parent.find('label').length == 0) {
  330. $icon.css('top', 0);
  331. }
  332. // Fix feedback icons in input-group
  333. if ($parent.find('.input-group-addon').length != 0) {
  334. $icon.css({
  335. 'top': 0,
  336. 'z-index': 100
  337. });
  338. }
  339. }
  340. }
  341. // Set live mode
  342. var trigger = this.options.fields[field].trigger || this.options.trigger || event,
  343. events = $.map(trigger.split(' '), function(item) {
  344. return item + '.live.bv';
  345. }).join(' ');
  346. switch (this.options.live) {
  347. case 'submitted':
  348. break;
  349. case 'disabled':
  350. fields.off(events);
  351. break;
  352. case 'enabled':
  353. default:
  354. fields.off(events).on(events, function() {
  355. if (that._exceedThreshold($(this))) {
  356. that.validateField($(this));
  357. }
  358. });
  359. break;
  360. }
  361. this.$form.trigger($.Event('init.field.bv'), {
  362. field: field,
  363. element: fields
  364. });
  365. },
  366. /**
  367. * Get the element to place the error messages
  368. *
  369. * @param {jQuery} $field The field element
  370. * @returns {jQuery}
  371. */
  372. _getMessageContainer: function($field) {
  373. var $parent = $field.parent();
  374. if ($parent.hasClass('form-group')) {
  375. return $parent;
  376. }
  377. var cssClasses = $parent.attr('class');
  378. if (!cssClasses) {
  379. return this._getMessageContainer($parent);
  380. }
  381. cssClasses = cssClasses.split(' ');
  382. var n = cssClasses.length;
  383. for (var i = 0; i < n; i++) {
  384. if (/^col-(xs|sm|md|lg)-\d+$/.test(cssClasses[i]) || /^col-(xs|sm|md|lg)-offset-\d+$/.test(cssClasses[i])) {
  385. return $parent;
  386. }
  387. }
  388. return this._getMessageContainer($parent);
  389. },
  390. /**
  391. * Called when all validations are completed
  392. */
  393. _submit: function() {
  394. var isValid = this.isValid(),
  395. eventType = isValid ? 'success.form.bv' : 'error.form.bv',
  396. e = $.Event(eventType);
  397. this.$form.trigger(e);
  398. // Call default handler
  399. // Check if whether the submit button is clicked
  400. if (this.$submitButton) {
  401. isValid ? this._onSuccess(e) : this._onError(e);
  402. }
  403. },
  404. /**
  405. * Check if the field is excluded.
  406. * Returning true means that the field will not be validated
  407. *
  408. * @param {jQuery} $field The field element
  409. * @returns {Boolean}
  410. */
  411. _isExcluded: function($field) {
  412. if (this.options.excluded) {
  413. // Convert to array first
  414. if ('string' == typeof this.options.excluded) {
  415. this.options.excluded = $.map(this.options.excluded.split(','), function(item) {
  416. // Trim the spaces
  417. return $.trim(item);
  418. });
  419. }
  420. var length = this.options.excluded.length;
  421. for (var i = 0; i < length; i++) {
  422. if (('string' == typeof this.options.excluded[i] && $field.is(this.options.excluded[i]))
  423. || ('function' == typeof this.options.excluded[i] && this.options.excluded[i].call(this, $field, this) == true))
  424. {
  425. return true;
  426. }
  427. }
  428. }
  429. return false;
  430. },
  431. /**
  432. * Check if the number of characters of field value exceed the threshold or not
  433. *
  434. * @param {jQuery} $field The field element
  435. * @returns {Boolean}
  436. */
  437. _exceedThreshold: function($field) {
  438. var field = $field.attr('data-bv-field'),
  439. threshold = this.options.fields[field].threshold || this.options.threshold;
  440. if (!threshold) {
  441. return true;
  442. }
  443. var cannotType = $.inArray($field.attr('type'), ['button', 'checkbox', 'file', 'hidden', 'image', 'radio', 'reset', 'submit']) != -1;
  444. return (cannotType || $field.val().length >= threshold);
  445. },
  446. // --- Events ---
  447. /**
  448. * The default handler of error.form.bv event.
  449. * It will be called when there is a invalid field
  450. *
  451. * @param {jQuery.Event} e The jQuery event object
  452. */
  453. _onError: function(e) {
  454. if (e.isDefaultPrevented()) {
  455. return;
  456. }
  457. if ('submitted' == this.options.live) {
  458. // Enable live mode
  459. this.options.live = 'enabled';
  460. var that = this;
  461. for (var field in this.options.fields) {
  462. (function(f) {
  463. var fields = that.getFieldElements(f);
  464. if (fields.length) {
  465. var type = $(fields[0]).attr('type'),
  466. event = ('radio' == type || 'checkbox' == type || 'file' == type || 'SELECT' == $(fields[0]).get(0).tagName) ? 'change' : that._changeEvent,
  467. trigger = that.options.fields[field].trigger || that.options.trigger || event,
  468. events = $.map(trigger.split(' '), function(item) {
  469. return item + '.live.bv';
  470. }).join(' ');
  471. fields.off(events).on(events, function() {
  472. if (that._exceedThreshold($(this))) {
  473. that.validateField($(this));
  474. }
  475. });
  476. }
  477. })(field);
  478. }
  479. }
  480. var $invalidField = this.$invalidFields.eq(0);
  481. if ($invalidField) {
  482. // Activate the tab containing the invalid field if exists
  483. var $tabPane = $invalidField.parents('.tab-pane'), tabId;
  484. if ($tabPane && (tabId = $tabPane.attr('id'))) {
  485. $('a[href="#' + tabId + '"][data-toggle="tab"]').tab('show');
  486. }
  487. // Focus to the first invalid field
  488. $invalidField.focus();
  489. }
  490. },
  491. /**
  492. * The default handler of success.form.bv event.
  493. * It will be called when all the fields are valid
  494. *
  495. * @param {jQuery.Event} e The jQuery event object
  496. */
  497. _onSuccess: function(e) {
  498. if (e.isDefaultPrevented()) {
  499. return;
  500. }
  501. // Call the custom submission if enabled
  502. if (this.options.submitHandler && 'function' == typeof this.options.submitHandler) {
  503. // If you want to submit the form inside your submit handler, please call defaultSubmit() method
  504. this.options.submitHandler.call(this, this, this.$form);
  505. } else {
  506. this.disableSubmitButtons(true).defaultSubmit();
  507. }
  508. },
  509. /**
  510. * Called after validating a field element
  511. *
  512. * @param {jQuery} $field The field element
  513. * @param {String} [validatorName] The validator name
  514. */
  515. _onFieldValidated: function($field, validatorName) {
  516. var field = $field.attr('data-bv-field'),
  517. validators = this.options.fields[field].validators,
  518. counter = {},
  519. numValidators = 0;
  520. // Trigger an event after given validator completes
  521. if (validatorName) {
  522. var data = {
  523. field: field,
  524. element: $field,
  525. validator: validatorName
  526. };
  527. switch ($field.data('bv.result.' + validatorName)) {
  528. case this.STATUS_INVALID:
  529. this.$form.trigger($.Event('error.validator.bv'), data);
  530. break;
  531. case this.STATUS_VALID:
  532. this.$form.trigger($.Event('success.validator.bv'), data);
  533. break;
  534. default:
  535. break;
  536. }
  537. }
  538. counter[this.STATUS_NOT_VALIDATED] = 0;
  539. counter[this.STATUS_VALIDATING] = 0;
  540. counter[this.STATUS_INVALID] = 0;
  541. counter[this.STATUS_VALID] = 0;
  542. for (var v in validators) {
  543. numValidators++;
  544. var result = $field.data('bv.result.' + v);
  545. if (result) {
  546. counter[result]++;
  547. }
  548. }
  549. if (counter[this.STATUS_VALID] == numValidators) {
  550. // Remove from the list of invalid fields
  551. this.$invalidFields = this.$invalidFields.not($field);
  552. this.$form.trigger($.Event('success.field.bv'), {
  553. field: field,
  554. element: $field
  555. });
  556. }
  557. // If all validators are completed and there is at least one validator which doesn't pass
  558. else if (counter[this.STATUS_NOT_VALIDATED] == 0 && counter[this.STATUS_VALIDATING] == 0 && counter[this.STATUS_INVALID] > 0) {
  559. // Add to the list of invalid fields
  560. this.$invalidFields = this.$invalidFields.add($field);
  561. this.$form.trigger($.Event('error.field.bv'), {
  562. field: field,
  563. element: $field
  564. });
  565. }
  566. },
  567. // --- Public methods ---
  568. /**
  569. * Retrieve the field elements by given name
  570. *
  571. * @param {String} field The field name
  572. * @returns {null|jQuery[]}
  573. */
  574. getFieldElements: function(field) {
  575. if (!this._cacheFields[field]) {
  576. this._cacheFields[field] = (this.options.fields[field] && this.options.fields[field].selector)
  577. ? $(this.options.fields[field].selector)
  578. : this.$form.find('[name="' + field + '"]');
  579. }
  580. return this._cacheFields[field];
  581. },
  582. /**
  583. * Disable/enable submit buttons
  584. *
  585. * @param {Boolean} disabled Can be true or false
  586. * @returns {BootstrapValidator}
  587. */
  588. disableSubmitButtons: function(disabled) {
  589. if (!disabled) {
  590. this.$form.find(this.options.submitButtons).removeAttr('disabled');
  591. } else if (this.options.live != 'disabled') {
  592. // Don't disable if the live validating mode is disabled
  593. this.$form.find(this.options.submitButtons).attr('disabled', 'disabled');
  594. }
  595. return this;
  596. },
  597. /**
  598. * Validate the form
  599. *
  600. * @returns {BootstrapValidator}
  601. */
  602. validate: function() {
  603. if (!this.options.fields) {
  604. return this;
  605. }
  606. this.disableSubmitButtons(true);
  607. for (var field in this.options.fields) {
  608. this.validateField(field);
  609. }
  610. this._submit();
  611. return this;
  612. },
  613. /**
  614. * Validate given field
  615. *
  616. * @param {String|jQuery} field The field name or field element
  617. * @returns {BootstrapValidator}
  618. */
  619. validateField: function(field) {
  620. var fields = $([]), type;
  621. switch (typeof field) {
  622. case 'object':
  623. fields = field;
  624. field = field.attr('data-bv-field');
  625. break;
  626. case 'string':
  627. fields = this.getFieldElements(field);
  628. break;
  629. default:
  630. break;
  631. }
  632. if (this.options.fields[field] && this.options.fields[field]['enabled'] == false) {
  633. return this;
  634. }
  635. var that = this,
  636. type = fields.attr('type'),
  637. total = ('radio' == type || 'checkbox' == type) ? 1 : fields.length,
  638. updateAll = ('radio' == type || 'checkbox' == type),
  639. validators = this.options.fields[field].validators,
  640. validatorName,
  641. validateResult;
  642. for (var i = 0; i < total; i++) {
  643. var $field = fields.eq(i);
  644. if (this._isExcluded($field)) {
  645. continue;
  646. }
  647. for (validatorName in validators) {
  648. if ($field.data('bv.dfs.' + validatorName)) {
  649. $field.data('bv.dfs.' + validatorName).reject();
  650. }
  651. // Don't validate field if it is already done
  652. var result = $field.data('bv.result.' + validatorName);
  653. if (result == this.STATUS_VALID || result == this.STATUS_INVALID) {
  654. this._onFieldValidated($field, validatorName);
  655. continue;
  656. }
  657. $field.data('bv.result.' + validatorName, this.STATUS_VALIDATING);
  658. validateResult = $.fn.bootstrapValidator.validators[validatorName].validate(this, $field, validators[validatorName]);
  659. // validateResult can be a $.Deferred object ...
  660. if ('object' == typeof validateResult) {
  661. this.updateStatus(updateAll ? field : $field, this.STATUS_VALIDATING, validatorName);
  662. $field.data('bv.dfs.' + validatorName, validateResult);
  663. validateResult.done(function($f, v, isValid, message) {
  664. // v is validator name
  665. $f.removeData('bv.dfs.' + v);
  666. if (message) {
  667. // Update the error message
  668. $field.data('bv.messages').find('.help-block[data-bv-validator="' + v + '"][data-bv-for="' + $f.attr('data-bv-field') + '"]').html(message);
  669. }
  670. that.updateStatus(updateAll ? $f.attr('data-bv-field') : $f, isValid ? that.STATUS_VALID : that.STATUS_INVALID, v);
  671. if (isValid && that._submitIfValid == true) {
  672. // If a remote validator returns true and the form is ready to submit, then do it
  673. that._submit();
  674. }
  675. });
  676. }
  677. // ... or a boolean value
  678. else if ('boolean' == typeof validateResult) {
  679. this.updateStatus(updateAll ? field : $field, validateResult ? this.STATUS_VALID : this.STATUS_INVALID, validatorName);
  680. }
  681. }
  682. }
  683. return this;
  684. },
  685. /**
  686. * Update all validating results of field
  687. *
  688. * @param {String|jQuery} field The field name or field element
  689. * @param {String} status The status. Can be 'NOT_VALIDATED', 'VALIDATING', 'INVALID' or 'VALID'
  690. * @param {String} [validatorName] The validator name. If null, the method updates validity result for all validators
  691. * @returns {BootstrapValidator}
  692. */
  693. updateStatus: function(field, status, validatorName) {
  694. var fields = $([]), type;
  695. switch (typeof field) {
  696. case 'object':
  697. fields = field;
  698. field = field.attr('data-bv-field');
  699. break;
  700. case 'string':
  701. fields = this.getFieldElements(field);
  702. break;
  703. default:
  704. break;
  705. }
  706. var that = this,
  707. type = fields.attr('type'),
  708. total = ('radio' == type || 'checkbox' == type) ? 1 : fields.length;
  709. for (var i = 0; i < total; i++) {
  710. var $field = fields.eq(i),
  711. $parent = $field.parents('.form-group'),
  712. $message = $field.data('bv.messages'),
  713. $allErrors = $message.find('.help-block[data-bv-validator][data-bv-for="' + field + '"]'),
  714. $errors = validatorName ? $allErrors.filter('[data-bv-validator="' + validatorName + '"]') : $allErrors,
  715. $icon = $parent.find('.form-control-feedback[data-bv-icon-for="' + field + '"]'),
  716. container = this.options.fields[field].container || this.options.container,
  717. isValidField = null;
  718. // Update status
  719. if (validatorName) {
  720. $field.data('bv.result.' + validatorName, status);
  721. } else {
  722. for (var v in this.options.fields[field].validators) {
  723. $field.data('bv.result.' + v, status);
  724. }
  725. }
  726. // Show/hide error elements and feedback icons
  727. $errors.attr('data-bv-result', status);
  728. // Determine the tab containing the element
  729. var $tabPane = $field.parents('.tab-pane'),
  730. tabId, $tab;
  731. if ($tabPane && (tabId = $tabPane.attr('id'))) {
  732. $tab = $('a[href="#' + tabId + '"][data-toggle="tab"]').parent();
  733. }
  734. switch (status) {
  735. case this.STATUS_VALIDATING:
  736. isValidField = null;
  737. this.disableSubmitButtons(true);
  738. $parent.removeClass('has-success').removeClass('has-error');
  739. if ($icon) {
  740. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.invalid).addClass(this.options.feedbackIcons.validating).show();
  741. }
  742. if ($tab) {
  743. $tab.removeClass('bv-tab-success').removeClass('bv-tab-error');
  744. }
  745. break;
  746. case this.STATUS_INVALID:
  747. isValidField = false;
  748. this.disableSubmitButtons(true);
  749. $parent.removeClass('has-success').addClass('has-error');
  750. if ($icon) {
  751. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.validating).addClass(this.options.feedbackIcons.invalid).show();
  752. }
  753. if ($tab) {
  754. $tab.removeClass('bv-tab-success').addClass('bv-tab-error');
  755. }
  756. break;
  757. case this.STATUS_VALID:
  758. // If the field is valid (passes all validators)
  759. isValidField = ($allErrors.filter('[data-bv-result="' + this.STATUS_NOT_VALIDATED +'"]').length == 0)
  760. ? ($allErrors.filter('[data-bv-result="' + this.STATUS_VALID +'"]').length == $allErrors.length) // All validators are completed
  761. : null; // There are some validators that have not done
  762. if (isValidField != null) {
  763. this.disableSubmitButtons(this.$submitButton ? !this.isValid() : !isValidField);
  764. if ($icon) {
  765. $icon
  766. .removeClass(this.options.feedbackIcons.invalid).removeClass(this.options.feedbackIcons.validating).removeClass(this.options.feedbackIcons.valid)
  767. .addClass(isValidField ? this.options.feedbackIcons.valid : this.options.feedbackIcons.invalid)
  768. .show();
  769. }
  770. }
  771. $parent.removeClass('has-error has-success').addClass(this.isValidContainer($parent) ? 'has-success' : 'has-error');
  772. if ($tab) {
  773. $tab.removeClass('bv-tab-success').removeClass('bv-tab-error').addClass(this.isValidContainer($tabPane) ? 'bv-tab-success' : 'bv-tab-error');
  774. }
  775. break;
  776. case this.STATUS_NOT_VALIDATED:
  777. default:
  778. isValidField = null;
  779. this.disableSubmitButtons(false);
  780. $parent.removeClass('has-success').removeClass('has-error');
  781. if ($icon) {
  782. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.invalid).removeClass(this.options.feedbackIcons.validating).hide();
  783. }
  784. if ($tab) {
  785. $tab.removeClass('bv-tab-success').removeClass('bv-tab-error');
  786. }
  787. break;
  788. }
  789. switch (true) {
  790. // Only show the first error message if it is placed inside a tooltip ...
  791. case ($icon && 'tooltip' == container):
  792. (isValidField === false)
  793. ? $icon.css('cursor', 'pointer').tooltip('destroy').tooltip({
  794. html: true,
  795. placement: 'top',
  796. title: $allErrors.filter('[data-bv-result="' + that.STATUS_INVALID + '"]').eq(0).html()
  797. })
  798. : $icon.css('cursor', '').tooltip('destroy');
  799. break;
  800. // ... or popover
  801. case ($icon && 'popover' == container):
  802. (isValidField === false)
  803. ? $icon.css('cursor', 'pointer').popover('destroy').popover({
  804. content: $allErrors.filter('[data-bv-result="' + that.STATUS_INVALID + '"]').eq(0).html(),
  805. html: true,
  806. placement: 'top',
  807. trigger: 'hover click'
  808. })
  809. : $icon.css('cursor', '').popover('destroy');
  810. break;
  811. default:
  812. (status == this.STATUS_INVALID) ? $errors.show() : $errors.hide();
  813. break;
  814. }
  815. // Trigger an event
  816. this.$form.trigger($.Event('status.field.bv'), {
  817. field: field,
  818. element: $field,
  819. status: status
  820. });
  821. this._onFieldValidated($field, validatorName);
  822. }
  823. return this;
  824. },
  825. /**
  826. * Check the form validity
  827. *
  828. * @returns {Boolean}
  829. */
  830. isValid: function() {
  831. for (var field in this.options.fields) {
  832. if (!this.isValidField(field)) {
  833. return false;
  834. }
  835. }
  836. return true;
  837. },
  838. /**
  839. * Check if the field is valid or not
  840. *
  841. * @param {String|jQuery} field The field name or field element
  842. * @returns {Boolean}
  843. */
  844. isValidField: function(field) {
  845. var fields = $([]);
  846. switch (typeof field) {
  847. case 'object':
  848. fields = field;
  849. field = field.attr('data-bv-field');
  850. break;
  851. case 'string':
  852. fields = this.getFieldElements(field);
  853. break;
  854. default:
  855. break;
  856. }
  857. if (fields.length == 0 || this.options.fields[field] == null || this.options.fields[field]['enabled'] == false) {
  858. return true;
  859. }
  860. var type = fields.attr('type'),
  861. total = ('radio' == type || 'checkbox' == type) ? 1 : fields.length,
  862. $field, validatorName, status;
  863. for (var i = 0; i < total; i++) {
  864. $field = fields.eq(i);
  865. if (this._isExcluded($field)) {
  866. continue;
  867. }
  868. for (validatorName in this.options.fields[field].validators) {
  869. status = $field.data('bv.result.' + validatorName);
  870. if (status != this.STATUS_VALID) {
  871. return false;
  872. }
  873. }
  874. }
  875. return true;
  876. },
  877. /**
  878. * Check if all fields inside a given container are valid.
  879. * It's useful when working with a wizard-like such as tab, collapse
  880. *
  881. * @param {jQuery} $container The container element
  882. * @returns {Boolean}
  883. */
  884. isValidContainer: function($container) {
  885. var that = this, map = {};
  886. $container.find('[data-bv-field]').each(function() {
  887. var field = $(this).attr('data-bv-field');
  888. if (!map[field]) {
  889. map[field] = $(this);
  890. }
  891. });
  892. for (var field in map) {
  893. var $f = map[field];
  894. if ($f.data('bv.messages')
  895. .find('.help-block[data-bv-validator][data-bv-for="' + field + '"]')
  896. .filter(function() {
  897. var v = $(this).attr('data-bv-validator');
  898. return ($f.data('bv.result.' + v) && $f.data('bv.result.' + v) != that.STATUS_VALID);
  899. })
  900. .length != 0)
  901. {
  902. // The field is not valid
  903. return false;
  904. }
  905. }
  906. return true;
  907. },
  908. /**
  909. * Submit the form using default submission.
  910. * It also does not perform any validations when submitting the form
  911. *
  912. * It might be used when you want to submit the form right inside the submitHandler()
  913. */
  914. defaultSubmit: function() {
  915. if (this.$submitButton) {
  916. // Create hidden input to send the submit buttons
  917. $('<input/>')
  918. .attr('type', 'hidden')
  919. .attr('data-bv-submit-hidden', '')
  920. .attr('name', this.$submitButton.attr('name'))
  921. .val(this.$submitButton.val())
  922. .appendTo(this.$form);
  923. }
  924. // Submit form
  925. this.$form.off('submit.bv').submit();
  926. },
  927. // Useful APIs which aren't used internally
  928. /**
  929. * Get the list of invalid fields
  930. *
  931. * @returns {jQuery[]}
  932. */
  933. getInvalidFields: function() {
  934. return this.$invalidFields;
  935. },
  936. /**
  937. * Returns the clicked submit button
  938. *
  939. * @returns {jQuery}
  940. */
  941. getSubmitButton: function() {
  942. return this.$submitButton;
  943. },
  944. /**
  945. * Get the error messages
  946. *
  947. * @param {String|jQuery} [field] The field name or field element
  948. * If the field is not defined, the method returns all error messages of all fields
  949. * @returns {String[]}
  950. */
  951. getErrors: function(field) {
  952. var that = this,
  953. messages = [],
  954. $fields = $([]);
  955. switch (true) {
  956. case (field && 'object' == typeof field):
  957. $fields = field;
  958. break;
  959. case (field && 'string' == typeof field):
  960. var f = this.getFieldElements(field);
  961. if (f.length > 0) {
  962. var type = f.attr('type');
  963. $fields = ('radio' == type || 'checkbox' == type) ? f.eq(0) : f;
  964. }
  965. break;
  966. default:
  967. $fields = this.$invalidFields;
  968. break;
  969. }
  970. $fields.each(function() {
  971. messages = messages.concat(
  972. $(this)
  973. .data('bv.messages')
  974. .find('.help-block[data-bv-for="' + $(this).attr('data-bv-field') + '"][data-bv-result="' + that.STATUS_INVALID + '"]')
  975. .map(function() {
  976. return $(this).html()
  977. })
  978. .get()
  979. );
  980. });
  981. return messages;
  982. },
  983. /**
  984. * Add a new field
  985. *
  986. * @param {String|jQuery} field The field name or field element
  987. * @param {Object} [options] The validator rules
  988. * @returns {BootstrapValidator}
  989. */
  990. addField: function(field, options) {
  991. var fields = $([]);
  992. switch (typeof field) {
  993. case 'object':
  994. fields = field;
  995. field = field.attr('data-bv-field') || field.attr('name');
  996. break;
  997. case 'string':
  998. delete this._cacheFields[field];
  999. fields = this.getFieldElements(field);
  1000. break;
  1001. default:
  1002. break;
  1003. }
  1004. fields.attr('data-bv-field', field);
  1005. var type = fields.attr('type'),
  1006. total = ('radio' == type || 'checkbox' == type) ? 1 : fields.length;
  1007. for (var i = 0; i < total; i++) {
  1008. var $field = fields.eq(i);
  1009. // Try to parse the options from HTML attributes
  1010. var opts = this._parseOptions($field);
  1011. opts = (opts == null) ? options : $.extend(true, options, opts);
  1012. this.options.fields[field] = $.extend(true, this.options.fields[field], opts);
  1013. // Update the cache
  1014. this._cacheFields[field] = this._cacheFields[field] ? this._cacheFields[field].add($field) : $field;
  1015. // Init the element
  1016. this._initField(('checkbox' == type || 'radio' == type) ? field : $field);
  1017. }
  1018. this.disableSubmitButtons(false);
  1019. // Trigger an event
  1020. this.$form.trigger($.Event('added.field.bv'), {
  1021. field: field,
  1022. element: fields,
  1023. options: this.options.fields[field]
  1024. });
  1025. return this;
  1026. },
  1027. /**
  1028. * Remove a given field
  1029. *
  1030. * @param {String|jQuery} field The field name or field element
  1031. * @returns {BootstrapValidator}
  1032. */
  1033. removeField: function(field) {
  1034. var fields = $([]);
  1035. switch (typeof field) {
  1036. case 'object':
  1037. fields = field;
  1038. field = field.attr('data-bv-field') || field.attr('name');
  1039. fields.attr('data-bv-field', field);
  1040. break;
  1041. case 'string':
  1042. fields = this.getFieldElements(field);
  1043. break;
  1044. default:
  1045. break;
  1046. }
  1047. if (fields.length == 0) {
  1048. return this;
  1049. }
  1050. var type = fields.attr('type'),
  1051. total = ('radio' == type || 'checkbox' == type) ? 1 : fields.length;
  1052. for (var i = 0; i < total; i++) {
  1053. var $field = fields.eq(i);
  1054. // Remove from the list of invalid fields
  1055. this.$invalidFields = this.$invalidFields.not($field);
  1056. // Update the cache
  1057. this._cacheFields[field] = this._cacheFields[field].not($field);
  1058. }
  1059. if (!this._cacheFields[field] || this._cacheFields[field].length == 0) {
  1060. delete this.options.fields[field];
  1061. }
  1062. if ('checkbox' == type || 'radio' == type) {
  1063. this._initField(field);
  1064. }
  1065. this.disableSubmitButtons(false);
  1066. // Trigger an event
  1067. this.$form.trigger($.Event('removed.field.bv'), {
  1068. field: field,
  1069. element: fields
  1070. });
  1071. return this;
  1072. },
  1073. /**
  1074. * Reset the form
  1075. *
  1076. * @param {Boolean} [resetFormData] Reset current form data
  1077. * @returns {BootstrapValidator}
  1078. */
  1079. resetForm: function(resetFormData) {
  1080. var field, fields, total, type, validator;
  1081. for (field in this.options.fields) {
  1082. fields = this.getFieldElements(field);
  1083. total = fields.length;
  1084. for (var i = 0; i < total; i++) {
  1085. for (validator in this.options.fields[field].validators) {
  1086. fields.eq(i).removeData('bv.dfs.' + validator);
  1087. }
  1088. }
  1089. // Mark field as not validated yet
  1090. this.updateStatus(field, this.STATUS_NOT_VALIDATED);
  1091. if (resetFormData) {
  1092. type = fields.attr('type');
  1093. ('radio' == type || 'checkbox' == type) ? fields.removeAttr('checked').removeAttr('selected') : fields.val('');
  1094. }
  1095. }
  1096. this.$invalidFields = $([]);
  1097. this.$submitButton = null;
  1098. // Enable submit buttons
  1099. this.disableSubmitButtons(false);
  1100. return this;
  1101. },
  1102. /**
  1103. * Enable/Disable all validators to given field
  1104. *
  1105. * @param {String} field The field name
  1106. * @param {Boolean} enabled Enable/Disable field validators
  1107. * @returns {BootstrapValidator}
  1108. */
  1109. enableFieldValidators: function(field, enabled) {
  1110. this.options.fields[field]['enabled'] = enabled;
  1111. this.updateStatus(field, this.STATUS_NOT_VALIDATED);
  1112. return this;
  1113. },
  1114. /**
  1115. * Destroy the plugin
  1116. * It will remove all error messages, feedback icons and turn off the events
  1117. */
  1118. destroy: function() {
  1119. var field, fields, $field, validator, $icon, container;
  1120. for (field in this.options.fields) {
  1121. fields = this.getFieldElements(field);
  1122. container = this.options.fields[field].container || this.options.container;
  1123. for (var i = 0; i < fields.length; i++) {
  1124. $field = fields.eq(i);
  1125. $field
  1126. // Remove all error messages
  1127. .data('bv.messages')
  1128. .find('.help-block[data-bv-validator][data-bv-for="' + field + '"]').remove().end()
  1129. .end()
  1130. .removeData('bv.messages')
  1131. // Remove feedback classes
  1132. .parents('.form-group')
  1133. .removeClass('has-feedback has-error has-success')
  1134. .end()
  1135. // Turn off events
  1136. .off('.bv')
  1137. .removeAttr('data-bv-field');
  1138. // Remove feedback icons, tooltip/popover container
  1139. $icon = $field.parents('.form-group').find('i[data-bv-icon-for="' + field + '"]');
  1140. if ($icon) {
  1141. switch (container) {
  1142. case 'tooltip':
  1143. $icon.tooltip('destroy').remove();
  1144. break;
  1145. case 'popover':
  1146. $icon.popover('destroy').remove();
  1147. break;
  1148. default:
  1149. $icon.remove();
  1150. break;
  1151. }
  1152. }
  1153. for (validator in this.options.fields[field].validators) {
  1154. $field.removeData('bv.result.' + validator).removeData('bv.dfs.' + validator);
  1155. }
  1156. }
  1157. }
  1158. // Enable submit buttons
  1159. this.disableSubmitButtons(false);
  1160. this.$form
  1161. .removeClass(this.options.elementClass)
  1162. .off('.bv')
  1163. .removeData('bootstrapValidator')
  1164. // Remove generated hidden elements
  1165. .find('[data-bv-submit-hidden]').remove();
  1166. }
  1167. };
  1168. // Plugin definition
  1169. $.fn.bootstrapValidator = function(option) {
  1170. var params = arguments;
  1171. return this.each(function() {
  1172. var $this = $(this),
  1173. data = $this.data('bootstrapValidator'),
  1174. options = 'object' == typeof option && option;
  1175. if (!data) {
  1176. data = new BootstrapValidator(this, options);
  1177. $this.data('bootstrapValidator', data);
  1178. }
  1179. // Allow to call plugin method
  1180. if ('string' == typeof option) {
  1181. data[option].apply(data, Array.prototype.slice.call(params, 1));
  1182. }
  1183. });
  1184. };
  1185. // Available validators
  1186. $.fn.bootstrapValidator.validators = {};
  1187. $.fn.bootstrapValidator.Constructor = BootstrapValidator;
  1188. // Helper methods, which can be used in validator class
  1189. $.fn.bootstrapValidator.helpers = {
  1190. /**
  1191. * Validate a date
  1192. *
  1193. * @param {Number} year The full year in 4 digits
  1194. * @param {Number} month The month number
  1195. * @param {Number} day The day number
  1196. * @param {Boolean} [notInFuture] If true, the date must not be in the future
  1197. * @returns {Boolean}
  1198. */
  1199. date: function(year, month, day, notInFuture) {
  1200. if (isNaN(year) || isNaN(month) || isNaN(day)) {
  1201. return false;
  1202. }
  1203. if (year < 1000 || year > 9999 || month == 0 || month > 12) {
  1204. return false;
  1205. }
  1206. var numDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  1207. // Update the number of days in Feb of leap year
  1208. if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
  1209. numDays[1] = 29;
  1210. }
  1211. // Check the day
  1212. if (day < 0 || day > numDays[month - 1]) {
  1213. return false;
  1214. }
  1215. if (notInFuture === true) {
  1216. var currentDate = new Date(),
  1217. currentYear = currentDate.getFullYear(),
  1218. currentMonth = currentDate.getMonth(),
  1219. currentDay = currentDate.getDate();
  1220. return (year < currentYear
  1221. || (year == currentYear && month - 1 < currentMonth)
  1222. || (year == currentYear && month - 1 == currentMonth && day < currentDay));
  1223. }
  1224. return true;
  1225. },
  1226. /**
  1227. * Implement Luhn validation algorithm
  1228. * Credit to https://gist.github.com/ShirtlessKirk/2134376
  1229. *
  1230. * @see http://en.wikipedia.org/wiki/Luhn
  1231. * @param {String} value
  1232. * @returns {Boolean}
  1233. */
  1234. luhn: function(value) {
  1235. var length = value.length,
  1236. mul = 0,
  1237. prodArr = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]],
  1238. sum = 0;
  1239. while (length--) {
  1240. sum += prodArr[mul][parseInt(value.charAt(length), 10)];
  1241. mul ^= 1;
  1242. }
  1243. return (sum % 10 === 0 && sum > 0);
  1244. },
  1245. /**
  1246. * Implement modulus 11, 10 (ISO 7064) algorithm
  1247. *
  1248. * @param {String} value
  1249. * @returns {Boolean}
  1250. */
  1251. mod_11_10: function(value) {
  1252. var check = 5,
  1253. length = value.length;
  1254. for (var i = 0; i < length; i++) {
  1255. check = (((check || 10) * 2) % 11 + parseInt(value.charAt(i), 10)) % 10;
  1256. }
  1257. return (check == 1);
  1258. },
  1259. /**
  1260. * Implements Mod 37, 36 (ISO 7064) algorithm
  1261. * Usages:
  1262. * mod_37_36('A12425GABC1234002M')
  1263. * mod_37_36('002006673085', '0123456789')
  1264. *
  1265. * @param {String} value
  1266. * @param {String} alphabet
  1267. * @returns {Boolean}
  1268. */
  1269. mod_37_36: function(value, alphabet) {
  1270. alphabet = alphabet || '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  1271. var modulus = alphabet.length,
  1272. length = value.length,
  1273. check = Math.floor(modulus / 2);
  1274. for (var i = 0; i < length; i++) {
  1275. check = (((check || modulus) * 2) % (modulus + 1) + alphabet.indexOf(value.charAt(i))) % modulus;
  1276. }
  1277. return (check == 1);
  1278. }
  1279. };
  1280. }(window.jQuery));