bootstrapValidator.js 119 KB

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