bootstrapValidator.js 237 KB

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