bootstrapValidator.js 188 KB

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