bootstrapValidator.js 98 KB

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