bootstrapValidator.js 264 KB

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