bootstrapValidator.js 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192
  1. /**
  2. * BootstrapValidator (http://bootstrapvalidator.com)
  3. *
  4. * A jQuery plugin to validate form fields. Use with Bootstrap 3
  5. *
  6. * @version v0.4.1-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.$invalidField = null; // First invalid field
  16. this.$submitButton = null; // The submit button which is clicked to submit form
  17. // Validating status
  18. this.STATUS_NOT_VALIDATED = 'NOT_VALIDATED';
  19. this.STATUS_VALIDATING = 'VALIDATING';
  20. this.STATUS_INVALID = 'INVALID';
  21. this.STATUS_VALID = 'VALID';
  22. // Determine the event that is fired when user change the field value
  23. // Most modern browsers supports input event except IE 7, 8.
  24. // IE 9 supports input event but the event is still not fired if I press the backspace key.
  25. // In that case I will use the keydown event
  26. var el = document.createElement('div');
  27. this._changeEvent = ('oninput' in el) ? 'input' : 'keydown';
  28. this._init();
  29. };
  30. // The default options
  31. BootstrapValidator.DEFAULT_OPTIONS = {
  32. // The form CSS class
  33. elementClass: 'bootstrap-validator-form',
  34. // Default invalid message
  35. message: 'This value is not valid',
  36. // Shows ok/error/loading icons based on the field validity.
  37. // This feature requires Bootstrap v3.1.0 or later (http://getbootstrap.com/css/#forms-control-validation).
  38. // Since Bootstrap doesn't provide any methods to know its version, this option cannot be on/off automatically.
  39. // In other word, to use this feature you have to upgrade your Bootstrap to v3.1.0 or later.
  40. //
  41. // Examples:
  42. // - Use Glyphicons icons:
  43. // feedbackIcons: {
  44. // valid: 'glyphicon glyphicon-ok',
  45. // invalid: 'glyphicon glyphicon-remove',
  46. // validating: 'glyphicon glyphicon-refresh'
  47. // }
  48. // - Use FontAwesome icons:
  49. // feedbackIcons: {
  50. // valid: 'fa fa-check',
  51. // invalid: 'fa fa-times',
  52. // validating: 'fa fa-refresh'
  53. // }
  54. feedbackIcons: {
  55. valid: null,
  56. invalid: null,
  57. validating: null
  58. },
  59. // The submit buttons selector
  60. // These buttons will be disabled to prevent the valid form from multiple submissions
  61. submitButtons: 'button[type="submit"]',
  62. // The custom submit handler
  63. // It will prevent the form from the default submission
  64. //
  65. // submitHandler: function(validator, form) {
  66. // - validator is the BootstrapValidator instance
  67. // - form is the jQuery object present the current form
  68. // }
  69. submitHandler: null,
  70. // Live validating option
  71. // Can be one of 3 values:
  72. // - enabled: The plugin validates fields as soon as they are changed
  73. // - disabled: Disable the live validating. The error messages are only shown after the form is submitted
  74. // - submitted: The live validating is enabled after the form is submitted
  75. live: 'enabled',
  76. // Map the field name with validator rules
  77. fields: null
  78. };
  79. BootstrapValidator.prototype = {
  80. constructor: BootstrapValidator,
  81. /**
  82. * Init form
  83. */
  84. _init: function() {
  85. var that = this,
  86. options = {
  87. trigger: this.$form.attr('data-bv-trigger'),
  88. message: this.$form.attr('data-bv-message'),
  89. submitButtons: this.$form.attr('data-bv-submitbuttons'),
  90. live: this.$form.attr('data-bv-live'),
  91. fields: {},
  92. feedbackIcons: {
  93. valid: this.$form.attr('data-bv-feedbackicons-valid'),
  94. invalid: this.$form.attr('data-bv-feedbackicons-invalid'),
  95. validating: this.$form.attr('data-bv-feedbackicons-validating')
  96. }
  97. },
  98. validator,
  99. v, // Validator name
  100. enabled,
  101. optionName,
  102. optionValue,
  103. html5AttrName,
  104. html5Attrs;
  105. this.$form
  106. // Disable client side validation in HTML 5
  107. .attr('novalidate', 'novalidate')
  108. .addClass(this.options.elementClass)
  109. // Disable the default submission first
  110. .on('submit.bv', function(e) {
  111. e.preventDefault();
  112. that.validate();
  113. })
  114. .on('click', this.options.submitButtons, function() {
  115. that.$submitButton = $(this);
  116. })
  117. // Find all fields which have either "name" or "data-bv-field" attribute
  118. .find('[name], [data-bv-field]').each(function() {
  119. var $field = $(this);
  120. // Don't initialize hidden input
  121. if ('hidden' == $field.attr('type')) {
  122. return;
  123. }
  124. var field = $field.attr('name') || $field.attr('data-bv-field');
  125. $field.attr('data-bv-field', field);
  126. options.fields[field] = $.extend({}, {
  127. trigger: $field.attr('data-bv-trigger'),
  128. message: $field.attr('data-bv-message'),
  129. container: $field.attr('data-bv-container'),
  130. selector: $field.attr('data-bv-selector'),
  131. validators: {}
  132. }, options.fields[field]);
  133. for (v in $.fn.bootstrapValidator.validators) {
  134. validator = $.fn.bootstrapValidator.validators[v];
  135. enabled = $field.attr('data-bv-' + v.toLowerCase()) + '';
  136. html5Attrs = ('function' == typeof validator.enableByHtml5) ? validator.enableByHtml5($(this)) : null;
  137. if ((html5Attrs && enabled != 'false')
  138. || (html5Attrs !== true && ('' == enabled || 'true' == enabled)))
  139. {
  140. // Try to parse the options via attributes
  141. validator.html5Attributes = validator.html5Attributes || { message: 'message' };
  142. options.fields[field]['validators'][v] = $.extend({}, html5Attrs == true ? {} : html5Attrs, options.fields[field]['validators'][v]);
  143. for (html5AttrName in validator.html5Attributes) {
  144. optionName = validator.html5Attributes[html5AttrName];
  145. optionValue = $field.attr('data-bv-' + v.toLowerCase() + '-' + html5AttrName);
  146. if (optionValue) {
  147. if ('true' == optionValue) {
  148. optionValue = true;
  149. } else if ('false' == optionValue) {
  150. optionValue = false;
  151. }
  152. options.fields[field]['validators'][v][optionName] = optionValue;
  153. }
  154. }
  155. }
  156. }
  157. });
  158. this.options = $.extend(true, this.options, options);
  159. for (var field in this.options.fields) {
  160. this._initField(field);
  161. }
  162. this.setLiveMode(this.options.live);
  163. },
  164. /**
  165. * Init field
  166. *
  167. * @param {String} field The field name
  168. */
  169. _initField: function(field) {
  170. if (this.options.fields[field] == null || this.options.fields[field].validators == null) {
  171. return;
  172. }
  173. var fields = this.getFieldElements(field);
  174. // We don't need to validate non-existing fields
  175. if (fields == null) {
  176. delete this.options.fields[field];
  177. return;
  178. }
  179. for (var validatorName in this.options.fields[field].validators) {
  180. if (!$.fn.bootstrapValidator.validators[validatorName]) {
  181. delete this.options.fields[field].validators[validatorName];
  182. }
  183. }
  184. var that = this,
  185. type = fields.attr('type'),
  186. event = ('radio' == type || 'checkbox' == type || 'file' == type || 'SELECT' == fields[0].tagName) ? 'change' : that._changeEvent,
  187. total = fields.length,
  188. updateAll = (total == 1) || ('radio' == type) || ('checkbox' == type);
  189. for (var i = 0; i < total; i++) {
  190. var $field = $(fields[i]),
  191. $parent = $field.parents('.form-group'),
  192. // Allow user to indicate where the error messages are shown
  193. $message = this.options.fields[field].container ? $parent.find(this.options.fields[field].container) : this._getMessageContainer($field);
  194. // Set the attribute to indicate the fields which are defined by selector
  195. if (!$field.attr('data-bv-field')) {
  196. $field.attr('data-bv-field', field);
  197. }
  198. // Whenever the user change the field value, mark it as not validated yet
  199. $field.on(event + '.update.bv', function() {
  200. updateAll ? that.updateStatus(field, that.STATUS_NOT_VALIDATED, null)
  201. : that.updateElementStatus($(this), that.STATUS_NOT_VALIDATED, null);
  202. });
  203. // Create help block elements for showing the error messages
  204. $field.data('bv.messages', $message);
  205. for (validatorName in this.options.fields[field].validators) {
  206. $field.data('bv.result.' + validatorName, this.STATUS_NOT_VALIDATED);
  207. if (!updateAll || i == total - 1) {
  208. $('<small/>')
  209. .css('display', 'none')
  210. .attr('data-bv-validator', validatorName)
  211. .html(this.options.fields[field].validators[validatorName].message || this.options.fields[field].message || this.options.message)
  212. .addClass('help-block')
  213. .appendTo($message);
  214. }
  215. }
  216. // Prepare the feedback icons
  217. // Available from Bootstrap 3.1 (http://getbootstrap.com/css/#forms-control-validation)
  218. if (this.options.feedbackIcons
  219. && this.options.feedbackIcons.validating && this.options.feedbackIcons.invalid && this.options.feedbackIcons.valid
  220. && (!updateAll || i == total - 1))
  221. {
  222. $parent.addClass('has-feedback');
  223. var $icon = $('<i/>').css('display', 'none').addClass('form-control-feedback').attr('data-bv-field', field).insertAfter($field);
  224. // The feedback icon does not render correctly if there is no label
  225. // https://github.com/twbs/bootstrap/issues/12873
  226. if ($parent.find('label').length == 0) {
  227. $icon.css('top', 0);
  228. }
  229. }
  230. }
  231. if (this.options.fields[field]['enabled'] == null) {
  232. this.options.fields[field]['enabled'] = true;
  233. }
  234. },
  235. /**
  236. * Get the element to place the error messages
  237. *
  238. * @param {jQuery} $field The field element
  239. * @returns {jQuery}
  240. */
  241. _getMessageContainer: function($field) {
  242. var $parent = $field.parent();
  243. if ($parent.hasClass('form-group')) {
  244. return $parent;
  245. }
  246. var cssClasses = $parent.attr('class');
  247. if (!cssClasses) {
  248. return this._getMessageContainer($parent);
  249. }
  250. cssClasses = cssClasses.split(' ');
  251. var n = cssClasses.length;
  252. for (var i = 0; i < n; i++) {
  253. if (/^col-(xs|sm|md|lg)-\d+$/.test(cssClasses[i]) || /^col-(xs|sm|md|lg)-offset-\d+$/.test(cssClasses[i])) {
  254. return $parent;
  255. }
  256. }
  257. return this._getMessageContainer($parent);
  258. },
  259. /**
  260. * Called when all validations are completed
  261. */
  262. _submit: function() {
  263. if (!this.isValid()) {
  264. if ('submitted' == this.options.live) {
  265. this.setLiveMode('enabled');
  266. }
  267. // Focus to the first invalid field
  268. if (this.$invalidField) {
  269. this.$invalidField.focus();
  270. }
  271. return;
  272. }
  273. // Call the custom submission if enabled
  274. if (this.options.submitHandler && 'function' == typeof this.options.submitHandler) {
  275. // Turn off the submit handler, so user can call form.submit() inside their submitHandler method
  276. this.$form.off('submit.bv');
  277. this.options.submitHandler.call(this, this, this.$form, this.$submitButton);
  278. } else {
  279. this.disableSubmitButtons(true);
  280. // Submit form
  281. this.$form.off('submit.bv').submit();
  282. }
  283. },
  284. // --- Public methods ---
  285. /**
  286. * Retrieve the field elements by given name
  287. *
  288. * @param {String} field The field name
  289. * @returns {null|jQuery[]}
  290. */
  291. getFieldElements: function(field) {
  292. var fields = this.$form.find(this.options.fields[field].selector || '[name="' + field + '"]');
  293. return (fields.length == 0) ? null : fields;
  294. },
  295. /**
  296. * Set live validating mode
  297. *
  298. * @param {String} mode Live validating mode. Can be 'enabled', 'disabled', 'submitted'
  299. * @returns {BootstrapValidator}
  300. */
  301. setLiveMode: function(mode) {
  302. this.options.live = mode;
  303. if ('submitted' == mode) {
  304. return this;
  305. }
  306. var that = this;
  307. for (var field in this.options.fields) {
  308. (function(f) {
  309. var fields = that.getFieldElements(f);
  310. if (fields) {
  311. var type = fields.attr('type'),
  312. total = fields.length,
  313. updateAll = (total == 1) || ('radio' == type) || ('checkbox' == type),
  314. trigger = that.options.fields[field].trigger
  315. || that.options.trigger
  316. || (('radio' == type || 'checkbox' == type || 'file' == type || 'SELECT' == fields[0].tagName) ? 'change' : that._changeEvent),
  317. events = $.map(trigger.split(' '), function(item) {
  318. return item + '.live.bv';
  319. }).join(' ');
  320. for (var i = 0; i < total; i++) {
  321. ('enabled' == mode)
  322. ? $(fields[i]).on(events, function() {
  323. updateAll ? that.validateField(f) : that.validateFieldElement($(this), false);
  324. })
  325. : $(fields[i]).off(events);
  326. }
  327. }
  328. })(field);
  329. }
  330. return this;
  331. },
  332. /**
  333. * Disable/enable submit buttons
  334. *
  335. * @param {Boolean} disabled Can be true or false
  336. * @returns {BootstrapValidator}
  337. */
  338. disableSubmitButtons: function(disabled) {
  339. if (!disabled) {
  340. this.$form.find(this.options.submitButtons).removeAttr('disabled');
  341. } else if (this.options.live != 'disabled') {
  342. // Don't disable if the live validating mode is disabled
  343. this.$form.find(this.options.submitButtons).attr('disabled', 'disabled');
  344. }
  345. return this;
  346. },
  347. /**
  348. * Validate the form
  349. *
  350. * @return {BootstrapValidator}
  351. */
  352. validate: function() {
  353. if (!this.options.fields) {
  354. return this;
  355. }
  356. this.disableSubmitButtons(true);
  357. for (var field in this.options.fields) {
  358. this.validateField(field);
  359. }
  360. this._submit();
  361. return this;
  362. },
  363. /**
  364. * Validate given field
  365. *
  366. * @param {String} field The field name
  367. * @returns {BootstrapValidator}
  368. */
  369. validateField: function(field) {
  370. var fields = this.getFieldElements(field),
  371. type = fields.attr('type'),
  372. n = (('radio' == type) || ('checkbox' == type)) ? 1 : fields.length;
  373. for (var i = 0; i < n; i++) {
  374. this.validateFieldElement($(fields[i]), (n == 1));
  375. }
  376. return this;
  377. },
  378. /**
  379. * Validate field element
  380. *
  381. * @param {jQuery} $field The field element
  382. * @param {Boolean} updateAll If true, update status of all elements which have the same name
  383. * @returns {BootstrapValidator}
  384. */
  385. validateFieldElement: function($field, updateAll) {
  386. var that = this,
  387. field = $field.attr('data-bv-field'),
  388. validators = this.options.fields[field].validators,
  389. validatorName,
  390. validateResult;
  391. // We don't need to validate disabled, hidden field
  392. if ($field.is(':disabled') || $field.is(':hidden') || !$field.is(':visible')) {
  393. return this;
  394. }
  395. for (validatorName in validators) {
  396. if ($field.data('bv.dfs.' + validatorName)) {
  397. $field.data('bv.dfs.' + validatorName).reject();
  398. }
  399. // Don't validate field if it is already done
  400. var result = $field.data('bv.result.' + validatorName);
  401. if (result == this.STATUS_VALID || result == this.STATUS_INVALID) {
  402. continue;
  403. }
  404. $field.data('bv.result.' + validatorName, this.STATUS_VALIDATING);
  405. validateResult = $.fn.bootstrapValidator.validators[validatorName].validate(this, $field, validators[validatorName]);
  406. if ('object' == typeof validateResult) {
  407. updateAll ? this.updateStatus(field, this.STATUS_VALIDATING, validatorName)
  408. : this.updateElementStatus($field, this.STATUS_VALIDATING, validatorName);
  409. $field.data('bv.dfs.' + validatorName, validateResult);
  410. validateResult.done(function($f, v, isValid) {
  411. // v is validator name
  412. $f.removeData('bv.dfs.' + v);
  413. updateAll ? that.updateStatus($f.attr('data-bv-field'), isValid ? that.STATUS_VALID : that.STATUS_INVALID, v)
  414. : that.updateElementStatus($f, isValid ? that.STATUS_VALID : that.STATUS_INVALID, v);
  415. if (isValid && 'disabled' == that.options.live) {
  416. that._submit();
  417. }
  418. });
  419. } else if ('boolean' == typeof validateResult) {
  420. updateAll ? this.updateStatus(field, validateResult ? this.STATUS_VALID : this.STATUS_INVALID, validatorName)
  421. : this.updateElementStatus($field, validateResult ? this.STATUS_VALID : this.STATUS_INVALID, validatorName);
  422. }
  423. }
  424. return this;
  425. },
  426. /**
  427. * Update all validating results of elements which have the same field name
  428. *
  429. * @param {String} field The field name
  430. * @param {String} status The status. Can be 'NOT_VALIDATED', 'VALIDATING', 'INVALID' or 'VALID'
  431. * @param {String} [validatorName] The validator name. If null, the method updates validity result for all validators
  432. * @return {BootstrapValidator}
  433. */
  434. updateStatus: function(field, status, validatorName) {
  435. var fields = this.getFieldElements(field),
  436. type = fields.attr('type'),
  437. n = (('radio' == type) || ('checkbox' == type)) ? 1 : fields.length;
  438. for (var i = 0; i < n; i++) {
  439. this.updateElementStatus($(fields[i]), status, validatorName);
  440. }
  441. return this;
  442. },
  443. /**
  444. * Update validating result of given element
  445. *
  446. * @param {jQuery} $field The field element
  447. * @param {String} status The status. Can be 'NOT_VALIDATED', 'VALIDATING', 'INVALID' or 'VALID'
  448. * @param {String} [validatorName] The validator name. If null, the method updates validity result for all validators
  449. * @return {BootstrapValidator}
  450. */
  451. updateElementStatus: function($field, status, validatorName) {
  452. var that = this,
  453. field = $field.attr('data-bv-field'),
  454. $parent = $field.parents('.form-group'),
  455. $message = $field.data('bv.messages'),
  456. $rowErrors = $parent.find('.help-block[data-bv-validator]'),
  457. $errors = $message.find('.help-block[data-bv-validator]'),
  458. $icon = $parent.find('.form-control-feedback[data-bv-field="' + field + '"]');
  459. // Update status
  460. if (validatorName) {
  461. $field.data('bv.result.' + validatorName, status);
  462. } else {
  463. for (var v in this.options.fields[field].validators) {
  464. $field.data('bv.result.' + v, status);
  465. }
  466. }
  467. // Show/hide error elements and feedback icons
  468. switch (status) {
  469. case this.STATUS_VALIDATING:
  470. this.disableSubmitButtons(true);
  471. $parent.removeClass('has-success').removeClass('has-error');
  472. // TODO: Show validating message
  473. validatorName ? $errors.filter('.help-block[data-bv-validator="' + validatorName + '"]').hide() : $errors.hide();
  474. if ($icon) {
  475. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.invalid).addClass(this.options.feedbackIcons.validating).show();
  476. }
  477. break;
  478. case this.STATUS_INVALID:
  479. this.disableSubmitButtons(true);
  480. $parent.removeClass('has-success').addClass('has-error');
  481. validatorName ? $errors.filter('[data-bv-validator="' + validatorName + '"]').show() : $errors.show();
  482. if ($icon) {
  483. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.validating).addClass(this.options.feedbackIcons.invalid).show();
  484. }
  485. break;
  486. case this.STATUS_VALID:
  487. validatorName ? $errors.filter('[data-bv-validator="' + validatorName + '"]').hide() : $errors.hide();
  488. // If the field is valid (passes all validators)
  489. var validField = ($errors.filter(function() {
  490. var display = $(this).css('display'), v = $(this).attr('data-bv-validator');
  491. return ('block' == display) || ($field.data('bv.result.' + v) != that.STATUS_VALID);
  492. }).length == 0);
  493. this.disableSubmitButtons(validField ? false : true);
  494. if ($icon) {
  495. $icon
  496. .removeClass(this.options.feedbackIcons.invalid).removeClass(this.options.feedbackIcons.validating).removeClass(this.options.feedbackIcons.valid)
  497. .addClass(validField ? this.options.feedbackIcons.valid : this.options.feedbackIcons.invalid)
  498. .show();
  499. }
  500. // Check if all fields in the same row are valid
  501. var validRow = ($rowErrors.filter(function() {
  502. var display = $(this).css('display'), v = $(this).attr('data-bv-validator');
  503. return ('block' == display) || ($field.data('bv.result.' + v) != that.STATUS_VALID);
  504. }).length == 0);
  505. $parent.removeClass('has-error has-success').addClass(validRow ? 'has-success' : 'has-error');
  506. break;
  507. case this.STATUS_NOT_VALIDATED:
  508. default:
  509. this.disableSubmitButtons(false);
  510. $parent.removeClass('has-success').removeClass('has-error');
  511. validatorName ? $errors.filter('.help-block[data-bv-validator="' + validatorName + '"]').hide() : $errors.hide();
  512. if ($icon) {
  513. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.invalid).removeClass(this.options.feedbackIcons.validating).hide();
  514. }
  515. break;
  516. }
  517. return this;
  518. },
  519. /**
  520. * Check the form validity
  521. *
  522. * @returns {Boolean}
  523. */
  524. isValid: function() {
  525. var fields, field, $field,
  526. type, status, validatorName,
  527. n, i;
  528. for (field in this.options.fields) {
  529. if (this.options.fields[field] == null || !this.options.fields[field]['enabled']) {
  530. continue;
  531. }
  532. fields = this.getFieldElements(field);
  533. type = fields.attr('type');
  534. n = (('radio' == type) || ('checkbox' == type)) ? 1 : fields.length;
  535. for (i = 0; i < n; i++) {
  536. $field = $(fields[i]);
  537. if ($field.is(':disabled') || $field.is(':hidden') || !$field.is(':visible')) {
  538. continue;
  539. }
  540. for (validatorName in this.options.fields[field].validators) {
  541. status = $field.data('bv.result.' + validatorName);
  542. if (status == this.STATUS_NOT_VALIDATED || status == this.STATUS_VALIDATING) {
  543. return false;
  544. }
  545. if (status == this.STATUS_INVALID) {
  546. this.$invalidField = $field;
  547. return false;
  548. }
  549. }
  550. }
  551. }
  552. return true;
  553. },
  554. // Useful APIs which aren't used internally
  555. /**
  556. * Reset the form
  557. *
  558. * @param {Boolean} resetFormData Reset current form data
  559. * @return {BootstrapValidator}
  560. */
  561. resetForm: function(resetFormData) {
  562. var field, fields, total, type, validator;
  563. for (field in this.options.fields) {
  564. fields = this.getFieldElements(field);
  565. total = fields.length;
  566. for (var i = 0; i < total; i++) {
  567. for (validator in this.options.fields[field].validators) {
  568. $(fields[i]).removeData('bv.dfs.' + validator);
  569. }
  570. }
  571. // Mark field as not validated yet
  572. this.updateStatus(field, this.STATUS_NOT_VALIDATED, null);
  573. if (resetFormData) {
  574. type = fields.attr('type');
  575. ('radio' == type || 'checkbox' == type) ? fields.removeAttr('checked').removeAttr('selected') : fields.val('');
  576. }
  577. }
  578. this.$invalidField = null;
  579. this.$submitButton = null;
  580. // Enable submit buttons
  581. this.disableSubmitButtons(false);
  582. return this;
  583. },
  584. /**
  585. * Enable/Disable all validators to given field
  586. *
  587. * @param {String} field The field name
  588. * @param {Boolean} enabled Enable/Disable field validators
  589. * @return {BootstrapValidator}
  590. */
  591. enableFieldValidators: function(field, enabled) {
  592. this.options.fields[field]['enabled'] = enabled;
  593. this.updateStatus(field, this.STATUS_NOT_VALIDATED, null);
  594. return this;
  595. }
  596. };
  597. // Plugin definition
  598. $.fn.bootstrapValidator = function(options) {
  599. return this.each(function() {
  600. var $this = $(this), data = $this.data('bootstrapValidator');
  601. if (!data) {
  602. $this.data('bootstrapValidator', (data = new BootstrapValidator(this, options)));
  603. }
  604. if ('string' == typeof options) {
  605. data[options]();
  606. }
  607. });
  608. };
  609. // Available validators
  610. $.fn.bootstrapValidator.validators = {};
  611. $.fn.bootstrapValidator.Constructor = BootstrapValidator;
  612. }(window.jQuery));
  613. ;(function($) {
  614. $.fn.bootstrapValidator.validators.base64 = {
  615. /**
  616. * Return true if the input value is a base 64 encoded string.
  617. *
  618. * @param {BootstrapValidator} validator The validator plugin instance
  619. * @param {jQuery} $field Field element
  620. * @param {Object} options Can consist of the following keys:
  621. * - message: The invalid message
  622. * @returns {Boolean}
  623. */
  624. validate: function(validator, $field, options) {
  625. var value = $field.val();
  626. if (value == '') {
  627. return true;
  628. }
  629. return /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$/.test(value);
  630. }
  631. };
  632. }(window.jQuery));
  633. ;(function($) {
  634. $.fn.bootstrapValidator.validators.between = {
  635. html5Attributes: {
  636. message: 'message',
  637. min: 'min',
  638. max: 'max',
  639. inclusive: 'inclusive'
  640. },
  641. enableByHtml5: function($field) {
  642. if ('range' == $field.attr('type')) {
  643. return {
  644. min: $field.attr('min'),
  645. max: $field.attr('max')
  646. };
  647. }
  648. return false;
  649. },
  650. /**
  651. * Return true if the input value is between (strictly or not) two given numbers
  652. *
  653. * @param {BootstrapValidator} validator The validator plugin instance
  654. * @param {jQuery} $field Field element
  655. * @param {Object} options Can consist of the following keys:
  656. * - min
  657. * - max
  658. * - inclusive [optional]: Can be true or false. Default is true
  659. * - message: The invalid message
  660. * @returns {Boolean}
  661. */
  662. validate: function(validator, $field, options) {
  663. var value = $field.val();
  664. if (value == '') {
  665. return true;
  666. }
  667. value = parseFloat(value);
  668. return (options.inclusive === true)
  669. ? (value > options.min && value < options.max)
  670. : (value >= options.min && value <= options.max);
  671. }
  672. };
  673. }(window.jQuery));
  674. ;(function($) {
  675. $.fn.bootstrapValidator.validators.callback = {
  676. /**
  677. * Return result from the callback method
  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. * - callback: The callback method that passes 2 parameters:
  683. * callback: function(fieldValue, validator) {
  684. * // fieldValue is the value of field
  685. * // validator is instance of BootstrapValidator
  686. * }
  687. * - message: The invalid message
  688. * @returns {Boolean|Deferred}
  689. */
  690. validate: function(validator, $field, options) {
  691. var value = $field.val();
  692. if (options.callback && 'function' == typeof options.callback) {
  693. var dfd = new $.Deferred();
  694. dfd.resolve($field, 'callback', options.callback.call(this, value, validator));
  695. return dfd;
  696. }
  697. return true;
  698. }
  699. };
  700. }(window.jQuery));
  701. ;(function($) {
  702. $.fn.bootstrapValidator.validators.choice = {
  703. html5Attributes: {
  704. message: 'message',
  705. min: 'min',
  706. max: 'max'
  707. },
  708. /**
  709. * Check if the number of checked boxes are less or more than a given number
  710. *
  711. * @param {BootstrapValidator} validator The validator plugin instance
  712. * @param {jQuery} $field Field element
  713. * @param {Object} options Consists of following keys:
  714. * - min
  715. * - max
  716. * At least one of two keys is required
  717. * - message: The invalid message
  718. * @returns {Boolean}
  719. */
  720. validate: function(validator, $field, options) {
  721. var numChoices = validator
  722. .getFieldElements($field.attr('data-bv-field'))
  723. .filter(':checked')
  724. .length;
  725. if ((options.min && numChoices < options.min) || (options.max && numChoices > options.max)) {
  726. return false;
  727. }
  728. return true;
  729. }
  730. };
  731. }(window.jQuery));
  732. ;(function($) {
  733. $.fn.bootstrapValidator.validators.creditCard = {
  734. /**
  735. * Return true if the input value is valid credit card number
  736. * Based on https://gist.github.com/DiegoSalazar/4075533
  737. *
  738. * @param {BootstrapValidator} validator The validator plugin instance
  739. * @param {jQuery} $field Field element
  740. * @param {Object} options Can consist of the following key:
  741. * - message: The invalid message
  742. * @returns {Boolean}
  743. */
  744. validate: function(validator, $field, options) {
  745. var value = $field.val();
  746. if (value == '') {
  747. return true;
  748. }
  749. // Accept only digits, dashes or spaces
  750. if (/[^0-9-\s]+/.test(value)) {
  751. return false;
  752. }
  753. value = value.replace(/\D/g, '');
  754. // Validate the check sum
  755. // The Luhn Algorithm
  756. // http://en.wikipedia.org/wiki/Luhn
  757. var check = 0, digit = 0, even = false, length = value.length;
  758. for (var n = length - 1; n >= 0; n--) {
  759. digit = parseInt(value.charAt(n), 10);
  760. if (even) {
  761. if ((digit *= 2) > 9) {
  762. digit -= 9;
  763. }
  764. }
  765. check += digit;
  766. even = !even;
  767. }
  768. if ((check % 10) != 0) {
  769. return false;
  770. }
  771. // Validate the card number based on prefix (IIN ranges) and length
  772. var cards = {
  773. AMERICAN_EXPRESS: {
  774. length: [15],
  775. prefix: ['34', '37']
  776. },
  777. DINERS_CLUB: {
  778. length: [14],
  779. prefix: ['300', '301', '302', '303', '304', '305', '36']
  780. },
  781. DINERS_CLUB_US: {
  782. length: [16],
  783. prefix: ['54', '55']
  784. },
  785. DISCOVER: {
  786. length: [16],
  787. prefix: ['6011', '622126', '622127', '622128', '622129', '62213',
  788. '62214', '62215', '62216', '62217', '62218', '62219',
  789. '6222', '6223', '6224', '6225', '6226', '6227', '6228',
  790. '62290', '62291', '622920', '622921', '622922', '622923',
  791. '622924', '622925', '644', '645', '646', '647', '648',
  792. '649', '65']
  793. },
  794. JCB: {
  795. length: [16],
  796. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  797. },
  798. LASER: {
  799. length: [16, 17, 18, 19],
  800. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  801. },
  802. MAESTRO: {
  803. length: [12, 13, 14, 15, 16, 17, 18, 19],
  804. prefix: ['5018', '5020', '5038', '6304', '6759', '6761', '6762', '6763', '6764', '6765', '6766']
  805. },
  806. MASTERCARD: {
  807. length: [16],
  808. prefix: ['51', '52', '53', '54', '55']
  809. },
  810. SOLO: {
  811. length: [16, 18, 19],
  812. prefix: ['6334', '6767']
  813. },
  814. UNIONPAY: {
  815. length: [16, 17, 18, 19],
  816. prefix: ['622126', '622127', '622128', '622129', '62213', '62214',
  817. '62215', '62216', '62217', '62218', '62219', '6222', '6223',
  818. '6224', '6225', '6226', '6227', '6228', '62290', '62291',
  819. '622920', '622921', '622922', '622923', '622924', '622925']
  820. },
  821. VISA: {
  822. length: [16],
  823. prefix: ['4']
  824. }
  825. };
  826. var type, i;
  827. for (type in cards) {
  828. for (i in cards[type]['prefix']) {
  829. if (value.substr(0, cards[type]['prefix'][i].length) == cards[type]['prefix'][i] // Check the prefix
  830. && cards[type]['length'].indexOf(value.length) != -1) // and length
  831. {
  832. return true;
  833. }
  834. }
  835. }
  836. return false;
  837. }
  838. };
  839. }(window.jQuery));
  840. ;(function($) {
  841. $.fn.bootstrapValidator.validators.cvv = {
  842. html5Attributes: {
  843. message: 'message',
  844. ccfield: 'creditCardField'
  845. },
  846. /**
  847. * Return true if the input value is a valid CVV number.
  848. *
  849. * @param {BootstrapValidator} validator The validator plugin instance
  850. * @param {jQuery} $field Field element
  851. * @param {Object} options Can consist of the following keys:
  852. * - creditCardField: The credit card number field. It can be null
  853. * - message: The invalid message
  854. * @returns {Boolean}
  855. */
  856. validate: function(validator, $field, options) {
  857. var value = $field.val();
  858. if (value == '') {
  859. return true;
  860. }
  861. if (!/^[0-9]{3,4}$/.test(value)) {
  862. return false;
  863. }
  864. if (!options.creditCardField) {
  865. return true;
  866. }
  867. // Get the credit card number
  868. var creditCard = validator.getFieldElements(options.creditCardField).val();
  869. if (creditCard == '') {
  870. return true;
  871. }
  872. // Supported credit card types
  873. var cards = {
  874. AMERICAN_EXPRESS: {
  875. length: [15],
  876. prefix: ['34', '37']
  877. },
  878. DINERS_CLUB: {
  879. length: [14],
  880. prefix: ['300', '301', '302', '303', '304', '305', '36']
  881. },
  882. DINERS_CLUB_US: {
  883. length: [16],
  884. prefix: ['54', '55']
  885. },
  886. DISCOVER: {
  887. length: [16],
  888. prefix: ['6011', '622126', '622127', '622128', '622129', '62213',
  889. '62214', '62215', '62216', '62217', '62218', '62219',
  890. '6222', '6223', '6224', '6225', '6226', '6227', '6228',
  891. '62290', '62291', '622920', '622921', '622922', '622923',
  892. '622924', '622925', '644', '645', '646', '647', '648',
  893. '649', '65']
  894. },
  895. JCB: {
  896. length: [16],
  897. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  898. },
  899. LASER: {
  900. length: [16, 17, 18, 19],
  901. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  902. },
  903. MAESTRO: {
  904. length: [12, 13, 14, 15, 16, 17, 18, 19],
  905. prefix: ['5018', '5020', '5038', '6304', '6759', '6761', '6762', '6763', '6764', '6765', '6766']
  906. },
  907. MASTERCARD: {
  908. length: [16],
  909. prefix: ['51', '52', '53', '54', '55']
  910. },
  911. SOLO: {
  912. length: [16, 18, 19],
  913. prefix: ['6334', '6767']
  914. },
  915. UNIONPAY: {
  916. length: [16, 17, 18, 19],
  917. prefix: ['622126', '622127', '622128', '622129', '62213', '62214',
  918. '62215', '62216', '62217', '62218', '62219', '6222', '6223',
  919. '6224', '6225', '6226', '6227', '6228', '62290', '62291',
  920. '622920', '622921', '622922', '622923', '622924', '622925']
  921. },
  922. VISA: {
  923. length: [16],
  924. prefix: ['4']
  925. }
  926. };
  927. var type, i, creditCardType = null;
  928. for (type in cards) {
  929. for (i in cards[type]['prefix']) {
  930. if (creditCard.substr(0, cards[type]['prefix'][i].length) == cards[type]['prefix'][i] // Check the prefix
  931. && cards[type]['length'].indexOf(creditCard.length) != -1) // and length
  932. {
  933. creditCardType = type;
  934. break;
  935. }
  936. }
  937. }
  938. return (creditCardType == null)
  939. ? false
  940. : (('AMERICAN_EXPRESS' == creditCardType) ? (value.length == 4) : (value.length == 3));
  941. }
  942. };
  943. }(window.jQuery));
  944. ;(function($) {
  945. $.fn.bootstrapValidator.validators.date = {
  946. html5Attributes: {
  947. message: 'message',
  948. format: 'format'
  949. },
  950. /**
  951. * Return true if the input value is valid date
  952. *
  953. * @param {BootstrapValidator} validator The validator plugin instance
  954. * @param {jQuery} $field Field element
  955. * @param {Object} options Can consist of the following keys:
  956. * - format: The date format. Default is MM/DD/YYYY
  957. * Support the following formats:
  958. * YYYY/DD/MM
  959. * YYYY/DD/MM h:m A
  960. * YYYY/MM/DD
  961. * YYYY/MM/DD h:m A
  962. *
  963. * YYYY-DD-MM
  964. * YYYY-DD-MM h:m A
  965. * YYYY-MM-DD
  966. * YYYY-MM-DD h:m A
  967. *
  968. * MM/DD/YYYY
  969. * MM/DD/YYYY h:m A
  970. * DD/MM/YYYY
  971. * DD/MM/YYYY h:m A
  972. *
  973. * MM-DD-YYYY
  974. * MM-DD-YYYY h:m A
  975. * DD-MM-YYYY
  976. * DD-MM-YYYY h:m A
  977. * - message: The invalid message
  978. * @returns {Boolean}
  979. */
  980. validate: function(validator, $field, options) {
  981. var value = $field.val();
  982. if (value == '') {
  983. return true;
  984. }
  985. // Determine the separator
  986. options.format = options.format || 'MM/DD/YYYY';
  987. var separator = (options.format.indexOf('/') != -1)
  988. ? '/'
  989. : ((options.format.indexOf('-') != -1) ? '-' : null);
  990. if (separator == null) {
  991. return false;
  992. }
  993. var month, day, year, minutes = null, hours = null, matches;
  994. switch (true) {
  995. case (separator == '/' && (matches = value.match(/^(\d{4})\/(\d{1,2})\/(\d{1,2})$/i)) && options.format == 'YYYY/DD/MM'):
  996. case (separator == '-' && (matches = value.match(/^(\d{4})-(\d{1,2})-(\d{1,2})$/i)) && options.format == 'YYYY-DD-MM'):
  997. year = matches[1]; day = matches[2]; month = matches[3];
  998. break;
  999. case (separator == '/' && (matches = value.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/i)) && options.format == 'DD/MM/YYYY'):
  1000. case (separator == '-' && (matches = value.match(/^(\d{1,2})-(\d{1,2})-(\d{4})$/i)) && options.format == 'DD-MM-YYYY'):
  1001. day = matches[1]; month = matches[2]; year = matches[3];
  1002. break;
  1003. case (separator == '/' && (matches = value.match(/^(\d{4})\/(\d{1,2})\/(\d{1,2})$/i)) && options.format == 'YYYY/MM/DD'):
  1004. case (separator == '-' && (matches = value.match(/^(\d{4})-(\d{1,2})-(\d{1,2})$/i)) && options.format == 'YYYY-MM-DD'):
  1005. year = matches[1]; month = matches[2]; day = matches[3];
  1006. break;
  1007. case (separator == '/' && (matches = value.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/i)) && options.format == 'MM/DD/YYYY'):
  1008. case (separator == '-' && (matches = value.match(/^(\d{1,2})-(\d{1,2})-(\d{4})$/i)) && options.format == 'MM-DD-YYYY'):
  1009. month = matches[1]; day = matches[2]; year = matches[3];
  1010. break;
  1011. 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'):
  1012. 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'):
  1013. year = matches[1]; day = matches[2]; month = matches[3]; hours = matches[4]; minutes = matches[5];
  1014. break;
  1015. 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'):
  1016. 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'):
  1017. day = matches[1]; month = matches[2]; year = matches[3]; hours = matches[4]; minutes = matches[5];
  1018. break;
  1019. 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'):
  1020. 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'):
  1021. year = matches[1]; month = matches[2]; day = matches[3]; hours = matches[4]; minutes = matches[5];
  1022. break;
  1023. 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'):
  1024. 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'):
  1025. month = matches[1]; day = matches[2]; year = matches[3]; hours = matches[4]; minutes = matches[5];
  1026. break;
  1027. default:
  1028. return false;
  1029. }
  1030. // Validate hours and minutes
  1031. if (hours && minutes) {
  1032. hours = parseInt(hours, 10);
  1033. minutes = parseInt(minutes, 10);
  1034. if (hours < 1 || hours > 12 || minutes < 0 || minutes > 59) {
  1035. return false;
  1036. }
  1037. }
  1038. // Validate day, month, and year
  1039. day = parseInt(day, 10);
  1040. month = parseInt(month, 10);
  1041. year = parseInt(year, 10);
  1042. if (year < 1000 || year > 9999 || month == 0 || month > 12) {
  1043. return false;
  1044. }
  1045. var numDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  1046. // Update the number of days in Feb of leap year
  1047. if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
  1048. numDays[1] = 29;
  1049. }
  1050. // Check the day
  1051. return (day > 0 && day <= numDays[month - 1]);
  1052. }
  1053. };
  1054. }(window.jQuery));
  1055. ;(function($) {
  1056. $.fn.bootstrapValidator.validators.different = {
  1057. html5Attributes: {
  1058. message: 'message',
  1059. field: 'field'
  1060. },
  1061. /**
  1062. * Return true if the input value is different with given field's value
  1063. *
  1064. * @param {BootstrapValidator} validator The validator plugin instance
  1065. * @param {jQuery} $field Field element
  1066. * @param {Object} options Consists of the following key:
  1067. * - field: The name of field that will be used to compare with current one
  1068. * - message: The invalid message
  1069. * @returns {Boolean}
  1070. */
  1071. validate: function(validator, $field, options) {
  1072. var value = $field.val();
  1073. if (value == '') {
  1074. return true;
  1075. }
  1076. var compareWith = validator.getFieldElements(options.field);
  1077. if (compareWith == null) {
  1078. return true;
  1079. }
  1080. if (value != compareWith.val()) {
  1081. validator.updateStatus(options.field, validator.STATUS_VALID, 'different');
  1082. return true;
  1083. } else {
  1084. return false;
  1085. }
  1086. }
  1087. };
  1088. }(window.jQuery));
  1089. ;(function($) {
  1090. $.fn.bootstrapValidator.validators.digits = {
  1091. /**
  1092. * Return true if the input value contains digits only
  1093. *
  1094. * @param {BootstrapValidator} validator Validate plugin instance
  1095. * @param {jQuery} $field Field element
  1096. * @param {Object} options
  1097. * @returns {Boolean}
  1098. */
  1099. validate: function(validator, $field, options) {
  1100. var value = $field.val();
  1101. if (value == '') {
  1102. return true;
  1103. }
  1104. return /^\d+$/.test(value);
  1105. }
  1106. }
  1107. }(window.jQuery));
  1108. ;(function($) {
  1109. $.fn.bootstrapValidator.validators.emailAddress = {
  1110. enableByHtml5: function($field) {
  1111. return ('email' == $field.attr('type'));
  1112. },
  1113. /**
  1114. * Return true if and only if the input value is a valid email address
  1115. *
  1116. * @param {BootstrapValidator} validator Validate plugin instance
  1117. * @param {jQuery} $field Field element
  1118. * @param {Object} options
  1119. * @returns {Boolean}
  1120. */
  1121. validate: function(validator, $field, options) {
  1122. var value = $field.val();
  1123. if (value == '') {
  1124. return true;
  1125. }
  1126. // Email address regular expression
  1127. // http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
  1128. 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,}))$/;
  1129. return emailRegExp.test(value);
  1130. }
  1131. }
  1132. }(window.jQuery));
  1133. ;(function($) {
  1134. $.fn.bootstrapValidator.validators.greaterThan = {
  1135. html5Attributes: {
  1136. message: 'message',
  1137. value: 'value',
  1138. inclusive: 'inclusive'
  1139. },
  1140. enableByHtml5: function($field) {
  1141. var min = $field.attr('min');
  1142. if (min) {
  1143. return {
  1144. value: min
  1145. };
  1146. }
  1147. return false;
  1148. },
  1149. /**
  1150. * Return true if the input value is greater than or equals to given number
  1151. *
  1152. * @param {BootstrapValidator} validator Validate plugin instance
  1153. * @param {jQuery} $field Field element
  1154. * @param {Object} options Can consist of the following keys:
  1155. * - value: The number used to compare to
  1156. * - inclusive [optional]: Can be true or false. Default is true
  1157. * - message: The invalid message
  1158. * @returns {Boolean}
  1159. */
  1160. validate: function(validator, $field, options) {
  1161. var value = $field.val();
  1162. if (value == '') {
  1163. return true;
  1164. }
  1165. value = parseFloat(value);
  1166. return (options.inclusive === true) ? (value > options.value) : (value >= options.value);
  1167. }
  1168. }
  1169. }(window.jQuery));
  1170. ;(function($) {
  1171. $.fn.bootstrapValidator.validators.hex = {
  1172. /**
  1173. * Return true if and only if the input value is a valid hexadecimal number
  1174. *
  1175. * @param {BootstrapValidator} validator The validator plugin instance
  1176. * @param {jQuery} $field Field element
  1177. * @param {Object} options Consist of key:
  1178. * - message: The invalid message
  1179. * @returns {Boolean}
  1180. */
  1181. validate: function(validator, $field, options) {
  1182. var value = $field.val();
  1183. if (value == '') {
  1184. return true;
  1185. }
  1186. return /^[0-9a-fA-F]+$/.test(value);
  1187. }
  1188. };
  1189. }(window.jQuery));
  1190. ;(function($) {
  1191. $.fn.bootstrapValidator.validators.hexColor = {
  1192. enableByHtml5: function($field) {
  1193. return ('color' == $field.attr('type'));
  1194. },
  1195. /**
  1196. * Return true if the input value is a valid hex color
  1197. *
  1198. * @param {BootstrapValidator} validator The validator plugin instance
  1199. * @param {jQuery} $field Field element
  1200. * @param {Object} options Can consist of the following keys:
  1201. * - message: The invalid message
  1202. * @returns {Boolean}
  1203. */
  1204. validate: function(validator, $field, options) {
  1205. var value = $field.val();
  1206. if (value == '') {
  1207. return true;
  1208. }
  1209. return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(value);
  1210. }
  1211. };
  1212. }(window.jQuery));
  1213. ;(function($) {
  1214. $.fn.bootstrapValidator.validators.iban = {
  1215. html5Attributes: {
  1216. message: 'message',
  1217. country: 'country'
  1218. },
  1219. /**
  1220. * Validate an International Bank Account Number (IBAN)
  1221. * To test it, take the sample IBAN from
  1222. * http://www.nordea.com/Our+services/International+products+and+services/Cash+Management/IBAN+countries/908462.html
  1223. *
  1224. * @param {BootstrapValidator} validator The validator plugin instance
  1225. * @param {jQuery} $field Field element
  1226. * @param {Object} options Can consist of the following keys:
  1227. * - message: The invalid message
  1228. * - country: The ISO 3166-1 country code
  1229. * @returns {Boolean}
  1230. */
  1231. validate: function(validator, $field, options) {
  1232. var value = $field.val();
  1233. if (value == '') {
  1234. return true;
  1235. }
  1236. // See
  1237. // http://www.swift.com/dsp/resources/documents/IBAN_Registry.pdf
  1238. // http://en.wikipedia.org/wiki/International_Bank_Account_Number#IBAN_formats_by_country
  1239. var ibanRegex = {
  1240. 'AD': 'AD[0-9]{2}[0-9]{4}[0-9]{4}[A-Z0-9]{12}', // Andorra
  1241. 'AE': 'AE[0-9]{2}[0-9]{3}[0-9]{16}', // United Arab Emirates
  1242. 'AL': 'AL[0-9]{2}[0-9]{8}[A-Z0-9]{16}', // Albania
  1243. 'AO': 'AO[0-9]{2}[0-9]{21}', // Angola
  1244. 'AT': 'AT[0-9]{2}[0-9]{5}[0-9]{11}', // Austria
  1245. 'AZ': 'AZ[0-9]{2}[A-Z]{4}[A-Z0-9]{20}', // Azerbaijan
  1246. 'BA': 'BA[0-9]{2}[0-9]{3}[0-9]{3}[0-9]{8}[0-9]{2}', // Bosnia and Herzegovina
  1247. 'BE': 'BE[0-9]{2}[0-9]{3}[0-9]{7}[0-9]{2}', // Belgium
  1248. 'BF': 'BF[0-9]{2}[0-9]{23}', // Burkina Faso
  1249. 'BG': 'BG[0-9]{2}[A-Z]{4}[0-9]{4}[0-9]{2}[A-Z0-9]{8}', // Bulgaria
  1250. 'BH': 'BH[0-9]{2}[A-Z]{4}[A-Z0-9]{14}', // Bahrain
  1251. 'BI': 'BI[0-9]{2}[0-9]{12}', // Burundi
  1252. 'BJ': 'BJ[0-9]{2}[A-Z]{1}[0-9]{23}', // Benin
  1253. 'BR': 'BR[0-9]{2}[0-9]{8}[0-9]{5}[0-9]{10}[A-Z][A-Z0-9]', // Brazil
  1254. 'CH': 'CH[0-9]{2}[0-9]{5}[A-Z0-9]{12}', // Switzerland
  1255. 'CI': 'CI[0-9]{2}[A-Z]{1}[0-9]{23}', // Ivory Coast
  1256. 'CM': 'CM[0-9]{2}[0-9]{23}', // Cameroon
  1257. 'CR': 'CR[0-9]{2}[0-9]{3}[0-9]{14}', // Costa Rica
  1258. 'CV': 'CV[0-9]{2}[0-9]{21}', // Cape Verde
  1259. 'CY': 'CY[0-9]{2}[0-9]{3}[0-9]{5}[A-Z0-9]{16}', // Cyprus
  1260. 'CZ': 'CZ[0-9]{2}[0-9]{20}', // Czech Republic
  1261. 'DE': 'DE[0-9]{2}[0-9]{8}[0-9]{10}', // Germany
  1262. 'DK': 'DK[0-9]{2}[0-9]{14}', // Denmark
  1263. 'DO': 'DO[0-9]{2}[A-Z0-9]{4}[0-9]{20}', // Dominican Republic
  1264. 'DZ': 'DZ[0-9]{2}[0-9]{20}', // Algeria
  1265. 'EE': 'EE[0-9]{2}[0-9]{2}[0-9]{2}[0-9]{11}[0-9]{1}', // Estonia
  1266. 'ES': 'ES[0-9]{2}[0-9]{4}[0-9]{4}[0-9]{1}[0-9]{1}[0-9]{10}', // Spain
  1267. 'FI': 'FI[0-9]{2}[0-9]{6}[0-9]{7}[0-9]{1}', // Finland
  1268. 'FO': 'FO[0-9]{2}[0-9]{4}[0-9]{9}[0-9]{1}', // Faroe Islands
  1269. 'FR': 'FR[0-9]{2}[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}', // France
  1270. 'GB': 'GB[0-9]{2}[A-Z]{4}[0-9]{6}[0-9]{8}', // United Kingdom
  1271. 'GE': 'GE[0-9]{2}[A-Z]{2}[0-9]{16}', // Georgia
  1272. 'GI': 'GI[0-9]{2}[A-Z]{4}[A-Z0-9]{15}', // Gibraltar
  1273. 'GL': 'GL[0-9]{2}[0-9]{4}[0-9]{9}[0-9]{1}', // Greenland[
  1274. 'GR': 'GR[0-9]{2}[0-9]{3}[0-9]{4}[A-Z0-9]{16}', // Greece
  1275. 'GT': 'GT[0-9]{2}[A-Z0-9]{4}[A-Z0-9]{20}', // Guatemala
  1276. 'HR': 'HR[0-9]{2}[0-9]{7}[0-9]{10}', // Croatia
  1277. 'HU': 'HU[0-9]{2}[0-9]{3}[0-9]{4}[0-9]{1}[0-9]{15}[0-9]{1}', // Hungary
  1278. 'IE': 'IE[0-9]{2}[A-Z]{4}[0-9]{6}[0-9]{8}', // Ireland
  1279. 'IL': 'IL[0-9]{2}[0-9]{3}[0-9]{3}[0-9]{13}', // Israel
  1280. 'IR': 'IR[0-9]{2}[0-9]{22}', // Iran
  1281. 'IS': 'IS[0-9]{2}[0-9]{4}[0-9]{2}[0-9]{6}[0-9]{10}', // Iceland
  1282. 'IT': 'IT[0-9]{2}[A-Z]{1}[0-9]{5}[0-9]{5}[A-Z0-9]{12}', // Italy
  1283. 'JO': 'JO[0-9]{2}[A-Z]{4}[0-9]{4}[0]{8}[A-Z0-9]{10}', // Jordan
  1284. 'KW': 'KW[0-9]{2}[A-Z]{4}[0-9]{22}', // Kuwait
  1285. 'KZ': 'KZ[0-9]{2}[0-9]{3}[A-Z0-9]{13}', // Kazakhstan
  1286. 'LB': 'LB[0-9]{2}[0-9]{4}[A-Z0-9]{20}', // Lebanon
  1287. 'LI': 'LI[0-9]{2}[0-9]{5}[A-Z0-9]{12}', // Liechtenstein
  1288. 'LT': 'LT[0-9]{2}[0-9]{5}[0-9]{11}', // Lithuania
  1289. 'LU': 'LU[0-9]{2}[0-9]{3}[A-Z0-9]{13}', // Luxembourg
  1290. 'LV': 'LV[0-9]{2}[A-Z]{4}[A-Z0-9]{13}', // Latvia
  1291. 'MC': 'MC[0-9]{2}[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}', // Monaco
  1292. 'MD': 'MD[0-9]{2}[A-Z0-9]{20}', // Moldova
  1293. 'ME': 'ME[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}', // Montenegro
  1294. 'MG': 'MG[0-9]{2}[0-9]{23}', // Madagascar
  1295. 'MK': 'MK[0-9]{2}[0-9]{3}[A-Z0-9]{10}[0-9]{2}', // Macedonia
  1296. 'ML': 'ML[0-9]{2}[A-Z]{1}[0-9]{23}', // Mali
  1297. 'MR': 'MR13[0-9]{5}[0-9]{5}[0-9]{11}[0-9]{2}', // Mauritania
  1298. 'MT': 'MT[0-9]{2}[A-Z]{4}[0-9]{5}[A-Z0-9]{18}', // Malta
  1299. 'MU': 'MU[0-9]{2}[A-Z]{4}[0-9]{2}[0-9]{2}[0-9]{12}[0-9]{3}[A-Z]{3}',// Mauritius
  1300. 'MZ': 'MZ[0-9]{2}[0-9]{21}', // Mozambique
  1301. 'NL': 'NL[0-9]{2}[A-Z]{4}[0-9]{10}', // Netherlands
  1302. 'NO': 'NO[0-9]{2}[0-9]{4}[0-9]{6}[0-9]{1}', // Norway
  1303. 'PK': 'PK[0-9]{2}[A-Z]{4}[A-Z0-9]{16}', // Pakistan
  1304. 'PL': 'PL[0-9]{2}[0-9]{8}[0-9]{16}', // Poland
  1305. 'PS': 'PS[0-9]{2}[A-Z]{4}[A-Z0-9]{21}', // Palestinian
  1306. 'PT': 'PT[0-9]{2}[0-9]{4}[0-9]{4}[0-9]{11}[0-9]{2}', // Portugal
  1307. 'QA': 'QA[0-9]{2}[A-Z]{4}[A-Z0-9]{21}', // Qatar
  1308. 'RO': 'RO[0-9]{2}[A-Z]{4}[A-Z0-9]{16}', // Romania
  1309. 'RS': 'RS[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}', // Serbia
  1310. 'SA': 'SA[0-9]{2}[0-9]{2}[A-Z0-9]{18}', // Saudi Arabia
  1311. 'SE': 'SE[0-9]{2}[0-9]{3}[0-9]{16}[0-9]{1}', // Sweden
  1312. 'SI': 'SI[0-9]{2}[0-9]{5}[0-9]{8}[0-9]{2}', // Slovenia
  1313. 'SK': 'SK[0-9]{2}[0-9]{4}[0-9]{6}[0-9]{10}', // Slovakia
  1314. 'SM': 'SM[0-9]{2}[A-Z]{1}[0-9]{5}[0-9]{5}[A-Z0-9]{12}', // San Marino
  1315. 'SN': 'SN[0-9]{2}[A-Z]{1}[0-9]{23}', // Senegal
  1316. 'TN': 'TN59[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}', // Tunisia
  1317. 'TR': 'TR[0-9]{2}[0-9]{5}[A-Z0-9]{1}[A-Z0-9]{16}', // Turkey
  1318. 'VG': 'VG[0-9]{2}[A-Z]{4}[0-9]{16}' // Virgin Islands, British
  1319. };
  1320. value = value.replace(/[^a-zA-Z0-9]/g, '').toUpperCase();
  1321. var country = options.country || value.substr(0, 2);
  1322. if (!ibanRegex[country]) {
  1323. return false;
  1324. }
  1325. if (!(new RegExp('^' + ibanRegex[country] + '$')).test(value)) {
  1326. return false;
  1327. }
  1328. value = value.substr(4) + value.substr(0, 4);
  1329. value = value.split('').map(function(n) {
  1330. var code = n.charCodeAt(0);
  1331. return (code >= 'A'.charCodeAt(0) && code <= 'Z'.charCodeAt(0))
  1332. // Replace A, B, C, ..., Z with 10, 11, ..., 35
  1333. ? (code - 'A'.charCodeAt(0) + 10)
  1334. : n;
  1335. }).join('');
  1336. var temp = parseInt(value.substr(0, 1), 10),
  1337. length = value.length;
  1338. for (var i = 1; i < length; ++i) {
  1339. temp = (temp * 10 + parseInt(value.substr(i, 1), 10)) % 97;
  1340. }
  1341. return (temp == 1);
  1342. }
  1343. };
  1344. }(window.jQuery));
  1345. ;(function($) {
  1346. $.fn.bootstrapValidator.validators.identical = {
  1347. html5Attributes: {
  1348. message: 'message',
  1349. field: 'field'
  1350. },
  1351. /**
  1352. * Check if input value equals to value of particular one
  1353. *
  1354. * @param {BootstrapValidator} validator The validator plugin instance
  1355. * @param {jQuery} $field Field element
  1356. * @param {Object} options Consists of the following key:
  1357. * - field: The name of field that will be used to compare with current one
  1358. * @returns {Boolean}
  1359. */
  1360. validate: function(validator, $field, options) {
  1361. var value = $field.val();
  1362. if (value == '') {
  1363. return true;
  1364. }
  1365. var compareWith = validator.getFieldElements(options.field);
  1366. if (compareWith == null) {
  1367. return true;
  1368. }
  1369. if (value == compareWith.val()) {
  1370. validator.updateStatus(options.field, validator.STATUS_VALID, 'identical');
  1371. return true;
  1372. } else {
  1373. return false;
  1374. }
  1375. }
  1376. };
  1377. }(window.jQuery));
  1378. ;(function($) {
  1379. $.fn.bootstrapValidator.validators.integer = {
  1380. enableByHtml5: function($field) {
  1381. return ('number' == $field.attr('type'));
  1382. },
  1383. /**
  1384. * Return true if the input value is an integer
  1385. *
  1386. * @param {BootstrapValidator} validator The validator plugin instance
  1387. * @param {jQuery} $field Field element
  1388. * @param {Object} options Can consist of the following key:
  1389. * - message: The invalid message
  1390. * @returns {Boolean}
  1391. */
  1392. validate: function(validator, $field, options) {
  1393. var value = $field.val();
  1394. if (value == '') {
  1395. return true;
  1396. }
  1397. return /^(?:-?(?:0|[1-9][0-9]*))$/.test(value);
  1398. }
  1399. };
  1400. }(window.jQuery));
  1401. ;(function($) {
  1402. $.fn.bootstrapValidator.validators.ip = {
  1403. html5Attributes: {
  1404. message: 'message',
  1405. ipv4: 'ipv4',
  1406. ipv6: 'ipv6'
  1407. },
  1408. /**
  1409. * Return true if the input value is a IP address.
  1410. *
  1411. * @param {BootstrapValidator} validator The validator plugin instance
  1412. * @param {jQuery} $field Field element
  1413. * @param {Object} options Can consist of the following keys:
  1414. * - ipv4: Enable IPv4 validator, default to true
  1415. * - ipv6: Enable IPv6 validator, default to true
  1416. * - message: The invalid message
  1417. * @returns {Boolean}
  1418. */
  1419. validate: function(validator, $field, options) {
  1420. var value = $field.val();
  1421. if (value == '') {
  1422. return true;
  1423. }
  1424. options = $.extend({}, { ipv4: true, ipv6: true }, options);
  1425. if (options.ipv4) {
  1426. 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);
  1427. } else if (options.ipv6) {
  1428. 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);
  1429. }
  1430. return false;
  1431. }
  1432. };
  1433. }(window.jQuery));
  1434. ;(function($) {
  1435. $.fn.bootstrapValidator.validators.isbn = {
  1436. /**
  1437. * Return true if the input value is a valid ISBN 10 or ISBN 13 number
  1438. *
  1439. * @param {BootstrapValidator} validator The validator plugin instance
  1440. * @param {jQuery} $field Field element
  1441. * @param {Object} options Can consist of the following keys:
  1442. * - message: The invalid message
  1443. * @returns {Boolean}
  1444. */
  1445. validate: function(validator, $field, options) {
  1446. var value = $field.val();
  1447. if (value == '') {
  1448. return true;
  1449. }
  1450. // Replace all special characters except digits and X
  1451. value = value.replace(/[^\dX]/gi, '');
  1452. var chars = value.split(''),
  1453. sum = 0,
  1454. checksum;
  1455. // See http://en.wikipedia.org/wiki/International_Standard_Book_Number
  1456. switch (chars.length) {
  1457. // ISBN 10
  1458. case 10:
  1459. sum = 0;
  1460. for (var i = 0; i < 9; i++) {
  1461. sum += ((10 - i) * parseInt(chars[i]));
  1462. }
  1463. checksum = 11 - (sum % 11);
  1464. if (checksum == 11) {
  1465. checksum = 0;
  1466. } else if (checksum == 10) {
  1467. checksum = 'X';
  1468. }
  1469. return (checksum == chars[9]);
  1470. // ISBN 13
  1471. case 13:
  1472. sum = 0;
  1473. for (var i = 0; i < 12; i++) {
  1474. sum += ((i % 2 == 0) ? parseInt(chars[i]) : (parseInt(chars[i]) * 3));
  1475. }
  1476. checksum = 10 - (sum % 10);
  1477. if (checksum == 10) {
  1478. checksum = '0';
  1479. }
  1480. return (checksum == chars[12]);
  1481. default:
  1482. return false;
  1483. }
  1484. }
  1485. };
  1486. }(window.jQuery));
  1487. ;(function($) {
  1488. $.fn.bootstrapValidator.validators.lessThan = {
  1489. html5Attributes: {
  1490. message: 'message',
  1491. value: 'value',
  1492. inclusive: 'inclusive'
  1493. },
  1494. enableByHtml5: function($field) {
  1495. var max = $field.attr('max');
  1496. if (max) {
  1497. return {
  1498. value: max
  1499. };
  1500. }
  1501. return false;
  1502. },
  1503. /**
  1504. * Return true if the input value is less than or equal to given number
  1505. *
  1506. * @param {BootstrapValidator} validator The validator plugin instance
  1507. * @param {jQuery} $field Field element
  1508. * @param {Object} options Can consist of the following keys:
  1509. * - value: The number used to compare to
  1510. * - inclusive [optional]: Can be true or false. Default is true
  1511. * - message: The invalid message
  1512. * @returns {Boolean}
  1513. */
  1514. validate: function(validator, $field, options) {
  1515. var value = $field.val();
  1516. if (value == '') {
  1517. return true;
  1518. }
  1519. value = parseFloat(value);
  1520. return (options.inclusive === false) ? (value <= options.value) : (value < options.value);
  1521. }
  1522. };
  1523. }(window.jQuery));
  1524. ;(function($) {
  1525. $.fn.bootstrapValidator.validators.mac = {
  1526. /**
  1527. * Return true if the input value is a MAC address.
  1528. *
  1529. * @param {BootstrapValidator} validator The validator plugin instance
  1530. * @param {jQuery} $field Field element
  1531. * @param {Object} options Can consist of the following keys:
  1532. * - message: The invalid message
  1533. * @returns {Boolean}
  1534. */
  1535. validate: function(validator, $field, options) {
  1536. var value = $field.val();
  1537. if (value == '') {
  1538. return true;
  1539. }
  1540. return /^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$/.test(value);
  1541. }
  1542. };
  1543. }(window.jQuery));
  1544. ;(function($) {
  1545. $.fn.bootstrapValidator.validators.notEmpty = {
  1546. enableByHtml5: function($field) {
  1547. var required = $field.attr('required') + '';
  1548. return ('required' == required || 'true' == required);
  1549. },
  1550. /**
  1551. * Check if input value is empty or not
  1552. *
  1553. * @param {BootstrapValidator} validator The validator plugin instance
  1554. * @param {jQuery} $field Field element
  1555. * @param {Object} options
  1556. * @returns {Boolean}
  1557. */
  1558. validate: function(validator, $field, options) {
  1559. var type = $field.attr('type');
  1560. if ('radio' == type || 'checkbox' == type) {
  1561. return validator
  1562. .getFieldElements($field.attr('data-bv-field'))
  1563. .filter(':checked')
  1564. .length > 0;
  1565. }
  1566. return $.trim($field.val()) != '';
  1567. }
  1568. };
  1569. }(window.jQuery));
  1570. ;(function($) {
  1571. $.fn.bootstrapValidator.validators.numeric = {
  1572. /**
  1573. * Validate decimal number
  1574. *
  1575. * @param {BootstrapValidator} validator The validator plugin instance
  1576. * @param {jQuery} $field Field element
  1577. * @param {Object} options Consist of key:
  1578. * - message: The invalid message
  1579. * @returns {Boolean}
  1580. */
  1581. validate: function(validator, $field, options) {
  1582. var value = $field.val();
  1583. if (value == '') {
  1584. return true;
  1585. }
  1586. return !isNaN(parseFloat(value)) && isFinite(value);
  1587. }
  1588. };
  1589. }(window.jQuery));
  1590. ;(function($) {
  1591. $.fn.bootstrapValidator.validators.phone = {
  1592. html5Attributes: {
  1593. message: 'message',
  1594. country: 'country'
  1595. },
  1596. /**
  1597. * Return true if the input value contains a valid US phone number only
  1598. *
  1599. * @param {BootstrapValidator} validator Validate plugin instance
  1600. * @param {jQuery} $field Field element
  1601. * @param {Object} options Consist of key:
  1602. * - message: The invalid message
  1603. * - country: The ISO 3166 country code
  1604. * Currently it only supports United State (US) country
  1605. * @returns {Boolean}
  1606. */
  1607. validate: function(validator, $field, options) {
  1608. var value = $field.val();
  1609. if (value == '') {
  1610. return true;
  1611. }
  1612. options.country = options.country || 'US';
  1613. switch (options.country.toUpperCase()) {
  1614. case 'US':
  1615. default:
  1616. value = value.replace(/\(|\)|\s+/g, '');
  1617. return (/^(?:1\-?)?(\d{3})[\-\.]?(\d{3})[\-\.]?(\d{4})$/).test(value);
  1618. }
  1619. }
  1620. }
  1621. }(window.jQuery));
  1622. ;(function($) {
  1623. $.fn.bootstrapValidator.validators.regexp = {
  1624. html5Attributes: {
  1625. message: 'message',
  1626. regexp: 'regexp'
  1627. },
  1628. enableByHtml5: function($field) {
  1629. var pattern = $field.attr('pattern');
  1630. if (pattern) {
  1631. return {
  1632. regexp: pattern
  1633. };
  1634. }
  1635. return false;
  1636. },
  1637. /**
  1638. * Check if the element value matches given regular expression
  1639. *
  1640. * @param {BootstrapValidator} validator The validator plugin instance
  1641. * @param {jQuery} $field Field element
  1642. * @param {Object} options Consists of the following key:
  1643. * - regexp: The regular expression you need to check
  1644. * @returns {Boolean}
  1645. */
  1646. validate: function(validator, $field, options) {
  1647. var value = $field.val();
  1648. if (value == '') {
  1649. return true;
  1650. }
  1651. var regexp = ('string' == typeof options.regexp) ? new RegExp(options.regexp) : options.regexp;
  1652. return regexp.test(value);
  1653. }
  1654. };
  1655. }(window.jQuery));
  1656. ;(function($) {
  1657. $.fn.bootstrapValidator.validators.remote = {
  1658. html5Attributes: {
  1659. message: 'message',
  1660. url: 'url'
  1661. },
  1662. /**
  1663. * Request a remote server to check the input value
  1664. *
  1665. * @param {BootstrapValidator} validator Plugin instance
  1666. * @param {jQuery} $field Field element
  1667. * @param {Object} options Can consist of the following keys:
  1668. * - url
  1669. * - data [optional]: By default, it will take the value
  1670. * {
  1671. * <fieldName>: <fieldValue>
  1672. * }
  1673. * - message: The invalid message
  1674. * @returns {Boolean|Deferred}
  1675. */
  1676. validate: function(validator, $field, options) {
  1677. var value = $field.val();
  1678. if (value == '') {
  1679. return true;
  1680. }
  1681. var name = $field.attr('data-bv-field'), data = options.data;
  1682. if (data == null) {
  1683. data = {};
  1684. }
  1685. // Support dynamic data
  1686. if ('function' == typeof data) {
  1687. data = data.call(this, validator);
  1688. }
  1689. data[name] = value;
  1690. var dfd = new $.Deferred();
  1691. var xhr = $.ajax({
  1692. type: 'POST',
  1693. url: options.url,
  1694. dataType: 'json',
  1695. data: data
  1696. });
  1697. xhr.then(function(response) {
  1698. dfd.resolve($field, 'remote', response.valid === true || response.valid === 'true');
  1699. });
  1700. dfd.fail(function() {
  1701. xhr.abort();
  1702. });
  1703. return dfd;
  1704. }
  1705. };
  1706. }(window.jQuery));
  1707. ;(function($) {
  1708. $.fn.bootstrapValidator.validators.step = {
  1709. html5Attributes: {
  1710. message: 'message',
  1711. base: 'baseValue',
  1712. step: 'step'
  1713. },
  1714. /**
  1715. * Return true if the input value is valid step one
  1716. *
  1717. * @param {BootstrapValidator} validator The validator plugin instance
  1718. * @param {jQuery} $field Field element
  1719. * @param {Object} options Can consist of the following keys:
  1720. * - baseValue: The base value
  1721. * - step: The step
  1722. * - message: The invalid message
  1723. * @returns {Boolean}
  1724. */
  1725. validate: function(validator, $field, options) {
  1726. var value = $field.val();
  1727. if (value == '') {
  1728. return true;
  1729. }
  1730. options = $.extend({}, { baseValue: 0, step: 1 }, options);
  1731. value = parseFloat(value);
  1732. if (isNaN(value) || !isFinite(value)) {
  1733. return false;
  1734. }
  1735. var round = function(x, precision) {
  1736. var m = Math.pow(10, precision);
  1737. x = x * m;
  1738. var sign = (x > 0) | -(x < 0),
  1739. isHalf = (x % 1 === 0.5 * sign);
  1740. if (isHalf) {
  1741. return (Math.floor(x) + (sign > 0)) / m;
  1742. } else {
  1743. return Math.round(x) / m;
  1744. }
  1745. },
  1746. floatMod = function(x, y) {
  1747. if (y == 0.0) {
  1748. return 1.0;
  1749. }
  1750. var dotX = (x + '').split('.'),
  1751. dotY = (y + '').split('.'),
  1752. precision = ((dotX.length == 1) ? 0 : dotX[1].length) + ((dotY.length == 1) ? 0 : dotY[1].length);
  1753. return round(x - y * Math.floor(x / y), precision);
  1754. };
  1755. var mod = floatMod(value - options.baseValue, options.step);
  1756. return (mod == 0.0 || mod == options.step);
  1757. }
  1758. };
  1759. }(window.jQuery));
  1760. ;(function($) {
  1761. $.fn.bootstrapValidator.validators.stringCase = {
  1762. html5Attributes: {
  1763. message: 'message',
  1764. 'case': 'case'
  1765. },
  1766. /**
  1767. * Check if a string is a lower or upper case one
  1768. *
  1769. * @param {BootstrapValidator} validator The validator plugin instance
  1770. * @param {jQuery} $field Field element
  1771. * @param {Object} options Consist of key:
  1772. * - message: The invalid message
  1773. * - case: Can be 'lower' (default) or 'upper'
  1774. * @returns {Boolean}
  1775. */
  1776. validate: function(validator, $field, options) {
  1777. var value = $field.val();
  1778. if (value == '') {
  1779. return true;
  1780. }
  1781. var stringCase = (options['case'] || 'lower').toLowerCase();
  1782. switch (stringCase) {
  1783. case 'upper':
  1784. return value === value.toUpperCase();
  1785. case 'lower':
  1786. default:
  1787. return value === value.toLowerCase();
  1788. }
  1789. }
  1790. };
  1791. }(window.jQuery));
  1792. ;(function($) {
  1793. $.fn.bootstrapValidator.validators.stringLength = {
  1794. html5Attributes: {
  1795. message: 'message',
  1796. min: 'min',
  1797. max: 'max'
  1798. },
  1799. enableByHtml5: function($field) {
  1800. var maxLength = $field.attr('maxlength');
  1801. if (maxLength) {
  1802. return {
  1803. max: parseInt(maxLength, 10)
  1804. };
  1805. }
  1806. return false;
  1807. },
  1808. /**
  1809. * Check if the length of element value is less or more than given number
  1810. *
  1811. * @param {BootstrapValidator} validator The validator plugin instance
  1812. * @param {jQuery} $field Field element
  1813. * @param {Object} options Consists of following keys:
  1814. * - min
  1815. * - max
  1816. * At least one of two keys is required
  1817. * - message: The invalid message
  1818. * @returns {Boolean}
  1819. */
  1820. validate: function(validator, $field, options) {
  1821. var value = $field.val();
  1822. if (value == '') {
  1823. return true;
  1824. }
  1825. var length = $.trim(value).length;
  1826. if ((options.min && length < options.min) || (options.max && length > options.max)) {
  1827. return false;
  1828. }
  1829. return true;
  1830. }
  1831. };
  1832. }(window.jQuery));
  1833. ;(function($) {
  1834. $.fn.bootstrapValidator.validators.uri = {
  1835. enableByHtml5: function($field) {
  1836. return ('url' == $field.attr('type'));
  1837. },
  1838. /**
  1839. * Return true if the input value is a valid URL
  1840. *
  1841. * @param {BootstrapValidator} validator The validator plugin instance
  1842. * @param {jQuery} $field Field element
  1843. * @param {Object} options
  1844. * @returns {Boolean}
  1845. */
  1846. validate: function(validator, $field, options) {
  1847. var value = $field.val();
  1848. if (value == '') {
  1849. return true;
  1850. }
  1851. // Credit to https://gist.github.com/dperini/729294
  1852. //
  1853. // Regular Expression for URL validation
  1854. //
  1855. // Author: Diego Perini
  1856. // Updated: 2010/12/05
  1857. //
  1858. // the regular expression composed & commented
  1859. // could be easily tweaked for RFC compliance,
  1860. // it was expressly modified to fit & satisfy
  1861. // these test for an URL shortener:
  1862. //
  1863. // http://mathiasbynens.be/demo/url-regex
  1864. //
  1865. // Notes on possible differences from a standard/generic validation:
  1866. //
  1867. // - utf-8 char class take in consideration the full Unicode range
  1868. // - TLDs have been made mandatory so single names like "localhost" fails
  1869. // - protocols have been restricted to ftp, http and https only as requested
  1870. //
  1871. // Changes:
  1872. //
  1873. // - IP address dotted notation validation, range: 1.0.0.0 - 223.255.255.255
  1874. // first and last IP address of each class is considered invalid
  1875. // (since they are broadcast/network addresses)
  1876. //
  1877. // - Added exclusion of private, reserved and/or local networks ranges
  1878. //
  1879. // Compressed one-line versions:
  1880. //
  1881. // Javascript version
  1882. //
  1883. // /^(?:(?: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
  1884. //
  1885. // PHP version
  1886. //
  1887. // _^(?:(?: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
  1888. var urlExp = new RegExp(
  1889. "^" +
  1890. // protocol identifier
  1891. "(?:(?:https?|ftp)://)" +
  1892. // user:pass authentication
  1893. "(?:\\S+(?::\\S*)?@)?" +
  1894. "(?:" +
  1895. // IP address exclusion
  1896. // private & local networks
  1897. "(?!10(?:\\.\\d{1,3}){3})" +
  1898. "(?!127(?:\\.\\d{1,3}){3})" +
  1899. "(?!169\\.254(?:\\.\\d{1,3}){2})" +
  1900. "(?!192\\.168(?:\\.\\d{1,3}){2})" +
  1901. "(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})" +
  1902. // IP address dotted notation octets
  1903. // excludes loopback network 0.0.0.0
  1904. // excludes reserved space >= 224.0.0.0
  1905. // excludes network & broacast addresses
  1906. // (first & last IP address of each class)
  1907. "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
  1908. "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
  1909. "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
  1910. "|" +
  1911. // host name
  1912. "(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)" +
  1913. // domain name
  1914. "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*" +
  1915. // TLD identifier
  1916. "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" +
  1917. ")" +
  1918. // port number
  1919. "(?::\\d{2,5})?" +
  1920. // resource path
  1921. "(?:/[^\\s]*)?" +
  1922. "$", "i"
  1923. );
  1924. return urlExp.test(value);
  1925. }
  1926. };
  1927. }(window.jQuery));
  1928. ;(function($) {
  1929. $.fn.bootstrapValidator.validators.uuid = {
  1930. html5Attributes: {
  1931. message: 'message',
  1932. version: 'version'
  1933. },
  1934. /**
  1935. * Return true if and only if the input value is a valid UUID string
  1936. *
  1937. * @see http://en.wikipedia.org/wiki/Universally_unique_identifier
  1938. * @param {BootstrapValidator} validator The validator plugin instance
  1939. * @param {jQuery} $field Field element
  1940. * @param {Object} options Consist of key:
  1941. * - message: The invalid message
  1942. * - version: Can be 3, 4, 5, null
  1943. * @returns {Boolean}
  1944. */
  1945. validate: function(validator, $field, options) {
  1946. var value = $field.val();
  1947. if (value == '') {
  1948. return true;
  1949. }
  1950. // See the format at http://en.wikipedia.org/wiki/Universally_unique_identifier#Variants_and_versions
  1951. var patterns = {
  1952. '3': /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
  1953. '4': /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
  1954. '5': /^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
  1955. all: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i
  1956. },
  1957. version = options.version ? (options.version + '') : 'all';
  1958. return (null == patterns[version]) ? true : patterns[version].test(value);
  1959. }
  1960. };
  1961. }(window.jQuery));
  1962. ;(function($) {
  1963. $.fn.bootstrapValidator.validators.zipCode = {
  1964. html5Attributes: {
  1965. message: 'message',
  1966. country: 'country'
  1967. },
  1968. /**
  1969. * Return true if and only if the input value is a valid country zip code
  1970. *
  1971. * @param {BootstrapValidator} validator The validator plugin instance
  1972. * @param {jQuery} $field Field element
  1973. * @param {Object} options Consist of key:
  1974. * - message: The invalid message
  1975. * - country: The ISO 3166 country code
  1976. *
  1977. * Currently it supports the following countries:
  1978. * - US (United State)
  1979. * - DK (Denmark)
  1980. * - SE (Sweden)
  1981. * @returns {Boolean}
  1982. */
  1983. validate: function(validator, $field, options) {
  1984. var value = $field.val();
  1985. if (value == '' || !options.country) {
  1986. return true;
  1987. }
  1988. options.country = options.country || 'US';
  1989. switch (options.country.toUpperCase()) {
  1990. case 'DK':
  1991. return /^(DK(-|\s)?)?\d{4}$/i.test(value);
  1992. case 'SE':
  1993. return /^(S-)?\d{3}\s?\d{2}$/i.test(value);
  1994. case 'US':
  1995. default:
  1996. return /^\d{5}([\-]\d{4})?$/.test(value);
  1997. }
  1998. }
  1999. };
  2000. }(window.jQuery));