bootstrapValidator.js 66 KB

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