bootstrapValidator.js 275 KB

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