bootstrapValidator.js 69 KB

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