bootstrapValidator.js 89 KB

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