bootstrapValidator.js 62 KB

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