bootstrapValidator.js 169 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587
  1. /**
  2. * BootstrapValidator (http://bootstrapvalidator.com)
  3. *
  4. * A jQuery plugin to validate form fields. Use with Bootstrap 3
  5. *
  6. * @version v0.4.5-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. // Get IE version
  26. // https://gist.github.com/padolsey/527683/#comment-7595
  27. var ieVersion = (function() {
  28. var v = 3, div = document.createElement('div'), a = div.all || [];
  29. while (div.innerHTML = '<!--[if gt IE '+(++v)+']><br><![endif]-->', a[0]);
  30. return v > 4 ? v : !v;
  31. }());
  32. var el = document.createElement('div');
  33. this._changeEvent = (ieVersion === 9 || !('oninput' in el)) ? 'keyup' : 'input';
  34. // The flag to indicate that the form is ready to submit when a remote/callback validator returns
  35. this._submitIfValid = null;
  36. this._init();
  37. };
  38. // The default options
  39. BootstrapValidator.DEFAULT_OPTIONS = {
  40. // The form CSS class
  41. elementClass: 'bv-form',
  42. // Default invalid message
  43. message: 'This value is not valid',
  44. // The field will not be live validated if its length is less than this number of characters
  45. threshold: null,
  46. // Indicate fields which won't be validated
  47. // By default, the plugin will not validate the following kind of fields:
  48. // - disabled
  49. // - hidden
  50. // - invisible
  51. //
  52. // The setting consists of jQuery filters. Accept 3 formats:
  53. // - A string. Use a comma to separate filter
  54. // - An array. Each element is a filter
  55. // - An array. Each element can be a callback function
  56. // function($field, validator) {
  57. // $field is jQuery object representing the field element
  58. // validator is the BootstrapValidator instance
  59. // return true or false;
  60. // }
  61. //
  62. // The 3 following settings are equivalent:
  63. //
  64. // 1) ':disabled, :hidden, :not(:visible)'
  65. // 2) [':disabled', ':hidden', ':not(:visible)']
  66. // 3) [':disabled', ':hidden', function($field) {
  67. // return !$field.is(':visible');
  68. // }]
  69. excluded: [':disabled', ':hidden', ':not(:visible)'],
  70. // Shows ok/error/loading icons based on the field validity.
  71. // This feature requires Bootstrap v3.1.0 or later (http://getbootstrap.com/css/#forms-control-validation).
  72. // Since Bootstrap doesn't provide any methods to know its version, this option cannot be on/off automatically.
  73. // In other word, to use this feature you have to upgrade your Bootstrap to v3.1.0 or later.
  74. //
  75. // Examples:
  76. // - Use Glyphicons icons:
  77. // feedbackIcons: {
  78. // valid: 'glyphicon glyphicon-ok',
  79. // invalid: 'glyphicon glyphicon-remove',
  80. // validating: 'glyphicon glyphicon-refresh'
  81. // }
  82. // - Use FontAwesome icons:
  83. // feedbackIcons: {
  84. // valid: 'fa fa-check',
  85. // invalid: 'fa fa-times',
  86. // validating: 'fa fa-refresh'
  87. // }
  88. feedbackIcons: {
  89. valid: null,
  90. invalid: null,
  91. validating: null
  92. },
  93. // The submit buttons selector
  94. // These buttons will be disabled to prevent the valid form from multiple submissions
  95. submitButtons: '[type="submit"]',
  96. // The custom submit handler
  97. // It will prevent the form from the default submission
  98. //
  99. // submitHandler: function(validator, form) {
  100. // - validator is the BootstrapValidator instance
  101. // - form is the jQuery object present the current form
  102. // }
  103. submitHandler: null,
  104. // Live validating option
  105. // Can be one of 3 values:
  106. // - enabled: The plugin validates fields as soon as they are changed
  107. // - disabled: Disable the live validating. The error messages are only shown after the form is submitted
  108. // - submitted: The live validating is enabled after the form is submitted
  109. live: 'enabled',
  110. // Map the field name with validator rules
  111. fields: null
  112. };
  113. BootstrapValidator.prototype = {
  114. constructor: BootstrapValidator,
  115. /**
  116. * Init form
  117. */
  118. _init: function() {
  119. var that = this,
  120. options = {
  121. excluded: this.$form.attr('data-bv-excluded'),
  122. trigger: this.$form.attr('data-bv-trigger'),
  123. message: this.$form.attr('data-bv-message'),
  124. submitButtons: this.$form.attr('data-bv-submitbuttons'),
  125. threshold: this.$form.attr('data-bv-threshold'),
  126. live: this.$form.attr('data-bv-live'),
  127. fields: {},
  128. feedbackIcons: {
  129. valid: this.$form.attr('data-bv-feedbackicons-valid'),
  130. invalid: this.$form.attr('data-bv-feedbackicons-invalid'),
  131. validating: this.$form.attr('data-bv-feedbackicons-validating')
  132. }
  133. },
  134. validator,
  135. v, // Validator name
  136. enabled,
  137. optionName,
  138. optionValue,
  139. html5AttrName,
  140. html5Attrs;
  141. this.$form
  142. // Disable client side validation in HTML 5
  143. .attr('novalidate', 'novalidate')
  144. .addClass(this.options.elementClass)
  145. // Disable the default submission first
  146. .on('submit.bv', function(e) {
  147. e.preventDefault();
  148. that.validate();
  149. })
  150. .on('click', this.options.submitButtons, function() {
  151. that.$submitButton = $(this);
  152. // The user just click the submit button
  153. that._submitIfValid = true;
  154. })
  155. // Find all fields which have either "name" or "data-bv-field" attribute
  156. .find('[name], [data-bv-field]')
  157. .each(function() {
  158. var $field = $(this);
  159. if (that._isExcluded($field)) {
  160. return;
  161. }
  162. var field = $field.attr('name') || $field.attr('data-bv-field'),
  163. validators = {};
  164. for (v in $.fn.bootstrapValidator.validators) {
  165. validator = $.fn.bootstrapValidator.validators[v];
  166. enabled = $field.attr('data-bv-' + v.toLowerCase()) + '';
  167. html5Attrs = ('function' == typeof validator.enableByHtml5) ? validator.enableByHtml5($(this)) : null;
  168. if ((html5Attrs && enabled != 'false')
  169. || (html5Attrs !== true && ('' == enabled || 'true' == enabled)))
  170. {
  171. // Try to parse the options via attributes
  172. validator.html5Attributes = validator.html5Attributes || { message: 'message' };
  173. validators[v] = $.extend({}, html5Attrs == true ? {} : html5Attrs, validators[v]);
  174. for (html5AttrName in validator.html5Attributes) {
  175. optionName = validator.html5Attributes[html5AttrName];
  176. optionValue = $field.attr('data-bv-' + v.toLowerCase() + '-' + html5AttrName);
  177. if (optionValue) {
  178. if ('true' == optionValue) {
  179. optionValue = true;
  180. } else if ('false' == optionValue) {
  181. optionValue = false;
  182. }
  183. validators[v][optionName] = optionValue;
  184. }
  185. }
  186. }
  187. }
  188. var opts = {
  189. trigger: $field.attr('data-bv-trigger'),
  190. message: $field.attr('data-bv-message'),
  191. container: $field.attr('data-bv-container'),
  192. selector: $field.attr('data-bv-selector'),
  193. threshold: $field.attr('data-bv-threshold'),
  194. validators: validators
  195. };
  196. // Check if there is any validators set using HTML attributes
  197. if (!$.isEmptyObject(opts.validators) && !$.isEmptyObject(opts)) {
  198. $field.attr('data-bv-field', field);
  199. options.fields[field] = $.extend({}, opts, options.fields[field]);
  200. }
  201. })
  202. .end()
  203. // Create hidden inputs to send the submit buttons
  204. .find(this.options.submitButtons)
  205. .each(function() {
  206. $('<input/>')
  207. .attr('type', 'hidden')
  208. .attr('name', $(this).attr('name'))
  209. .val($(this).val())
  210. .appendTo(that.$form);
  211. });
  212. this.options = $.extend(true, this.options, options);
  213. if ('string' == typeof this.options.excluded) {
  214. this.options.excluded = $.map(this.options.excluded.split(','), function(item) {
  215. // Trim the spaces
  216. return $.trim(item);
  217. });
  218. }
  219. for (var field in this.options.fields) {
  220. this._initField(field);
  221. }
  222. this.setLiveMode(this.options.live);
  223. },
  224. /**
  225. * Init field
  226. *
  227. * @param {String} field The field name
  228. */
  229. _initField: function(field) {
  230. if (this.options.fields[field] == null || this.options.fields[field].validators == null) {
  231. return;
  232. }
  233. var fields = this.getFieldElements(field);
  234. // We don't need to validate non-existing fields
  235. if (fields == null) {
  236. delete this.options.fields[field];
  237. return;
  238. }
  239. for (var validatorName in this.options.fields[field].validators) {
  240. if (!$.fn.bootstrapValidator.validators[validatorName]) {
  241. delete this.options.fields[field].validators[validatorName];
  242. }
  243. }
  244. var that = this,
  245. type = fields.attr('type'),
  246. event = ('radio' == type || 'checkbox' == type || 'file' == type || 'SELECT' == fields[0].tagName) ? 'change' : that._changeEvent,
  247. total = fields.length,
  248. updateAll = (total == 1) || ('radio' == type) || ('checkbox' == type);
  249. for (var i = 0; i < total; i++) {
  250. var $field = $(fields[i]),
  251. $parent = $field.parents('.form-group'),
  252. // Allow user to indicate where the error messages are shown
  253. $message = this.options.fields[field].container ? $parent.find(this.options.fields[field].container) : this._getMessageContainer($field);
  254. // Set the attribute to indicate the fields which are defined by selector
  255. if (!$field.attr('data-bv-field')) {
  256. $field.attr('data-bv-field', field);
  257. }
  258. // Whenever the user change the field value, mark it as not validated yet
  259. $field.on(event + '.update.bv', function() {
  260. // Reset the flag
  261. that._submitIfValid = false;
  262. updateAll ? that.updateStatus(field, that.STATUS_NOT_VALIDATED, null)
  263. : that.updateElementStatus($(this), that.STATUS_NOT_VALIDATED, null);
  264. });
  265. // Create help block elements for showing the error messages
  266. $field.data('bv.messages', $message);
  267. for (validatorName in this.options.fields[field].validators) {
  268. $field.data('bv.result.' + validatorName, this.STATUS_NOT_VALIDATED);
  269. if (!updateAll || i == total - 1) {
  270. $('<small/>')
  271. .css('display', 'none')
  272. .attr('data-bv-validator', validatorName)
  273. .attr('data-bv-validator-for', field)
  274. .html(this.options.fields[field].validators[validatorName].message || this.options.fields[field].message || this.options.message)
  275. .addClass('help-block')
  276. .appendTo($message);
  277. }
  278. }
  279. // Prepare the feedback icons
  280. // Available from Bootstrap 3.1 (http://getbootstrap.com/css/#forms-control-validation)
  281. if (this.options.feedbackIcons
  282. && this.options.feedbackIcons.validating && this.options.feedbackIcons.invalid && this.options.feedbackIcons.valid
  283. && (!updateAll || i == total - 1))
  284. {
  285. $parent.addClass('has-feedback');
  286. var $icon = $('<i/>').css('display', 'none').addClass('form-control-feedback').attr('data-bv-icon-for', field).insertAfter($field);
  287. // The feedback icon does not render correctly if there is no label
  288. // https://github.com/twbs/bootstrap/issues/12873
  289. if ($parent.find('label').length == 0) {
  290. $icon.css('top', 0);
  291. }
  292. }
  293. }
  294. if (this.options.fields[field]['enabled'] == null) {
  295. this.options.fields[field]['enabled'] = true;
  296. }
  297. },
  298. /**
  299. * Get the element to place the error messages
  300. *
  301. * @param {jQuery} $field The field element
  302. * @returns {jQuery}
  303. */
  304. _getMessageContainer: function($field) {
  305. var $parent = $field.parent();
  306. if ($parent.hasClass('form-group')) {
  307. return $parent;
  308. }
  309. var cssClasses = $parent.attr('class');
  310. if (!cssClasses) {
  311. return this._getMessageContainer($parent);
  312. }
  313. cssClasses = cssClasses.split(' ');
  314. var n = cssClasses.length;
  315. for (var i = 0; i < n; i++) {
  316. if (/^col-(xs|sm|md|lg)-\d+$/.test(cssClasses[i]) || /^col-(xs|sm|md|lg)-offset-\d+$/.test(cssClasses[i])) {
  317. return $parent;
  318. }
  319. }
  320. return this._getMessageContainer($parent);
  321. },
  322. /**
  323. * Called when all validations are completed
  324. */
  325. _submit: function() {
  326. if (!this.isValid()) {
  327. if ('submitted' == this.options.live) {
  328. this.setLiveMode('enabled');
  329. }
  330. // Focus to the first invalid field
  331. if (this.$invalidField) {
  332. // Activate the tab containing the invalid field if exists
  333. var $tab = this.$invalidField.parents('.tab-pane'),
  334. tabId;
  335. if ($tab && (tabId = $tab.attr('id'))) {
  336. $('a[href="#' + tabId + '"][data-toggle="tab"]').trigger('click.bs.tab.data-api');
  337. }
  338. this.$invalidField.focus();
  339. }
  340. return;
  341. }
  342. // Call the custom submission if enabled
  343. if (this.options.submitHandler && 'function' == typeof this.options.submitHandler) {
  344. // If you want to submit the form inside your submit handler, please call defaultSubmit() method
  345. this.options.submitHandler.call(this, this, this.$form, this.$submitButton);
  346. } else {
  347. this.disableSubmitButtons(true).defaultSubmit();
  348. }
  349. },
  350. /**
  351. * Check if the field is excluded.
  352. * Returning true means that the field will not be validated
  353. *
  354. * @param {jQuery} $field The field element
  355. * @return {Boolean}
  356. */
  357. _isExcluded: function($field) {
  358. if (this.options.excluded) {
  359. var length = this.options.excluded.length;
  360. for (var i = 0; i < length; i++) {
  361. if (('string' == typeof this.options.excluded[i] && $field.is(this.options.excluded[i]))
  362. || ('function' == typeof this.options.excluded[i] && this.options.excluded[i].call(this, $field, this) == true))
  363. {
  364. return true;
  365. }
  366. }
  367. }
  368. return false;
  369. },
  370. /**
  371. * Check if the number of characters of field value exceed the threshold or not
  372. *
  373. * @param {jQuery} $field The field element
  374. * @returns {Boolean}
  375. */
  376. _exceedThreshold: function($field) {
  377. var field = $field.attr('data-bv-field'),
  378. threshold = this.options.fields[field].threshold || this.options.threshold;
  379. if (!threshold) {
  380. return true;
  381. }
  382. var type = $field.attr('type'),
  383. cannotType = ['button', 'checkbox', 'file', 'hidden', 'image', 'radio', 'reset', 'submit'].indexOf(type) != -1;
  384. return (cannotType || $field.val().length >= threshold);
  385. },
  386. // --- Public methods ---
  387. /**
  388. * Retrieve the field elements by given name
  389. *
  390. * @param {String} field The field name
  391. * @returns {null|jQuery[]}
  392. */
  393. getFieldElements: function(field) {
  394. var fields = this.options.fields[field].selector ? $(this.options.fields[field].selector) : this.$form.find('[name="' + field + '"]');
  395. return (fields.length == 0) ? null : fields;
  396. },
  397. /**
  398. * Set live validating mode
  399. *
  400. * @param {String} mode Live validating mode. Can be 'enabled', 'disabled', 'submitted'
  401. * @returns {BootstrapValidator}
  402. */
  403. setLiveMode: function(mode) {
  404. this.options.live = mode;
  405. if ('submitted' == mode) {
  406. return this;
  407. }
  408. var that = this;
  409. for (var field in this.options.fields) {
  410. (function(f) {
  411. var fields = that.getFieldElements(f);
  412. if (fields) {
  413. var type = fields.attr('type'),
  414. total = fields.length,
  415. updateAll = (total == 1) || ('radio' == type) || ('checkbox' == type),
  416. trigger = that.options.fields[field].trigger
  417. || that.options.trigger
  418. || (('radio' == type || 'checkbox' == type || 'file' == type || 'SELECT' == fields[0].tagName) ? 'change' : that._changeEvent),
  419. events = $.map(trigger.split(' '), function(item) {
  420. return item + '.live.bv';
  421. }).join(' ');
  422. for (var i = 0; i < total; i++) {
  423. ('enabled' == mode)
  424. ? $(fields[i]).on(events, function() {
  425. if (that._exceedThreshold($(this))) {
  426. updateAll ? that.validateField(f) : that.validateFieldElement($(this), false);
  427. }
  428. })
  429. : $(fields[i]).off(events);
  430. }
  431. }
  432. })(field);
  433. }
  434. return this;
  435. },
  436. /**
  437. * Disable/enable submit buttons
  438. *
  439. * @param {Boolean} disabled Can be true or false
  440. * @returns {BootstrapValidator}
  441. */
  442. disableSubmitButtons: function(disabled) {
  443. if (!disabled) {
  444. this.$form.find(this.options.submitButtons).removeAttr('disabled');
  445. } else if (this.options.live != 'disabled') {
  446. // Don't disable if the live validating mode is disabled
  447. this.$form.find(this.options.submitButtons).attr('disabled', 'disabled');
  448. }
  449. return this;
  450. },
  451. /**
  452. * Validate the form
  453. *
  454. * @return {BootstrapValidator}
  455. */
  456. validate: function() {
  457. if (!this.options.fields) {
  458. return this;
  459. }
  460. this.disableSubmitButtons(true);
  461. for (var field in this.options.fields) {
  462. this.validateField(field);
  463. }
  464. // Check if whether the submit button is clicked
  465. if (this.$submitButton) {
  466. this._submit();
  467. }
  468. return this;
  469. },
  470. /**
  471. * Validate given field
  472. *
  473. * @param {String} field The field name
  474. * @returns {BootstrapValidator}
  475. */
  476. validateField: function(field) {
  477. var fields = this.getFieldElements(field),
  478. type = fields.attr('type'),
  479. n = (('radio' == type) || ('checkbox' == type)) ? 1 : fields.length;
  480. for (var i = 0; i < n; i++) {
  481. this.validateFieldElement($(fields[i]), (n == 1));
  482. }
  483. return this;
  484. },
  485. /**
  486. * Validate field element
  487. *
  488. * @param {jQuery} $field The field element
  489. * @param {Boolean} updateAll If true, update status of all elements which have the same name
  490. * @returns {BootstrapValidator}
  491. */
  492. validateFieldElement: function($field, updateAll) {
  493. var that = this,
  494. field = $field.attr('data-bv-field'),
  495. validators = this.options.fields[field].validators,
  496. validatorName,
  497. validateResult;
  498. if (!this.options.fields[field]['enabled'] || this._isExcluded($field)) {
  499. return this;
  500. }
  501. for (validatorName in validators) {
  502. if ($field.data('bv.dfs.' + validatorName)) {
  503. $field.data('bv.dfs.' + validatorName).reject();
  504. }
  505. // Don't validate field if it is already done
  506. var result = $field.data('bv.result.' + validatorName);
  507. if (result == this.STATUS_VALID || result == this.STATUS_INVALID) {
  508. continue;
  509. }
  510. $field.data('bv.result.' + validatorName, this.STATUS_VALIDATING);
  511. validateResult = $.fn.bootstrapValidator.validators[validatorName].validate(this, $field, validators[validatorName]);
  512. if ('object' == typeof validateResult) {
  513. updateAll ? this.updateStatus(field, this.STATUS_VALIDATING, validatorName)
  514. : this.updateElementStatus($field, this.STATUS_VALIDATING, validatorName);
  515. $field.data('bv.dfs.' + validatorName, validateResult);
  516. validateResult.done(function($f, v, isValid) {
  517. // v is validator name
  518. $f.removeData('bv.dfs.' + v);
  519. updateAll ? that.updateStatus($f.attr('data-bv-field'), isValid ? that.STATUS_VALID : that.STATUS_INVALID, v)
  520. : that.updateElementStatus($f, isValid ? that.STATUS_VALID : that.STATUS_INVALID, v);
  521. if (isValid && that._submitIfValid == true) {
  522. // If a remote validator returns true and the form is ready to submit, then do it
  523. that._submit();
  524. }
  525. });
  526. } else if ('boolean' == typeof validateResult) {
  527. updateAll ? this.updateStatus(field, validateResult ? this.STATUS_VALID : this.STATUS_INVALID, validatorName)
  528. : this.updateElementStatus($field, validateResult ? this.STATUS_VALID : this.STATUS_INVALID, validatorName);
  529. }
  530. }
  531. return this;
  532. },
  533. /**
  534. * Update all validating results of elements which have the same field name
  535. *
  536. * @param {String} field The field name
  537. * @param {String} status The status. Can be 'NOT_VALIDATED', 'VALIDATING', 'INVALID' or 'VALID'
  538. * @param {String} [validatorName] The validator name. If null, the method updates validity result for all validators
  539. * @return {BootstrapValidator}
  540. */
  541. updateStatus: function(field, status, validatorName) {
  542. var fields = this.getFieldElements(field),
  543. type = fields.attr('type'),
  544. n = (('radio' == type) || ('checkbox' == type)) ? 1 : fields.length;
  545. for (var i = 0; i < n; i++) {
  546. this.updateElementStatus($(fields[i]), status, validatorName);
  547. }
  548. return this;
  549. },
  550. /**
  551. * Update validating result of given element
  552. *
  553. * @param {jQuery} $field The field element
  554. * @param {String} status The status. Can be 'NOT_VALIDATED', 'VALIDATING', 'INVALID' or 'VALID'
  555. * @param {String} [validatorName] The validator name. If null, the method updates validity result for all validators
  556. * @return {BootstrapValidator}
  557. */
  558. updateElementStatus: function($field, status, validatorName) {
  559. var that = this,
  560. field = $field.attr('data-bv-field'),
  561. $parent = $field.parents('.form-group'),
  562. $message = $field.data('bv.messages'),
  563. $errors = $message.find('.help-block[data-bv-validator]'),
  564. $icon = $parent.find('.form-control-feedback[data-bv-icon-for="' + field + '"]');
  565. // Update status
  566. if (validatorName) {
  567. $field.data('bv.result.' + validatorName, status);
  568. } else {
  569. for (var v in this.options.fields[field].validators) {
  570. $field.data('bv.result.' + v, status);
  571. }
  572. }
  573. // Determine the tab containing the element
  574. var $tabPane = $field.parents('.tab-pane'),
  575. tabId,
  576. $tab;
  577. if ($tabPane && (tabId = $tabPane.attr('id'))) {
  578. $tab = $('a[href="#' + tabId + '"][data-toggle="tab"]').parent();
  579. }
  580. // Show/hide error elements and feedback icons
  581. switch (status) {
  582. case this.STATUS_VALIDATING:
  583. this.disableSubmitButtons(true);
  584. $parent.removeClass('has-success').removeClass('has-error');
  585. // TODO: Show validating message
  586. validatorName ? $errors.filter('.help-block[data-bv-validator="' + validatorName + '"]').hide() : $errors.hide();
  587. if ($icon) {
  588. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.invalid).addClass(this.options.feedbackIcons.validating).show();
  589. }
  590. if ($tab) {
  591. $tab.removeClass('bv-tab-success').removeClass('bv-tab-error');
  592. }
  593. break;
  594. case this.STATUS_INVALID:
  595. this.disableSubmitButtons(true);
  596. $parent.removeClass('has-success').addClass('has-error');
  597. validatorName ? $errors.filter('[data-bv-validator="' + validatorName + '"]').show() : $errors.show();
  598. if ($icon) {
  599. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.validating).addClass(this.options.feedbackIcons.invalid).show();
  600. }
  601. if ($tab) {
  602. $tab.removeClass('bv-tab-success').addClass('bv-tab-error');
  603. }
  604. break;
  605. case this.STATUS_VALID:
  606. validatorName ? $errors.filter('[data-bv-validator="' + validatorName + '"]').hide() : $errors.hide();
  607. // If the field is valid (passes all validators)
  608. var validField = ($errors.filter(function() {
  609. var display = $(this).css('display'), v = $(this).attr('data-bv-validator');
  610. return ('block' == display) || ($field.data('bv.result.' + v) != that.STATUS_VALID);
  611. }).length == 0);
  612. this.disableSubmitButtons(validField ? false : true);
  613. if ($icon) {
  614. $icon
  615. .removeClass(this.options.feedbackIcons.invalid).removeClass(this.options.feedbackIcons.validating).removeClass(this.options.feedbackIcons.valid)
  616. .addClass(validField ? this.options.feedbackIcons.valid : this.options.feedbackIcons.invalid)
  617. .show();
  618. }
  619. // Check if all elements in given container are valid
  620. var isValidContainer = function($container) {
  621. return $container
  622. .find('.help-block[data-bv-validator]')
  623. .filter(function() {
  624. var display = $(this).css('display'), v = $(this).attr('data-bv-validator');
  625. return ('block' == display) || ($field.data('bv.result.' + v) && $field.data('bv.result.' + v) != that.STATUS_VALID);
  626. })
  627. .length == 0;
  628. };
  629. $parent.removeClass('has-error has-success').addClass(isValidContainer($parent) ? 'has-success' : 'has-error');
  630. if ($tab) {
  631. $tab.removeClass('bv-tab-success').removeClass('bv-tab-error').addClass(isValidContainer($tabPane) ? 'bv-tab-success' : 'bv-tab-error');
  632. }
  633. break;
  634. case this.STATUS_NOT_VALIDATED:
  635. default:
  636. this.disableSubmitButtons(false);
  637. $parent.removeClass('has-success').removeClass('has-error');
  638. validatorName ? $errors.filter('.help-block[data-bv-validator="' + validatorName + '"]').hide() : $errors.hide();
  639. if ($icon) {
  640. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.invalid).removeClass(this.options.feedbackIcons.validating).hide();
  641. }
  642. if ($tab) {
  643. $tab.removeClass('bv-tab-success').removeClass('bv-tab-error');
  644. }
  645. break;
  646. }
  647. return this;
  648. },
  649. /**
  650. * Check the form validity
  651. *
  652. * @returns {Boolean}
  653. */
  654. isValid: function() {
  655. var fields, field, $field,
  656. type, status, validatorName,
  657. n, i;
  658. for (field in this.options.fields) {
  659. if (this.options.fields[field] == null || !this.options.fields[field]['enabled']) {
  660. continue;
  661. }
  662. fields = this.getFieldElements(field);
  663. type = fields.attr('type');
  664. n = (('radio' == type) || ('checkbox' == type)) ? 1 : fields.length;
  665. for (i = 0; i < n; i++) {
  666. $field = $(fields[i]);
  667. if (this._isExcluded($field)) {
  668. continue;
  669. }
  670. for (validatorName in this.options.fields[field].validators) {
  671. status = $field.data('bv.result.' + validatorName);
  672. if (status == this.STATUS_NOT_VALIDATED || status == this.STATUS_VALIDATING) {
  673. return false;
  674. }
  675. if (status == this.STATUS_INVALID) {
  676. this.$invalidField = $field;
  677. return false;
  678. }
  679. }
  680. }
  681. }
  682. return true;
  683. },
  684. /**
  685. * Submit the form using default submission.
  686. * It also does not perform any validations when submitting the form
  687. *
  688. * It might be used when you want to submit the form right inside the submitHandler()
  689. */
  690. defaultSubmit: function() {
  691. this.$form.off('submit.bv').submit();
  692. },
  693. // Useful APIs which aren't used internally
  694. /**
  695. * Reset the form
  696. *
  697. * @param {Boolean} resetFormData Reset current form data
  698. * @return {BootstrapValidator}
  699. */
  700. resetForm: function(resetFormData) {
  701. var field, fields, total, type, validator;
  702. for (field in this.options.fields) {
  703. fields = this.getFieldElements(field);
  704. total = fields.length;
  705. for (var i = 0; i < total; i++) {
  706. for (validator in this.options.fields[field].validators) {
  707. $(fields[i]).removeData('bv.dfs.' + validator);
  708. }
  709. }
  710. // Mark field as not validated yet
  711. this.updateStatus(field, this.STATUS_NOT_VALIDATED, null);
  712. if (resetFormData) {
  713. type = fields.attr('type');
  714. ('radio' == type || 'checkbox' == type) ? fields.removeAttr('checked').removeAttr('selected') : fields.val('');
  715. }
  716. }
  717. this.$invalidField = null;
  718. this.$submitButton = null;
  719. // Enable submit buttons
  720. this.disableSubmitButtons(false);
  721. return this;
  722. },
  723. /**
  724. * Enable/Disable all validators to given field
  725. *
  726. * @param {String} field The field name
  727. * @param {Boolean} enabled Enable/Disable field validators
  728. * @return {BootstrapValidator}
  729. */
  730. enableFieldValidators: function(field, enabled) {
  731. this.options.fields[field]['enabled'] = enabled;
  732. this.updateStatus(field, this.STATUS_NOT_VALIDATED, null);
  733. return this;
  734. }
  735. };
  736. // Plugin definition
  737. $.fn.bootstrapValidator = function(option, params) {
  738. return this.each(function() {
  739. var $this = $(this),
  740. data = $this.data('bootstrapValidator'),
  741. options = 'object' == typeof option && option;
  742. if (!data) {
  743. data = new BootstrapValidator(this, options);
  744. $this.data('bootstrapValidator', data);
  745. }
  746. // Allow to call plugin method
  747. if ('string' == typeof option) {
  748. data[option](params);
  749. }
  750. });
  751. };
  752. // Available validators
  753. $.fn.bootstrapValidator.validators = {};
  754. $.fn.bootstrapValidator.Constructor = BootstrapValidator;
  755. // Helper methods, which can be used in validator class
  756. $.fn.bootstrapValidator.helpers = {
  757. /**
  758. * Validate a date
  759. *
  760. * @param {Number} year The full year in 4 digits
  761. * @param {Number} month The month number
  762. * @param {Number} day The day number
  763. * @returns {Boolean}
  764. */
  765. date: function(year, month, day) {
  766. if (year < 1000 || year > 9999 || month == 0 || month > 12) {
  767. return false;
  768. }
  769. var numDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  770. // Update the number of days in Feb of leap year
  771. if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
  772. numDays[1] = 29;
  773. }
  774. // Check the day
  775. return (day > 0 && day <= numDays[month - 1]);
  776. },
  777. /**
  778. * Implement Luhn validation algorithm ((http://en.wikipedia.org/wiki/Luhn))
  779. * Credit to https://gist.github.com/ShirtlessKirk/2134376
  780. *
  781. * @param {String} value
  782. * @returns {Boolean}
  783. */
  784. luhn: function(value) {
  785. var length = value.length,
  786. mul = 0,
  787. prodArr = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]],
  788. sum = 0;
  789. while (length--) {
  790. sum += prodArr[mul][parseInt(value.charAt(length), 10)];
  791. mul ^= 1;
  792. }
  793. return (sum % 10 === 0 && sum > 0);
  794. },
  795. /**
  796. * Implement modulus 11, 10 (ISO 7064) algorithm
  797. *
  798. * @param {String} value
  799. * @returns {Boolean}
  800. */
  801. mod_11_10: function(value) {
  802. var check = 5,
  803. length = value.length;
  804. for (var i = 0; i < length; i++) {
  805. check = (((check || 10) * 2) % 11 + parseInt(value.charAt(i), 10)) % 10;
  806. }
  807. return (check == 1);
  808. },
  809. /**
  810. * Implements Mod 37, 36 (ISO 7064) algorithm
  811. * Usages:
  812. * mod_37_36('A12425GABC1234002M')
  813. * mod_37_36('002006673085', '0123456789')
  814. *
  815. * @param {String} value
  816. * @param {String} alphabet
  817. * @returns {Boolean}
  818. */
  819. mod_37_36: function(value, alphabet) {
  820. alphabet = alphabet || '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  821. var modulus = alphabet.length,
  822. length = value.length,
  823. check = Math.floor(modulus / 2);
  824. for (var i = 0; i < length; i++) {
  825. check = (((check || modulus) * 2) % (modulus + 1) + alphabet.indexOf(value.charAt(i))) % modulus;
  826. }
  827. return (check == 1);
  828. }
  829. };
  830. }(window.jQuery));
  831. ;(function($) {
  832. $.fn.bootstrapValidator.validators.base64 = {
  833. /**
  834. * Return true if the input value is a base 64 encoded string.
  835. *
  836. * @param {BootstrapValidator} validator The validator plugin instance
  837. * @param {jQuery} $field Field element
  838. * @param {Object} options Can consist of the following keys:
  839. * - message: The invalid message
  840. * @returns {Boolean}
  841. */
  842. validate: function(validator, $field, options) {
  843. var value = $field.val();
  844. if (value == '') {
  845. return true;
  846. }
  847. return /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$/.test(value);
  848. }
  849. };
  850. }(window.jQuery));
  851. ;(function($) {
  852. $.fn.bootstrapValidator.validators.between = {
  853. html5Attributes: {
  854. message: 'message',
  855. min: 'min',
  856. max: 'max',
  857. inclusive: 'inclusive'
  858. },
  859. enableByHtml5: function($field) {
  860. if ('range' == $field.attr('type')) {
  861. return {
  862. min: $field.attr('min'),
  863. max: $field.attr('max')
  864. };
  865. }
  866. return false;
  867. },
  868. /**
  869. * Return true if the input value is between (strictly or not) two given numbers
  870. *
  871. * @param {BootstrapValidator} validator The validator plugin instance
  872. * @param {jQuery} $field Field element
  873. * @param {Object} options Can consist of the following keys:
  874. * - min
  875. * - max
  876. * - inclusive [optional]: Can be true or false. Default is true
  877. * - message: The invalid message
  878. * @returns {Boolean}
  879. */
  880. validate: function(validator, $field, options) {
  881. var value = $field.val();
  882. if (value == '') {
  883. return true;
  884. }
  885. value = parseFloat(value);
  886. return (options.inclusive === true)
  887. ? (value > options.min && value < options.max)
  888. : (value >= options.min && value <= options.max);
  889. }
  890. };
  891. }(window.jQuery));
  892. ;(function($) {
  893. $.fn.bootstrapValidator.validators.callback = {
  894. /**
  895. * Return result from the callback method
  896. *
  897. * @param {BootstrapValidator} validator The validator plugin instance
  898. * @param {jQuery} $field Field element
  899. * @param {Object} options Can consist of the following keys:
  900. * - callback: The callback method that passes 2 parameters:
  901. * callback: function(fieldValue, validator) {
  902. * // fieldValue is the value of field
  903. * // validator is instance of BootstrapValidator
  904. * }
  905. * - message: The invalid message
  906. * @returns {Boolean|Deferred}
  907. */
  908. validate: function(validator, $field, options) {
  909. var value = $field.val();
  910. if (options.callback && 'function' == typeof options.callback) {
  911. var dfd = new $.Deferred();
  912. dfd.resolve($field, 'callback', options.callback.call(this, value, validator));
  913. return dfd;
  914. }
  915. return true;
  916. }
  917. };
  918. }(window.jQuery));
  919. ;(function($) {
  920. $.fn.bootstrapValidator.validators.choice = {
  921. html5Attributes: {
  922. message: 'message',
  923. min: 'min',
  924. max: 'max'
  925. },
  926. /**
  927. * Check if the number of checked boxes are less or more than a given number
  928. *
  929. * @param {BootstrapValidator} validator The validator plugin instance
  930. * @param {jQuery} $field Field element
  931. * @param {Object} options Consists of following keys:
  932. * - min
  933. * - max
  934. * At least one of two keys is required
  935. * - message: The invalid message
  936. * @returns {Boolean}
  937. */
  938. validate: function(validator, $field, options) {
  939. var numChoices = $field.is('select')
  940. ? validator.getFieldElements($field.attr('data-bv-field')).find('option').filter(':selected').length
  941. : validator.getFieldElements($field.attr('data-bv-field')).filter(':checked').length;
  942. if ((options.min && numChoices < options.min) || (options.max && numChoices > options.max)) {
  943. return false;
  944. }
  945. return true;
  946. }
  947. };
  948. }(window.jQuery));
  949. ;(function($) {
  950. $.fn.bootstrapValidator.validators.creditCard = {
  951. /**
  952. * Return true if the input value is valid credit card number
  953. * Based on https://gist.github.com/DiegoSalazar/4075533
  954. *
  955. * @param {BootstrapValidator} validator The validator plugin instance
  956. * @param {jQuery} $field Field element
  957. * @param {Object} options Can consist of the following key:
  958. * - message: The invalid message
  959. * @returns {Boolean}
  960. */
  961. validate: function(validator, $field, options) {
  962. var value = $field.val();
  963. if (value == '') {
  964. return true;
  965. }
  966. // Accept only digits, dashes or spaces
  967. if (/[^0-9-\s]+/.test(value)) {
  968. return false;
  969. }
  970. value = value.replace(/\D/g, '');
  971. if (!$.fn.bootstrapValidator.helpers.luhn(value)) {
  972. return false;
  973. }
  974. // Validate the card number based on prefix (IIN ranges) and length
  975. var cards = {
  976. AMERICAN_EXPRESS: {
  977. length: [15],
  978. prefix: ['34', '37']
  979. },
  980. DINERS_CLUB: {
  981. length: [14],
  982. prefix: ['300', '301', '302', '303', '304', '305', '36']
  983. },
  984. DINERS_CLUB_US: {
  985. length: [16],
  986. prefix: ['54', '55']
  987. },
  988. DISCOVER: {
  989. length: [16],
  990. prefix: ['6011', '622126', '622127', '622128', '622129', '62213',
  991. '62214', '62215', '62216', '62217', '62218', '62219',
  992. '6222', '6223', '6224', '6225', '6226', '6227', '6228',
  993. '62290', '62291', '622920', '622921', '622922', '622923',
  994. '622924', '622925', '644', '645', '646', '647', '648',
  995. '649', '65']
  996. },
  997. JCB: {
  998. length: [16],
  999. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  1000. },
  1001. LASER: {
  1002. length: [16, 17, 18, 19],
  1003. prefix: ['6304', '6706', '6771', '6709']
  1004. },
  1005. MAESTRO: {
  1006. length: [12, 13, 14, 15, 16, 17, 18, 19],
  1007. prefix: ['5018', '5020', '5038', '6304', '6759', '6761', '6762', '6763', '6764', '6765', '6766']
  1008. },
  1009. MASTERCARD: {
  1010. length: [16],
  1011. prefix: ['51', '52', '53', '54', '55']
  1012. },
  1013. SOLO: {
  1014. length: [16, 18, 19],
  1015. prefix: ['6334', '6767']
  1016. },
  1017. UNIONPAY: {
  1018. length: [16, 17, 18, 19],
  1019. prefix: ['622126', '622127', '622128', '622129', '62213', '62214',
  1020. '62215', '62216', '62217', '62218', '62219', '6222', '6223',
  1021. '6224', '6225', '6226', '6227', '6228', '62290', '62291',
  1022. '622920', '622921', '622922', '622923', '622924', '622925']
  1023. },
  1024. VISA: {
  1025. length: [16],
  1026. prefix: ['4']
  1027. }
  1028. };
  1029. var type, i;
  1030. for (type in cards) {
  1031. for (i in cards[type]['prefix']) {
  1032. if (value.substr(0, cards[type]['prefix'][i].length) == cards[type]['prefix'][i] // Check the prefix
  1033. && cards[type]['length'].indexOf(value.length) != -1) // and length
  1034. {
  1035. return true;
  1036. }
  1037. }
  1038. }
  1039. return false;
  1040. }
  1041. };
  1042. }(window.jQuery));
  1043. ;(function($) {
  1044. $.fn.bootstrapValidator.validators.cvv = {
  1045. html5Attributes: {
  1046. message: 'message',
  1047. ccfield: 'creditCardField'
  1048. },
  1049. /**
  1050. * Return true if the input value is a valid CVV number.
  1051. *
  1052. * @param {BootstrapValidator} validator The validator plugin instance
  1053. * @param {jQuery} $field Field element
  1054. * @param {Object} options Can consist of the following keys:
  1055. * - creditCardField: The credit card number field. It can be null
  1056. * - message: The invalid message
  1057. * @returns {Boolean}
  1058. */
  1059. validate: function(validator, $field, options) {
  1060. var value = $field.val();
  1061. if (value == '') {
  1062. return true;
  1063. }
  1064. if (!/^[0-9]{3,4}$/.test(value)) {
  1065. return false;
  1066. }
  1067. if (!options.creditCardField) {
  1068. return true;
  1069. }
  1070. // Get the credit card number
  1071. var creditCard = validator.getFieldElements(options.creditCardField).val();
  1072. if (creditCard == '') {
  1073. return true;
  1074. }
  1075. creditCard = creditCard.replace(/\D/g, '');
  1076. // Supported credit card types
  1077. var cards = {
  1078. AMERICAN_EXPRESS: {
  1079. length: [15],
  1080. prefix: ['34', '37']
  1081. },
  1082. DINERS_CLUB: {
  1083. length: [14],
  1084. prefix: ['300', '301', '302', '303', '304', '305', '36']
  1085. },
  1086. DINERS_CLUB_US: {
  1087. length: [16],
  1088. prefix: ['54', '55']
  1089. },
  1090. DISCOVER: {
  1091. length: [16],
  1092. prefix: ['6011', '622126', '622127', '622128', '622129', '62213',
  1093. '62214', '62215', '62216', '62217', '62218', '62219',
  1094. '6222', '6223', '6224', '6225', '6226', '6227', '6228',
  1095. '62290', '62291', '622920', '622921', '622922', '622923',
  1096. '622924', '622925', '644', '645', '646', '647', '648',
  1097. '649', '65']
  1098. },
  1099. JCB: {
  1100. length: [16],
  1101. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  1102. },
  1103. LASER: {
  1104. length: [16, 17, 18, 19],
  1105. prefix: ['6304', '6706', '6771', '6709']
  1106. },
  1107. MAESTRO: {
  1108. length: [12, 13, 14, 15, 16, 17, 18, 19],
  1109. prefix: ['5018', '5020', '5038', '6304', '6759', '6761', '6762', '6763', '6764', '6765', '6766']
  1110. },
  1111. MASTERCARD: {
  1112. length: [16],
  1113. prefix: ['51', '52', '53', '54', '55']
  1114. },
  1115. SOLO: {
  1116. length: [16, 18, 19],
  1117. prefix: ['6334', '6767']
  1118. },
  1119. UNIONPAY: {
  1120. length: [16, 17, 18, 19],
  1121. prefix: ['622126', '622127', '622128', '622129', '62213', '62214',
  1122. '62215', '62216', '62217', '62218', '62219', '6222', '6223',
  1123. '6224', '6225', '6226', '6227', '6228', '62290', '62291',
  1124. '622920', '622921', '622922', '622923', '622924', '622925']
  1125. },
  1126. VISA: {
  1127. length: [16],
  1128. prefix: ['4']
  1129. }
  1130. };
  1131. var type, i, creditCardType = null;
  1132. for (type in cards) {
  1133. for (i in cards[type]['prefix']) {
  1134. if (creditCard.substr(0, cards[type]['prefix'][i].length) == cards[type]['prefix'][i] // Check the prefix
  1135. && cards[type]['length'].indexOf(creditCard.length) != -1) // and length
  1136. {
  1137. creditCardType = type;
  1138. break;
  1139. }
  1140. }
  1141. }
  1142. return (creditCardType == null)
  1143. ? false
  1144. : (('AMERICAN_EXPRESS' == creditCardType) ? (value.length == 4) : (value.length == 3));
  1145. }
  1146. };
  1147. }(window.jQuery));
  1148. ;(function($) {
  1149. $.fn.bootstrapValidator.validators.date = {
  1150. html5Attributes: {
  1151. message: 'message',
  1152. format: 'format'
  1153. },
  1154. /**
  1155. * Return true if the input value is valid date
  1156. *
  1157. * @param {BootstrapValidator} validator The validator plugin instance
  1158. * @param {jQuery} $field Field element
  1159. * @param {Object} options Can consist of the following keys:
  1160. * - message: The invalid message
  1161. * - format: The date format. Default is MM/DD/YYYY
  1162. * The format can be:
  1163. *
  1164. * i) date: Consist of DD, MM, YYYY parts which are separated by /
  1165. * ii) date and time:
  1166. * The time can consist of h, m, s parts which are separated by :
  1167. * ii) date, time and A (indicating AM or PM)
  1168. * @returns {Boolean}
  1169. */
  1170. validate: function(validator, $field, options) {
  1171. var value = $field.val();
  1172. if (value == '') {
  1173. return true;
  1174. }
  1175. options.format = options.format || 'MM/DD/YYYY';
  1176. var formats = options.format.split(' '),
  1177. dateFormat = formats[0],
  1178. timeFormat = (formats.length > 1) ? formats[1] : null,
  1179. amOrPm = (formats.length > 2) ? formats[2] : null,
  1180. sections = value.split(' '),
  1181. date = sections[0],
  1182. time = (sections.length > 1) ? sections[1] : null;
  1183. if (formats.length != sections.length) {
  1184. return false;
  1185. }
  1186. // Determine the separator
  1187. var separator = (date.indexOf('/') != -1) ? '/' : ((date.indexOf('-') != -1) ? '-' : null);
  1188. if (separator == null) {
  1189. return false;
  1190. }
  1191. // Determine the date
  1192. date = date.split(separator);
  1193. dateFormat = dateFormat.split(separator);
  1194. var year = date[dateFormat.indexOf('YYYY')],
  1195. month = date[dateFormat.indexOf('MM')],
  1196. day = date[dateFormat.indexOf('DD')];
  1197. // Determine the time
  1198. var minutes = null, hours = null, seconds = null;
  1199. if (timeFormat) {
  1200. timeFormat = timeFormat.split(':'),
  1201. time = time.split(':');
  1202. if (timeFormat.length != time.length) {
  1203. return false;
  1204. }
  1205. hours = time.length > 0 ? time[0] : null;
  1206. minutes = time.length > 1 ? time[1] : null;
  1207. seconds = time.length > 2 ? time[2] : null;
  1208. // Validate seconds
  1209. if (seconds) {
  1210. seconds = parseInt(seconds, 10);
  1211. if (seconds < 0 || seconds > 60) {
  1212. return false;
  1213. }
  1214. }
  1215. // Validate hours
  1216. if (hours) {
  1217. hours = parseInt(hours, 10);
  1218. if (hours < 0 || hours >= 24 || (amOrPm && hours > 12)) {
  1219. return false;
  1220. }
  1221. }
  1222. // Validate minutes
  1223. if (minutes) {
  1224. minutes = parseInt(minutes, 10);
  1225. if (minutes < 0 || minutes > 59) {
  1226. return false;
  1227. }
  1228. }
  1229. }
  1230. // Validate day, month, and year
  1231. day = parseInt(day, 10);
  1232. month = parseInt(month, 10);
  1233. year = parseInt(year, 10);
  1234. return $.fn.bootstrapValidator.helpers.date(year, month, day);
  1235. }
  1236. };
  1237. }(window.jQuery));
  1238. ;(function($) {
  1239. $.fn.bootstrapValidator.validators.different = {
  1240. html5Attributes: {
  1241. message: 'message',
  1242. field: 'field'
  1243. },
  1244. /**
  1245. * Return true if the input value is different with given field's value
  1246. *
  1247. * @param {BootstrapValidator} validator The validator plugin instance
  1248. * @param {jQuery} $field Field element
  1249. * @param {Object} options Consists of the following key:
  1250. * - field: The name of field that will be used to compare with current one
  1251. * - message: The invalid message
  1252. * @returns {Boolean}
  1253. */
  1254. validate: function(validator, $field, options) {
  1255. var value = $field.val();
  1256. if (value == '') {
  1257. return true;
  1258. }
  1259. var compareWith = validator.getFieldElements(options.field);
  1260. if (compareWith == null) {
  1261. return true;
  1262. }
  1263. if (value != compareWith.val()) {
  1264. validator.updateStatus(options.field, validator.STATUS_VALID, 'different');
  1265. return true;
  1266. } else {
  1267. return false;
  1268. }
  1269. }
  1270. };
  1271. }(window.jQuery));
  1272. ;(function($) {
  1273. $.fn.bootstrapValidator.validators.digits = {
  1274. /**
  1275. * Return true if the input value contains digits only
  1276. *
  1277. * @param {BootstrapValidator} validator Validate plugin instance
  1278. * @param {jQuery} $field Field element
  1279. * @param {Object} options
  1280. * @returns {Boolean}
  1281. */
  1282. validate: function(validator, $field, options) {
  1283. var value = $field.val();
  1284. if (value == '') {
  1285. return true;
  1286. }
  1287. return /^\d+$/.test(value);
  1288. }
  1289. }
  1290. }(window.jQuery));
  1291. ;(function($) {
  1292. $.fn.bootstrapValidator.validators.ean = {
  1293. /**
  1294. * Validate EAN (International Article Number)
  1295. * Examples:
  1296. * - Valid: 73513537, 9780471117094, 4006381333931
  1297. * - Invalid: 73513536
  1298. *
  1299. * @see http://en.wikipedia.org/wiki/European_Article_Number
  1300. * @param {BootstrapValidator} validator The validator plugin instance
  1301. * @param {jQuery} $field Field element
  1302. * @param {Object} options Can consist of the following keys:
  1303. * - message: The invalid message
  1304. * @returns {Boolean}
  1305. */
  1306. validate: function(validator, $field, options) {
  1307. var value = $field.val();
  1308. if (value == '') {
  1309. return true;
  1310. }
  1311. if (!/^(\d{8}|\d{12}|\d{13})$/.test(value)) {
  1312. return false;
  1313. }
  1314. var length = value.length,
  1315. sum = 0,
  1316. weight = (length == 8) ? [3, 1] : [1, 3];
  1317. for (var i = 0; i < length - 1; i++) {
  1318. sum += parseInt(value.charAt(i)) * weight[i % 2];
  1319. }
  1320. sum = 10 - sum % 10;
  1321. return (sum == value.charAt(length - 1));
  1322. }
  1323. };
  1324. }(window.jQuery));
  1325. ;(function($) {
  1326. $.fn.bootstrapValidator.validators.emailAddress = {
  1327. enableByHtml5: function($field) {
  1328. return ('email' == $field.attr('type'));
  1329. },
  1330. /**
  1331. * Return true if and only if the input value is a valid email address
  1332. *
  1333. * @param {BootstrapValidator} validator Validate plugin instance
  1334. * @param {jQuery} $field Field element
  1335. * @param {Object} options
  1336. * @returns {Boolean}
  1337. */
  1338. validate: function(validator, $field, options) {
  1339. var value = $field.val();
  1340. if (value == '') {
  1341. return true;
  1342. }
  1343. // Email address regular expression
  1344. // http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
  1345. 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,}))$/;
  1346. return emailRegExp.test(value);
  1347. }
  1348. }
  1349. }(window.jQuery));
  1350. ;(function($) {
  1351. $.fn.bootstrapValidator.validators.file = {
  1352. html5Attributes: {
  1353. extension: 'extension',
  1354. maxsize: 'maxSize',
  1355. message: 'message',
  1356. type: 'type'
  1357. },
  1358. /**
  1359. * Validate upload file. Use HTML 5 API if the browser supports
  1360. *
  1361. * @param {BootstrapValidator} validator The validator plugin instance
  1362. * @param {jQuery} $field Field element
  1363. * @param {Object} options Can consist of the following keys:
  1364. * - extension: The allowed extensions, separated by a comma
  1365. * - maxSize: The maximum size in bytes
  1366. * - message: The invalid message
  1367. * - type: The allowed MIME type, separated by a comma
  1368. * @returns {Boolean}
  1369. */
  1370. validate: function(validator, $field, options) {
  1371. var value = $field.val();
  1372. if (value == '') {
  1373. return true;
  1374. }
  1375. var ext,
  1376. extensions = options.extension ? options.extension.split(',') : null,
  1377. types = options.type ? options.type.split(',') : null,
  1378. html5 = (window.File && window.FileList && window.FileReader);
  1379. if (html5) {
  1380. // Get FileList instance
  1381. var files = $field.get(0).files,
  1382. total = files.length;
  1383. for (var i = 0; i < total; i++) {
  1384. // Check file size
  1385. if (options.maxSize && files[i].size > parseInt(options.maxSize)) {
  1386. return false;
  1387. }
  1388. // Check file extension
  1389. ext = files[i].name.substr(files[i].name.lastIndexOf('.') + 1);
  1390. if (extensions && extensions.indexOf(ext) == -1) {
  1391. return false;
  1392. }
  1393. // Check file type
  1394. if (types && types.indexOf(files[i].type) == -1) {
  1395. return false;
  1396. }
  1397. }
  1398. } else {
  1399. // Check file extension
  1400. ext = value.substr(value.lastIndexOf('.') + 1);
  1401. if (extensions && extensions.indexOf(ext) == -1) {
  1402. return false;
  1403. }
  1404. }
  1405. return true;
  1406. }
  1407. };
  1408. }(window.jQuery));
  1409. ;(function($) {
  1410. $.fn.bootstrapValidator.validators.greaterThan = {
  1411. html5Attributes: {
  1412. message: 'message',
  1413. value: 'value',
  1414. inclusive: 'inclusive'
  1415. },
  1416. enableByHtml5: function($field) {
  1417. var min = $field.attr('min');
  1418. if (min) {
  1419. return {
  1420. value: min
  1421. };
  1422. }
  1423. return false;
  1424. },
  1425. /**
  1426. * Return true if the input value is greater than or equals to given number
  1427. *
  1428. * @param {BootstrapValidator} validator Validate plugin instance
  1429. * @param {jQuery} $field Field element
  1430. * @param {Object} options Can consist of the following keys:
  1431. * - value: The number used to compare to
  1432. * - inclusive [optional]: Can be true or false. Default is true
  1433. * - message: The invalid message
  1434. * @returns {Boolean}
  1435. */
  1436. validate: function(validator, $field, options) {
  1437. var value = $field.val();
  1438. if (value == '') {
  1439. return true;
  1440. }
  1441. value = parseFloat(value);
  1442. return (options.inclusive === true) ? (value > options.value) : (value >= options.value);
  1443. }
  1444. }
  1445. }(window.jQuery));
  1446. ;(function($) {
  1447. $.fn.bootstrapValidator.validators.grid = {
  1448. /**
  1449. * Validate GRId (Global Release Identifier)
  1450. * Examples:
  1451. * - Valid: A12425GABC1234002M, A1-2425G-ABC1234002-M, A1 2425G ABC1234002 M, Grid:A1-2425G-ABC1234002-M
  1452. * - Invalid: A1-2425G-ABC1234002-Q
  1453. *
  1454. * @see http://en.wikipedia.org/wiki/Global_Release_Identifier
  1455. * @param {BootstrapValidator} validator The validator plugin instance
  1456. * @param {jQuery} $field Field element
  1457. * @param {Object} options Can consist of the following keys:
  1458. * - message: The invalid message
  1459. * @returns {Boolean}
  1460. */
  1461. validate: function(validator, $field, options) {
  1462. var value = $field.val();
  1463. if (value == '') {
  1464. return true;
  1465. }
  1466. value = value.toUpperCase();
  1467. if (!/^[GRID:]*([0-9A-Z]{2})[-\s]*([0-9A-Z]{5})[-\s]*([0-9A-Z]{10})[-\s]*([0-9A-Z]{1})$/g.test(value)) {
  1468. return false;
  1469. }
  1470. value = value.replace(/\s/g, '').replace(/-/g, '');
  1471. if ('GRID:' == value.substr(0, 5)) {
  1472. value = value.substr(5);
  1473. }
  1474. return $.fn.bootstrapValidator.helpers.mod_37_36(value);
  1475. }
  1476. };
  1477. }(window.jQuery));
  1478. ;(function($) {
  1479. $.fn.bootstrapValidator.validators.hex = {
  1480. /**
  1481. * Return true if and only if the input value is a valid hexadecimal number
  1482. *
  1483. * @param {BootstrapValidator} validator The validator plugin instance
  1484. * @param {jQuery} $field Field element
  1485. * @param {Object} options Consist of key:
  1486. * - message: The invalid message
  1487. * @returns {Boolean}
  1488. */
  1489. validate: function(validator, $field, options) {
  1490. var value = $field.val();
  1491. if (value == '') {
  1492. return true;
  1493. }
  1494. return /^[0-9a-fA-F]+$/.test(value);
  1495. }
  1496. };
  1497. }(window.jQuery));
  1498. ;(function($) {
  1499. $.fn.bootstrapValidator.validators.hexColor = {
  1500. enableByHtml5: function($field) {
  1501. return ('color' == $field.attr('type'));
  1502. },
  1503. /**
  1504. * Return true if the input value is a valid hex color
  1505. *
  1506. * @param {BootstrapValidator} validator The validator plugin instance
  1507. * @param {jQuery} $field Field element
  1508. * @param {Object} options Can consist of the following keys:
  1509. * - message: The invalid message
  1510. * @returns {Boolean}
  1511. */
  1512. validate: function(validator, $field, options) {
  1513. var value = $field.val();
  1514. if (value == '') {
  1515. return true;
  1516. }
  1517. return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(value);
  1518. }
  1519. };
  1520. }(window.jQuery));
  1521. ;(function($) {
  1522. $.fn.bootstrapValidator.validators.iban = {
  1523. html5Attributes: {
  1524. message: 'message',
  1525. country: 'country'
  1526. },
  1527. /**
  1528. * Validate an International Bank Account Number (IBAN)
  1529. * To test it, take the sample IBAN from
  1530. * http://www.nordea.com/Our+services/International+products+and+services/Cash+Management/IBAN+countries/908462.html
  1531. *
  1532. * @param {BootstrapValidator} validator The validator plugin instance
  1533. * @param {jQuery} $field Field element
  1534. * @param {Object} options Can consist of the following keys:
  1535. * - message: The invalid message
  1536. * - country: The ISO 3166-1 country code
  1537. * @returns {Boolean}
  1538. */
  1539. validate: function(validator, $field, options) {
  1540. var value = $field.val();
  1541. if (value == '') {
  1542. return true;
  1543. }
  1544. // See
  1545. // http://www.swift.com/dsp/resources/documents/IBAN_Registry.pdf
  1546. // http://en.wikipedia.org/wiki/International_Bank_Account_Number#IBAN_formats_by_country
  1547. var ibanRegex = {
  1548. 'AD': 'AD[0-9]{2}[0-9]{4}[0-9]{4}[A-Z0-9]{12}', // Andorra
  1549. 'AE': 'AE[0-9]{2}[0-9]{3}[0-9]{16}', // United Arab Emirates
  1550. 'AL': 'AL[0-9]{2}[0-9]{8}[A-Z0-9]{16}', // Albania
  1551. 'AO': 'AO[0-9]{2}[0-9]{21}', // Angola
  1552. 'AT': 'AT[0-9]{2}[0-9]{5}[0-9]{11}', // Austria
  1553. 'AZ': 'AZ[0-9]{2}[A-Z]{4}[A-Z0-9]{20}', // Azerbaijan
  1554. 'BA': 'BA[0-9]{2}[0-9]{3}[0-9]{3}[0-9]{8}[0-9]{2}', // Bosnia and Herzegovina
  1555. 'BE': 'BE[0-9]{2}[0-9]{3}[0-9]{7}[0-9]{2}', // Belgium
  1556. 'BF': 'BF[0-9]{2}[0-9]{23}', // Burkina Faso
  1557. 'BG': 'BG[0-9]{2}[A-Z]{4}[0-9]{4}[0-9]{2}[A-Z0-9]{8}', // Bulgaria
  1558. 'BH': 'BH[0-9]{2}[A-Z]{4}[A-Z0-9]{14}', // Bahrain
  1559. 'BI': 'BI[0-9]{2}[0-9]{12}', // Burundi
  1560. 'BJ': 'BJ[0-9]{2}[A-Z]{1}[0-9]{23}', // Benin
  1561. 'BR': 'BR[0-9]{2}[0-9]{8}[0-9]{5}[0-9]{10}[A-Z][A-Z0-9]', // Brazil
  1562. 'CH': 'CH[0-9]{2}[0-9]{5}[A-Z0-9]{12}', // Switzerland
  1563. 'CI': 'CI[0-9]{2}[A-Z]{1}[0-9]{23}', // Ivory Coast
  1564. 'CM': 'CM[0-9]{2}[0-9]{23}', // Cameroon
  1565. 'CR': 'CR[0-9]{2}[0-9]{3}[0-9]{14}', // Costa Rica
  1566. 'CV': 'CV[0-9]{2}[0-9]{21}', // Cape Verde
  1567. 'CY': 'CY[0-9]{2}[0-9]{3}[0-9]{5}[A-Z0-9]{16}', // Cyprus
  1568. 'CZ': 'CZ[0-9]{2}[0-9]{20}', // Czech Republic
  1569. 'DE': 'DE[0-9]{2}[0-9]{8}[0-9]{10}', // Germany
  1570. 'DK': 'DK[0-9]{2}[0-9]{14}', // Denmark
  1571. 'DO': 'DO[0-9]{2}[A-Z0-9]{4}[0-9]{20}', // Dominican Republic
  1572. 'DZ': 'DZ[0-9]{2}[0-9]{20}', // Algeria
  1573. 'EE': 'EE[0-9]{2}[0-9]{2}[0-9]{2}[0-9]{11}[0-9]{1}', // Estonia
  1574. 'ES': 'ES[0-9]{2}[0-9]{4}[0-9]{4}[0-9]{1}[0-9]{1}[0-9]{10}', // Spain
  1575. 'FI': 'FI[0-9]{2}[0-9]{6}[0-9]{7}[0-9]{1}', // Finland
  1576. 'FO': 'FO[0-9]{2}[0-9]{4}[0-9]{9}[0-9]{1}', // Faroe Islands
  1577. 'FR': 'FR[0-9]{2}[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}', // France
  1578. 'GB': 'GB[0-9]{2}[A-Z]{4}[0-9]{6}[0-9]{8}', // United Kingdom
  1579. 'GE': 'GE[0-9]{2}[A-Z]{2}[0-9]{16}', // Georgia
  1580. 'GI': 'GI[0-9]{2}[A-Z]{4}[A-Z0-9]{15}', // Gibraltar
  1581. 'GL': 'GL[0-9]{2}[0-9]{4}[0-9]{9}[0-9]{1}', // Greenland[
  1582. 'GR': 'GR[0-9]{2}[0-9]{3}[0-9]{4}[A-Z0-9]{16}', // Greece
  1583. 'GT': 'GT[0-9]{2}[A-Z0-9]{4}[A-Z0-9]{20}', // Guatemala
  1584. 'HR': 'HR[0-9]{2}[0-9]{7}[0-9]{10}', // Croatia
  1585. 'HU': 'HU[0-9]{2}[0-9]{3}[0-9]{4}[0-9]{1}[0-9]{15}[0-9]{1}', // Hungary
  1586. 'IE': 'IE[0-9]{2}[A-Z]{4}[0-9]{6}[0-9]{8}', // Ireland
  1587. 'IL': 'IL[0-9]{2}[0-9]{3}[0-9]{3}[0-9]{13}', // Israel
  1588. 'IR': 'IR[0-9]{2}[0-9]{22}', // Iran
  1589. 'IS': 'IS[0-9]{2}[0-9]{4}[0-9]{2}[0-9]{6}[0-9]{10}', // Iceland
  1590. 'IT': 'IT[0-9]{2}[A-Z]{1}[0-9]{5}[0-9]{5}[A-Z0-9]{12}', // Italy
  1591. 'JO': 'JO[0-9]{2}[A-Z]{4}[0-9]{4}[0]{8}[A-Z0-9]{10}', // Jordan
  1592. 'KW': 'KW[0-9]{2}[A-Z]{4}[0-9]{22}', // Kuwait
  1593. 'KZ': 'KZ[0-9]{2}[0-9]{3}[A-Z0-9]{13}', // Kazakhstan
  1594. 'LB': 'LB[0-9]{2}[0-9]{4}[A-Z0-9]{20}', // Lebanon
  1595. 'LI': 'LI[0-9]{2}[0-9]{5}[A-Z0-9]{12}', // Liechtenstein
  1596. 'LT': 'LT[0-9]{2}[0-9]{5}[0-9]{11}', // Lithuania
  1597. 'LU': 'LU[0-9]{2}[0-9]{3}[A-Z0-9]{13}', // Luxembourg
  1598. 'LV': 'LV[0-9]{2}[A-Z]{4}[A-Z0-9]{13}', // Latvia
  1599. 'MC': 'MC[0-9]{2}[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}', // Monaco
  1600. 'MD': 'MD[0-9]{2}[A-Z0-9]{20}', // Moldova
  1601. 'ME': 'ME[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}', // Montenegro
  1602. 'MG': 'MG[0-9]{2}[0-9]{23}', // Madagascar
  1603. 'MK': 'MK[0-9]{2}[0-9]{3}[A-Z0-9]{10}[0-9]{2}', // Macedonia
  1604. 'ML': 'ML[0-9]{2}[A-Z]{1}[0-9]{23}', // Mali
  1605. 'MR': 'MR13[0-9]{5}[0-9]{5}[0-9]{11}[0-9]{2}', // Mauritania
  1606. 'MT': 'MT[0-9]{2}[A-Z]{4}[0-9]{5}[A-Z0-9]{18}', // Malta
  1607. 'MU': 'MU[0-9]{2}[A-Z]{4}[0-9]{2}[0-9]{2}[0-9]{12}[0-9]{3}[A-Z]{3}',// Mauritius
  1608. 'MZ': 'MZ[0-9]{2}[0-9]{21}', // Mozambique
  1609. 'NL': 'NL[0-9]{2}[A-Z]{4}[0-9]{10}', // Netherlands
  1610. 'NO': 'NO[0-9]{2}[0-9]{4}[0-9]{6}[0-9]{1}', // Norway
  1611. 'PK': 'PK[0-9]{2}[A-Z]{4}[A-Z0-9]{16}', // Pakistan
  1612. 'PL': 'PL[0-9]{2}[0-9]{8}[0-9]{16}', // Poland
  1613. 'PS': 'PS[0-9]{2}[A-Z]{4}[A-Z0-9]{21}', // Palestinian
  1614. 'PT': 'PT[0-9]{2}[0-9]{4}[0-9]{4}[0-9]{11}[0-9]{2}', // Portugal
  1615. 'QA': 'QA[0-9]{2}[A-Z]{4}[A-Z0-9]{21}', // Qatar
  1616. 'RO': 'RO[0-9]{2}[A-Z]{4}[A-Z0-9]{16}', // Romania
  1617. 'RS': 'RS[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}', // Serbia
  1618. 'SA': 'SA[0-9]{2}[0-9]{2}[A-Z0-9]{18}', // Saudi Arabia
  1619. 'SE': 'SE[0-9]{2}[0-9]{3}[0-9]{16}[0-9]{1}', // Sweden
  1620. 'SI': 'SI[0-9]{2}[0-9]{5}[0-9]{8}[0-9]{2}', // Slovenia
  1621. 'SK': 'SK[0-9]{2}[0-9]{4}[0-9]{6}[0-9]{10}', // Slovakia
  1622. 'SM': 'SM[0-9]{2}[A-Z]{1}[0-9]{5}[0-9]{5}[A-Z0-9]{12}', // San Marino
  1623. 'SN': 'SN[0-9]{2}[A-Z]{1}[0-9]{23}', // Senegal
  1624. 'TN': 'TN59[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}', // Tunisia
  1625. 'TR': 'TR[0-9]{2}[0-9]{5}[A-Z0-9]{1}[A-Z0-9]{16}', // Turkey
  1626. 'VG': 'VG[0-9]{2}[A-Z]{4}[0-9]{16}' // Virgin Islands, British
  1627. };
  1628. value = value.replace(/[^a-zA-Z0-9]/g, '').toUpperCase();
  1629. var country = options.country || value.substr(0, 2);
  1630. if (!ibanRegex[country]) {
  1631. return false;
  1632. }
  1633. if (!(new RegExp('^' + ibanRegex[country] + '$')).test(value)) {
  1634. return false;
  1635. }
  1636. value = value.substr(4) + value.substr(0, 4);
  1637. value = value.split('').map(function(n) {
  1638. var code = n.charCodeAt(0);
  1639. return (code >= 'A'.charCodeAt(0) && code <= 'Z'.charCodeAt(0))
  1640. // Replace A, B, C, ..., Z with 10, 11, ..., 35
  1641. ? (code - 'A'.charCodeAt(0) + 10)
  1642. : n;
  1643. }).join('');
  1644. var temp = parseInt(value.substr(0, 1), 10),
  1645. length = value.length;
  1646. for (var i = 1; i < length; ++i) {
  1647. temp = (temp * 10 + parseInt(value.substr(i, 1), 10)) % 97;
  1648. }
  1649. return (temp == 1);
  1650. }
  1651. };
  1652. }(window.jQuery));
  1653. ;(function($) {
  1654. $.fn.bootstrapValidator.validators.id = {
  1655. html5Attributes: {
  1656. message: 'message',
  1657. country: 'country'
  1658. },
  1659. /**
  1660. * Validate identification number in different countries
  1661. *
  1662. * @see http://en.wikipedia.org/wiki/National_identification_number
  1663. * @param {BootstrapValidator} validator The validator plugin instance
  1664. * @param {jQuery} $field Field element
  1665. * @param {Object} options Consist of key:
  1666. * - message: The invalid message
  1667. * - country: The ISO 3166-1 country code
  1668. * @returns {Boolean}
  1669. */
  1670. validate: function(validator, $field, options) {
  1671. var value = $field.val();
  1672. if (value == '') {
  1673. return true;
  1674. }
  1675. var country = options.country || value.substr(0, 2),
  1676. method = ['_', country.toLowerCase()].join('');
  1677. if (this[method] && 'function' == typeof this[method]) {
  1678. return this[method](value);
  1679. }
  1680. return true;
  1681. },
  1682. /**
  1683. * Validate Unique Master Citizen Number which uses in
  1684. * - Bosnia and Herzegovina (country code: BA)
  1685. * - Macedonia (MK)
  1686. * - Montenegro (ME)
  1687. * - Serbia (RS)
  1688. * - Slovenia (SI)
  1689. *
  1690. * @see http://en.wikipedia.org/wiki/Unique_Master_Citizen_Number
  1691. * @param {String} value The ID
  1692. * @param {String} countryCode The ISO country code, can be BA, MK, ME, RS, SI
  1693. * @returns {Boolean}
  1694. */
  1695. _validateJMBG: function(value, countryCode) {
  1696. if (!/^\d{13}$/.test(value)) {
  1697. return false;
  1698. }
  1699. var day = parseInt(value.substr(0, 2), 10),
  1700. month = parseInt(value.substr(2, 2), 10),
  1701. year = parseInt(value.substr(4, 3), 10),
  1702. rr = parseInt(value.substr(7, 2), 10),
  1703. k = parseInt(value.substr(12, 1), 10);
  1704. // Validate date of birth
  1705. // FIXME: Validate the year of birth
  1706. if (day > 31 || month > 12) {
  1707. return false;
  1708. }
  1709. // Validate checksum
  1710. var sum = 0;
  1711. for (var i = 0; i < 6; i++) {
  1712. sum += (7 - i) * (parseInt(value.charAt(i)) + parseInt(value.charAt(i + 6)));
  1713. }
  1714. sum = 11 - sum % 11;
  1715. if (sum == 10 || sum == 11) {
  1716. sum = 0;
  1717. }
  1718. if (sum != k) {
  1719. return false;
  1720. }
  1721. // Validate political region
  1722. // rr is the political region of birth, which can be in ranges:
  1723. // 10-19: Bosnia and Herzegovina
  1724. // 20-29: Montenegro
  1725. // 30-39: Croatia (not used anymore)
  1726. // 41-49: Macedonia
  1727. // 50-59: Slovenia (only 50 is used)
  1728. // 70-79: Central Serbia
  1729. // 80-89: Serbian province of Vojvodina
  1730. // 90-99: Kosovo
  1731. switch (countryCode.toUpperCase()) {
  1732. case 'BA':
  1733. return (10 <= rr && rr <= 19);
  1734. case 'MK':
  1735. return (41 <= rr && rr <= 49);
  1736. case 'ME':
  1737. return (20 <= rr && rr <= 29);
  1738. case 'RS':
  1739. return (70 <= rr && rr <= 99);
  1740. case 'SI':
  1741. return (50 <= rr && rr <= 59);
  1742. default:
  1743. return true;
  1744. }
  1745. return true;
  1746. },
  1747. _ba: function(value) {
  1748. return this._validateJMBG(value, 'BA');
  1749. },
  1750. _mk: function(value) {
  1751. return this._validateJMBG(value, 'MK');
  1752. },
  1753. _me: function(value) {
  1754. return this._validateJMBG(value, 'ME');
  1755. },
  1756. _rs: function(value) {
  1757. return this._validateJMBG(value, 'RS');
  1758. },
  1759. /**
  1760. * Examples: 0101006500006
  1761. */
  1762. _si: function(value) {
  1763. return this._validateJMBG(value, 'SI');
  1764. },
  1765. /**
  1766. * Validate Bulgarian national identification number (EGN)
  1767. * Examples:
  1768. * - Valid: 7523169263, 8032056031, 803205 603 1, 8001010008, 7501020018, 7552010005, 7542011030
  1769. * - Invalid: 8019010008
  1770. *
  1771. * @see http://en.wikipedia.org/wiki/Uniform_civil_number
  1772. * @param {String} value The ID
  1773. * @returns {Boolean}
  1774. */
  1775. _bg: function(value) {
  1776. if (!/^\d{10}$/.test(value) && !/^\d{6}\s\d{3}\s\d{1}$/.test(value)) {
  1777. return false;
  1778. }
  1779. value = value.replace(/\s/g, '');
  1780. // Check the birth date
  1781. var year = parseInt(value.substr(0, 2), 10) + 1900,
  1782. month = parseInt(value.substr(2, 2), 10),
  1783. day = parseInt(value.substr(4, 2), 10);
  1784. if (month > 40) {
  1785. year += 100;
  1786. month -= 40;
  1787. } else if (month > 20) {
  1788. year -= 100;
  1789. month -= 20;
  1790. }
  1791. if (!$.fn.bootstrapValidator.helpers.date(year, month, day)) {
  1792. return false;
  1793. }
  1794. var sum = 0,
  1795. weight = [2, 4, 8, 5, 10, 9, 7, 3, 6];
  1796. for (var i = 0; i < 9; i++) {
  1797. sum += parseInt(value.charAt(i)) * weight[i];
  1798. }
  1799. sum = (sum % 11) % 10;
  1800. return (sum == value.substr(9, 1));
  1801. },
  1802. /**
  1803. * Validate Brazilian national identification number (CPF)
  1804. * Examples:
  1805. * - Valid: 39053344705, 390.533.447-05, 111.444.777-35
  1806. * - Invalid: 231.002.999-00
  1807. *
  1808. * @see http://en.wikipedia.org/wiki/Cadastro_de_Pessoas_F%C3%ADsicas
  1809. * @param {String} value The ID
  1810. * @returns {Boolean}
  1811. */
  1812. _br: function(value) {
  1813. if (/^1{11}|2{11}|3{11}|4{11}|5{11}|6{11}|7{11}|8{11}|9{11}|0{11}$/.test(value)) {
  1814. return false;
  1815. }
  1816. if (!/^\d{11}$/.test(value) && !/^\d{3}\.\d{3}\.\d{3}-\d{2}$/.test(value)) {
  1817. return false;
  1818. }
  1819. value = value.replace(/\./g, '').replace(/-/g, '');
  1820. var d1 = 0;
  1821. for (var i = 0; i < 9; i++) {
  1822. d1 += (10 - i) * parseInt(value.charAt(i));
  1823. }
  1824. d1 = 11 - d1 % 11;
  1825. if (d1 == 10 || d1 == 11) {
  1826. d1 = 0;
  1827. }
  1828. if (d1 != value.charAt(9)) {
  1829. return false;
  1830. }
  1831. var d2 = 0;
  1832. for (i = 0; i < 10; i++) {
  1833. d2 += (11 - i) * parseInt(value.charAt(i));
  1834. }
  1835. d2 = 11 - d2 % 11;
  1836. if (d2 == 10 || d2 == 11) {
  1837. d2 = 0;
  1838. }
  1839. return (d2 == value.charAt(10));
  1840. },
  1841. /**
  1842. * Validate Czech national identification number (RC)
  1843. * Examples:
  1844. * - Valid: 7103192745, 991231123
  1845. * - Invalid: 1103492745, 590312123
  1846. *
  1847. * @param {String} value The ID
  1848. * @returns {Boolean}
  1849. */
  1850. _cz: function(value) {
  1851. if (!/^\d{9,10}$/.test(value)) {
  1852. return false;
  1853. }
  1854. var year = 1900 + parseInt(value.substr(0, 2)),
  1855. month = parseInt(value.substr(2, 2)) % 50 % 20,
  1856. day = parseInt(value.substr(4, 2));
  1857. if (value.length == 9) {
  1858. if (year >= 1980) {
  1859. year -= 100;
  1860. }
  1861. if (year > 1953) {
  1862. return false;
  1863. }
  1864. } else if (year < 1954) {
  1865. year += 100;
  1866. }
  1867. if (!$.fn.bootstrapValidator.helpers.date(year, month, day)) {
  1868. return false;
  1869. }
  1870. // Check that the birth date is not in the future
  1871. if (value.length == 10) {
  1872. var check = parseInt(value.substr(0, 9), 10) % 11;
  1873. if (year < 1985) {
  1874. check = check % 10;
  1875. }
  1876. return (check == value.substr(9, 1));
  1877. }
  1878. return true;
  1879. },
  1880. /**
  1881. * Validate Danish Personal Identification number (CPR)
  1882. * Examples:
  1883. * - Valid: 2110625629, 211062-5629
  1884. * - Invalid: 511062-5629
  1885. *
  1886. * @see https://en.wikipedia.org/wiki/Personal_identification_number_(Denmark)
  1887. * @param {String} value The ID
  1888. * @returns {Boolean}
  1889. */
  1890. _dk: function(value) {
  1891. if (!/^[0-9]{6}[-]{0,1}[0-9]{4}$/.test(value)) {
  1892. return false;
  1893. }
  1894. value = value.replace(/-/g, '');
  1895. var day = parseInt(value.substr(0, 2), 10),
  1896. month = parseInt(value.substr(2, 2), 10),
  1897. year = parseInt(value.substr(4, 2), 10);
  1898. switch (true) {
  1899. case ('5678'.indexOf(value.charAt(6)) != -1 && year >= 58):
  1900. year += 1800;
  1901. break;
  1902. case ('0123'.indexOf(value.charAt(6)) != -1):
  1903. case ('49'.indexOf(value.charAt(6)) != -1 && year >= 37):
  1904. year += 1900;
  1905. break;
  1906. default:
  1907. year += 2000;
  1908. break;
  1909. }
  1910. return $.fn.bootstrapValidator.helpers.date(year, month, day);
  1911. },
  1912. /**
  1913. * Validate Spanish personal identity code (DNI)
  1914. * Support i) DNI (for Spanish citizens) and ii) NIE (for foreign people)
  1915. *
  1916. * Examples:
  1917. * - Valid: i) 54362315K, 54362315-K; ii) X2482300W, X-2482300W, X-2482300-W
  1918. * - Invalid: i) 54362315Z; ii) X-2482300A
  1919. *
  1920. * @see https://en.wikipedia.org/wiki/National_identification_number#Spain
  1921. * @param {String} value The ID
  1922. * @returns {Boolean}
  1923. */
  1924. _es: function(value) {
  1925. if (!/^[0-9A-Z]{8}[-]{0,1}[0-9A-Z]$/.test(value) // DNI
  1926. && !/^[XYZ][-]{0,1}[0-9]{7}[-]{0,1}[0-9A-Z]$/.test(value)) { // NIE
  1927. return false;
  1928. }
  1929. value = value.replace(/-/g, '');
  1930. var index = 'XYZ'.indexOf(value.charAt(0));
  1931. if (index != -1) {
  1932. // It is NIE number
  1933. value = index + value.substr(1) + '';
  1934. }
  1935. var check = parseInt(value.substr(0, 8), 10);
  1936. check = 'TRWAGMYFPDXBNJZSQVHLCKE'[check % 23];
  1937. return (check == value.substr(8, 1));
  1938. },
  1939. /**
  1940. * Validate Finnish Personal Identity Code (HETU)
  1941. * Examples:
  1942. * - Valid: 311280-888Y, 131052-308T
  1943. * - Invalid: 131052-308U, 310252-308Y
  1944. *
  1945. * @param {String} value The ID
  1946. * @returns {Boolean}
  1947. */
  1948. _fi: function(value) {
  1949. if (!/^[0-9]{6}[-+A][0-9]{3}[0-9ABCDEFHJKLMNPRSTUVWXY]$/.test(value)) {
  1950. return false;
  1951. }
  1952. var day = parseInt(value.substr(0, 2), 10),
  1953. month = parseInt(value.substr(2, 2), 10),
  1954. year = parseInt(value.substr(4, 2), 10),
  1955. centuries = {
  1956. '+': 1800,
  1957. '-': 1900,
  1958. 'A': 2000
  1959. };
  1960. year = centuries[value.charAt(6)] + year;
  1961. if (!$.fn.bootstrapValidator.helpers.date(year, month, day)) {
  1962. return false;
  1963. }
  1964. var individual = parseInt(value.substr(7, 3));
  1965. if (individual < 2) {
  1966. return false;
  1967. }
  1968. var n = value.substr(0, 6) + value.substr(7, 3) + '';
  1969. n = parseInt(n);
  1970. return '0123456789ABCDEFHJKLMNPRSTUVWXY'.charAt(n % 31) == value.charAt(10);
  1971. },
  1972. /**
  1973. * Validate Croatian personal identification number (OIB)
  1974. * Examples:
  1975. * - Valid: 33392005961
  1976. * - Invalid: 33392005962
  1977. *
  1978. * @param {String} value The ID
  1979. * @returns {Boolean}
  1980. */
  1981. _hr: function(value) {
  1982. if (!/^[0-9]{11}$/.test(value)) {
  1983. return false;
  1984. }
  1985. return $.fn.bootstrapValidator.helpers.mod_11_10(value);
  1986. },
  1987. /**
  1988. * Validate Irish Personal Public Service Number (PPS)
  1989. * Examples:
  1990. * - Valid: 6433435F, 6433435FT, 6433435FW, 6433435OA, 6433435IH, 1234567TW, 1234567FA
  1991. * - Invalid: 6433435E, 6433435VH
  1992. *
  1993. * @see https://en.wikipedia.org/wiki/Personal_Public_Service_Number
  1994. * @param {String} value The ID
  1995. * @returns {Boolean}
  1996. */
  1997. _ie: function(value) {
  1998. if (!/^\d{7}[A-W][AHWTX]?$/.test(value)) {
  1999. return false;
  2000. }
  2001. var getCheckDigit = function(value) {
  2002. while (value.length < 7) {
  2003. value = '0' + value;
  2004. }
  2005. var alphabet = 'WABCDEFGHIJKLMNOPQRSTUV',
  2006. sum = 0;
  2007. for (var i = 0; i < 7; i++) {
  2008. sum += parseInt(value.charAt(i)) * (8 - i);
  2009. }
  2010. sum += 9 * alphabet.indexOf(value.substr(7));
  2011. return alphabet[sum % 23];
  2012. };
  2013. // 2013 format
  2014. if (value.length == 9 && ('A' == value.charAt(8) || 'H' == value.charAt(8))) {
  2015. return value.charAt(7) == getCheckDigit(value.substr(0, 7) + value.substr(8) + '');
  2016. }
  2017. // The old format
  2018. else {
  2019. return value.charAt(7) == getCheckDigit(value.substr(0, 7));
  2020. }
  2021. },
  2022. /**
  2023. * Validate Dutch national identification number (BSN)
  2024. * Examples:
  2025. * - Valid: 111222333, 941331490, 9413.31.490
  2026. * - Invalid: 111252333
  2027. *
  2028. * @see https://nl.wikipedia.org/wiki/Burgerservicenummer
  2029. * @param {String} value The ID
  2030. * @returns {Boolean}
  2031. */
  2032. _nl: function(value) {
  2033. while (value.length < 9) {
  2034. value = '0' + value;
  2035. }
  2036. if (!/^[0-9]{4}[.]{0,1}[0-9]{2}[.]{0,1}[0-9]{3}$/.test(value)) {
  2037. return false;
  2038. }
  2039. value = value.replace(/\./g, '');
  2040. if (parseInt(value, 10) == 0) {
  2041. return false;
  2042. }
  2043. var sum = 0,
  2044. length = value.length;
  2045. for (var i = 0; i < length - 1; i++) {
  2046. sum += (9 - i) * parseInt(value.charAt(i));
  2047. }
  2048. sum = sum % 11;
  2049. if (sum == 10) {
  2050. sum = 0;
  2051. }
  2052. return (sum == value.charAt(length - 1));
  2053. },
  2054. /**
  2055. * Validate Romanian numerical personal code (CNP)
  2056. * Examples:
  2057. * - Valid: 1630615123457, 1800101221144
  2058. * - Invalid: 8800101221144, 1632215123457, 1630615123458
  2059. *
  2060. * @see http://en.wikipedia.org/wiki/National_identification_number#Romania
  2061. * @param {String} value The ID
  2062. * @returns {Boolean}
  2063. */
  2064. _ro: function(value) {
  2065. if (!/^[0-9]{13}$/.test(value)) {
  2066. return false;
  2067. }
  2068. var gender = parseInt(value.charAt(0));
  2069. if (gender == 0 || gender == 7 || gender == 8) {
  2070. return false;
  2071. }
  2072. // Determine the date of birth
  2073. var year = parseInt(value.substr(1, 2), 10),
  2074. month = parseInt(value.substr(3, 2), 10),
  2075. day = parseInt(value.substr(5, 2), 10),
  2076. // The year of date is determined base on the gender
  2077. centuries = {
  2078. '1': 1900, // Male born between 1900 and 1999
  2079. '2': 1900, // Female born between 1900 and 1999
  2080. '3': 1800, // Male born between 1800 and 1899
  2081. '4': 1800, // Female born between 1800 and 1899
  2082. '5': 2000, // Male born after 2000
  2083. '6': 2000 // Female born after 2000
  2084. };
  2085. if (day > 31 && month > 12) {
  2086. return false;
  2087. }
  2088. if (gender != 9) {
  2089. year = centuries[gender + ''] + year;
  2090. if (!$.fn.bootstrapValidator.helpers.date(year, month, day)) {
  2091. return false;
  2092. }
  2093. }
  2094. // Validate the check digit
  2095. var sum = 0,
  2096. weight = [2, 7, 9, 1, 4, 6, 3, 5, 8, 2, 7, 9],
  2097. length = value.length;
  2098. for (var i = 0; i < length - 1; i++) {
  2099. sum += parseInt(value.charAt(i)) * weight[i];
  2100. }
  2101. sum = sum % 11;
  2102. if (sum == 10) {
  2103. sum = 1;
  2104. }
  2105. return (sum == value.charAt(length - 1));
  2106. },
  2107. /**
  2108. * Validate Swedish personal identity number (personnummer)
  2109. * Examples:
  2110. * - Valid: 8112289874, 811228-9874, 811228+9874
  2111. * - Invalid: 811228-9873
  2112. *
  2113. * @see http://en.wikipedia.org/wiki/Personal_identity_number_(Sweden)
  2114. * @param {String} value The ID
  2115. * @returns {Boolean}
  2116. */
  2117. _se: function(value) {
  2118. if (!/^[0-9]{10}$/.test(value) && !/^[0-9]{6}[-|+][0-9]{4}$/.test(value)) {
  2119. return false;
  2120. }
  2121. value = value.replace(/[^0-9]/g, '');
  2122. var year = parseInt(value.substr(0, 2)) + 1900,
  2123. month = parseInt(value.substr(2, 2)),
  2124. day = parseInt(value.substr(4, 2));
  2125. if (!$.fn.bootstrapValidator.helpers.date(year, month, day)) {
  2126. return false;
  2127. }
  2128. // Validate the last check digit
  2129. return $.fn.bootstrapValidator.helpers.luhn(value);
  2130. },
  2131. /**
  2132. * Validate Slovak national identifier number (RC)
  2133. * Examples:
  2134. * - Valid: 7103192745, 991231123
  2135. * - Invalid: 7103192746, 1103492745
  2136. *
  2137. * @param {String} value The ID
  2138. * @returns {Boolean}
  2139. */
  2140. _sk: function(value) {
  2141. // Slovakia uses the same format as Czech Republic
  2142. return this._cz(value);
  2143. },
  2144. /**
  2145. * Validate San Marino citizen number
  2146. *
  2147. * @see http://en.wikipedia.org/wiki/National_identification_number#San_Marino
  2148. * @param {String} value The ID
  2149. * @returns {Boolean}
  2150. */
  2151. _sm: function(value) {
  2152. return /^\d{5}$/.test(value);
  2153. },
  2154. /**
  2155. * Validate South African ID
  2156. * Example:
  2157. * - Valid: 8001015009087
  2158. * - Invalid: 8001015009287, 8001015009086
  2159. *
  2160. * @see http://en.wikipedia.org/wiki/National_identification_number#South_Africa
  2161. * @param {String} value The ID
  2162. * @returns {Boolean}
  2163. */
  2164. _za: function(value) {
  2165. if (!/^[0-9]{10}[0|1][8|9][0-9]$/.test(value)) {
  2166. return false;
  2167. }
  2168. var year = parseInt(value.substr(0, 2)),
  2169. currentYear = new Date().getFullYear() % 100,
  2170. month = parseInt(value.substr(2, 2)),
  2171. day = parseInt(value.substr(4, 2));
  2172. year = (year >= currentYear) ? (year + 1900) : (year + 2000);
  2173. if (!$.fn.bootstrapValidator.helpers.date(year, month, day)) {
  2174. return false;
  2175. }
  2176. // Validate the last check digit
  2177. return $.fn.bootstrapValidator.helpers.luhn(value);
  2178. }
  2179. };
  2180. }(window.jQuery));
  2181. ;(function($) {
  2182. $.fn.bootstrapValidator.validators.identical = {
  2183. html5Attributes: {
  2184. message: 'message',
  2185. field: 'field'
  2186. },
  2187. /**
  2188. * Check if input value equals to value of particular one
  2189. *
  2190. * @param {BootstrapValidator} validator The validator plugin instance
  2191. * @param {jQuery} $field Field element
  2192. * @param {Object} options Consists of the following key:
  2193. * - field: The name of field that will be used to compare with current one
  2194. * @returns {Boolean}
  2195. */
  2196. validate: function(validator, $field, options) {
  2197. var value = $field.val();
  2198. if (value == '') {
  2199. return true;
  2200. }
  2201. var compareWith = validator.getFieldElements(options.field);
  2202. if (compareWith == null) {
  2203. return true;
  2204. }
  2205. if (value == compareWith.val()) {
  2206. validator.updateStatus(options.field, validator.STATUS_VALID, 'identical');
  2207. return true;
  2208. } else {
  2209. return false;
  2210. }
  2211. }
  2212. };
  2213. }(window.jQuery));
  2214. ;(function($) {
  2215. $.fn.bootstrapValidator.validators.imei = {
  2216. /**
  2217. * Validate IMEI (International Mobile Station Equipment Identity)
  2218. * Examples:
  2219. * - Valid: 35-209900-176148-1, 35-209900-176148-23, 3568680000414120, 490154203237518
  2220. * - Invalid: 490154203237517
  2221. *
  2222. * @see http://en.wikipedia.org/wiki/International_Mobile_Station_Equipment_Identity
  2223. * @param {BootstrapValidator} validator The validator plugin instance
  2224. * @param {jQuery} $field Field element
  2225. * @param {Object} options Can consist of the following keys:
  2226. * - message: The invalid message
  2227. * @returns {Boolean}
  2228. */
  2229. validate: function(validator, $field, options) {
  2230. var value = $field.val();
  2231. if (value == '') {
  2232. return true;
  2233. }
  2234. switch (true) {
  2235. case /^\d{15}$/.test(value):
  2236. case /^\d{2}-\d{6}-\d{6}-\d{1}$/.test(value):
  2237. case /^\d{2}\s\d{6}\s\d{6}\s\d{1}$/.test(value):
  2238. value = value.replace(/[^0-9]/g, '');
  2239. return $.fn.bootstrapValidator.helpers.luhn(value);
  2240. break;
  2241. case /^\d{14}$/.test(value):
  2242. case /^\d{16}$/.test(value):
  2243. case /^\d{2}-\d{6}-\d{6}(|-\d{2})$/.test(value):
  2244. case /^\d{2}\s\d{6}\s\d{6}(|\s\d{2})$/.test(value):
  2245. return true;
  2246. default:
  2247. return false;
  2248. }
  2249. }
  2250. };
  2251. }(window.jQuery));
  2252. ;(function($) {
  2253. $.fn.bootstrapValidator.validators.integer = {
  2254. enableByHtml5: function($field) {
  2255. return ('number' == $field.attr('type'));
  2256. },
  2257. /**
  2258. * Return true if the input value is an integer
  2259. *
  2260. * @param {BootstrapValidator} validator The validator plugin instance
  2261. * @param {jQuery} $field Field element
  2262. * @param {Object} options Can consist of the following key:
  2263. * - message: The invalid message
  2264. * @returns {Boolean}
  2265. */
  2266. validate: function(validator, $field, options) {
  2267. var value = $field.val();
  2268. if (value == '') {
  2269. return true;
  2270. }
  2271. return /^(?:-?(?:0|[1-9][0-9]*))$/.test(value);
  2272. }
  2273. };
  2274. }(window.jQuery));
  2275. ;(function($) {
  2276. $.fn.bootstrapValidator.validators.ip = {
  2277. html5Attributes: {
  2278. message: 'message',
  2279. ipv4: 'ipv4',
  2280. ipv6: 'ipv6'
  2281. },
  2282. /**
  2283. * Return true if the input value is a IP address.
  2284. *
  2285. * @param {BootstrapValidator} validator The validator plugin instance
  2286. * @param {jQuery} $field Field element
  2287. * @param {Object} options Can consist of the following keys:
  2288. * - ipv4: Enable IPv4 validator, default to true
  2289. * - ipv6: Enable IPv6 validator, default to true
  2290. * - message: The invalid message
  2291. * @returns {Boolean}
  2292. */
  2293. validate: function(validator, $field, options) {
  2294. var value = $field.val();
  2295. if (value == '') {
  2296. return true;
  2297. }
  2298. options = $.extend({}, { ipv4: true, ipv6: true }, options);
  2299. if (options.ipv4) {
  2300. 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);
  2301. } else if (options.ipv6) {
  2302. 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);
  2303. }
  2304. return false;
  2305. }
  2306. };
  2307. }(window.jQuery));
  2308. ;(function($) {
  2309. $.fn.bootstrapValidator.validators.isbn = {
  2310. /**
  2311. * Return true if the input value is a valid ISBN 10 or ISBN 13 number
  2312. * Examples:
  2313. * - Valid:
  2314. * 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
  2315. * ISBN 13: 978-0-306-40615-7
  2316. * - Invalid:
  2317. * ISBN 10: 99921-58-10-6
  2318. * ISBN 13: 978-0-306-40615-6
  2319. *
  2320. * @see http://en.wikipedia.org/wiki/International_Standard_Book_Number
  2321. * @param {BootstrapValidator} validator The validator plugin instance
  2322. * @param {jQuery} $field Field element
  2323. * @param {Object} options Can consist of the following keys:
  2324. * - message: The invalid message
  2325. * @returns {Boolean}
  2326. */
  2327. validate: function(validator, $field, options) {
  2328. var value = $field.val();
  2329. if (value == '') {
  2330. return true;
  2331. }
  2332. // http://en.wikipedia.org/wiki/International_Standard_Book_Number#Overview
  2333. // Groups are separated by a hyphen or a space
  2334. var type;
  2335. switch (true) {
  2336. case /^\d{9}[\dX]$/.test(value):
  2337. case (value.length == 13 && /^(\d+)-(\d+)-(\d+)-([\dX])$/.test(value)):
  2338. case (value.length == 13 && /^(\d+)\s(\d+)\s(\d+)\s([\dX])$/.test(value)):
  2339. type = 'ISBN10';
  2340. break;
  2341. case /^(978|979)\d{9}[\dX]$/.test(value):
  2342. case (value.length == 17 && /^(978|979)-(\d+)-(\d+)-(\d+)-([\dX])$/.test(value)):
  2343. case (value.length == 17 && /^(978|979)\s(\d+)\s(\d+)\s(\d+)\s([\dX])$/.test(value)):
  2344. type = 'ISBN13';
  2345. break;
  2346. default:
  2347. return false;
  2348. }
  2349. // Replace all special characters except digits and X
  2350. value = value.replace(/[^0-9X]/gi, '');
  2351. var chars = value.split(''),
  2352. length = chars.length,
  2353. sum = 0,
  2354. checksum;
  2355. switch (type) {
  2356. case 'ISBN10':
  2357. sum = 0;
  2358. for (var i = 0; i < length - 1; i++) {
  2359. sum += ((10 - i) * parseInt(chars[i]));
  2360. }
  2361. checksum = 11 - (sum % 11);
  2362. if (checksum == 11) {
  2363. checksum = 0;
  2364. } else if (checksum == 10) {
  2365. checksum = 'X';
  2366. }
  2367. return (checksum + '' == chars[length - 1]);
  2368. case 'ISBN13':
  2369. sum = 0;
  2370. for (var i = 0; i < length - 1; i++) {
  2371. sum += ((i % 2 == 0) ? parseInt(chars[i]) : (parseInt(chars[i]) * 3));
  2372. }
  2373. checksum = 10 - (sum % 10);
  2374. if (checksum == 10) {
  2375. checksum = '0';
  2376. }
  2377. return (checksum + '' == chars[length - 1]);
  2378. default:
  2379. return false;
  2380. }
  2381. }
  2382. };
  2383. }(window.jQuery));
  2384. ;(function($) {
  2385. $.fn.bootstrapValidator.validators.ismn = {
  2386. /**
  2387. * Validate ISMN (International Standard Music Number)
  2388. * Examples:
  2389. * - Valid: M230671187, 979-0-0601-1561-5, 979 0 3452 4680 5, 9790060115615
  2390. * - Invalid: 9790060115614
  2391. *
  2392. * @see http://en.wikipedia.org/wiki/International_Standard_Music_Number
  2393. * @param {BootstrapValidator} validator The validator plugin instance
  2394. * @param {jQuery} $field Field element
  2395. * @param {Object} options Can consist of the following keys:
  2396. * - message: The invalid message
  2397. * @returns {Boolean}
  2398. */
  2399. validate: function(validator, $field, options) {
  2400. var value = $field.val();
  2401. if (value == '') {
  2402. return true;
  2403. }
  2404. // Groups are separated by a hyphen or a space
  2405. var type;
  2406. switch (true) {
  2407. case /^M\d{9}$/.test(value):
  2408. case /^M-\d{4}-\d{4}-\d{1}$/.test(value):
  2409. case /^M\s\d{4}\s\d{4}\s\d{1}$/.test(value):
  2410. type = 'ISMN10';
  2411. break;
  2412. case /^9790\d{9}$/.test(value):
  2413. case /^979-0-\d{4}-\d{4}-\d{1}$/.test(value):
  2414. case /^979\s0\s\d{4}\s\d{4}\s\d{1}$/.test(value):
  2415. type = 'ISMN13';
  2416. break;
  2417. default:
  2418. return false;
  2419. }
  2420. if ('ISMN10' == type) {
  2421. value = '9790' + value.substr(1);
  2422. }
  2423. // Replace all special characters except digits
  2424. value = value.replace(/[^0-9]/gi, '');
  2425. var length = value.length,
  2426. sum = 0,
  2427. weight = [1, 3];
  2428. for (var i = 0; i < length - 1; i++) {
  2429. sum += parseInt(value.charAt(i)) * weight[i % 2];
  2430. }
  2431. sum = 10 - sum % 10;
  2432. return (sum == value.charAt(length - 1));
  2433. }
  2434. };
  2435. }(window.jQuery));
  2436. ;(function($) {
  2437. $.fn.bootstrapValidator.validators.issn = {
  2438. /**
  2439. * Validate ISSN (International Standard Serial Number)
  2440. * Examples:
  2441. * - Valid: 0378-5955, 0024-9319, 0032-1478
  2442. * - Invalid: 0032-147X
  2443. *
  2444. * @see http://en.wikipedia.org/wiki/International_Standard_Serial_Number
  2445. * @param {BootstrapValidator} validator The validator plugin instance
  2446. * @param {jQuery} $field Field element
  2447. * @param {Object} options Can consist of the following keys:
  2448. * - message: The invalid message
  2449. * @returns {Boolean}
  2450. */
  2451. validate: function(validator, $field, options) {
  2452. var value = $field.val();
  2453. if (value == '') {
  2454. return true;
  2455. }
  2456. // Groups are separated by a hyphen or a space
  2457. if (!/^\d{4}\-\d{3}[\dX]$/.test(value)) {
  2458. return false;
  2459. }
  2460. // Replace all special characters except digits and X
  2461. value = value.replace(/[^0-9X]/gi, '');
  2462. var chars = value.split(''),
  2463. length = chars.length,
  2464. sum = 0;
  2465. if (chars[7] == 'X') {
  2466. chars[7] = 10;
  2467. }
  2468. for (var i = 0; i < length; i++) {
  2469. sum += ((8 - i) * parseInt(chars[i]));
  2470. }
  2471. return (sum % 11 == 0);
  2472. }
  2473. };
  2474. }(window.jQuery));
  2475. ;(function($) {
  2476. $.fn.bootstrapValidator.validators.lessThan = {
  2477. html5Attributes: {
  2478. message: 'message',
  2479. value: 'value',
  2480. inclusive: 'inclusive'
  2481. },
  2482. enableByHtml5: function($field) {
  2483. var max = $field.attr('max');
  2484. if (max) {
  2485. return {
  2486. value: max
  2487. };
  2488. }
  2489. return false;
  2490. },
  2491. /**
  2492. * Return true if the input value is less than or equal to given number
  2493. *
  2494. * @param {BootstrapValidator} validator The validator plugin instance
  2495. * @param {jQuery} $field Field element
  2496. * @param {Object} options Can consist of the following keys:
  2497. * - value: The number used to compare to
  2498. * - inclusive [optional]: Can be true or false. Default is true
  2499. * - message: The invalid message
  2500. * @returns {Boolean}
  2501. */
  2502. validate: function(validator, $field, options) {
  2503. var value = $field.val();
  2504. if (value == '') {
  2505. return true;
  2506. }
  2507. value = parseFloat(value);
  2508. return (options.inclusive === false) ? (value <= options.value) : (value < options.value);
  2509. }
  2510. };
  2511. }(window.jQuery));
  2512. ;(function($) {
  2513. $.fn.bootstrapValidator.validators.mac = {
  2514. /**
  2515. * Return true if the input value is a MAC address.
  2516. *
  2517. * @param {BootstrapValidator} validator The validator plugin instance
  2518. * @param {jQuery} $field Field element
  2519. * @param {Object} options Can consist of the following keys:
  2520. * - message: The invalid message
  2521. * @returns {Boolean}
  2522. */
  2523. validate: function(validator, $field, options) {
  2524. var value = $field.val();
  2525. if (value == '') {
  2526. return true;
  2527. }
  2528. return /^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$/.test(value);
  2529. }
  2530. };
  2531. }(window.jQuery));
  2532. ;(function($) {
  2533. $.fn.bootstrapValidator.validators.notEmpty = {
  2534. enableByHtml5: function($field) {
  2535. var required = $field.attr('required') + '';
  2536. return ('required' == required || 'true' == required);
  2537. },
  2538. /**
  2539. * Check if input value is empty or not
  2540. *
  2541. * @param {BootstrapValidator} validator The validator plugin instance
  2542. * @param {jQuery} $field Field element
  2543. * @param {Object} options
  2544. * @returns {Boolean}
  2545. */
  2546. validate: function(validator, $field, options) {
  2547. var type = $field.attr('type');
  2548. if ('radio' == type || 'checkbox' == type) {
  2549. return validator
  2550. .getFieldElements($field.attr('data-bv-field'))
  2551. .filter(':checked')
  2552. .length > 0;
  2553. }
  2554. return $.trim($field.val()) != '';
  2555. }
  2556. };
  2557. }(window.jQuery));
  2558. ;(function($) {
  2559. $.fn.bootstrapValidator.validators.numeric = {
  2560. html5Attributes: {
  2561. message: 'message',
  2562. separator: 'separator'
  2563. },
  2564. /**
  2565. * Validate decimal number
  2566. *
  2567. * @param {BootstrapValidator} validator The validator plugin instance
  2568. * @param {jQuery} $field Field element
  2569. * @param {Object} options Consist of key:
  2570. * - message: The invalid message
  2571. * - separator: The decimal separator. Can be "." (default), ","
  2572. * @returns {Boolean}
  2573. */
  2574. validate: function(validator, $field, options) {
  2575. var value = $field.val();
  2576. if (value == '') {
  2577. return true;
  2578. }
  2579. var separator = options.separator || '.';
  2580. if (separator != '.') {
  2581. value = value.replace(separator, '.');
  2582. }
  2583. return !isNaN(parseFloat(value)) && isFinite(value);
  2584. }
  2585. };
  2586. }(window.jQuery));
  2587. ;(function($) {
  2588. $.fn.bootstrapValidator.validators.phone = {
  2589. html5Attributes: {
  2590. message: 'message',
  2591. country: 'country'
  2592. },
  2593. /**
  2594. * Return true if the input value contains a valid US phone number only
  2595. *
  2596. * @param {BootstrapValidator} validator Validate plugin instance
  2597. * @param {jQuery} $field Field element
  2598. * @param {Object} options Consist of key:
  2599. * - message: The invalid message
  2600. * - country: The ISO 3166 country code
  2601. * Currently it only supports United State (US) country
  2602. * @returns {Boolean}
  2603. */
  2604. validate: function(validator, $field, options) {
  2605. var value = $field.val();
  2606. if (value == '') {
  2607. return true;
  2608. }
  2609. var country = (options.country || 'US').toUpperCase();
  2610. switch (country) {
  2611. case 'US':
  2612. default:
  2613. // Make sure US phone numbers have 10 digits
  2614. // May start with 1, +1, or 1-; should discard
  2615. // Area code may be delimited with (), & sections may be delimited with . or -
  2616. // Test: http://regexr.com/38mqi
  2617. value = value.replace(/\D/g, '');
  2618. return (/^(?:(1\-?)|(\+1 ?))?\(?(\d{3})[\)\-\.]?(\d{3})[\-\.]?(\d{4})$/).test(value) && (value.length == 10);
  2619. }
  2620. }
  2621. }
  2622. }(window.jQuery));
  2623. ;(function($) {
  2624. $.fn.bootstrapValidator.validators.regexp = {
  2625. html5Attributes: {
  2626. message: 'message',
  2627. regexp: 'regexp'
  2628. },
  2629. enableByHtml5: function($field) {
  2630. var pattern = $field.attr('pattern');
  2631. if (pattern) {
  2632. return {
  2633. regexp: pattern
  2634. };
  2635. }
  2636. return false;
  2637. },
  2638. /**
  2639. * Check if the element value matches given regular expression
  2640. *
  2641. * @param {BootstrapValidator} validator The validator plugin instance
  2642. * @param {jQuery} $field Field element
  2643. * @param {Object} options Consists of the following key:
  2644. * - regexp: The regular expression you need to check
  2645. * @returns {Boolean}
  2646. */
  2647. validate: function(validator, $field, options) {
  2648. var value = $field.val();
  2649. if (value == '') {
  2650. return true;
  2651. }
  2652. var regexp = ('string' == typeof options.regexp) ? new RegExp(options.regexp) : options.regexp;
  2653. return regexp.test(value);
  2654. }
  2655. };
  2656. }(window.jQuery));
  2657. ;(function ($) {
  2658. $.fn.bootstrapValidator.validators.remote = {
  2659. html5Attributes: {
  2660. message: 'message',
  2661. url: 'url',
  2662. name: 'name'
  2663. },
  2664. /**
  2665. * Request a remote server to check the input value
  2666. *
  2667. * @param {BootstrapValidator} validator Plugin instance
  2668. * @param {jQuery} $field Field element
  2669. * @param {Object} options Can consist of the following keys:
  2670. * - url
  2671. * - data [optional]: By default, it will take the value
  2672. * {
  2673. * <fieldName>: <fieldValue>
  2674. * }
  2675. * - name [optional]: Override the field name for the request.
  2676. * - message: The invalid message
  2677. * @returns {Boolean|Deferred}
  2678. */
  2679. validate: function(validator, $field, options) {
  2680. var value = $field.val();
  2681. if (value == '') {
  2682. return true;
  2683. }
  2684. var name = $field.attr('data-bv-field'), data = options.data;
  2685. if (data == null) {
  2686. data = {};
  2687. }
  2688. // Support dynamic data
  2689. if ('function' == typeof data) {
  2690. data = data.call(this, validator);
  2691. }
  2692. data[options.name || name] = value;
  2693. var dfd = new $.Deferred();
  2694. var xhr = $.ajax({
  2695. type: 'POST',
  2696. url: options.url,
  2697. dataType: 'json',
  2698. data: data
  2699. });
  2700. xhr.then(function(response) {
  2701. dfd.resolve($field, 'remote', response.valid === true || response.valid === 'true');
  2702. });
  2703. dfd.fail(function() {
  2704. xhr.abort();
  2705. });
  2706. return dfd;
  2707. }
  2708. };
  2709. }(window.jQuery));
  2710. ;(function($) {
  2711. $.fn.bootstrapValidator.validators.siren = {
  2712. /**
  2713. * Check if a string is a siren number
  2714. *
  2715. * @param {BootstrapValidator} validator The validator plugin instance
  2716. * @param {jQuery} $field Field element
  2717. * @param {Object} options Consist of key:
  2718. * - message: The invalid message
  2719. * @returns {Boolean}
  2720. */
  2721. validate: function(validator, $field, options) {
  2722. var value = $field.val();
  2723. if (value == '') {
  2724. return true;
  2725. }
  2726. if (!/^\d{9}$/.test(value)) {
  2727. return false;
  2728. }
  2729. return $.fn.bootstrapValidator.helpers.luhn(value);
  2730. }
  2731. };
  2732. }(window.jQuery));
  2733. ;(function($) {
  2734. $.fn.bootstrapValidator.validators.siret = {
  2735. /**
  2736. * Check if a string is a siret number
  2737. *
  2738. * @param {BootstrapValidator} validator The validator plugin instance
  2739. * @param {jQuery} $field Field element
  2740. * @param {Object} options Consist of key:
  2741. * - message: The invalid message
  2742. * @returns {Boolean}
  2743. */
  2744. validate: function(validator, $field, options) {
  2745. var value = $field.val();
  2746. if (value == '') {
  2747. return true;
  2748. }
  2749. var sum = 0,
  2750. length = value.length,
  2751. tmp;
  2752. for (var i = 0; i < length; i++) {
  2753. tmp = parseInt(value.charAt(i), 10);
  2754. if ((i % 2) == 0) {
  2755. tmp = tmp * 2;
  2756. if (tmp > 9) {
  2757. tmp -= 9;
  2758. }
  2759. }
  2760. sum += tmp;
  2761. }
  2762. return (sum % 10 == 0);
  2763. }
  2764. };
  2765. }(window.jQuery));
  2766. ;(function($) {
  2767. $.fn.bootstrapValidator.validators.step = {
  2768. html5Attributes: {
  2769. message: 'message',
  2770. base: 'baseValue',
  2771. step: 'step'
  2772. },
  2773. /**
  2774. * Return true if the input value is valid step one
  2775. *
  2776. * @param {BootstrapValidator} validator The validator plugin instance
  2777. * @param {jQuery} $field Field element
  2778. * @param {Object} options Can consist of the following keys:
  2779. * - baseValue: The base value
  2780. * - step: The step
  2781. * - message: The invalid message
  2782. * @returns {Boolean}
  2783. */
  2784. validate: function(validator, $field, options) {
  2785. var value = $field.val();
  2786. if (value == '') {
  2787. return true;
  2788. }
  2789. options = $.extend({}, { baseValue: 0, step: 1 }, options);
  2790. value = parseFloat(value);
  2791. if (isNaN(value) || !isFinite(value)) {
  2792. return false;
  2793. }
  2794. var round = function(x, precision) {
  2795. var m = Math.pow(10, precision);
  2796. x = x * m;
  2797. var sign = (x > 0) | -(x < 0),
  2798. isHalf = (x % 1 === 0.5 * sign);
  2799. if (isHalf) {
  2800. return (Math.floor(x) + (sign > 0)) / m;
  2801. } else {
  2802. return Math.round(x) / m;
  2803. }
  2804. },
  2805. floatMod = function(x, y) {
  2806. if (y == 0.0) {
  2807. return 1.0;
  2808. }
  2809. var dotX = (x + '').split('.'),
  2810. dotY = (y + '').split('.'),
  2811. precision = ((dotX.length == 1) ? 0 : dotX[1].length) + ((dotY.length == 1) ? 0 : dotY[1].length);
  2812. return round(x - y * Math.floor(x / y), precision);
  2813. };
  2814. var mod = floatMod(value - options.baseValue, options.step);
  2815. return (mod == 0.0 || mod == options.step);
  2816. }
  2817. };
  2818. }(window.jQuery));
  2819. ;(function($) {
  2820. $.fn.bootstrapValidator.validators.stringCase = {
  2821. html5Attributes: {
  2822. message: 'message',
  2823. 'case': 'case'
  2824. },
  2825. /**
  2826. * Check if a string is a lower or upper case one
  2827. *
  2828. * @param {BootstrapValidator} validator The validator plugin instance
  2829. * @param {jQuery} $field Field element
  2830. * @param {Object} options Consist of key:
  2831. * - message: The invalid message
  2832. * - case: Can be 'lower' (default) or 'upper'
  2833. * @returns {Boolean}
  2834. */
  2835. validate: function(validator, $field, options) {
  2836. var value = $field.val();
  2837. if (value == '') {
  2838. return true;
  2839. }
  2840. var stringCase = (options['case'] || 'lower').toLowerCase();
  2841. switch (stringCase) {
  2842. case 'upper':
  2843. return value === value.toUpperCase();
  2844. case 'lower':
  2845. default:
  2846. return value === value.toLowerCase();
  2847. }
  2848. }
  2849. };
  2850. }(window.jQuery));
  2851. ;(function($) {
  2852. $.fn.bootstrapValidator.validators.stringLength = {
  2853. html5Attributes: {
  2854. message: 'message',
  2855. min: 'min',
  2856. max: 'max'
  2857. },
  2858. enableByHtml5: function($field) {
  2859. var maxLength = $field.attr('maxlength');
  2860. if (maxLength) {
  2861. return {
  2862. max: parseInt(maxLength, 10)
  2863. };
  2864. }
  2865. return false;
  2866. },
  2867. /**
  2868. * Check if the length of element value is less or more than given number
  2869. *
  2870. * @param {BootstrapValidator} validator The validator plugin instance
  2871. * @param {jQuery} $field Field element
  2872. * @param {Object} options Consists of following keys:
  2873. * - min
  2874. * - max
  2875. * At least one of two keys is required
  2876. * - message: The invalid message
  2877. * @returns {Boolean}
  2878. */
  2879. validate: function(validator, $field, options) {
  2880. var value = $field.val();
  2881. if (value == '') {
  2882. return true;
  2883. }
  2884. var length = $.trim(value).length;
  2885. if ((options.min && length < options.min) || (options.max && length > options.max)) {
  2886. return false;
  2887. }
  2888. return true;
  2889. }
  2890. };
  2891. }(window.jQuery));
  2892. ;(function($) {
  2893. $.fn.bootstrapValidator.validators.uri = {
  2894. enableByHtml5: function($field) {
  2895. return ('url' == $field.attr('type'));
  2896. },
  2897. /**
  2898. * Return true if the input value is a valid URL
  2899. *
  2900. * @param {BootstrapValidator} validator The validator plugin instance
  2901. * @param {jQuery} $field Field element
  2902. * @param {Object} options
  2903. * @returns {Boolean}
  2904. */
  2905. validate: function(validator, $field, options) {
  2906. var value = $field.val();
  2907. if (value == '') {
  2908. return true;
  2909. }
  2910. // Credit to https://gist.github.com/dperini/729294
  2911. //
  2912. // Regular Expression for URL validation
  2913. //
  2914. // Author: Diego Perini
  2915. // Updated: 2010/12/05
  2916. //
  2917. // the regular expression composed & commented
  2918. // could be easily tweaked for RFC compliance,
  2919. // it was expressly modified to fit & satisfy
  2920. // these test for an URL shortener:
  2921. //
  2922. // http://mathiasbynens.be/demo/url-regex
  2923. //
  2924. // Notes on possible differences from a standard/generic validation:
  2925. //
  2926. // - utf-8 char class take in consideration the full Unicode range
  2927. // - TLDs have been made mandatory so single names like "localhost" fails
  2928. // - protocols have been restricted to ftp, http and https only as requested
  2929. //
  2930. // Changes:
  2931. //
  2932. // - IP address dotted notation validation, range: 1.0.0.0 - 223.255.255.255
  2933. // first and last IP address of each class is considered invalid
  2934. // (since they are broadcast/network addresses)
  2935. //
  2936. // - Added exclusion of private, reserved and/or local networks ranges
  2937. //
  2938. // Compressed one-line versions:
  2939. //
  2940. // Javascript version
  2941. //
  2942. // /^(?:(?: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
  2943. //
  2944. // PHP version
  2945. //
  2946. // _^(?:(?: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
  2947. var urlExp = new RegExp(
  2948. "^" +
  2949. // protocol identifier
  2950. "(?:(?:https?|ftp)://)" +
  2951. // user:pass authentication
  2952. "(?:\\S+(?::\\S*)?@)?" +
  2953. "(?:" +
  2954. // IP address exclusion
  2955. // private & local networks
  2956. "(?!10(?:\\.\\d{1,3}){3})" +
  2957. "(?!127(?:\\.\\d{1,3}){3})" +
  2958. "(?!169\\.254(?:\\.\\d{1,3}){2})" +
  2959. "(?!192\\.168(?:\\.\\d{1,3}){2})" +
  2960. "(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})" +
  2961. // IP address dotted notation octets
  2962. // excludes loopback network 0.0.0.0
  2963. // excludes reserved space >= 224.0.0.0
  2964. // excludes network & broacast addresses
  2965. // (first & last IP address of each class)
  2966. "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
  2967. "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
  2968. "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
  2969. "|" +
  2970. // host name
  2971. "(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)" +
  2972. // domain name
  2973. "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*" +
  2974. // TLD identifier
  2975. "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" +
  2976. ")" +
  2977. // port number
  2978. "(?::\\d{2,5})?" +
  2979. // resource path
  2980. "(?:/[^\\s]*)?" +
  2981. "$", "i"
  2982. );
  2983. return urlExp.test(value);
  2984. }
  2985. };
  2986. }(window.jQuery));
  2987. ;(function($) {
  2988. $.fn.bootstrapValidator.validators.uuid = {
  2989. html5Attributes: {
  2990. message: 'message',
  2991. version: 'version'
  2992. },
  2993. /**
  2994. * Return true if and only if the input value is a valid UUID string
  2995. *
  2996. * @see http://en.wikipedia.org/wiki/Universally_unique_identifier
  2997. * @param {BootstrapValidator} validator The validator plugin instance
  2998. * @param {jQuery} $field Field element
  2999. * @param {Object} options Consist of key:
  3000. * - message: The invalid message
  3001. * - version: Can be 3, 4, 5, null
  3002. * @returns {Boolean}
  3003. */
  3004. validate: function(validator, $field, options) {
  3005. var value = $field.val();
  3006. if (value == '') {
  3007. return true;
  3008. }
  3009. // See the format at http://en.wikipedia.org/wiki/Universally_unique_identifier#Variants_and_versions
  3010. var patterns = {
  3011. '3': /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
  3012. '4': /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
  3013. '5': /^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
  3014. all: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i
  3015. },
  3016. version = options.version ? (options.version + '') : 'all';
  3017. return (null == patterns[version]) ? true : patterns[version].test(value);
  3018. }
  3019. };
  3020. }(window.jQuery));
  3021. ;(function($) {
  3022. $.fn.bootstrapValidator.validators.vat = {
  3023. html5Attributes: {
  3024. message: 'message',
  3025. country: 'country'
  3026. },
  3027. /**
  3028. * Validate an European VAT number
  3029. *
  3030. * @param {BootstrapValidator} validator The validator plugin instance
  3031. * @param {jQuery} $field Field element
  3032. * @param {Object} options Consist of key:
  3033. * - message: The invalid message
  3034. * - country: The ISO 3166-1 country code
  3035. * @returns {Boolean}
  3036. */
  3037. validate: function(validator, $field, options) {
  3038. var value = $field.val();
  3039. if (value == '') {
  3040. return true;
  3041. }
  3042. var country = options.country || value.substr(0, 2),
  3043. method = ['_', country.toLowerCase()].join('');
  3044. if (this[method] && 'function' == typeof this[method]) {
  3045. return this[method](value);
  3046. }
  3047. return true;
  3048. },
  3049. // VAT validators
  3050. /**
  3051. * Validate Austrian VAT number
  3052. * Example:
  3053. * - Valid: ATU13585627
  3054. * - Invalid: ATU13585626
  3055. *
  3056. * @param {String} value VAT number
  3057. * @returns {Boolean}
  3058. */
  3059. _at: function(value) {
  3060. if (!/^ATU[0-9]{8}$/.test(value)) {
  3061. return false;
  3062. }
  3063. value = value.substr(3);
  3064. var sum = 0,
  3065. weight = [1, 2, 1, 2, 1, 2, 1],
  3066. temp = 0;
  3067. for (var i = 0; i < 7; i++) {
  3068. temp = parseInt(value.charAt(i)) * weight[i];
  3069. if (temp > 9) {
  3070. temp = Math.floor(temp / 10) + temp % 10;
  3071. }
  3072. sum += temp;
  3073. }
  3074. sum = 10 - (sum + 4) % 10;
  3075. if (sum == 10) {
  3076. sum = 0;
  3077. }
  3078. return (sum == value.substr(7, 1));
  3079. },
  3080. /**
  3081. * Validate Belgian VAT number
  3082. * Example:
  3083. * - Valid: BE0428759497
  3084. * - Invalid: BE431150351
  3085. *
  3086. * @param {String} value VAT number
  3087. * @returns {Boolean}
  3088. */
  3089. _be: function(value) {
  3090. if (!/^BE[0]{0,1}[0-9]{9}$/.test(value)) {
  3091. return false;
  3092. }
  3093. value = value.substr(2);
  3094. if (value.length == 9) {
  3095. value = '0' + value;
  3096. }
  3097. if (value.substr(1, 1) == 0) {
  3098. return false;
  3099. }
  3100. var sum = parseInt(value.substr(0, 8), 10) + parseInt(value.substr(8, 2), 10);
  3101. return (sum % 97 == 0);
  3102. },
  3103. /**
  3104. * Validate Bulgarian VAT number
  3105. * Example:
  3106. * - Valid: BG175074752,
  3107. * BG7523169263, BG8032056031,
  3108. * BG7542011030,
  3109. * BG7111042925
  3110. * - Invalid: BG175074753, BG7552A10004, BG7111042922
  3111. *
  3112. * @param {String} value VAT number
  3113. * @returns {Boolean}
  3114. */
  3115. _bg: function(value) {
  3116. if (!/^BG[0-9]{9,10}$/.test(value)) {
  3117. return false;
  3118. }
  3119. value = value.substr(2);
  3120. var total = 0, sum = 0, weight = [], i = 0;
  3121. // Legal entities
  3122. if (value.length == 9) {
  3123. for (i = 0; i < 8; i++) {
  3124. sum += parseInt(value.charAt(i)) * (i + 1);
  3125. }
  3126. sum = sum % 11;
  3127. if (sum == 10) {
  3128. sum = 0;
  3129. for (i = 0; i < 8; i++) {
  3130. sum += parseInt(value.charAt(i)) * (i + 3);
  3131. }
  3132. }
  3133. sum = sum % 10;
  3134. return (sum == value.substr(8));
  3135. }
  3136. // Physical persons, foreigners and others
  3137. else if (value.length == 10) {
  3138. // Validate Bulgarian national identification numbers
  3139. var egn = function(value) {
  3140. // Check the birth date
  3141. var year = parseInt(value.substr(0, 2), 10) + 1900,
  3142. month = parseInt(value.substr(2, 2), 10),
  3143. day = parseInt(value.substr(4, 2), 10);
  3144. if (month > 40) {
  3145. year += 100;
  3146. month -= 40;
  3147. } else if (month > 20) {
  3148. year -= 100;
  3149. month -= 20;
  3150. }
  3151. if (!$.fn.bootstrapValidator.helpers.date(year, month, day)) {
  3152. return false;
  3153. }
  3154. var sum = 0,
  3155. weight = [2, 4, 8, 5, 10, 9, 7, 3, 6];
  3156. for (var i = 0; i < 9; i++) {
  3157. sum += parseInt(value.charAt(i)) * weight[i];
  3158. }
  3159. sum = (sum % 11) % 10;
  3160. return (sum == value.substr(9, 1));
  3161. },
  3162. // Validate Bulgarian personal number of a foreigner
  3163. pnf = function(value) {
  3164. var sum = 0,
  3165. weight = [21, 19, 17, 13, 11, 9, 7, 3, 1];
  3166. for (var i = 0; i < 9; i++) {
  3167. sum += parseInt(value.charAt(i)) * weight[i];
  3168. }
  3169. sum = sum % 10;
  3170. return (sum == value.substr(9, 1));
  3171. },
  3172. // Finally, consider it as a VAT number
  3173. vat = function(value) {
  3174. var sum = 0,
  3175. weight = [4, 3, 2, 7, 6, 5, 4, 3, 2];
  3176. for (var i = 0; i < 9; i++) {
  3177. sum += parseInt(value.charAt(i)) * weight[i];
  3178. }
  3179. sum = 11 - sum % 11;
  3180. if (sum == 10) {
  3181. return false;
  3182. }
  3183. if (sum == 11) {
  3184. sum = 0;
  3185. }
  3186. return (sum == value.substr(9, 1));
  3187. };
  3188. return (egn(value) || pnf(value) || vat(value));
  3189. }
  3190. return false;
  3191. },
  3192. /**
  3193. * Validate Swiss VAT number
  3194. *
  3195. * @param {String} value VAT number
  3196. * @returns {Boolean}
  3197. */
  3198. _ch: function(value) {
  3199. if (!/^CHE[0-9]{9}(MWST)?$/.test(value)) {
  3200. return false;
  3201. }
  3202. value = value.substr(3);
  3203. var sum = 0,
  3204. weight = [5, 4, 3, 2, 7, 6, 5, 4];
  3205. for (var i = 0; i < 8; i++) {
  3206. sum += parseInt(value.charAt(i), 10) * weight[i];
  3207. }
  3208. sum = 11 - sum % 11;
  3209. if (sum == 10) {
  3210. return false;
  3211. }
  3212. if (sum == 11) {
  3213. sum = 0;
  3214. }
  3215. return (sum == value.substr(8, 1));
  3216. },
  3217. /**
  3218. * Validate Cypriot VAT number
  3219. * Examples:
  3220. * - Valid: CY10259033P
  3221. * - Invalid: CY10259033Z
  3222. *
  3223. * @param {String} value VAT number
  3224. * @returns {Boolean}
  3225. */
  3226. _cy: function(value) {
  3227. if (!/^CY[0-5|9]{1}[0-9]{7}[A-Z]{1}$/.test(value)) {
  3228. return false;
  3229. }
  3230. value = value.substr(2);
  3231. // Do not allow to start with "12"
  3232. if (value.substr(0, 2) == '12') {
  3233. return false;
  3234. }
  3235. // Extract the next digit and multiply by the counter.
  3236. var sum = 0,
  3237. translation = {
  3238. '0': 1, '1': 0, '2': 5, '3': 7, '4': 9,
  3239. '5': 13, '6': 15, '7': 17, '8': 19, '9': 21
  3240. };
  3241. for (var i = 0; i < 8; i++) {
  3242. var temp = parseInt(value.charAt(i), 10);
  3243. if (i % 2 == 0) {
  3244. temp = translation[temp + ''];
  3245. }
  3246. sum += temp;
  3247. }
  3248. sum = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[sum % 26];
  3249. return (sum == value.substr(8, 1));
  3250. },
  3251. /**
  3252. * Validate Czech Republic VAT number
  3253. * Can be:
  3254. * i) Legal entities (8 digit numbers)
  3255. * ii) Individuals with a RC (the 9 or 10 digit Czech birth number)
  3256. * iii) Individuals without a RC (9 digit numbers beginning with 6)
  3257. *
  3258. * Examples:
  3259. * - Valid: i) CZ25123891; ii) CZ7103192745, CZ991231123; iii) CZ640903926
  3260. * - Invalid: i) CZ25123890; ii) CZ1103492745, CZ590312123
  3261. *
  3262. * @param {String} value VAT number
  3263. * @returns {Boolean}
  3264. */
  3265. _cz: function(value) {
  3266. if (!/^CZ[0-9]{8,10}$/.test(value)) {
  3267. return false;
  3268. }
  3269. value = value.substr(2);
  3270. var sum = 0,
  3271. i = 0;
  3272. if (value.length == 8) {
  3273. // Do not allow to start with '9'
  3274. if (value.charAt(0) + '' == '9') {
  3275. return false;
  3276. }
  3277. sum = 0;
  3278. for (i = 0; i < 7; i++) {
  3279. sum += parseInt(value.charAt(i), 10) * (8 - i);
  3280. }
  3281. sum = 11 - sum % 11;
  3282. if (sum == 10) {
  3283. sum = 0;
  3284. }
  3285. if (sum == 11) {
  3286. sum = 1;
  3287. }
  3288. return (sum == value.substr(7, 1));
  3289. } else if (value.length == 9 && (value.charAt(0) + '' == '6')) {
  3290. sum = 0;
  3291. // Skip the first (which is 6)
  3292. for (i = 0; i < 7; i++) {
  3293. sum += parseInt(value.charAt(i + 1), 10) * (8 - i);
  3294. }
  3295. sum = 11 - sum % 11;
  3296. if (sum == 10) {
  3297. sum = 0;
  3298. }
  3299. if (sum == 11) {
  3300. sum = 1;
  3301. }
  3302. sum = [8, 7, 6, 5, 4, 3, 2, 1, 0, 9, 10][sum - 1];
  3303. return (sum == value.substr(8, 1));
  3304. } else if (value.length == 9 || value.length == 10) {
  3305. // Validate Czech birth number (Rodné číslo), which is also national identifier
  3306. var year = 1900 + parseInt(value.substr(0, 2)),
  3307. month = parseInt(value.substr(2, 2)) % 50 % 20,
  3308. day = parseInt(value.substr(4, 2));
  3309. if (value.length == 9) {
  3310. if (year >= 1980) {
  3311. year -= 100;
  3312. }
  3313. if (year > 1953) {
  3314. return false;
  3315. }
  3316. } else if (year < 1954) {
  3317. year += 100;
  3318. }
  3319. if (!$.fn.bootstrapValidator.helpers.date(year, month, day)) {
  3320. return false;
  3321. }
  3322. // Check that the birth date is not in the future
  3323. if (value.length == 10) {
  3324. var check = parseInt(value.substr(0, 9), 10) % 11;
  3325. if (year < 1985) {
  3326. check = check % 10;
  3327. }
  3328. return (check == value.substr(9, 1));
  3329. }
  3330. return true;
  3331. }
  3332. return false;
  3333. },
  3334. /**
  3335. * Validate German VAT number
  3336. * Examples:
  3337. * - Valid: DE136695976
  3338. * - Invalid: DE136695978
  3339. *
  3340. * @param {String} value VAT number
  3341. * @returns {Boolean}
  3342. */
  3343. _de: function(value) {
  3344. if (!/^DE[0-9]{9}$/.test(value)) {
  3345. return false;
  3346. }
  3347. value = value.substr(2);
  3348. return $.fn.bootstrapValidator.helpers.mod_11_10(value);
  3349. },
  3350. /**
  3351. * Validate Danish VAT number
  3352. * Example:
  3353. * - Valid: DK13585628
  3354. * - Invalid: DK13585627
  3355. *
  3356. * @param {String} value VAT number
  3357. * @returns {Boolean}
  3358. */
  3359. _dk: function(value) {
  3360. if (!/^DK[0-9]{8}$/.test(value)) {
  3361. return false;
  3362. }
  3363. value = value.substr(2);
  3364. var sum = 0,
  3365. weight = [2, 7, 6, 5, 4, 3, 2, 1];
  3366. for (var i = 0; i < 8; i++) {
  3367. sum += parseInt(value.charAt(i), 10) * weight[i];
  3368. }
  3369. return (sum % 11 == 0);
  3370. },
  3371. /**
  3372. * Validate Estonian VAT number
  3373. * Examples:
  3374. * - Valid: EE100931558, EE100594102
  3375. * - Invalid: EE100594103
  3376. *
  3377. * @param {String} value VAT number
  3378. * @returns {Boolean}
  3379. */
  3380. _ee: function(value) {
  3381. if (!/^EE[0-9]{9}$/.test(value)) {
  3382. return false;
  3383. }
  3384. value = value.substr(2);
  3385. var sum = 0,
  3386. weight = [3, 7, 1, 3, 7, 1, 3, 7, 1];
  3387. for (var i = 0; i < 9; i++) {
  3388. sum += parseInt(value.charAt(i)) * weight[i];
  3389. }
  3390. return (sum % 10 == 0);
  3391. },
  3392. /**
  3393. * Validate Spanish VAT number (NIF - Número de Identificación Fiscal)
  3394. * Can be:
  3395. * i) DNI (Documento nacional de identidad), for Spaniards
  3396. * ii) NIE (Número de Identificación de Extranjeros), for foreigners
  3397. * iii) CIF (Certificado de Identificación Fiscal), for legal entities and others
  3398. *
  3399. * Examples:
  3400. * - Valid: i) ES54362315K; ii) ESX2482300W, ESX5253868R; iii) ESM1234567L, ESJ99216582, ESB58378431, ESB64717838
  3401. * - Invalid: i) ES54362315Z; ii) ESX2482300A; iii) ESJ99216583
  3402. *
  3403. * @param {String} value VAT number
  3404. * @returns {Boolean}
  3405. */
  3406. _es: function(value) {
  3407. if (!/^ES[0-9A-Z][0-9]{7}[0-9A-Z]$/.test(value)) {
  3408. return false;
  3409. }
  3410. value = value.substr(2);
  3411. var dni = function(value) {
  3412. var check = parseInt(value.substr(0, 8), 10);
  3413. check = 'TRWAGMYFPDXBNJZSQVHLCKE'[check % 23];
  3414. return (check == value.substr(8, 1));
  3415. },
  3416. nie = function(value) {
  3417. var check = ['XYZ'.indexOf(value.charAt(0)), value.substr(1)].join('');
  3418. check = parseInt(check, 10);
  3419. check = 'TRWAGMYFPDXBNJZSQVHLCKE'[check % 23];
  3420. return (check == value.substr(8, 1));
  3421. },
  3422. cif = function(value) {
  3423. var first = value.charAt(0), check;
  3424. if ('KLM'.indexOf(first) != -1) {
  3425. // K: Spanish younger than 14 year old
  3426. // L: Spanish living outside Spain without DNI
  3427. // M: Granted the tax to foreigners who have no NIE
  3428. check = parseInt(value.substr(1, 8), 10);
  3429. check = 'TRWAGMYFPDXBNJZSQVHLCKE'[check % 23];
  3430. return (check == value.substr(8, 1));
  3431. } else if ('ABCDEFGHJNPQRSUVW'.indexOf(first) != -1) {
  3432. var sum = 0,
  3433. weight = [2, 1, 2, 1, 2, 1, 2],
  3434. temp = 0;
  3435. for (var i = 0; i < 7; i++) {
  3436. temp = parseInt(value.charAt(i + 1)) * weight[i];
  3437. if (temp > 9) {
  3438. temp = Math.floor(temp / 10) + temp % 10;
  3439. }
  3440. sum += temp;
  3441. }
  3442. sum = 10 - sum % 10;
  3443. return (sum == value.substr(8, 1) || 'JABCDEFGHI'[sum] == value.substr(8, 1));
  3444. }
  3445. return false;
  3446. };
  3447. var first = value.charAt(0);
  3448. if (/^[0-9]$/.test(first)) {
  3449. return dni(value);
  3450. } else if (/^[XYZ]$/.test(first)) {
  3451. return nie(value);
  3452. } else {
  3453. return cif(value);
  3454. }
  3455. },
  3456. /**
  3457. * Validate Finnish VAT number
  3458. * Examples:
  3459. * - Valid: FI20774740
  3460. * - Invalid: FI20774741
  3461. *
  3462. * @param {String} value VAT number
  3463. * @returns {Boolean}
  3464. */
  3465. _fi: function(value) {
  3466. if (!/^FI[0-9]{8}$/.test(value)) {
  3467. return false;
  3468. }
  3469. value = value.substr(2);
  3470. var sum = 0,
  3471. weight = [7, 9, 10, 5, 8, 4, 2, 1];
  3472. for (var i = 0; i < 8; i++) {
  3473. sum += parseInt(value.charAt(i)) * weight[i];
  3474. }
  3475. return (sum % 11 == 0);
  3476. },
  3477. /**
  3478. * Validate French VAT number (TVA - taxe sur la valeur ajoutée)
  3479. * It's constructed by a SIREN number, prefixed by two characters.
  3480. *
  3481. * Examples:
  3482. * - Valid: FR40303265045, FR23334175221, FRK7399859412, FR4Z123456782
  3483. * - Invalid: FR84323140391
  3484. *
  3485. * @param {String} value VAT number
  3486. * @returns {Boolean}
  3487. */
  3488. _fr: function(value) {
  3489. if (!/^FR[0-9A-Z]{2}[0-9]{9}$/.test(value)) {
  3490. return false;
  3491. }
  3492. value = value.substr(2);
  3493. if (!$.fn.bootstrapValidator.helpers.luhn(value.substr(2))) {
  3494. return false;
  3495. }
  3496. if (/^[0-9]{2}$/.test(value.substr(0, 2))) {
  3497. // First two characters are digits
  3498. return value.substr(0, 2) == (parseInt(value.substr(2) + '12', 10) % 97);
  3499. } else {
  3500. // The first characters cann't be O and I
  3501. var alphabet = '0123456789ABCDEFGHJKLMNPQRSTUVWXYZ',
  3502. check;
  3503. // First one is digit
  3504. if (/^[0-9]{1}$/.test(value.charAt(0))) {
  3505. check = alphabet.indexOf(value.charAt(0)) * 24 + alphabet.indexOf(value.charAt(1)) - 10;
  3506. } else {
  3507. check = alphabet.indexOf(value.charAt(0)) * 34 + alphabet.indexOf(value.charAt(1)) - 100;
  3508. }
  3509. return ((parseInt(value.substr(2), 10) + 1 + Math.floor(check / 11)) % 11) == (check % 11);
  3510. }
  3511. },
  3512. /**
  3513. * Validate United Kingdom VAT number
  3514. * Example:
  3515. * - Valid: GB980780684
  3516. * - Invalid: GB802311781
  3517. *
  3518. * @param {String} value VAT number
  3519. * @returns {Boolean}
  3520. */
  3521. _gb: function(value) {
  3522. if (!/^GB[0-9]{9}$/.test(value) // Standard
  3523. && !/^GB[0-9]{12}$/.test(value) // Branches
  3524. && !/^GBGD[0-9]{3}$/.test(value) // Government department
  3525. && !/^GBHA[0-9]{3}$/.test(value) // Health authority
  3526. && !/^GB(GD|HA)8888[0-9]{5}$/.test(value))
  3527. {
  3528. return false;
  3529. }
  3530. value = value.substr(2);
  3531. var length = value.length;
  3532. if (length == 5) {
  3533. var firstTwo = value.substr(0, 2),
  3534. lastThree = parseInt(value.substr(2));
  3535. return ('GD' == firstTwo && lastThree < 500) || ('HA' == firstTwo && lastThree >= 500);
  3536. } else if (length == 11 && ('GD8888' == value.substr(0, 6) || 'HA8888' == value.substr(0, 6))) {
  3537. if (('GD' == value.substr(0, 2) && parseInt(value.substr(6, 3)) >= 500)
  3538. || ('HA' == value.substr(0, 2) && parseInt(value.substr(6, 3)) < 500))
  3539. {
  3540. return false;
  3541. }
  3542. return (parseInt(value.substr(6, 3)) % 97 == parseInt(value.substr(9, 2)));
  3543. } else if (length == 9 || length == 12) {
  3544. var sum = 0,
  3545. weight = [8, 7, 6, 5, 4, 3, 2, 10, 1];
  3546. for (var i = 0; i < 9; i++) {
  3547. sum += parseInt(value.charAt(i)) * weight[i];
  3548. }
  3549. sum = sum % 97;
  3550. if (parseInt(value.substr(0, 3)) >= 100) {
  3551. return (sum == 0 || sum == 42 || sum == 55);
  3552. } else {
  3553. return (sum == 0);
  3554. }
  3555. }
  3556. return true;
  3557. },
  3558. /**
  3559. * Validate Greek VAT number
  3560. * Examples:
  3561. * - Valid: GR023456780, EL094259216
  3562. * - Invalid: EL123456781
  3563. *
  3564. * @param {String} value VAT number
  3565. * @returns {Boolean}
  3566. */
  3567. _gr: function(value) {
  3568. if (!/^GR[0-9]{9}$/.test(value)) {
  3569. return false;
  3570. }
  3571. value = value.substr(2);
  3572. if (value.length == 8) {
  3573. value = '0' + value;
  3574. }
  3575. var sum = 0,
  3576. weight = [256, 128, 64, 32, 16, 8, 4, 2];
  3577. for (var i = 0; i < 8; i++) {
  3578. sum += parseInt(value.charAt(i)) * weight[i];
  3579. }
  3580. sum = (sum % 11) % 10;
  3581. return (sum == value.substr(8, 1));
  3582. },
  3583. // EL is traditionally prefix of Greek VAT numbers
  3584. _el: function(value) {
  3585. if (!/^EL[0-9]{9}$/.test(value)) {
  3586. return false;
  3587. }
  3588. value = 'GR' + value.substr(2);
  3589. return this._gr(value);
  3590. },
  3591. /**
  3592. * Validate Hungarian VAT number
  3593. * Examples:
  3594. * - Valid: HU12892312
  3595. * - Invalid: HU12892313
  3596. *
  3597. * @param {String} value VAT number
  3598. * @returns {Boolean}
  3599. */
  3600. _hu: function(value) {
  3601. if (!/^HU[0-9]{8}$/.test(value)) {
  3602. return false;
  3603. }
  3604. value = value.substr(2);
  3605. var sum = 0,
  3606. weight = [9, 7, 3, 1, 9, 7, 3, 1];
  3607. for (var i = 0; i < 8; i++) {
  3608. sum += parseInt(value.charAt(i)) * weight[i];
  3609. }
  3610. return (sum % 10 == 0);
  3611. },
  3612. /**
  3613. * Validate Croatian VAT number
  3614. * Examples:
  3615. * - Valid: HR33392005961
  3616. * - Invalid: HR33392005962
  3617. *
  3618. * @param {String} value VAT number
  3619. * @returns {Boolean}
  3620. */
  3621. _hr: function(value) {
  3622. if (!/^HR[0-9]{11}$/.test(value)) {
  3623. return false;
  3624. }
  3625. value = value.substr(2);
  3626. return $.fn.bootstrapValidator.helpers.mod_11_10(value);
  3627. },
  3628. /**
  3629. * Validate Irish VAT number
  3630. * Examples:
  3631. * - Valid: IE6433435F, IE6433435OA, IE8D79739I
  3632. * - Invalid: IE8D79738J
  3633. *
  3634. * @param {String} value VAT number
  3635. * @returns {Boolean}
  3636. */
  3637. _ie: function(value) {
  3638. if (!/^IE[0-9]{1}[0-9A-Z\*\+]{1}[0-9]{5}[A-Z]{1,2}$/.test(value)) {
  3639. return false;
  3640. }
  3641. value = value.substr(2);
  3642. var getCheckDigit = function(value) {
  3643. while (value.length < 7) {
  3644. value = '0' + value;
  3645. }
  3646. var alphabet = 'WABCDEFGHIJKLMNOPQRSTUV',
  3647. sum = 0;
  3648. for (var i = 0; i < 7; i++) {
  3649. sum += parseInt(value.charAt(i)) * (8 - i);
  3650. }
  3651. sum += 9 * alphabet.indexOf(value.substr(7));
  3652. return alphabet[sum % 23];
  3653. };
  3654. // The first 7 characters are digits
  3655. if (/^[0-9]+$/.test(value.substr(0, 7))) {
  3656. // New system
  3657. return value.charAt(7) == getCheckDigit(value.substr(0, 7) + value.substr(8) + '');
  3658. } else if ('ABCDEFGHIJKLMNOPQRSTUVWXYZ+*'.indexOf(value.charAt(1)) != -1) {
  3659. // Old system
  3660. return value.charAt(7) == getCheckDigit(value.substr(2, 5) + value.substr(0, 1) + '');
  3661. }
  3662. return true;
  3663. },
  3664. /**
  3665. * Validate Italian VAT number, which consists of 11 digits.
  3666. * - First 7 digits are a company identifier
  3667. * - Next 3 are the province of residence
  3668. * - The last one is a check digit
  3669. *
  3670. * Examples:
  3671. * - Valid: IT00743110157
  3672. * - Invalid: IT00743110158
  3673. *
  3674. * @param {String} value VAT number
  3675. * @returns {Boolean}
  3676. */
  3677. _it: function(value) {
  3678. if (!/^IT[0-9]{11}$/.test(value)) {
  3679. return false;
  3680. }
  3681. value = value.substr(2);
  3682. if (parseInt(value.substr(0, 7)) == 0) {
  3683. return false;
  3684. }
  3685. var lastThree = parseInt(value.substr(7, 3));
  3686. if ((lastThree < 1) || (lastThree > 201) && lastThree != 999 && lastThree != 888) {
  3687. return false;
  3688. }
  3689. return $.fn.bootstrapValidator.helpers.luhn(value);
  3690. },
  3691. /**
  3692. * Validate Lithuanian VAT number
  3693. * It can be:
  3694. * - 9 digits, for legal entities
  3695. * - 12 digits, for temporarily registered taxpayers
  3696. *
  3697. * Examples:
  3698. * - Valid: LT119511515, LT100001919017, LT100004801610
  3699. * - Invalid: LT100001919018
  3700. *
  3701. * @param {String} value VAT number
  3702. * @returns {Boolean}
  3703. */
  3704. _lt: function(value) {
  3705. if (!/^LT([0-9]{7}1[0-9]{1}|[0-9]{10}1[0-9]{1})$/.test(value)) {
  3706. return false;
  3707. }
  3708. value = value.substr(2);
  3709. var length = value.length,
  3710. sum = 0;
  3711. for (var i = 0; i < length - 1; i++) {
  3712. sum += parseInt(value.charAt(i)) * (1 + i % 9);
  3713. }
  3714. var check = sum % 11;
  3715. if (check == 10) {
  3716. sum = 0;
  3717. for (var i = 0; i < length - 1; i++) {
  3718. sum += parseInt(value.charAt(i)) * (1 + (i + 2) % 9);
  3719. }
  3720. }
  3721. check = check % 11 % 10;
  3722. return (check == value.charAt(length - 1));
  3723. },
  3724. /**
  3725. * Validate Luxembourg VAT number
  3726. * Examples:
  3727. * - Valid: LU15027442
  3728. * - Invalid: LU15027443
  3729. *
  3730. * @param {String} value VAT number
  3731. * @returns {Boolean}
  3732. */
  3733. _lu: function(value) {
  3734. if (!/^LU[0-9]{8}$/.test(value)) {
  3735. return false;
  3736. }
  3737. value = value.substr(2);
  3738. return (value.substr(0, 6) % 89 == value.substr(6, 2));
  3739. },
  3740. /**
  3741. * Validate Latvian VAT number
  3742. * Examples:
  3743. * - Valid: LV40003521600, LV16117519997
  3744. * - Invalid: LV40003521601, LV16137519997
  3745. *
  3746. * @param {String} value VAT number
  3747. * @returns {Boolean}
  3748. */
  3749. _lv: function(value) {
  3750. if (!/^LV[0-9]{11}$/.test(value)) {
  3751. return false;
  3752. }
  3753. value = value.substr(2);
  3754. var first = parseInt(value.charAt(0)),
  3755. sum = 0,
  3756. weight = [],
  3757. i = 0,
  3758. length = value.length;
  3759. if (first > 3) {
  3760. // Legal entity
  3761. sum = 0;
  3762. weight = [9, 1, 4, 8, 3, 10, 2, 5, 7, 6, 1];
  3763. for (i = 0; i < length; i++) {
  3764. sum += parseInt(value.charAt(i)) * weight[i];
  3765. }
  3766. sum = sum % 11;
  3767. return (sum == 3);
  3768. } else {
  3769. // Check birth date
  3770. var day = parseInt(value.substr(0, 2)),
  3771. month = parseInt(value.substr(2, 2)),
  3772. year = parseInt(value.substr(4, 2));
  3773. year = year + 1800 + parseInt(value.charAt(6)) * 100;
  3774. if (!$.fn.bootstrapValidator.helpers.date(year, month, day)) {
  3775. return false;
  3776. }
  3777. // Check personal code
  3778. sum = 0;
  3779. weight = [10, 5, 8, 4, 2, 1, 6, 3, 7, 9];
  3780. for (i = 0; i < length - 1; i++) {
  3781. sum += parseInt(value.charAt(i)) * weight[i];
  3782. }
  3783. sum = (sum + 1) % 11 % 10;
  3784. return (sum == value.charAt(length - 1));
  3785. }
  3786. return true;
  3787. },
  3788. /**
  3789. * Validate Maltese VAT number
  3790. * Examples:
  3791. * - Valid: MT11679112
  3792. * - Invalid: MT11679113
  3793. *
  3794. * @param {String} value VAT number
  3795. * @returns {Boolean}
  3796. */
  3797. _mt: function(value) {
  3798. if (!/^MT[0-9]{8}$/.test(value)) {
  3799. return false;
  3800. }
  3801. value = value.substr(2);
  3802. var sum = 0,
  3803. weight = [3, 4, 6, 7, 8, 9, 10, 1];
  3804. for (var i = 0; i < 8; i++) {
  3805. sum += parseInt(value.charAt(i)) * weight[i];
  3806. }
  3807. return (sum % 37 == 0);
  3808. },
  3809. /**
  3810. * Validate Dutch VAT number
  3811. * Examples:
  3812. * - Valid: NL004495445B01
  3813. * - Invalid: NL123456789B90
  3814. *
  3815. * @param {String} value VAT number
  3816. * @returns {Boolean}
  3817. */
  3818. _nl: function(value) {
  3819. if (!/^NL[0-9]{9}B[0-9]{2}$/.test(value)) {
  3820. return false;
  3821. }
  3822. value = value.substr(2);
  3823. var sum = 0,
  3824. weight = [9, 8, 7, 6, 5, 4, 3, 2];
  3825. for (var i = 0; i < 8; i++) {
  3826. sum += parseInt(value.charAt(i)) * weight[i];
  3827. }
  3828. sum = sum % 11;
  3829. if (sum > 9) {
  3830. sum = 0;
  3831. }
  3832. return (sum == value.substr(8, 1));
  3833. },
  3834. /**
  3835. * Validate Norwegian VAT number
  3836. *
  3837. * @see http://www.brreg.no/english/coordination/number.html
  3838. * @param {String} value VAT number
  3839. * @returns {Boolean}
  3840. */
  3841. _no: function(value) {
  3842. if (!/^NO[0-9]{9}$/.test(value)) {
  3843. return false;
  3844. }
  3845. value = value.substr(2);
  3846. var sum = 0,
  3847. weight = [3, 2, 7, 6, 5, 4, 3, 2];
  3848. for (var i = 0; i < 8; i++) {
  3849. sum += parseInt(value.charAt(i)) * weight[i];
  3850. }
  3851. sum = 11 - sum % 11;
  3852. if (sum == 11) {
  3853. sum = 0;
  3854. }
  3855. return (sum == value.substr(8, 1));
  3856. },
  3857. /**
  3858. * Validate Polish VAT number
  3859. * Examples:
  3860. * - Valid: PL8567346215
  3861. * - Invalid: PL8567346216
  3862. *
  3863. * @param {String} value VAT number
  3864. * @returns {Boolean}
  3865. */
  3866. _pl: function(value) {
  3867. if (!/^PL[0-9]{10}$/.test(value)) {
  3868. return false;
  3869. }
  3870. value = value.substr(2);
  3871. var sum = 0,
  3872. weight = [6, 5, 7, 2, 3, 4, 5, 6, 7, -1];
  3873. for (var i = 0; i < 10; i++) {
  3874. sum += parseInt(value.charAt(i)) * weight[i];
  3875. }
  3876. return (sum % 11 == 0);
  3877. },
  3878. /**
  3879. * Validate Portuguese VAT number
  3880. * Examples:
  3881. * - Valid: PT501964843
  3882. * - Invalid: PT501964842
  3883. *
  3884. * @param {String} value VAT number
  3885. * @returns {Boolean}
  3886. */
  3887. _pt: function(value) {
  3888. if (!/^PT[0-9]{9}$/.test(value)) {
  3889. return false;
  3890. }
  3891. value = value.substr(2);
  3892. var sum = 0,
  3893. weight = [9, 8, 7, 6, 5, 4, 3, 2];
  3894. for (var i = 0; i < 8; i++) {
  3895. sum += parseInt(value.charAt(i)) * weight[i];
  3896. }
  3897. sum = 11 - sum % 11;
  3898. if (sum > 9) {
  3899. sum = 0;
  3900. }
  3901. return (sum == value.substr(8, 1));
  3902. },
  3903. /**
  3904. * Validate Romanian VAT number
  3905. * Examples:
  3906. * - Valid: RO18547290
  3907. * - Invalid: RO18547291
  3908. *
  3909. * @param {String} value VAT number
  3910. * @returns {Boolean}
  3911. */
  3912. _ro: function(value) {
  3913. if (!/^RO[1-9][0-9]{1,9}$/.test(value)) {
  3914. return false;
  3915. }
  3916. value = value.substr(2);
  3917. var length = value.length,
  3918. weight = [7, 5, 3, 2, 1, 7, 5, 3, 2].slice(10 - length),
  3919. sum = 0;
  3920. for (var i = 0; i < length - 1; i++) {
  3921. sum += parseInt(value.charAt(i)) * weight[i];
  3922. }
  3923. sum = (10 * sum) % 11 % 10;
  3924. return (sum == value.substr(length - 1, 1));
  3925. },
  3926. /**
  3927. * Validate Russian VAT number (Taxpayer Identification Number - INN)
  3928. *
  3929. * @param {String} value VAT number
  3930. * @returns {Boolean}
  3931. */
  3932. _ru: function(value) {
  3933. if (!/^RU([0-9]{9}|[0-9]{12})$/.test(value)) {
  3934. return false;
  3935. }
  3936. value = value.substr(2);
  3937. if (value.length == 10) {
  3938. var sum = 0,
  3939. weight = [2, 4, 10, 3, 5, 9, 4, 6, 8, 0];
  3940. for (var i = 0; i < 10; i++) {
  3941. sum += parseInt(value.charAt(i)) * weight[i];
  3942. }
  3943. sum = sum % 11;
  3944. if (sum > 9) {
  3945. sum = sum % 10;
  3946. }
  3947. return (sum == value.substr(9, 1));
  3948. } else if (value.length == 12) {
  3949. var sum1 = 0,
  3950. weight1 = [7, 2, 4, 10, 3, 5, 9, 4, 6, 8, 0],
  3951. sum2 = 0,
  3952. weight2 = [3, 7, 2, 4, 10, 3, 5, 9, 4, 6, 8, 0];
  3953. for (var i = 0; i < 11; i++) {
  3954. sum1 += parseInt(value.charAt(i)) * weight1[i];
  3955. sum2 += parseInt(value.charAt(i)) * weight2[i];
  3956. }
  3957. sum1 = sum1 % 11;
  3958. if (sum1 > 9) {
  3959. sum1 = sum1 % 10;
  3960. }
  3961. sum2 = sum2 % 11;
  3962. if (sum2 > 9) {
  3963. sum2 = sum2 % 10;
  3964. }
  3965. return (sum1 == value.substr(10, 1) && sum2 == value.substr(11, 1));
  3966. }
  3967. return false;
  3968. },
  3969. /**
  3970. * Validate Serbian VAT number
  3971. *
  3972. * @param {String} value VAT number
  3973. * @returns {Boolean}
  3974. */
  3975. _rs: function(value) {
  3976. if (!/^RS[0-9]{9}$/.test(value)) {
  3977. return false;
  3978. }
  3979. value = value.substr(2);
  3980. var sum = 10,
  3981. temp = 0;
  3982. for (var i = 0; i < 8; i++) {
  3983. temp = (parseInt(value.charAt(i)) + sum) % 10;
  3984. if (temp == 0) {
  3985. temp = 10;
  3986. }
  3987. sum = (2 * temp) % 11;
  3988. }
  3989. return ((sum + parseInt(value.substr(8, 1))) % 10 == 1);
  3990. },
  3991. /**
  3992. * Validate Swedish VAT number
  3993. * Examples:
  3994. * - Valid: SE123456789701
  3995. * - Invalid: SE123456789101
  3996. *
  3997. * @param {String} value VAT number
  3998. * @returns {Boolean}
  3999. */
  4000. _se: function(value) {
  4001. if (!/^SE[0-9]{10}01$/.test(value)) {
  4002. return false;
  4003. }
  4004. value = value.substr(2, 10);
  4005. return $.fn.bootstrapValidator.helpers.luhn(value);
  4006. },
  4007. /**
  4008. * Validate Slovenian VAT number
  4009. * Examples:
  4010. * - Valid: SI50223054
  4011. * - Invalid: SI50223055
  4012. *
  4013. * @param {String} value VAT number
  4014. * @returns {Boolean}
  4015. */
  4016. _si: function(value) {
  4017. if (!/^SI[0-9]{8}$/.test(value)) {
  4018. return false;
  4019. }
  4020. value = value.substr(2);
  4021. var sum = 0,
  4022. weight = [8, 7, 6, 5, 4, 3, 2];
  4023. for (var i = 0; i < 7; i++) {
  4024. sum += parseInt(value.charAt(i)) * weight[i];
  4025. }
  4026. sum = 11 - sum % 11;
  4027. if (sum == 10) {
  4028. sum = 0;
  4029. }
  4030. return (sum == value.substr(7, 1));
  4031. },
  4032. /**
  4033. * Validate Slovak VAT number
  4034. * Examples:
  4035. * - Valid: SK2022749619
  4036. * - Invalid: SK2022749618
  4037. *
  4038. * @param {String} value VAT number
  4039. * @returns {Boolean}
  4040. */
  4041. _sk: function(value) {
  4042. if (!/^SK[1-9][0-9][(2-4)|(6-9)][0-9]{7}$/.test(value)) {
  4043. return false;
  4044. }
  4045. value = value.substr(2);
  4046. return (value % 11 == 0);
  4047. }
  4048. };
  4049. }(window.jQuery));
  4050. ;(function($) {
  4051. $.fn.bootstrapValidator.validators.vin = {
  4052. /**
  4053. * Validate an US VIN (Vehicle Identification Number)
  4054. *
  4055. * @param {BootstrapValidator} validator The validator plugin instance
  4056. * @param {jQuery} $field Field element
  4057. * @param {Object} options Consist of key:
  4058. * - message: The invalid message
  4059. * @returns {Boolean}
  4060. */
  4061. validate: function(validator, $field, options) {
  4062. var value = $field.val();
  4063. if (value == '') {
  4064. return true;
  4065. }
  4066. // Don't accept I, O, Q characters
  4067. if (!/^[a-hj-npr-z0-9]{8}[0-9xX][a-hj-npr-z0-9]{8}$/i.test(value)) {
  4068. return false;
  4069. }
  4070. value = value.toUpperCase();
  4071. var chars = {
  4072. A: 1, B: 2, C: 3, D: 4, E: 5, F: 6, G: 7, H: 8,
  4073. J: 1, K: 2, L: 3, M: 4, N: 5, P: 7, R: 9,
  4074. S: 2, T: 3, U: 4, V: 5, W: 6, X: 7, Y: 8, Z: 9,
  4075. '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '0': 0
  4076. },
  4077. weights = [8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2],
  4078. sum = 0,
  4079. length = value.length;
  4080. for (var i = 0; i < length; i++) {
  4081. sum += chars[value.charAt(i) + ''] * weights[i];
  4082. }
  4083. var reminder = sum % 11;
  4084. if (reminder == 10) {
  4085. reminder = 'X';
  4086. }
  4087. return reminder == value.charAt(8);
  4088. }
  4089. };
  4090. }(window.jQuery));
  4091. ;(function($) {
  4092. $.fn.bootstrapValidator.validators.zipCode = {
  4093. html5Attributes: {
  4094. message: 'message',
  4095. country: 'country'
  4096. },
  4097. /**
  4098. * Return true if and only if the input value is a valid country zip code
  4099. *
  4100. * @param {BootstrapValidator} validator The validator plugin instance
  4101. * @param {jQuery} $field Field element
  4102. * @param {Object} options Consist of key:
  4103. * - message: The invalid message
  4104. * - country: The ISO 3166 country code
  4105. *
  4106. * Currently it supports the following countries:
  4107. * - US (United State)
  4108. * - CA (Canada)
  4109. * - DK (Denmark)
  4110. * - GB (United Kingdom)
  4111. * - IT (Italy)
  4112. * - NL (Netherlands)
  4113. * - SE (Sweden)
  4114. * @returns {Boolean}
  4115. */
  4116. validate: function(validator, $field, options) {
  4117. var value = $field.val();
  4118. if (value == '' || !options.country) {
  4119. return true;
  4120. }
  4121. var country = (options.country || 'US').toUpperCase();
  4122. switch (country) {
  4123. case 'CA': 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);
  4124. case 'DK': return /^(DK(-|\s)?)?\d{4}$/i.test(value);
  4125. case 'GB': return this._gb(value);
  4126. // http://en.wikipedia.org/wiki/List_of_postal_codes_in_Italy
  4127. case 'IT': return /^(I-|IT-)?\d{5}$/i.test(value);
  4128. // http://en.wikipedia.org/wiki/Postal_codes_in_the_Netherlands
  4129. case 'NL': return /^[1-9][0-9]{3} ?(?!sa|sd|ss)[a-z]{2}$/i.test(value);
  4130. case 'SE': return /^(S-)?\d{3}\s?\d{2}$/i.test(value);
  4131. case 'US':
  4132. default: return /^\d{4,5}([\-]\d{4})?$/.test(value);
  4133. }
  4134. },
  4135. /**
  4136. * Validate United Kingdom postcode
  4137. * Examples:
  4138. * - Standard: EC1A 1BB, W1A 1HQ, M1 1AA, B33 8TH, CR2 6XH, DN55 1PT
  4139. * - Special cases:
  4140. * AI-2640, ASCN 1ZZ, GIR 0AA
  4141. *
  4142. * @see http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
  4143. * @param {String} value The postcode
  4144. * @returns {Boolean}
  4145. */
  4146. _gb: function(value) {
  4147. var firstChar = '[ABCDEFGHIJKLMNOPRSTUWYZ]', // Does not accept QVX
  4148. secondChar = '[ABCDEFGHKLMNOPQRSTUVWXY]', // Does not accept IJZ
  4149. thirdChar = '[ABCDEFGHJKPMNRSTUVWXY]',
  4150. fourthChar = '[ABEHMNPRVWXY]',
  4151. fifthChar = '[ABDEFGHJLNPQRSTUWXYZ]',
  4152. regexps = [
  4153. // AN NAA, ANN NAA, AAN NAA, AANN NAA format
  4154. new RegExp('^(' + firstChar + '{1}' + secondChar + '?[0-9]{1,2})(\\s*)([0-9]{1}' + fifthChar + '{2})$', 'i'),
  4155. // ANA NAA
  4156. new RegExp('^(' + firstChar + '{1}[0-9]{1}' + thirdChar + '{1})(\\s*)([0-9]{1}' + fifthChar + '{2})$', 'i'),
  4157. // AANA NAA
  4158. new RegExp('^(' + firstChar + '{1}' + secondChar + '{1}?[0-9]{1}' + fourthChar + '{1})(\\s*)([0-9]{1}' + fifthChar + '{2})$', 'i'),
  4159. new RegExp('^(BF1)(\\s*)([0-6]{1}[ABDEFGHJLNPQRST]{1}[ABDEFGHJLNPQRSTUWZYZ]{1})$', 'i'), // BFPO postcodes
  4160. /^(GIR)(\s*)(0AA)$/i, // Special postcode GIR 0AA
  4161. /^(BFPO)(\s*)([0-9]{1,4})$/i, // Standard BFPO numbers
  4162. /^(BFPO)(\s*)(c\/o\s*[0-9]{1,3})$/i, // c/o BFPO numbers
  4163. /^([A-Z]{4})(\s*)(1ZZ)$/i, // Overseas Territories
  4164. /^(AI-2640)$/i // Anguilla
  4165. ];
  4166. for (var i = 0; i < regexps.length; i++) {
  4167. if (regexps[i].test(value)) {
  4168. return true;
  4169. }
  4170. }
  4171. return false;
  4172. }
  4173. };
  4174. }(window.jQuery));