bootstrapValidator.js 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203
  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. // If you want to submit the form inside your submit handler, please call defaultSubmit() method
  276. this.options.submitHandler.call(this, this, this.$form, this.$submitButton);
  277. } else {
  278. this.disableSubmitButtons(true).defaultSubmit();
  279. }
  280. },
  281. // --- Public methods ---
  282. /**
  283. * Retrieve the field elements by given name
  284. *
  285. * @param {String} field The field name
  286. * @returns {null|jQuery[]}
  287. */
  288. getFieldElements: function(field) {
  289. var fields = this.options.fields[field].selector ? $(this.options.fields[field].selector) : this.$form.find('[name="' + field + '"]');
  290. return (fields.length == 0) ? null : fields;
  291. },
  292. /**
  293. * Set live validating mode
  294. *
  295. * @param {String} mode Live validating mode. Can be 'enabled', 'disabled', 'submitted'
  296. * @returns {BootstrapValidator}
  297. */
  298. setLiveMode: function(mode) {
  299. this.options.live = mode;
  300. if ('submitted' == mode) {
  301. return this;
  302. }
  303. var that = this;
  304. for (var field in this.options.fields) {
  305. (function(f) {
  306. var fields = that.getFieldElements(f);
  307. if (fields) {
  308. var type = fields.attr('type'),
  309. total = fields.length,
  310. updateAll = (total == 1) || ('radio' == type) || ('checkbox' == type),
  311. trigger = that.options.fields[field].trigger
  312. || that.options.trigger
  313. || (('radio' == type || 'checkbox' == type || 'file' == type || 'SELECT' == fields[0].tagName) ? 'change' : that._changeEvent),
  314. events = $.map(trigger.split(' '), function(item) {
  315. return item + '.live.bv';
  316. }).join(' ');
  317. for (var i = 0; i < total; i++) {
  318. ('enabled' == mode)
  319. ? $(fields[i]).on(events, function() {
  320. updateAll ? that.validateField(f) : that.validateFieldElement($(this), false);
  321. })
  322. : $(fields[i]).off(events);
  323. }
  324. }
  325. })(field);
  326. }
  327. return this;
  328. },
  329. /**
  330. * Disable/enable submit buttons
  331. *
  332. * @param {Boolean} disabled Can be true or false
  333. * @returns {BootstrapValidator}
  334. */
  335. disableSubmitButtons: function(disabled) {
  336. if (!disabled) {
  337. this.$form.find(this.options.submitButtons).removeAttr('disabled');
  338. } else if (this.options.live != 'disabled') {
  339. // Don't disable if the live validating mode is disabled
  340. this.$form.find(this.options.submitButtons).attr('disabled', 'disabled');
  341. }
  342. return this;
  343. },
  344. /**
  345. * Validate the form
  346. *
  347. * @return {BootstrapValidator}
  348. */
  349. validate: function() {
  350. if (!this.options.fields) {
  351. return this;
  352. }
  353. this.disableSubmitButtons(true);
  354. for (var field in this.options.fields) {
  355. this.validateField(field);
  356. }
  357. // Check if whether the submit button is clicked
  358. if (this.$submitButton) {
  359. this._submit();
  360. }
  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 && that.$submitButton) {
  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. /**
  555. * Submit the form using default submission.
  556. * It also does not perform any validations when submitting the form
  557. *
  558. * It might be used when you want to submit the form right inside the submitHandler()
  559. */
  560. defaultSubmit: function() {
  561. this.$form.off('submit.bv').submit();
  562. },
  563. // Useful APIs which aren't used internally
  564. /**
  565. * Reset the form
  566. *
  567. * @param {Boolean} resetFormData Reset current form data
  568. * @return {BootstrapValidator}
  569. */
  570. resetForm: function(resetFormData) {
  571. var field, fields, total, type, validator;
  572. for (field in this.options.fields) {
  573. fields = this.getFieldElements(field);
  574. total = fields.length;
  575. for (var i = 0; i < total; i++) {
  576. for (validator in this.options.fields[field].validators) {
  577. $(fields[i]).removeData('bv.dfs.' + validator);
  578. }
  579. }
  580. // Mark field as not validated yet
  581. this.updateStatus(field, this.STATUS_NOT_VALIDATED, null);
  582. if (resetFormData) {
  583. type = fields.attr('type');
  584. ('radio' == type || 'checkbox' == type) ? fields.removeAttr('checked').removeAttr('selected') : fields.val('');
  585. }
  586. }
  587. this.$invalidField = null;
  588. this.$submitButton = null;
  589. // Enable submit buttons
  590. this.disableSubmitButtons(false);
  591. return this;
  592. },
  593. /**
  594. * Enable/Disable all validators to given field
  595. *
  596. * @param {String} field The field name
  597. * @param {Boolean} enabled Enable/Disable field validators
  598. * @return {BootstrapValidator}
  599. */
  600. enableFieldValidators: function(field, enabled) {
  601. this.options.fields[field]['enabled'] = enabled;
  602. this.updateStatus(field, this.STATUS_NOT_VALIDATED, null);
  603. return this;
  604. }
  605. };
  606. // Plugin definition
  607. $.fn.bootstrapValidator = function(options) {
  608. return this.each(function() {
  609. var $this = $(this), data = $this.data('bootstrapValidator');
  610. if (!data) {
  611. $this.data('bootstrapValidator', (data = new BootstrapValidator(this, options)));
  612. }
  613. if ('string' == typeof options) {
  614. data[options]();
  615. }
  616. });
  617. };
  618. // Available validators
  619. $.fn.bootstrapValidator.validators = {};
  620. $.fn.bootstrapValidator.Constructor = BootstrapValidator;
  621. }(window.jQuery));
  622. ;(function($) {
  623. $.fn.bootstrapValidator.validators.base64 = {
  624. /**
  625. * Return true if the input value is a base 64 encoded string.
  626. *
  627. * @param {BootstrapValidator} validator The validator plugin instance
  628. * @param {jQuery} $field Field element
  629. * @param {Object} options Can consist of the following keys:
  630. * - message: The invalid message
  631. * @returns {Boolean}
  632. */
  633. validate: function(validator, $field, options) {
  634. var value = $field.val();
  635. if (value == '') {
  636. return true;
  637. }
  638. return /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$/.test(value);
  639. }
  640. };
  641. }(window.jQuery));
  642. ;(function($) {
  643. $.fn.bootstrapValidator.validators.between = {
  644. html5Attributes: {
  645. message: 'message',
  646. min: 'min',
  647. max: 'max',
  648. inclusive: 'inclusive'
  649. },
  650. enableByHtml5: function($field) {
  651. if ('range' == $field.attr('type')) {
  652. return {
  653. min: $field.attr('min'),
  654. max: $field.attr('max')
  655. };
  656. }
  657. return false;
  658. },
  659. /**
  660. * Return true if the input value is between (strictly or not) two given numbers
  661. *
  662. * @param {BootstrapValidator} validator The validator plugin instance
  663. * @param {jQuery} $field Field element
  664. * @param {Object} options Can consist of the following keys:
  665. * - min
  666. * - max
  667. * - inclusive [optional]: Can be true or false. Default is true
  668. * - message: The invalid message
  669. * @returns {Boolean}
  670. */
  671. validate: function(validator, $field, options) {
  672. var value = $field.val();
  673. if (value == '') {
  674. return true;
  675. }
  676. value = parseFloat(value);
  677. return (options.inclusive === true)
  678. ? (value > options.min && value < options.max)
  679. : (value >= options.min && value <= options.max);
  680. }
  681. };
  682. }(window.jQuery));
  683. ;(function($) {
  684. $.fn.bootstrapValidator.validators.callback = {
  685. /**
  686. * Return result from the callback method
  687. *
  688. * @param {BootstrapValidator} validator The validator plugin instance
  689. * @param {jQuery} $field Field element
  690. * @param {Object} options Can consist of the following keys:
  691. * - callback: The callback method that passes 2 parameters:
  692. * callback: function(fieldValue, validator) {
  693. * // fieldValue is the value of field
  694. * // validator is instance of BootstrapValidator
  695. * }
  696. * - message: The invalid message
  697. * @returns {Boolean|Deferred}
  698. */
  699. validate: function(validator, $field, options) {
  700. var value = $field.val();
  701. if (options.callback && 'function' == typeof options.callback) {
  702. var dfd = new $.Deferred();
  703. dfd.resolve($field, 'callback', options.callback.call(this, value, validator));
  704. return dfd;
  705. }
  706. return true;
  707. }
  708. };
  709. }(window.jQuery));
  710. ;(function($) {
  711. $.fn.bootstrapValidator.validators.choice = {
  712. html5Attributes: {
  713. message: 'message',
  714. min: 'min',
  715. max: 'max'
  716. },
  717. /**
  718. * Check if the number of checked boxes are less or more than a given number
  719. *
  720. * @param {BootstrapValidator} validator The validator plugin instance
  721. * @param {jQuery} $field Field element
  722. * @param {Object} options Consists of following keys:
  723. * - min
  724. * - max
  725. * At least one of two keys is required
  726. * - message: The invalid message
  727. * @returns {Boolean}
  728. */
  729. validate: function(validator, $field, options) {
  730. var numChoices = validator
  731. .getFieldElements($field.attr('data-bv-field'))
  732. .filter(':checked')
  733. .length;
  734. if ((options.min && numChoices < options.min) || (options.max && numChoices > options.max)) {
  735. return false;
  736. }
  737. return true;
  738. }
  739. };
  740. }(window.jQuery));
  741. ;(function($) {
  742. $.fn.bootstrapValidator.validators.creditCard = {
  743. /**
  744. * Return true if the input value is valid credit card number
  745. * Based on https://gist.github.com/DiegoSalazar/4075533
  746. *
  747. * @param {BootstrapValidator} validator The validator plugin instance
  748. * @param {jQuery} $field Field element
  749. * @param {Object} options Can consist of the following key:
  750. * - message: The invalid message
  751. * @returns {Boolean}
  752. */
  753. validate: function(validator, $field, options) {
  754. var value = $field.val();
  755. if (value == '') {
  756. return true;
  757. }
  758. // Accept only digits, dashes or spaces
  759. if (/[^0-9-\s]+/.test(value)) {
  760. return false;
  761. }
  762. value = value.replace(/\D/g, '');
  763. // Validate the check sum
  764. // The Luhn Algorithm
  765. // http://en.wikipedia.org/wiki/Luhn
  766. var check = 0, digit = 0, even = false, length = value.length;
  767. for (var n = length - 1; n >= 0; n--) {
  768. digit = parseInt(value.charAt(n), 10);
  769. if (even) {
  770. if ((digit *= 2) > 9) {
  771. digit -= 9;
  772. }
  773. }
  774. check += digit;
  775. even = !even;
  776. }
  777. if ((check % 10) != 0) {
  778. return false;
  779. }
  780. // Validate the card number based on prefix (IIN ranges) and length
  781. var cards = {
  782. AMERICAN_EXPRESS: {
  783. length: [15],
  784. prefix: ['34', '37']
  785. },
  786. DINERS_CLUB: {
  787. length: [14],
  788. prefix: ['300', '301', '302', '303', '304', '305', '36']
  789. },
  790. DINERS_CLUB_US: {
  791. length: [16],
  792. prefix: ['54', '55']
  793. },
  794. DISCOVER: {
  795. length: [16],
  796. prefix: ['6011', '622126', '622127', '622128', '622129', '62213',
  797. '62214', '62215', '62216', '62217', '62218', '62219',
  798. '6222', '6223', '6224', '6225', '6226', '6227', '6228',
  799. '62290', '62291', '622920', '622921', '622922', '622923',
  800. '622924', '622925', '644', '645', '646', '647', '648',
  801. '649', '65']
  802. },
  803. JCB: {
  804. length: [16],
  805. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  806. },
  807. LASER: {
  808. length: [16, 17, 18, 19],
  809. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  810. },
  811. MAESTRO: {
  812. length: [12, 13, 14, 15, 16, 17, 18, 19],
  813. prefix: ['5018', '5020', '5038', '6304', '6759', '6761', '6762', '6763', '6764', '6765', '6766']
  814. },
  815. MASTERCARD: {
  816. length: [16],
  817. prefix: ['51', '52', '53', '54', '55']
  818. },
  819. SOLO: {
  820. length: [16, 18, 19],
  821. prefix: ['6334', '6767']
  822. },
  823. UNIONPAY: {
  824. length: [16, 17, 18, 19],
  825. prefix: ['622126', '622127', '622128', '622129', '62213', '62214',
  826. '62215', '62216', '62217', '62218', '62219', '6222', '6223',
  827. '6224', '6225', '6226', '6227', '6228', '62290', '62291',
  828. '622920', '622921', '622922', '622923', '622924', '622925']
  829. },
  830. VISA: {
  831. length: [16],
  832. prefix: ['4']
  833. }
  834. };
  835. var type, i;
  836. for (type in cards) {
  837. for (i in cards[type]['prefix']) {
  838. if (value.substr(0, cards[type]['prefix'][i].length) == cards[type]['prefix'][i] // Check the prefix
  839. && cards[type]['length'].indexOf(value.length) != -1) // and length
  840. {
  841. return true;
  842. }
  843. }
  844. }
  845. return false;
  846. }
  847. };
  848. }(window.jQuery));
  849. ;(function($) {
  850. $.fn.bootstrapValidator.validators.cvv = {
  851. html5Attributes: {
  852. message: 'message',
  853. ccfield: 'creditCardField'
  854. },
  855. /**
  856. * Return true if the input value is a valid CVV number.
  857. *
  858. * @param {BootstrapValidator} validator The validator plugin instance
  859. * @param {jQuery} $field Field element
  860. * @param {Object} options Can consist of the following keys:
  861. * - creditCardField: The credit card number field. It can be null
  862. * - message: The invalid message
  863. * @returns {Boolean}
  864. */
  865. validate: function(validator, $field, options) {
  866. var value = $field.val();
  867. if (value == '') {
  868. return true;
  869. }
  870. if (!/^[0-9]{3,4}$/.test(value)) {
  871. return false;
  872. }
  873. if (!options.creditCardField) {
  874. return true;
  875. }
  876. // Get the credit card number
  877. var creditCard = validator.getFieldElements(options.creditCardField).val();
  878. if (creditCard == '') {
  879. return true;
  880. }
  881. // Supported credit card types
  882. var cards = {
  883. AMERICAN_EXPRESS: {
  884. length: [15],
  885. prefix: ['34', '37']
  886. },
  887. DINERS_CLUB: {
  888. length: [14],
  889. prefix: ['300', '301', '302', '303', '304', '305', '36']
  890. },
  891. DINERS_CLUB_US: {
  892. length: [16],
  893. prefix: ['54', '55']
  894. },
  895. DISCOVER: {
  896. length: [16],
  897. prefix: ['6011', '622126', '622127', '622128', '622129', '62213',
  898. '62214', '62215', '62216', '62217', '62218', '62219',
  899. '6222', '6223', '6224', '6225', '6226', '6227', '6228',
  900. '62290', '62291', '622920', '622921', '622922', '622923',
  901. '622924', '622925', '644', '645', '646', '647', '648',
  902. '649', '65']
  903. },
  904. JCB: {
  905. length: [16],
  906. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  907. },
  908. LASER: {
  909. length: [16, 17, 18, 19],
  910. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  911. },
  912. MAESTRO: {
  913. length: [12, 13, 14, 15, 16, 17, 18, 19],
  914. prefix: ['5018', '5020', '5038', '6304', '6759', '6761', '6762', '6763', '6764', '6765', '6766']
  915. },
  916. MASTERCARD: {
  917. length: [16],
  918. prefix: ['51', '52', '53', '54', '55']
  919. },
  920. SOLO: {
  921. length: [16, 18, 19],
  922. prefix: ['6334', '6767']
  923. },
  924. UNIONPAY: {
  925. length: [16, 17, 18, 19],
  926. prefix: ['622126', '622127', '622128', '622129', '62213', '62214',
  927. '62215', '62216', '62217', '62218', '62219', '6222', '6223',
  928. '6224', '6225', '6226', '6227', '6228', '62290', '62291',
  929. '622920', '622921', '622922', '622923', '622924', '622925']
  930. },
  931. VISA: {
  932. length: [16],
  933. prefix: ['4']
  934. }
  935. };
  936. var type, i, creditCardType = null;
  937. for (type in cards) {
  938. for (i in cards[type]['prefix']) {
  939. if (creditCard.substr(0, cards[type]['prefix'][i].length) == cards[type]['prefix'][i] // Check the prefix
  940. && cards[type]['length'].indexOf(creditCard.length) != -1) // and length
  941. {
  942. creditCardType = type;
  943. break;
  944. }
  945. }
  946. }
  947. return (creditCardType == null)
  948. ? false
  949. : (('AMERICAN_EXPRESS' == creditCardType) ? (value.length == 4) : (value.length == 3));
  950. }
  951. };
  952. }(window.jQuery));
  953. ;(function($) {
  954. $.fn.bootstrapValidator.validators.date = {
  955. html5Attributes: {
  956. message: 'message',
  957. format: 'format'
  958. },
  959. /**
  960. * Return true if the input value is valid date
  961. *
  962. * @param {BootstrapValidator} validator The validator plugin instance
  963. * @param {jQuery} $field Field element
  964. * @param {Object} options Can consist of the following keys:
  965. * - format: The date format. Default is MM/DD/YYYY
  966. * Support the following formats:
  967. * YYYY/DD/MM
  968. * YYYY/DD/MM h:m A
  969. * YYYY/MM/DD
  970. * YYYY/MM/DD h:m A
  971. *
  972. * YYYY-DD-MM
  973. * YYYY-DD-MM h:m A
  974. * YYYY-MM-DD
  975. * YYYY-MM-DD h:m A
  976. *
  977. * MM/DD/YYYY
  978. * MM/DD/YYYY h:m A
  979. * DD/MM/YYYY
  980. * DD/MM/YYYY h:m A
  981. *
  982. * MM-DD-YYYY
  983. * MM-DD-YYYY h:m A
  984. * DD-MM-YYYY
  985. * DD-MM-YYYY h:m A
  986. * - message: The invalid message
  987. * @returns {Boolean}
  988. */
  989. validate: function(validator, $field, options) {
  990. var value = $field.val();
  991. if (value == '') {
  992. return true;
  993. }
  994. // Determine the separator
  995. options.format = options.format || 'MM/DD/YYYY';
  996. var separator = (options.format.indexOf('/') != -1)
  997. ? '/'
  998. : ((options.format.indexOf('-') != -1) ? '-' : null);
  999. if (separator == null) {
  1000. return false;
  1001. }
  1002. var month, day, year, minutes = null, hours = null, matches;
  1003. switch (true) {
  1004. case (separator == '/' && (matches = value.match(/^(\d{4})\/(\d{1,2})\/(\d{1,2})$/i)) && options.format == 'YYYY/DD/MM'):
  1005. case (separator == '-' && (matches = value.match(/^(\d{4})-(\d{1,2})-(\d{1,2})$/i)) && options.format == 'YYYY-DD-MM'):
  1006. year = matches[1]; day = matches[2]; month = matches[3];
  1007. break;
  1008. case (separator == '/' && (matches = value.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/i)) && options.format == 'DD/MM/YYYY'):
  1009. case (separator == '-' && (matches = value.match(/^(\d{1,2})-(\d{1,2})-(\d{4})$/i)) && options.format == 'DD-MM-YYYY'):
  1010. day = matches[1]; month = matches[2]; year = matches[3];
  1011. break;
  1012. case (separator == '/' && (matches = value.match(/^(\d{4})\/(\d{1,2})\/(\d{1,2})$/i)) && options.format == 'YYYY/MM/DD'):
  1013. case (separator == '-' && (matches = value.match(/^(\d{4})-(\d{1,2})-(\d{1,2})$/i)) && options.format == 'YYYY-MM-DD'):
  1014. year = matches[1]; month = matches[2]; day = matches[3];
  1015. break;
  1016. case (separator == '/' && (matches = value.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/i)) && options.format == 'MM/DD/YYYY'):
  1017. case (separator == '-' && (matches = value.match(/^(\d{1,2})-(\d{1,2})-(\d{4})$/i)) && options.format == 'MM-DD-YYYY'):
  1018. month = matches[1]; day = matches[2]; year = matches[3];
  1019. break;
  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/DD/MM h:m A'):
  1021. 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'):
  1022. year = matches[1]; day = matches[2]; month = matches[3]; hours = matches[4]; minutes = matches[5];
  1023. break;
  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 == 'DD/MM/YYYY h:m A'):
  1025. 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'):
  1026. day = matches[1]; month = matches[2]; year = matches[3]; hours = matches[4]; minutes = matches[5];
  1027. break;
  1028. 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'):
  1029. 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'):
  1030. year = matches[1]; month = matches[2]; day = matches[3]; hours = matches[4]; minutes = matches[5];
  1031. break;
  1032. 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'):
  1033. 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'):
  1034. month = matches[1]; day = matches[2]; year = matches[3]; hours = matches[4]; minutes = matches[5];
  1035. break;
  1036. default:
  1037. return false;
  1038. }
  1039. // Validate hours and minutes
  1040. if (hours && minutes) {
  1041. hours = parseInt(hours, 10);
  1042. minutes = parseInt(minutes, 10);
  1043. if (hours < 1 || hours > 12 || minutes < 0 || minutes > 59) {
  1044. return false;
  1045. }
  1046. }
  1047. // Validate day, month, and year
  1048. day = parseInt(day, 10);
  1049. month = parseInt(month, 10);
  1050. year = parseInt(year, 10);
  1051. if (year < 1000 || year > 9999 || month == 0 || month > 12) {
  1052. return false;
  1053. }
  1054. var numDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  1055. // Update the number of days in Feb of leap year
  1056. if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
  1057. numDays[1] = 29;
  1058. }
  1059. // Check the day
  1060. return (day > 0 && day <= numDays[month - 1]);
  1061. }
  1062. };
  1063. }(window.jQuery));
  1064. ;(function($) {
  1065. $.fn.bootstrapValidator.validators.different = {
  1066. html5Attributes: {
  1067. message: 'message',
  1068. field: 'field'
  1069. },
  1070. /**
  1071. * Return true if the input value is different with given field's value
  1072. *
  1073. * @param {BootstrapValidator} validator The validator plugin instance
  1074. * @param {jQuery} $field Field element
  1075. * @param {Object} options Consists of the following key:
  1076. * - field: The name of field that will be used to compare with current one
  1077. * - message: The invalid message
  1078. * @returns {Boolean}
  1079. */
  1080. validate: function(validator, $field, options) {
  1081. var value = $field.val();
  1082. if (value == '') {
  1083. return true;
  1084. }
  1085. var compareWith = validator.getFieldElements(options.field);
  1086. if (compareWith == null) {
  1087. return true;
  1088. }
  1089. if (value != compareWith.val()) {
  1090. validator.updateStatus(options.field, validator.STATUS_VALID, 'different');
  1091. return true;
  1092. } else {
  1093. return false;
  1094. }
  1095. }
  1096. };
  1097. }(window.jQuery));
  1098. ;(function($) {
  1099. $.fn.bootstrapValidator.validators.digits = {
  1100. /**
  1101. * Return true if the input value contains digits only
  1102. *
  1103. * @param {BootstrapValidator} validator Validate plugin instance
  1104. * @param {jQuery} $field Field element
  1105. * @param {Object} options
  1106. * @returns {Boolean}
  1107. */
  1108. validate: function(validator, $field, options) {
  1109. var value = $field.val();
  1110. if (value == '') {
  1111. return true;
  1112. }
  1113. return /^\d+$/.test(value);
  1114. }
  1115. }
  1116. }(window.jQuery));
  1117. ;(function($) {
  1118. $.fn.bootstrapValidator.validators.emailAddress = {
  1119. enableByHtml5: function($field) {
  1120. return ('email' == $field.attr('type'));
  1121. },
  1122. /**
  1123. * Return true if and only if the input value is a valid email address
  1124. *
  1125. * @param {BootstrapValidator} validator Validate plugin instance
  1126. * @param {jQuery} $field Field element
  1127. * @param {Object} options
  1128. * @returns {Boolean}
  1129. */
  1130. validate: function(validator, $field, options) {
  1131. var value = $field.val();
  1132. if (value == '') {
  1133. return true;
  1134. }
  1135. // Email address regular expression
  1136. // http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
  1137. 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,}))$/;
  1138. return emailRegExp.test(value);
  1139. }
  1140. }
  1141. }(window.jQuery));
  1142. ;(function($) {
  1143. $.fn.bootstrapValidator.validators.greaterThan = {
  1144. html5Attributes: {
  1145. message: 'message',
  1146. value: 'value',
  1147. inclusive: 'inclusive'
  1148. },
  1149. enableByHtml5: function($field) {
  1150. var min = $field.attr('min');
  1151. if (min) {
  1152. return {
  1153. value: min
  1154. };
  1155. }
  1156. return false;
  1157. },
  1158. /**
  1159. * Return true if the input value is greater than or equals to given number
  1160. *
  1161. * @param {BootstrapValidator} validator Validate plugin instance
  1162. * @param {jQuery} $field Field element
  1163. * @param {Object} options Can consist of the following keys:
  1164. * - value: The number used to compare to
  1165. * - inclusive [optional]: Can be true or false. Default is true
  1166. * - message: The invalid message
  1167. * @returns {Boolean}
  1168. */
  1169. validate: function(validator, $field, options) {
  1170. var value = $field.val();
  1171. if (value == '') {
  1172. return true;
  1173. }
  1174. value = parseFloat(value);
  1175. return (options.inclusive === true) ? (value > options.value) : (value >= options.value);
  1176. }
  1177. }
  1178. }(window.jQuery));
  1179. ;(function($) {
  1180. $.fn.bootstrapValidator.validators.hex = {
  1181. /**
  1182. * Return true if and only if the input value is a valid hexadecimal number
  1183. *
  1184. * @param {BootstrapValidator} validator The validator plugin instance
  1185. * @param {jQuery} $field Field element
  1186. * @param {Object} options Consist of key:
  1187. * - message: The invalid message
  1188. * @returns {Boolean}
  1189. */
  1190. validate: function(validator, $field, options) {
  1191. var value = $field.val();
  1192. if (value == '') {
  1193. return true;
  1194. }
  1195. return /^[0-9a-fA-F]+$/.test(value);
  1196. }
  1197. };
  1198. }(window.jQuery));
  1199. ;(function($) {
  1200. $.fn.bootstrapValidator.validators.hexColor = {
  1201. enableByHtml5: function($field) {
  1202. return ('color' == $field.attr('type'));
  1203. },
  1204. /**
  1205. * Return true if the input value is a valid hex color
  1206. *
  1207. * @param {BootstrapValidator} validator The validator plugin instance
  1208. * @param {jQuery} $field Field element
  1209. * @param {Object} options Can consist of the following keys:
  1210. * - message: The invalid message
  1211. * @returns {Boolean}
  1212. */
  1213. validate: function(validator, $field, options) {
  1214. var value = $field.val();
  1215. if (value == '') {
  1216. return true;
  1217. }
  1218. return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(value);
  1219. }
  1220. };
  1221. }(window.jQuery));
  1222. ;(function($) {
  1223. $.fn.bootstrapValidator.validators.iban = {
  1224. html5Attributes: {
  1225. message: 'message',
  1226. country: 'country'
  1227. },
  1228. /**
  1229. * Validate an International Bank Account Number (IBAN)
  1230. * To test it, take the sample IBAN from
  1231. * http://www.nordea.com/Our+services/International+products+and+services/Cash+Management/IBAN+countries/908462.html
  1232. *
  1233. * @param {BootstrapValidator} validator The validator plugin instance
  1234. * @param {jQuery} $field Field element
  1235. * @param {Object} options Can consist of the following keys:
  1236. * - message: The invalid message
  1237. * - country: The ISO 3166-1 country code
  1238. * @returns {Boolean}
  1239. */
  1240. validate: function(validator, $field, options) {
  1241. var value = $field.val();
  1242. if (value == '') {
  1243. return true;
  1244. }
  1245. // See
  1246. // http://www.swift.com/dsp/resources/documents/IBAN_Registry.pdf
  1247. // http://en.wikipedia.org/wiki/International_Bank_Account_Number#IBAN_formats_by_country
  1248. var ibanRegex = {
  1249. 'AD': 'AD[0-9]{2}[0-9]{4}[0-9]{4}[A-Z0-9]{12}', // Andorra
  1250. 'AE': 'AE[0-9]{2}[0-9]{3}[0-9]{16}', // United Arab Emirates
  1251. 'AL': 'AL[0-9]{2}[0-9]{8}[A-Z0-9]{16}', // Albania
  1252. 'AO': 'AO[0-9]{2}[0-9]{21}', // Angola
  1253. 'AT': 'AT[0-9]{2}[0-9]{5}[0-9]{11}', // Austria
  1254. 'AZ': 'AZ[0-9]{2}[A-Z]{4}[A-Z0-9]{20}', // Azerbaijan
  1255. 'BA': 'BA[0-9]{2}[0-9]{3}[0-9]{3}[0-9]{8}[0-9]{2}', // Bosnia and Herzegovina
  1256. 'BE': 'BE[0-9]{2}[0-9]{3}[0-9]{7}[0-9]{2}', // Belgium
  1257. 'BF': 'BF[0-9]{2}[0-9]{23}', // Burkina Faso
  1258. 'BG': 'BG[0-9]{2}[A-Z]{4}[0-9]{4}[0-9]{2}[A-Z0-9]{8}', // Bulgaria
  1259. 'BH': 'BH[0-9]{2}[A-Z]{4}[A-Z0-9]{14}', // Bahrain
  1260. 'BI': 'BI[0-9]{2}[0-9]{12}', // Burundi
  1261. 'BJ': 'BJ[0-9]{2}[A-Z]{1}[0-9]{23}', // Benin
  1262. 'BR': 'BR[0-9]{2}[0-9]{8}[0-9]{5}[0-9]{10}[A-Z][A-Z0-9]', // Brazil
  1263. 'CH': 'CH[0-9]{2}[0-9]{5}[A-Z0-9]{12}', // Switzerland
  1264. 'CI': 'CI[0-9]{2}[A-Z]{1}[0-9]{23}', // Ivory Coast
  1265. 'CM': 'CM[0-9]{2}[0-9]{23}', // Cameroon
  1266. 'CR': 'CR[0-9]{2}[0-9]{3}[0-9]{14}', // Costa Rica
  1267. 'CV': 'CV[0-9]{2}[0-9]{21}', // Cape Verde
  1268. 'CY': 'CY[0-9]{2}[0-9]{3}[0-9]{5}[A-Z0-9]{16}', // Cyprus
  1269. 'CZ': 'CZ[0-9]{2}[0-9]{20}', // Czech Republic
  1270. 'DE': 'DE[0-9]{2}[0-9]{8}[0-9]{10}', // Germany
  1271. 'DK': 'DK[0-9]{2}[0-9]{14}', // Denmark
  1272. 'DO': 'DO[0-9]{2}[A-Z0-9]{4}[0-9]{20}', // Dominican Republic
  1273. 'DZ': 'DZ[0-9]{2}[0-9]{20}', // Algeria
  1274. 'EE': 'EE[0-9]{2}[0-9]{2}[0-9]{2}[0-9]{11}[0-9]{1}', // Estonia
  1275. 'ES': 'ES[0-9]{2}[0-9]{4}[0-9]{4}[0-9]{1}[0-9]{1}[0-9]{10}', // Spain
  1276. 'FI': 'FI[0-9]{2}[0-9]{6}[0-9]{7}[0-9]{1}', // Finland
  1277. 'FO': 'FO[0-9]{2}[0-9]{4}[0-9]{9}[0-9]{1}', // Faroe Islands
  1278. 'FR': 'FR[0-9]{2}[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}', // France
  1279. 'GB': 'GB[0-9]{2}[A-Z]{4}[0-9]{6}[0-9]{8}', // United Kingdom
  1280. 'GE': 'GE[0-9]{2}[A-Z]{2}[0-9]{16}', // Georgia
  1281. 'GI': 'GI[0-9]{2}[A-Z]{4}[A-Z0-9]{15}', // Gibraltar
  1282. 'GL': 'GL[0-9]{2}[0-9]{4}[0-9]{9}[0-9]{1}', // Greenland[
  1283. 'GR': 'GR[0-9]{2}[0-9]{3}[0-9]{4}[A-Z0-9]{16}', // Greece
  1284. 'GT': 'GT[0-9]{2}[A-Z0-9]{4}[A-Z0-9]{20}', // Guatemala
  1285. 'HR': 'HR[0-9]{2}[0-9]{7}[0-9]{10}', // Croatia
  1286. 'HU': 'HU[0-9]{2}[0-9]{3}[0-9]{4}[0-9]{1}[0-9]{15}[0-9]{1}', // Hungary
  1287. 'IE': 'IE[0-9]{2}[A-Z]{4}[0-9]{6}[0-9]{8}', // Ireland
  1288. 'IL': 'IL[0-9]{2}[0-9]{3}[0-9]{3}[0-9]{13}', // Israel
  1289. 'IR': 'IR[0-9]{2}[0-9]{22}', // Iran
  1290. 'IS': 'IS[0-9]{2}[0-9]{4}[0-9]{2}[0-9]{6}[0-9]{10}', // Iceland
  1291. 'IT': 'IT[0-9]{2}[A-Z]{1}[0-9]{5}[0-9]{5}[A-Z0-9]{12}', // Italy
  1292. 'JO': 'JO[0-9]{2}[A-Z]{4}[0-9]{4}[0]{8}[A-Z0-9]{10}', // Jordan
  1293. 'KW': 'KW[0-9]{2}[A-Z]{4}[0-9]{22}', // Kuwait
  1294. 'KZ': 'KZ[0-9]{2}[0-9]{3}[A-Z0-9]{13}', // Kazakhstan
  1295. 'LB': 'LB[0-9]{2}[0-9]{4}[A-Z0-9]{20}', // Lebanon
  1296. 'LI': 'LI[0-9]{2}[0-9]{5}[A-Z0-9]{12}', // Liechtenstein
  1297. 'LT': 'LT[0-9]{2}[0-9]{5}[0-9]{11}', // Lithuania
  1298. 'LU': 'LU[0-9]{2}[0-9]{3}[A-Z0-9]{13}', // Luxembourg
  1299. 'LV': 'LV[0-9]{2}[A-Z]{4}[A-Z0-9]{13}', // Latvia
  1300. 'MC': 'MC[0-9]{2}[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}', // Monaco
  1301. 'MD': 'MD[0-9]{2}[A-Z0-9]{20}', // Moldova
  1302. 'ME': 'ME[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}', // Montenegro
  1303. 'MG': 'MG[0-9]{2}[0-9]{23}', // Madagascar
  1304. 'MK': 'MK[0-9]{2}[0-9]{3}[A-Z0-9]{10}[0-9]{2}', // Macedonia
  1305. 'ML': 'ML[0-9]{2}[A-Z]{1}[0-9]{23}', // Mali
  1306. 'MR': 'MR13[0-9]{5}[0-9]{5}[0-9]{11}[0-9]{2}', // Mauritania
  1307. 'MT': 'MT[0-9]{2}[A-Z]{4}[0-9]{5}[A-Z0-9]{18}', // Malta
  1308. 'MU': 'MU[0-9]{2}[A-Z]{4}[0-9]{2}[0-9]{2}[0-9]{12}[0-9]{3}[A-Z]{3}',// Mauritius
  1309. 'MZ': 'MZ[0-9]{2}[0-9]{21}', // Mozambique
  1310. 'NL': 'NL[0-9]{2}[A-Z]{4}[0-9]{10}', // Netherlands
  1311. 'NO': 'NO[0-9]{2}[0-9]{4}[0-9]{6}[0-9]{1}', // Norway
  1312. 'PK': 'PK[0-9]{2}[A-Z]{4}[A-Z0-9]{16}', // Pakistan
  1313. 'PL': 'PL[0-9]{2}[0-9]{8}[0-9]{16}', // Poland
  1314. 'PS': 'PS[0-9]{2}[A-Z]{4}[A-Z0-9]{21}', // Palestinian
  1315. 'PT': 'PT[0-9]{2}[0-9]{4}[0-9]{4}[0-9]{11}[0-9]{2}', // Portugal
  1316. 'QA': 'QA[0-9]{2}[A-Z]{4}[A-Z0-9]{21}', // Qatar
  1317. 'RO': 'RO[0-9]{2}[A-Z]{4}[A-Z0-9]{16}', // Romania
  1318. 'RS': 'RS[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}', // Serbia
  1319. 'SA': 'SA[0-9]{2}[0-9]{2}[A-Z0-9]{18}', // Saudi Arabia
  1320. 'SE': 'SE[0-9]{2}[0-9]{3}[0-9]{16}[0-9]{1}', // Sweden
  1321. 'SI': 'SI[0-9]{2}[0-9]{5}[0-9]{8}[0-9]{2}', // Slovenia
  1322. 'SK': 'SK[0-9]{2}[0-9]{4}[0-9]{6}[0-9]{10}', // Slovakia
  1323. 'SM': 'SM[0-9]{2}[A-Z]{1}[0-9]{5}[0-9]{5}[A-Z0-9]{12}', // San Marino
  1324. 'SN': 'SN[0-9]{2}[A-Z]{1}[0-9]{23}', // Senegal
  1325. 'TN': 'TN59[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}', // Tunisia
  1326. 'TR': 'TR[0-9]{2}[0-9]{5}[A-Z0-9]{1}[A-Z0-9]{16}', // Turkey
  1327. 'VG': 'VG[0-9]{2}[A-Z]{4}[0-9]{16}' // Virgin Islands, British
  1328. };
  1329. value = value.replace(/[^a-zA-Z0-9]/g, '').toUpperCase();
  1330. var country = options.country || value.substr(0, 2);
  1331. if (!ibanRegex[country]) {
  1332. return false;
  1333. }
  1334. if (!(new RegExp('^' + ibanRegex[country] + '$')).test(value)) {
  1335. return false;
  1336. }
  1337. value = value.substr(4) + value.substr(0, 4);
  1338. value = value.split('').map(function(n) {
  1339. var code = n.charCodeAt(0);
  1340. return (code >= 'A'.charCodeAt(0) && code <= 'Z'.charCodeAt(0))
  1341. // Replace A, B, C, ..., Z with 10, 11, ..., 35
  1342. ? (code - 'A'.charCodeAt(0) + 10)
  1343. : n;
  1344. }).join('');
  1345. var temp = parseInt(value.substr(0, 1), 10),
  1346. length = value.length;
  1347. for (var i = 1; i < length; ++i) {
  1348. temp = (temp * 10 + parseInt(value.substr(i, 1), 10)) % 97;
  1349. }
  1350. return (temp == 1);
  1351. }
  1352. };
  1353. }(window.jQuery));
  1354. ;(function($) {
  1355. $.fn.bootstrapValidator.validators.identical = {
  1356. html5Attributes: {
  1357. message: 'message',
  1358. field: 'field'
  1359. },
  1360. /**
  1361. * Check if input value equals to value of particular one
  1362. *
  1363. * @param {BootstrapValidator} validator The validator plugin instance
  1364. * @param {jQuery} $field Field element
  1365. * @param {Object} options Consists of the following key:
  1366. * - field: The name of field that will be used to compare with current one
  1367. * @returns {Boolean}
  1368. */
  1369. validate: function(validator, $field, options) {
  1370. var value = $field.val();
  1371. if (value == '') {
  1372. return true;
  1373. }
  1374. var compareWith = validator.getFieldElements(options.field);
  1375. if (compareWith == null) {
  1376. return true;
  1377. }
  1378. if (value == compareWith.val()) {
  1379. validator.updateStatus(options.field, validator.STATUS_VALID, 'identical');
  1380. return true;
  1381. } else {
  1382. return false;
  1383. }
  1384. }
  1385. };
  1386. }(window.jQuery));
  1387. ;(function($) {
  1388. $.fn.bootstrapValidator.validators.integer = {
  1389. enableByHtml5: function($field) {
  1390. return ('number' == $field.attr('type'));
  1391. },
  1392. /**
  1393. * Return true if the input value is an integer
  1394. *
  1395. * @param {BootstrapValidator} validator The validator plugin instance
  1396. * @param {jQuery} $field Field element
  1397. * @param {Object} options Can consist of the following key:
  1398. * - message: The invalid message
  1399. * @returns {Boolean}
  1400. */
  1401. validate: function(validator, $field, options) {
  1402. var value = $field.val();
  1403. if (value == '') {
  1404. return true;
  1405. }
  1406. return /^(?:-?(?:0|[1-9][0-9]*))$/.test(value);
  1407. }
  1408. };
  1409. }(window.jQuery));
  1410. ;(function($) {
  1411. $.fn.bootstrapValidator.validators.ip = {
  1412. html5Attributes: {
  1413. message: 'message',
  1414. ipv4: 'ipv4',
  1415. ipv6: 'ipv6'
  1416. },
  1417. /**
  1418. * Return true if the input value is a IP address.
  1419. *
  1420. * @param {BootstrapValidator} validator The validator plugin instance
  1421. * @param {jQuery} $field Field element
  1422. * @param {Object} options Can consist of the following keys:
  1423. * - ipv4: Enable IPv4 validator, default to true
  1424. * - ipv6: Enable IPv6 validator, default to true
  1425. * - message: The invalid message
  1426. * @returns {Boolean}
  1427. */
  1428. validate: function(validator, $field, options) {
  1429. var value = $field.val();
  1430. if (value == '') {
  1431. return true;
  1432. }
  1433. options = $.extend({}, { ipv4: true, ipv6: true }, options);
  1434. if (options.ipv4) {
  1435. 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);
  1436. } else if (options.ipv6) {
  1437. 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);
  1438. }
  1439. return false;
  1440. }
  1441. };
  1442. }(window.jQuery));
  1443. ;(function($) {
  1444. $.fn.bootstrapValidator.validators.isbn = {
  1445. /**
  1446. * Return true if the input value is a valid ISBN 10 or ISBN 13 number
  1447. *
  1448. * @param {BootstrapValidator} validator The validator plugin instance
  1449. * @param {jQuery} $field Field element
  1450. * @param {Object} options Can consist of the following keys:
  1451. * - message: The invalid message
  1452. * @returns {Boolean}
  1453. */
  1454. validate: function(validator, $field, options) {
  1455. var value = $field.val();
  1456. if (value == '') {
  1457. return true;
  1458. }
  1459. // Replace all special characters except digits and X
  1460. value = value.replace(/[^\dX]/gi, '');
  1461. var chars = value.split(''),
  1462. sum = 0,
  1463. checksum;
  1464. // See http://en.wikipedia.org/wiki/International_Standard_Book_Number
  1465. switch (chars.length) {
  1466. // ISBN 10
  1467. case 10:
  1468. sum = 0;
  1469. for (var i = 0; i < 9; i++) {
  1470. sum += ((10 - i) * parseInt(chars[i]));
  1471. }
  1472. checksum = 11 - (sum % 11);
  1473. if (checksum == 11) {
  1474. checksum = 0;
  1475. } else if (checksum == 10) {
  1476. checksum = 'X';
  1477. }
  1478. return (checksum == chars[9]);
  1479. // ISBN 13
  1480. case 13:
  1481. sum = 0;
  1482. for (var i = 0; i < 12; i++) {
  1483. sum += ((i % 2 == 0) ? parseInt(chars[i]) : (parseInt(chars[i]) * 3));
  1484. }
  1485. checksum = 10 - (sum % 10);
  1486. if (checksum == 10) {
  1487. checksum = '0';
  1488. }
  1489. return (checksum == chars[12]);
  1490. default:
  1491. return false;
  1492. }
  1493. }
  1494. };
  1495. }(window.jQuery));
  1496. ;(function($) {
  1497. $.fn.bootstrapValidator.validators.lessThan = {
  1498. html5Attributes: {
  1499. message: 'message',
  1500. value: 'value',
  1501. inclusive: 'inclusive'
  1502. },
  1503. enableByHtml5: function($field) {
  1504. var max = $field.attr('max');
  1505. if (max) {
  1506. return {
  1507. value: max
  1508. };
  1509. }
  1510. return false;
  1511. },
  1512. /**
  1513. * Return true if the input value is less than or equal to given number
  1514. *
  1515. * @param {BootstrapValidator} validator The validator plugin instance
  1516. * @param {jQuery} $field Field element
  1517. * @param {Object} options Can consist of the following keys:
  1518. * - value: The number used to compare to
  1519. * - inclusive [optional]: Can be true or false. Default is true
  1520. * - message: The invalid message
  1521. * @returns {Boolean}
  1522. */
  1523. validate: function(validator, $field, options) {
  1524. var value = $field.val();
  1525. if (value == '') {
  1526. return true;
  1527. }
  1528. value = parseFloat(value);
  1529. return (options.inclusive === false) ? (value <= options.value) : (value < options.value);
  1530. }
  1531. };
  1532. }(window.jQuery));
  1533. ;(function($) {
  1534. $.fn.bootstrapValidator.validators.mac = {
  1535. /**
  1536. * Return true if the input value is a MAC address.
  1537. *
  1538. * @param {BootstrapValidator} validator The validator plugin instance
  1539. * @param {jQuery} $field Field element
  1540. * @param {Object} options Can consist of the following keys:
  1541. * - message: The invalid message
  1542. * @returns {Boolean}
  1543. */
  1544. validate: function(validator, $field, options) {
  1545. var value = $field.val();
  1546. if (value == '') {
  1547. return true;
  1548. }
  1549. return /^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$/.test(value);
  1550. }
  1551. };
  1552. }(window.jQuery));
  1553. ;(function($) {
  1554. $.fn.bootstrapValidator.validators.notEmpty = {
  1555. enableByHtml5: function($field) {
  1556. var required = $field.attr('required') + '';
  1557. return ('required' == required || 'true' == required);
  1558. },
  1559. /**
  1560. * Check if input value is empty or not
  1561. *
  1562. * @param {BootstrapValidator} validator The validator plugin instance
  1563. * @param {jQuery} $field Field element
  1564. * @param {Object} options
  1565. * @returns {Boolean}
  1566. */
  1567. validate: function(validator, $field, options) {
  1568. var type = $field.attr('type');
  1569. if ('radio' == type || 'checkbox' == type) {
  1570. return validator
  1571. .getFieldElements($field.attr('data-bv-field'))
  1572. .filter(':checked')
  1573. .length > 0;
  1574. }
  1575. return $.trim($field.val()) != '';
  1576. }
  1577. };
  1578. }(window.jQuery));
  1579. ;(function($) {
  1580. $.fn.bootstrapValidator.validators.numeric = {
  1581. /**
  1582. * Validate decimal number
  1583. *
  1584. * @param {BootstrapValidator} validator The validator plugin instance
  1585. * @param {jQuery} $field Field element
  1586. * @param {Object} options Consist of key:
  1587. * - message: The invalid message
  1588. * @returns {Boolean}
  1589. */
  1590. validate: function(validator, $field, options) {
  1591. var value = $field.val();
  1592. if (value == '') {
  1593. return true;
  1594. }
  1595. return !isNaN(parseFloat(value)) && isFinite(value);
  1596. }
  1597. };
  1598. }(window.jQuery));
  1599. ;(function($) {
  1600. $.fn.bootstrapValidator.validators.phone = {
  1601. html5Attributes: {
  1602. message: 'message',
  1603. country: 'country'
  1604. },
  1605. /**
  1606. * Return true if the input value contains a valid US phone number only
  1607. *
  1608. * @param {BootstrapValidator} validator Validate plugin instance
  1609. * @param {jQuery} $field Field element
  1610. * @param {Object} options Consist of key:
  1611. * - message: The invalid message
  1612. * - country: The ISO 3166 country code
  1613. * Currently it only supports United State (US) country
  1614. * @returns {Boolean}
  1615. */
  1616. validate: function(validator, $field, options) {
  1617. var value = $field.val();
  1618. if (value == '') {
  1619. return true;
  1620. }
  1621. options.country = options.country || 'US';
  1622. switch (options.country.toUpperCase()) {
  1623. case 'US':
  1624. default:
  1625. value = value.replace(/\(|\)|\s+/g, '');
  1626. return (/^(?:1\-?)?(\d{3})[\-\.]?(\d{3})[\-\.]?(\d{4})$/).test(value);
  1627. }
  1628. }
  1629. }
  1630. }(window.jQuery));
  1631. ;(function($) {
  1632. $.fn.bootstrapValidator.validators.regexp = {
  1633. html5Attributes: {
  1634. message: 'message',
  1635. regexp: 'regexp'
  1636. },
  1637. enableByHtml5: function($field) {
  1638. var pattern = $field.attr('pattern');
  1639. if (pattern) {
  1640. return {
  1641. regexp: pattern
  1642. };
  1643. }
  1644. return false;
  1645. },
  1646. /**
  1647. * Check if the element value matches given regular expression
  1648. *
  1649. * @param {BootstrapValidator} validator The validator plugin instance
  1650. * @param {jQuery} $field Field element
  1651. * @param {Object} options Consists of the following key:
  1652. * - regexp: The regular expression you need to check
  1653. * @returns {Boolean}
  1654. */
  1655. validate: function(validator, $field, options) {
  1656. var value = $field.val();
  1657. if (value == '') {
  1658. return true;
  1659. }
  1660. var regexp = ('string' == typeof options.regexp) ? new RegExp(options.regexp) : options.regexp;
  1661. return regexp.test(value);
  1662. }
  1663. };
  1664. }(window.jQuery));
  1665. ;(function($) {
  1666. $.fn.bootstrapValidator.validators.remote = {
  1667. html5Attributes: {
  1668. message: 'message',
  1669. url: 'url'
  1670. },
  1671. /**
  1672. * Request a remote server to check the input value
  1673. *
  1674. * @param {BootstrapValidator} validator Plugin instance
  1675. * @param {jQuery} $field Field element
  1676. * @param {Object} options Can consist of the following keys:
  1677. * - url
  1678. * - data [optional]: By default, it will take the value
  1679. * {
  1680. * <fieldName>: <fieldValue>
  1681. * }
  1682. * - message: The invalid message
  1683. * @returns {Boolean|Deferred}
  1684. */
  1685. validate: function(validator, $field, options) {
  1686. var value = $field.val();
  1687. if (value == '') {
  1688. return true;
  1689. }
  1690. var name = $field.attr('data-bv-field'), data = options.data;
  1691. if (data == null) {
  1692. data = {};
  1693. }
  1694. // Support dynamic data
  1695. if ('function' == typeof data) {
  1696. data = data.call(this, validator);
  1697. }
  1698. data[name] = value;
  1699. var dfd = new $.Deferred();
  1700. var xhr = $.ajax({
  1701. type: 'POST',
  1702. url: options.url,
  1703. dataType: 'json',
  1704. data: data
  1705. });
  1706. xhr.then(function(response) {
  1707. dfd.resolve($field, 'remote', response.valid === true || response.valid === 'true');
  1708. });
  1709. dfd.fail(function() {
  1710. xhr.abort();
  1711. });
  1712. return dfd;
  1713. }
  1714. };
  1715. }(window.jQuery));
  1716. ;(function($) {
  1717. $.fn.bootstrapValidator.validators.step = {
  1718. html5Attributes: {
  1719. message: 'message',
  1720. base: 'baseValue',
  1721. step: 'step'
  1722. },
  1723. /**
  1724. * Return true if the input value is valid step one
  1725. *
  1726. * @param {BootstrapValidator} validator The validator plugin instance
  1727. * @param {jQuery} $field Field element
  1728. * @param {Object} options Can consist of the following keys:
  1729. * - baseValue: The base value
  1730. * - step: The step
  1731. * - message: The invalid message
  1732. * @returns {Boolean}
  1733. */
  1734. validate: function(validator, $field, options) {
  1735. var value = $field.val();
  1736. if (value == '') {
  1737. return true;
  1738. }
  1739. options = $.extend({}, { baseValue: 0, step: 1 }, options);
  1740. value = parseFloat(value);
  1741. if (isNaN(value) || !isFinite(value)) {
  1742. return false;
  1743. }
  1744. var round = function(x, precision) {
  1745. var m = Math.pow(10, precision);
  1746. x = x * m;
  1747. var sign = (x > 0) | -(x < 0),
  1748. isHalf = (x % 1 === 0.5 * sign);
  1749. if (isHalf) {
  1750. return (Math.floor(x) + (sign > 0)) / m;
  1751. } else {
  1752. return Math.round(x) / m;
  1753. }
  1754. },
  1755. floatMod = function(x, y) {
  1756. if (y == 0.0) {
  1757. return 1.0;
  1758. }
  1759. var dotX = (x + '').split('.'),
  1760. dotY = (y + '').split('.'),
  1761. precision = ((dotX.length == 1) ? 0 : dotX[1].length) + ((dotY.length == 1) ? 0 : dotY[1].length);
  1762. return round(x - y * Math.floor(x / y), precision);
  1763. };
  1764. var mod = floatMod(value - options.baseValue, options.step);
  1765. return (mod == 0.0 || mod == options.step);
  1766. }
  1767. };
  1768. }(window.jQuery));
  1769. ;(function($) {
  1770. $.fn.bootstrapValidator.validators.stringCase = {
  1771. html5Attributes: {
  1772. message: 'message',
  1773. 'case': 'case'
  1774. },
  1775. /**
  1776. * Check if a string is a lower or upper case one
  1777. *
  1778. * @param {BootstrapValidator} validator The validator plugin instance
  1779. * @param {jQuery} $field Field element
  1780. * @param {Object} options Consist of key:
  1781. * - message: The invalid message
  1782. * - case: Can be 'lower' (default) or 'upper'
  1783. * @returns {Boolean}
  1784. */
  1785. validate: function(validator, $field, options) {
  1786. var value = $field.val();
  1787. if (value == '') {
  1788. return true;
  1789. }
  1790. var stringCase = (options['case'] || 'lower').toLowerCase();
  1791. switch (stringCase) {
  1792. case 'upper':
  1793. return value === value.toUpperCase();
  1794. case 'lower':
  1795. default:
  1796. return value === value.toLowerCase();
  1797. }
  1798. }
  1799. };
  1800. }(window.jQuery));
  1801. ;(function($) {
  1802. $.fn.bootstrapValidator.validators.stringLength = {
  1803. html5Attributes: {
  1804. message: 'message',
  1805. min: 'min',
  1806. max: 'max'
  1807. },
  1808. enableByHtml5: function($field) {
  1809. var maxLength = $field.attr('maxlength');
  1810. if (maxLength) {
  1811. return {
  1812. max: parseInt(maxLength, 10)
  1813. };
  1814. }
  1815. return false;
  1816. },
  1817. /**
  1818. * Check if the length of element value is less or more than given number
  1819. *
  1820. * @param {BootstrapValidator} validator The validator plugin instance
  1821. * @param {jQuery} $field Field element
  1822. * @param {Object} options Consists of following keys:
  1823. * - min
  1824. * - max
  1825. * At least one of two keys is required
  1826. * - message: The invalid message
  1827. * @returns {Boolean}
  1828. */
  1829. validate: function(validator, $field, options) {
  1830. var value = $field.val();
  1831. if (value == '') {
  1832. return true;
  1833. }
  1834. var length = $.trim(value).length;
  1835. if ((options.min && length < options.min) || (options.max && length > options.max)) {
  1836. return false;
  1837. }
  1838. return true;
  1839. }
  1840. };
  1841. }(window.jQuery));
  1842. ;(function($) {
  1843. $.fn.bootstrapValidator.validators.uri = {
  1844. enableByHtml5: function($field) {
  1845. return ('url' == $field.attr('type'));
  1846. },
  1847. /**
  1848. * Return true if the input value is a valid URL
  1849. *
  1850. * @param {BootstrapValidator} validator The validator plugin instance
  1851. * @param {jQuery} $field Field element
  1852. * @param {Object} options
  1853. * @returns {Boolean}
  1854. */
  1855. validate: function(validator, $field, options) {
  1856. var value = $field.val();
  1857. if (value == '') {
  1858. return true;
  1859. }
  1860. // Credit to https://gist.github.com/dperini/729294
  1861. //
  1862. // Regular Expression for URL validation
  1863. //
  1864. // Author: Diego Perini
  1865. // Updated: 2010/12/05
  1866. //
  1867. // the regular expression composed & commented
  1868. // could be easily tweaked for RFC compliance,
  1869. // it was expressly modified to fit & satisfy
  1870. // these test for an URL shortener:
  1871. //
  1872. // http://mathiasbynens.be/demo/url-regex
  1873. //
  1874. // Notes on possible differences from a standard/generic validation:
  1875. //
  1876. // - utf-8 char class take in consideration the full Unicode range
  1877. // - TLDs have been made mandatory so single names like "localhost" fails
  1878. // - protocols have been restricted to ftp, http and https only as requested
  1879. //
  1880. // Changes:
  1881. //
  1882. // - IP address dotted notation validation, range: 1.0.0.0 - 223.255.255.255
  1883. // first and last IP address of each class is considered invalid
  1884. // (since they are broadcast/network addresses)
  1885. //
  1886. // - Added exclusion of private, reserved and/or local networks ranges
  1887. //
  1888. // Compressed one-line versions:
  1889. //
  1890. // Javascript version
  1891. //
  1892. // /^(?:(?: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
  1893. //
  1894. // PHP version
  1895. //
  1896. // _^(?:(?: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
  1897. var urlExp = new RegExp(
  1898. "^" +
  1899. // protocol identifier
  1900. "(?:(?:https?|ftp)://)" +
  1901. // user:pass authentication
  1902. "(?:\\S+(?::\\S*)?@)?" +
  1903. "(?:" +
  1904. // IP address exclusion
  1905. // private & local networks
  1906. "(?!10(?:\\.\\d{1,3}){3})" +
  1907. "(?!127(?:\\.\\d{1,3}){3})" +
  1908. "(?!169\\.254(?:\\.\\d{1,3}){2})" +
  1909. "(?!192\\.168(?:\\.\\d{1,3}){2})" +
  1910. "(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})" +
  1911. // IP address dotted notation octets
  1912. // excludes loopback network 0.0.0.0
  1913. // excludes reserved space >= 224.0.0.0
  1914. // excludes network & broacast addresses
  1915. // (first & last IP address of each class)
  1916. "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
  1917. "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
  1918. "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
  1919. "|" +
  1920. // host name
  1921. "(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)" +
  1922. // domain name
  1923. "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*" +
  1924. // TLD identifier
  1925. "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" +
  1926. ")" +
  1927. // port number
  1928. "(?::\\d{2,5})?" +
  1929. // resource path
  1930. "(?:/[^\\s]*)?" +
  1931. "$", "i"
  1932. );
  1933. return urlExp.test(value);
  1934. }
  1935. };
  1936. }(window.jQuery));
  1937. ;(function($) {
  1938. $.fn.bootstrapValidator.validators.uuid = {
  1939. html5Attributes: {
  1940. message: 'message',
  1941. version: 'version'
  1942. },
  1943. /**
  1944. * Return true if and only if the input value is a valid UUID string
  1945. *
  1946. * @see http://en.wikipedia.org/wiki/Universally_unique_identifier
  1947. * @param {BootstrapValidator} validator The validator plugin instance
  1948. * @param {jQuery} $field Field element
  1949. * @param {Object} options Consist of key:
  1950. * - message: The invalid message
  1951. * - version: Can be 3, 4, 5, null
  1952. * @returns {Boolean}
  1953. */
  1954. validate: function(validator, $field, options) {
  1955. var value = $field.val();
  1956. if (value == '') {
  1957. return true;
  1958. }
  1959. // See the format at http://en.wikipedia.org/wiki/Universally_unique_identifier#Variants_and_versions
  1960. var patterns = {
  1961. '3': /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
  1962. '4': /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
  1963. '5': /^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
  1964. all: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i
  1965. },
  1966. version = options.version ? (options.version + '') : 'all';
  1967. return (null == patterns[version]) ? true : patterns[version].test(value);
  1968. }
  1969. };
  1970. }(window.jQuery));
  1971. ;(function($) {
  1972. $.fn.bootstrapValidator.validators.zipCode = {
  1973. html5Attributes: {
  1974. message: 'message',
  1975. country: 'country'
  1976. },
  1977. /**
  1978. * Return true if and only if the input value is a valid country zip code
  1979. *
  1980. * @param {BootstrapValidator} validator The validator plugin instance
  1981. * @param {jQuery} $field Field element
  1982. * @param {Object} options Consist of key:
  1983. * - message: The invalid message
  1984. * - country: The ISO 3166 country code
  1985. *
  1986. * Currently it supports the following countries:
  1987. * - US (United State)
  1988. * - DK (Denmark)
  1989. * - SE (Sweden)
  1990. * @returns {Boolean}
  1991. */
  1992. validate: function(validator, $field, options) {
  1993. var value = $field.val();
  1994. if (value == '' || !options.country) {
  1995. return true;
  1996. }
  1997. options.country = options.country || 'US';
  1998. switch (options.country.toUpperCase()) {
  1999. case 'DK':
  2000. return /^(DK(-|\s)?)?\d{4}$/i.test(value);
  2001. case 'SE':
  2002. return /^(S-)?\d{3}\s?\d{2}$/i.test(value);
  2003. case 'US':
  2004. default:
  2005. return /^\d{5}([\-]\d{4})?$/.test(value);
  2006. }
  2007. }
  2008. };
  2009. }(window.jQuery));