bootstrapValidator.js 70 KB

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