bootstrapValidator.js 137 KB

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