spec.js 231 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543
  1. describe('api', function() {
  2. // Override the options
  3. $.extend($.fn.bootstrapValidator.DEFAULT_OPTIONS, {
  4. feedbackIcons: {
  5. valid: 'glyphicon glyphicon-ok',
  6. invalid: 'glyphicon glyphicon-remove',
  7. validating: 'glyphicon glyphicon-refresh'
  8. }
  9. });
  10. beforeEach(function() {
  11. $([
  12. '<form class="form-horizontal" id="apiForm">',
  13. '<div class="form-group">',
  14. '<input type="text" name="username" data-bv-notempty data-bv-stringlength data-bv-stringlength-min="8" />',
  15. '</div>',
  16. '<div class="form-group">',
  17. '<input type="text" name="email" data-bv-notempty data-bv-emailaddress />',
  18. '</div>',
  19. '</form>'
  20. ].join('\n')).appendTo('body');
  21. $('#apiForm').bootstrapValidator();
  22. this.bv = $('#apiForm').data('bootstrapValidator');
  23. this.$email = this.bv.getFieldElements('email');
  24. });
  25. afterEach(function() {
  26. $('#apiForm').bootstrapValidator('destroy').remove();
  27. });
  28. it('revalidateField()', function() {
  29. this.$email.val('email@domain.com');
  30. this.bv.validate();
  31. expect(this.bv.isValidField('email')).toBeTruthy();
  32. this.$email.val('invalid#email.address');
  33. this.bv.revalidateField('email');
  34. expect(this.bv.isValidField(this.$email)).toEqual(false);
  35. });
  36. it('destroy()', function() {
  37. this.bv.destroy();
  38. expect($('#apiForm').data('bootstrapValidator')).toBeUndefined();
  39. expect($('#apiForm').find('i[data-bv-icon-for]').length).toEqual(0);
  40. expect($('#apiForm').find('.help-block[data-bv-for]').length).toEqual(0);
  41. expect($('#apiForm').find('.has-feedback').length).toEqual(0);
  42. expect($('#apiForm').find('.has-success').length).toEqual(0);
  43. expect($('#apiForm').find('.has-error').length).toEqual(0);
  44. expect($('#apiForm').find('[data-bv-field]').length).toEqual(0);
  45. });
  46. it('getOptions()', function() {
  47. // Form options
  48. expect(this.bv.getOptions().feedbackIcons.valid).toEqual('glyphicon glyphicon-ok');
  49. // Field options
  50. expect(this.bv.getOptions('username', 'stringlength')).toBeNull();
  51. expect(this.bv.getOptions('username', 'stringlength', 'min')).toBeNull();
  52. expect(this.bv.getOptions('username', 'stringLength')).toBeDefined();
  53. expect(this.bv.getOptions('username', 'stringLength', 'min')).toEqual('8');
  54. expect(this.bv.getOptions('username', 'stringlength', 'max')).toBeNull();
  55. });
  56. });
  57. describe('container form option', function() {
  58. beforeEach(function() {
  59. $([
  60. '<form id="containerForm" class="form-horizontal">',
  61. '<div class="form-group">',
  62. '<label class="col-lg-3 control-label">Full name</label>',
  63. '<div class="col-lg-4">',
  64. '<input type="text" class="form-control" name="firstName" required placeholder="First name" data-bv-notempty-message="The first name is required" />',
  65. '</div>',
  66. '<div class="col-lg-4">',
  67. '<input type="text" class="form-control" name="lastName" required placeholder="Last name" data-bv-notempty-message="The last name is required" />',
  68. '</div>',
  69. '</div>',
  70. '<div id="errors"></div>',
  71. '</form>'
  72. ].join('')).appendTo('body');
  73. });
  74. afterEach(function() {
  75. $('#containerForm').bootstrapValidator('destroy').remove();
  76. });
  77. it('form container declarative', function() {
  78. $('#containerForm')
  79. .attr('data-bv-container', '#errors')
  80. .bootstrapValidator();
  81. this.bv = $('#containerForm').data('bootstrapValidator');
  82. this.$firstName = this.bv.getFieldElements('firstName');
  83. this.$lastName = this.bv.getFieldElements('lastName');
  84. expect($('#errors').find('.help-block').length).toBeGreaterThan(0);
  85. this.$firstName.val('First');
  86. this.$lastName.val('');
  87. this.bv.validate();
  88. expect($('#errors').find('.help-block:visible[data-bv-for="firstName"]').length).toEqual(0);
  89. expect($('#errors').find('.help-block:visible[data-bv-for="lastName"]').length).toBeGreaterThan(0);
  90. });
  91. it('form container programmatically', function() {
  92. $('#containerForm').bootstrapValidator({
  93. container: '#errors'
  94. });
  95. this.bv = $('#containerForm').data('bootstrapValidator');
  96. this.$firstName = this.bv.getFieldElements('firstName');
  97. this.$lastName = this.bv.getFieldElements('lastName');
  98. expect($('#errors').find('.help-block').length).toBeGreaterThan(0);
  99. this.$firstName.val('');
  100. this.$lastName.val('Last');
  101. this.bv.validate();
  102. expect($('#errors').find('.help-block:visible[data-bv-for="firstName"]').length).toBeGreaterThan(0);
  103. expect($('#errors').find('.help-block:visible[data-bv-for="lastName"]').length).toEqual(0);
  104. this.bv.resetForm();
  105. this.$firstName.val('First');
  106. this.$lastName.val('Last');
  107. this.bv.validate();
  108. expect($('#errors').find('.help-block:visible').length).toEqual(0);
  109. });
  110. });
  111. describe('container field option', function() {
  112. beforeEach(function() {
  113. $([
  114. '<form id="containerForm" class="form-horizontal">',
  115. '<div class="form-group">',
  116. '<label class="col-lg-3 control-label">Full name</label>',
  117. '<div class="col-lg-4">',
  118. '<input type="text" class="form-control" name="firstName" required placeholder="First name" data-bv-notempty-message="The first name is required" data-bv-container="#firstNameMessage" />',
  119. '<span class="help-block" id="firstNameMessage" />',
  120. '</div>',
  121. '<div class="col-lg-4">',
  122. '<input type="text" class="form-control" name="lastName" required placeholder="Last name" data-bv-notempty-message="The last name is required" />',
  123. '<span class="help-block lastNameMessage" />',
  124. '</div>',
  125. '</div>',
  126. '</form>'
  127. ].join('')).appendTo('body');
  128. $('#containerForm').bootstrapValidator({
  129. feedbackIcons: {
  130. valid: 'glyphicon glyphicon-ok',
  131. invalid: 'glyphicon glyphicon-remove',
  132. validating: 'glyphicon glyphicon-refresh'
  133. },
  134. fields: {
  135. lastName: {
  136. container: '.lastNameMessage'
  137. }
  138. }
  139. });
  140. this.bv = $('#containerForm').data('bootstrapValidator');
  141. this.$firstName = this.bv.getFieldElements('firstName');
  142. this.$lastName = this.bv.getFieldElements('lastName');
  143. });
  144. afterEach(function() {
  145. $('#containerForm').bootstrapValidator('destroy').remove();
  146. });
  147. it('field container declarative', function() {
  148. expect($.trim($('#firstNameMessage').text())).toEqual('The first name is required');
  149. expect($.trim($('.lastNameMessage').text())).toEqual('The last name is required');
  150. });
  151. it('field container programmatically', function() {
  152. this.$firstName.val('First');
  153. this.$lastName.val('');
  154. this.bv.validate();
  155. expect($('#firstNameMessage').find('.help-block:visible').length).toEqual(0);
  156. expect($('.lastNameMessage').find('.help-block:visible').length).toBeGreaterThan(0);
  157. this.bv.resetForm();
  158. this.$firstName.val('');
  159. this.$lastName.val('Last');
  160. this.bv.validate();
  161. expect($('#firstNameMessage').find('.help-block:visible').length).toBeGreaterThan(0);
  162. expect($('.lastNameMessage').find('.help-block:visible').length).toEqual(0);
  163. });
  164. });
  165. describe('container tooltip/popover', function() {
  166. beforeEach(function() {
  167. $([
  168. '<form id="containerForm" class="form-horizontal">',
  169. '<div class="form-group">',
  170. '<label class="col-lg-3 control-label">Full name</label>',
  171. '<div class="col-lg-4">',
  172. '<input type="text" class="form-control" name="firstName" required placeholder="First name" data-bv-notempty-message="The first name is required" />',
  173. '</div>',
  174. '<div class="col-lg-4">',
  175. '<input type="text" class="form-control" name="lastName" required placeholder="Last name" data-bv-notempty-message="The last name is required" />',
  176. '</div>',
  177. '</div>',
  178. '<div id="errors"></div>',
  179. '</form>'
  180. ].join('')).appendTo('body');
  181. });
  182. afterEach(function() {
  183. $('#containerForm').bootstrapValidator('destroy').remove();
  184. });
  185. it('container declarative', function() {
  186. $('#containerForm')
  187. .attr('data-bv-container', 'tooltip')
  188. .find('[name="lastName"]')
  189. .attr('data-bv-container', 'popover')
  190. .end()
  191. .bootstrapValidator();
  192. this.bv = $('#containerForm').data('bootstrapValidator');
  193. this.$firstName = this.bv.getFieldElements('firstName');
  194. this.$lastName = this.bv.getFieldElements('lastName');
  195. this.bv.validate();
  196. expect(this.$firstName.parent().find('i').data('bs.tooltip')).toBeDefined();
  197. expect(this.$firstName.parent().find('i').data('bs.tooltip').type).toEqual('tooltip');
  198. expect(this.$lastName.parent().find('i').data('bs.popover')).toBeDefined();
  199. expect(this.$lastName.parent().find('i').data('bs.popover').type).toEqual('popover');
  200. this.bv.resetForm();
  201. this.$firstName.val('First');
  202. this.$lastName.val('Last');
  203. this.bv.validate();
  204. expect(this.$firstName.parent().find('i').data('bs.tooltip')).toBeDefined();
  205. expect(this.$lastName.parent().find('i').data('bs.popover')).toBeDefined();
  206. });
  207. it('container programmatically', function() {
  208. $('#containerForm').bootstrapValidator({
  209. container: 'tooltip',
  210. fields: {
  211. lastName: {
  212. container: 'popover'
  213. }
  214. }
  215. });
  216. this.bv = $('#containerForm').data('bootstrapValidator');
  217. this.$firstName = this.bv.getFieldElements('firstName');
  218. this.$lastName = this.bv.getFieldElements('lastName');
  219. this.bv.validate();
  220. expect(this.$firstName.parent().find('i').data('bs.tooltip')).toBeDefined();
  221. expect(this.$firstName.parent().find('i').data('bs.tooltip').type).toEqual('tooltip');
  222. expect(this.$lastName.parent().find('i').data('bs.popover')).toBeDefined();
  223. expect(this.$lastName.parent().find('i').data('bs.popover').type).toEqual('popover');
  224. this.bv.resetForm();
  225. this.$firstName.val('First');
  226. this.$lastName.val('Last');
  227. this.bv.validate();
  228. expect(this.$firstName.parent().find('i').data('bs.tooltip')).toBeDefined();
  229. expect(this.$lastName.parent().find('i').data('bs.popover')).toBeDefined();
  230. });
  231. });
  232. describe('dynamic fields', function() {
  233. beforeEach(function() {
  234. $([
  235. '<form class="form-horizontal" id="dynamicForm">',
  236. '<div class="form-group">',
  237. '<input type="text" name="fullName" class="form-control" />',
  238. '</div>',
  239. '</form>'
  240. ].join('\n')).appendTo('body');
  241. $('#dynamicForm').bootstrapValidator({
  242. fields: {
  243. fullName: {
  244. validators: {
  245. notEmpty: {
  246. message: 'The full name is required and cannot be empty'
  247. },
  248. stringLength: {
  249. min: 8,
  250. max: 40,
  251. message: 'The full name must be more than %s and less than %s characters long'
  252. },
  253. regexp: {
  254. enabled: false,
  255. regexp: /^[a-zA-Z\s]+$/,
  256. message: 'The full name can only consist of alphabetical, number, and space'
  257. }
  258. }
  259. },
  260. // #725: Note that the email field isn't available in the form yet
  261. email: {
  262. validators: {
  263. emailAddress: {
  264. message: 'The email address is not valid'
  265. }
  266. }
  267. }
  268. }
  269. });
  270. this.bv = $('#dynamicForm').data('bootstrapValidator');
  271. this.$fullName = this.bv.getFieldElements('fullName');
  272. });
  273. afterEach(function() {
  274. $('#dynamicForm').bootstrapValidator('destroy').remove();
  275. });
  276. // https://github.com/nghuuphuoc/bootstrapvalidator/pull/725
  277. it('adding field [does not exist but is already set in "fields" option]', function() {
  278. var $div = $('<div/>').addClass('form-group').appendTo($('#dynamicForm'));
  279. $email = $('<input/>')
  280. .attr('type', 'text')
  281. .addClass('form-control')
  282. .attr('name', 'email')
  283. .appendTo($div);
  284. this.bv.addField('email');
  285. this.$fullName.val('Phuoc Nguyen');
  286. $email.val('not valid@email');
  287. this.bv.validate();
  288. expect(this.bv.isValidField('email')).toBeFalsy();
  289. expect(this.bv.isValid()).toBeFalsy();
  290. this.bv.resetForm();
  291. $email.val('valid@email.com');
  292. this.bv.validate();
  293. expect(this.bv.isValidField('email')).toBeTruthy();
  294. expect(this.bv.isValid()).toBeTruthy();
  295. });
  296. });
  297. describe('enable validators', function() {
  298. beforeEach(function() {
  299. $([
  300. '<form class="form-horizontal" id="enableForm">',
  301. '<div class="form-group">',
  302. '<input type="text" name="fullName" class="form-control" />',
  303. '</div>',
  304. '</form>'
  305. ].join('\n')).appendTo('body');
  306. $('#enableForm').bootstrapValidator({
  307. fields: {
  308. fullName: {
  309. validators: {
  310. notEmpty: {
  311. message: 'The full name is required and cannot be empty'
  312. },
  313. stringLength: {
  314. min: 8,
  315. max: 40,
  316. message: 'The full name must be more than %s and less than %s characters long'
  317. },
  318. regexp: {
  319. enabled: false,
  320. regexp: /^[a-zA-Z\s]+$/,
  321. message: 'The full name can only consist of alphabetical, number, and space'
  322. }
  323. }
  324. }
  325. }
  326. });
  327. this.bv = $('#enableForm').data('bootstrapValidator');
  328. this.$fullName = this.bv.getFieldElements('fullName');
  329. });
  330. afterEach(function() {
  331. $('#enableForm').bootstrapValidator('destroy').remove();
  332. });
  333. it('enable all validators', function() {
  334. this.$fullName.val('@ $full N@m3');
  335. this.bv.validate();
  336. expect(this.bv.isValid()).toBeTruthy();
  337. this.bv.resetForm();
  338. this.$fullName.val('Contain#$@');
  339. this.bv.enableFieldValidators('fullName', true);
  340. this.bv.validate();
  341. expect(this.bv.isValidField('fullName')).toEqual(false);
  342. expect(this.bv.isValid()).toEqual(false);
  343. });
  344. it('disable all validators', function() {
  345. this.bv.resetForm();
  346. this.bv.enableFieldValidators('fullName', false);
  347. this.bv.validate();
  348. expect(this.bv.isValid()).toBeTruthy();
  349. });
  350. it('enabled option particular validator', function() {
  351. this.$fullName.val('Contain@#$');
  352. this.bv.validate();
  353. expect(this.bv.isValid()).toBeTruthy();
  354. var messages = this.bv.getMessages('fullName');
  355. expect(messages.length).toEqual(0);
  356. });
  357. it('enable particular validators', function() {
  358. // Enable stringLength validator
  359. this.bv.resetForm();
  360. this.bv.enableFieldValidators('fullName', true, 'stringLength');
  361. this.bv.enableFieldValidators('fullName', true, 'regexp');
  362. this.$fullName.val('Full@');
  363. this.bv.validate();
  364. expect(this.bv.isValid()).toEqual(false);
  365. var messages = this.bv.getMessages('fullName');
  366. expect($.inArray('The full name must be more than 8 and less than 40 characters long', messages)).toBeGreaterThan(-1);
  367. expect($.inArray('The full name can only consist of alphabetical, number, and space', messages)).toBeGreaterThan(-1);
  368. });
  369. it('disable particular validators', function() {
  370. // Disable stringLength validator
  371. this.bv.enableFieldValidators('fullName', false, 'stringLength');
  372. this.$fullName.val('Full');
  373. this.bv.validate();
  374. expect(this.bv.isValid()).toBeTruthy();
  375. var messages = this.bv.getMessages('fullName');
  376. expect($.inArray('The full name must be more than 8 and less than 40 characters long', messages)).toEqual(-1);
  377. // Disable regexp validator
  378. this.bv.enableFieldValidators('fullName', false, 'regexp');
  379. this.$fullName.val('Special@#$');
  380. this.bv.validate();
  381. expect(this.bv.isValid()).toBeTruthy();
  382. var messages = this.bv.getMessages('fullName');
  383. expect($.inArray('The full name can only consist of alphabetical, number, and space', messages)).toEqual(-1);
  384. });
  385. });
  386. TestSuite = $.extend({}, TestSuite, {
  387. Event: {
  388. onEmailValid: function(e, data) {
  389. $('#msg').html('TestSuite.Event.onEmailValid() called, ' + data.field + ' is valid');
  390. },
  391. onEmailInvalid: function(e, data) {
  392. $('#msg').html('TestSuite.Event.onEmailInvalid() called, ' + data.field + ' is invalid');
  393. },
  394. onEmailStatus: function(e, data) {
  395. $('#status').html('TestSuite.Event.onEmailStatus() called; status = ' + data.status);
  396. },
  397. onFormValid: function(e) {
  398. $('#msg').html('TestSuite.Event.onFormValid() called, form ' + $(e.target).attr('id') + ' is valid');
  399. },
  400. onFormInvalid: function(e) {
  401. $('#msg').html('TestSuite.Event.onFormInvalid() called, form ' + $(e.target).attr('id') + ' is invalid');
  402. }
  403. }
  404. });
  405. // ---
  406. // Form events
  407. // ---
  408. function onFormValid(e) {
  409. $('#msg').html('form ' + $(e.target).attr('id') + ' is valid');
  410. };
  411. function onFormInvalid(e) {
  412. $('#msg').html('form ' + $(e.target).attr('id') + ' is invalid');
  413. };
  414. describe('event form attribute callback global', function() {
  415. beforeEach(function() {
  416. $([
  417. '<form class="form-horizontal" id="eventForm" data-bv-onsuccess="onFormValid" data-bv-onerror="onFormInvalid" >',
  418. '<div id="msg"></div>',
  419. '<div class="form-group">',
  420. '<input type="text" name="email" required data-bv-emailaddress />',
  421. '</div>',
  422. '</form>'
  423. ].join('\n')).appendTo('body');
  424. $('#eventForm').bootstrapValidator();
  425. this.bv = $('#eventForm').data('bootstrapValidator');
  426. this.$email = this.bv.getFieldElements('email');
  427. });
  428. afterEach(function() {
  429. $('#eventForm').bootstrapValidator('destroy').remove();
  430. });
  431. it('call data-bv-onsuccess', function() {
  432. this.$email.val('email@domain.com');
  433. this.bv.validate();
  434. expect($('#msg').html()).toEqual('form eventForm is valid');
  435. });
  436. it('call data-bv-onerror', function() {
  437. this.$email.val('a@b@c@example.com');
  438. this.bv.validate();
  439. expect($('#msg').html()).toEqual('form eventForm is invalid');
  440. });
  441. });
  442. describe('event form attribute callback namespace', function() {
  443. beforeEach(function() {
  444. $([
  445. '<form class="form-horizontal" id="eventForm" data-bv-onsuccess="TestSuite.Event.onFormValid" data-bv-onerror="TestSuite.Event.onFormInvalid" >',
  446. '<div id="msg"></div>',
  447. '<div class="form-group">',
  448. '<input type="text" name="email" required data-bv-emailaddress />',
  449. '</div>',
  450. '</form>'
  451. ].join('\n')).appendTo('body');
  452. $('#eventForm').bootstrapValidator();
  453. this.bv = $('#eventForm').data('bootstrapValidator');
  454. this.$email = this.bv.getFieldElements('email');
  455. });
  456. afterEach(function() {
  457. $('#eventForm').bootstrapValidator('destroy').remove();
  458. });
  459. it('call data-bv-onsuccess', function() {
  460. this.$email.val('email@domain.com');
  461. this.bv.validate();
  462. expect($('#msg').html()).toEqual('TestSuite.Event.onFormValid() called, form eventForm is valid');
  463. });
  464. it('call data-bv-onerror', function() {
  465. this.$email.val('just"not"right@example.com');
  466. this.bv.validate();
  467. expect($('#msg').html()).toEqual('TestSuite.Event.onFormInvalid() called, form eventForm is invalid');
  468. });
  469. });
  470. describe('event form trigger', function() {
  471. beforeEach(function() {
  472. $([
  473. '<form class="form-horizontal" id="eventForm">',
  474. '<div id="msg"></div>',
  475. '<div class="form-group">',
  476. '<input type="text" name="email" data-bv-emailaddress />',
  477. '</div>',
  478. '</form>'
  479. ].join('\n')).appendTo('body');
  480. $('#eventForm')
  481. .bootstrapValidator()
  482. .on('success.form.bv', function(e) {
  483. $('#msg').html('form ' + $(e.target).attr('id') + ' triggered success.form.bv event');
  484. })
  485. .on('error.form.bv', function(e) {
  486. $('#msg').html('form ' + $(e.target).attr('id') + ' triggered error.form.bv event');
  487. });
  488. this.bv = $('#eventForm').data('bootstrapValidator');
  489. this.$email = this.bv.getFieldElements('email');
  490. });
  491. afterEach(function() {
  492. $('#eventForm').bootstrapValidator('destroy').remove();
  493. });
  494. it('trigger success.form.bv', function() {
  495. this.$email.val('email@domain.com');
  496. this.bv.validate();
  497. expect($('#msg').html()).toEqual('form eventForm triggered success.form.bv event');
  498. });
  499. it('trigger error.form.bv', function() {
  500. this.$email.val('this is"not\\allowed@example.com');
  501. this.bv.validate();
  502. expect($('#msg').html()).toEqual('form eventForm triggered error.form.bv event');
  503. });
  504. });
  505. describe('event form programmatically', function() {
  506. beforeEach(function() {
  507. $([
  508. '<form class="form-horizontal" id="eventForm">',
  509. '<div id="msg"></div>',
  510. '<div class="form-group">',
  511. '<input type="text" name="email" data-bv-emailaddress />',
  512. '</div>',
  513. '</form>'
  514. ].join('\n')).appendTo('body');
  515. $('#eventForm').bootstrapValidator({
  516. onSuccess: function(e) {
  517. $('#msg').html('onSuccess() called');
  518. },
  519. onError: function(e) {
  520. $('#msg').html('onError() called');
  521. }
  522. });
  523. this.bv = $('#eventForm').data('bootstrapValidator');
  524. this.$email = this.bv.getFieldElements('email');
  525. });
  526. afterEach(function() {
  527. $('#eventForm').bootstrapValidator('destroy').remove();
  528. });
  529. it('call onSuccess()', function() {
  530. this.$email.val('email@domain.com');
  531. this.bv.validate();
  532. expect($('#msg').html()).toEqual('onSuccess() called');
  533. });
  534. it('call onError()', function() {
  535. this.$email.val('Abc.example.com');
  536. this.bv.validate();
  537. expect($('#msg').html()).toEqual('onError() called');
  538. });
  539. });
  540. // ---
  541. // Field events
  542. // ---
  543. function onEmailValid(e, data) {
  544. $('#msg').html(data.field + ' is valid');
  545. };
  546. function onEmailInvalid(e, data) {
  547. $('#msg').html(data.field + ' is invalid');
  548. };
  549. function onEmailStatus(e, data) {
  550. $('#status').html(data.status);
  551. };
  552. describe('event field attribute callback global', function() {
  553. beforeEach(function() {
  554. $([
  555. '<form class="form-horizontal" id="eventForm">',
  556. '<div id="msg"></div>',
  557. '<div id="status"></div>',
  558. '<div class="form-group">',
  559. '<input type="text" name="email" data-bv-emailaddress data-bv-onsuccess="onEmailValid" data-bv-onerror="onEmailInvalid" data-bv-onstatus="onEmailStatus" />',
  560. '</div>',
  561. '</form>'
  562. ].join('\n')).appendTo('body');
  563. $('#eventForm').bootstrapValidator();
  564. this.bv = $('#eventForm').data('bootstrapValidator');
  565. this.$email = this.bv.getFieldElements('email');
  566. });
  567. afterEach(function() {
  568. $('#eventForm').bootstrapValidator('destroy').remove();
  569. });
  570. it('call data-bv-onsuccess', function() {
  571. this.$email.val('email@domain.com');
  572. this.bv.validate();
  573. expect($('#msg').html()).toEqual('email is valid');
  574. expect($('#status').html()).toEqual(this.bv.STATUS_VALID);
  575. });
  576. it('call data-bv-onerror', function() {
  577. this.$email.val('A@b@c@example.com');
  578. this.bv.validate();
  579. expect($('#msg').html()).toEqual('email is invalid');
  580. expect($('#status').html()).toEqual(this.bv.STATUS_INVALID);
  581. });
  582. });
  583. describe('event field attribute callback namespace', function() {
  584. beforeEach(function() {
  585. $([
  586. '<form class="form-horizontal" id="eventForm">',
  587. '<div id="msg"></div>',
  588. '<div id="status"></div>',
  589. '<div class="form-group">',
  590. '<input type="text" name="email" data-bv-emailaddress data-bv-onsuccess="TestSuite.Event.onEmailValid" data-bv-onerror="TestSuite.Event.onEmailInvalid" data-bv-onstatus="TestSuite.Event.onEmailStatus" />',
  591. '</div>',
  592. '</form>'
  593. ].join('\n')).appendTo('body');
  594. $('#eventForm').bootstrapValidator();
  595. this.bv = $('#eventForm').data('bootstrapValidator');
  596. this.$email = this.bv.getFieldElements('email');
  597. });
  598. afterEach(function() {
  599. $('#eventForm').bootstrapValidator('destroy').remove();
  600. });
  601. it('call data-bv-onsuccess', function() {
  602. this.$email.val('email@domain.com');
  603. this.bv.validate();
  604. expect($('#msg').html()).toEqual('TestSuite.Event.onEmailValid() called, email is valid');
  605. expect($('#status').html()).toEqual('TestSuite.Event.onEmailStatus() called; status = ' + this.bv.STATUS_VALID);
  606. });
  607. it('call data-bv-onerror', function() {
  608. this.$email.val('a"b(c)d,e:f;gi[j\\k]l@example.com');
  609. this.bv.validate();
  610. expect($('#msg').html()).toEqual('TestSuite.Event.onEmailInvalid() called, email is invalid');
  611. expect($('#status').html()).toEqual('TestSuite.Event.onEmailStatus() called; status = ' + this.bv.STATUS_INVALID);
  612. });
  613. });
  614. describe('event field trigger', function() {
  615. beforeEach(function() {
  616. $([
  617. '<form class="form-horizontal" id="eventForm">',
  618. '<div id="msg"></div>',
  619. '<div class="form-group">',
  620. '<input type="text" name="email" data-bv-emailaddress />',
  621. '</div>',
  622. '</form>'
  623. ].join('\n')).appendTo('body');
  624. $('#eventForm')
  625. .bootstrapValidator()
  626. .on('success.field.bv', '[name="email"]', function(e, data) {
  627. $('#msg').html('triggered success.field.bv on ' + data.field);
  628. })
  629. .on('error.field.bv', '[name="email"]', function(e, data) {
  630. $('#msg').html('triggered error.field.bv on ' + data.field);
  631. });
  632. this.bv = $('#eventForm').data('bootstrapValidator');
  633. this.$email = this.bv.getFieldElements('email');
  634. });
  635. afterEach(function() {
  636. $('#eventForm').bootstrapValidator('destroy').remove();
  637. });
  638. it('trigger success.field.bv', function() {
  639. this.$email.val('email@domain.com');
  640. this.bv.validate();
  641. expect($('#msg').html()).toEqual('triggered success.field.bv on email');
  642. });
  643. it('trigger error.field.bv', function() {
  644. this.$email.val('just"not"right@example.com');
  645. this.bv.validate();
  646. expect($('#msg').html()).toEqual('triggered error.field.bv on email');
  647. });
  648. });
  649. describe('event field programmatically', function() {
  650. beforeEach(function() {
  651. $([
  652. '<form class="form-horizontal" id="eventForm">',
  653. '<div id="msg"></div>',
  654. '<div class="form-group">',
  655. '<input type="text" name="email" data-bv-emailaddress />',
  656. '</div>',
  657. '</form>'
  658. ].join('\n')).appendTo('body');
  659. $('#eventForm').bootstrapValidator({
  660. fields: {
  661. email: {
  662. onSuccess: function(e, data) {
  663. $('#msg').html('onSuccess() called');
  664. },
  665. onError: function(e, data) {
  666. $('#msg').html('onError() called');
  667. }
  668. }
  669. }
  670. });
  671. this.bv = $('#eventForm').data('bootstrapValidator');
  672. this.$email = this.bv.getFieldElements('email');
  673. });
  674. afterEach(function() {
  675. $('#eventForm').bootstrapValidator('destroy').remove();
  676. });
  677. it('call onSuccess()', function() {
  678. this.$email.val('email@domain.com');
  679. this.bv.validate();
  680. expect($('#msg').html()).toEqual('onSuccess() called');
  681. });
  682. it('call onError()', function() {
  683. this.$email.val('this is"not\\allowed@example.com');
  684. this.bv.validate();
  685. expect($('#msg').html()).toEqual('onError() called');
  686. });
  687. });
  688. // ---
  689. // Modifying default events
  690. // ---
  691. describe('event form trigger with default events', function() {
  692. beforeEach(function() {
  693. $([
  694. '<form class="form-horizontal" id="eventForm1">',
  695. '<div id="msg"></div>',
  696. '<div class="form-group">',
  697. '<input type="text" name="email" data-bv-emailaddress />',
  698. '</div>',
  699. '</form>'
  700. ].join('\n')).appendTo('body');
  701. $('#eventForm1')
  702. .bootstrapValidator()
  703. .on('bv.form.success', function(e) {
  704. $('#msg').html('form ' + $(e.target).attr('id') + ' triggered bv.form.success event');
  705. })
  706. .on('success.form.bv', function(e) {
  707. $('#msg').html('form ' + $(e.target).attr('id') + ' triggered success.form.bv event');
  708. })
  709. .on('bv.form.error', function(e) {
  710. $('#msg').html('form ' + $(e.target).attr('id') + ' triggered bv.form.error event');
  711. })
  712. .on('error.form.bv', function(e) {
  713. $('#msg').html('form ' + $(e.target).attr('id') + ' triggered error.form.bv event');
  714. });
  715. this.bv = $('#eventForm1').data('bootstrapValidator');
  716. this.$email = this.bv.getFieldElements('email');
  717. });
  718. afterEach(function() {
  719. $('#eventForm1').bootstrapValidator('destroy').remove();
  720. });
  721. it('does not trigger bv.form.success', function() {
  722. this.$email.val('email@domain.com');
  723. this.bv.validate();
  724. expect($('#msg').html()).not.toEqual('form eventForm1 triggered bv.form.success event');
  725. });
  726. it('triggers success.form.bv', function() {
  727. this.$email.val('email@domain.com');
  728. this.bv.validate();
  729. expect($('#msg').html()).toEqual('form eventForm1 triggered success.form.bv event');
  730. });
  731. it('does not trigger bv.form.error', function() {
  732. this.$email.val('A@b@c@example.com');
  733. this.bv.validate();
  734. expect($('#msg').html()).not.toEqual('form eventForm1 triggered bv.form.error event');
  735. });
  736. it('triggers error.form.bv', function() {
  737. this.$email.val('A@b@c@example.com');
  738. this.bv.validate();
  739. expect($('#msg').html()).toEqual('form eventForm1 triggered error.form.bv event');
  740. });
  741. });
  742. describe('event field trigger with default events', function() {
  743. beforeEach(function() {
  744. $([
  745. '<form class="form-horizontal" id="eventForm3">',
  746. '<div id="msg"></div>',
  747. '<div class="form-group">',
  748. '<input type="text" name="email" data-bv-emailaddress />',
  749. '</div>',
  750. '</form>'
  751. ].join('\n')).appendTo('body');
  752. $('#eventForm3')
  753. .bootstrapValidator()
  754. .on('success.field.bv', '[name="email"]', function(e, data) {
  755. $('#msg').html('triggered success.field.bv on ' + data.field);
  756. })
  757. .on('error.field.bv', '[name="email"]', function(e, data) {
  758. $('#msg').html('triggered error.field.bv on ' + data.field);
  759. })
  760. .on('bv.field.success', '[name="email"]', function(e, data) {
  761. $('#msg').html('triggered bv.field.success on ' + data.field);
  762. })
  763. .on('bv.field.error', '[name="email"]', function(e, data) {
  764. $('#msg').html('triggered bv.field.error on ' + data.field);
  765. });
  766. this.bv = $('#eventForm3').data('bootstrapValidator');
  767. this.$email = this.bv.getFieldElements('email');
  768. });
  769. afterEach(function() {
  770. $('#eventForm3').bootstrapValidator('destroy').remove();
  771. });
  772. it('triggers success.field.bv', function() {
  773. this.$email.val('email@domain.com');
  774. this.bv.validate();
  775. expect($('#msg').html()).toEqual('triggered success.field.bv on email');
  776. });
  777. it('does not trigger bv.field.success', function() {
  778. this.$email.val('email@domain.com');
  779. this.bv.validate();
  780. expect($('#msg').html()).not.toEqual('triggered bv.field.success on email');
  781. });
  782. it('does not trigger error.field.bv', function() {
  783. this.$email.val('just"not"right@example.com');
  784. this.bv.validate();
  785. expect($('#msg').html()).toEqual('triggered error.field.bv on email');
  786. });
  787. it('triggers bv.field.error', function() {
  788. this.$email.val('just"not"right@example.com');
  789. this.bv.validate();
  790. expect($('#msg').html()).not.toEqual('triggered bv.field.error on email');
  791. });
  792. });
  793. describe('event form trigger with events changed', function() {
  794. beforeEach(function() {
  795. $.fn.bootstrapValidator.DEFAULT_OPTIONS = $.extend({}, $.fn.bootstrapValidator.DEFAULT_OPTIONS, {
  796. events: {
  797. formInit: 'init.form.bv',
  798. formError: 'bv.form.error',
  799. formSuccess: 'bv.form.success',
  800. fieldAdded: 'added.field.bv',
  801. fieldRemoved: 'removed.field.bv',
  802. fieldInit: 'init.field.bv',
  803. fieldError: 'bv.field.error',
  804. fieldSuccess: 'bv.field.success',
  805. fieldStatus: 'status.field.bv',
  806. validatorError: 'bv.validator.error',
  807. validatorSuccess: 'success.validator.bv'
  808. }
  809. });
  810. $([
  811. '<form class="form-horizontal" id="eventForm2">',
  812. '<div id="msg"></div>',
  813. '<div class="form-group">',
  814. '<input type="text" name="email" data-bv-emailaddress />',
  815. '</div>',
  816. '</form>'
  817. ].join('\n')).appendTo('body');
  818. $('#eventForm2')
  819. .bootstrapValidator()
  820. .on('bv.form.success', function(e) {
  821. $('#msg').html('form ' + $(e.target).attr('id') + ' triggered bv.form.success event');
  822. })
  823. .on('success.form.bv', function(e) {
  824. $('#msg').html('form ' + $(e.target).attr('id') + ' triggered success.form.bv event');
  825. })
  826. .on('bv.form.error', function(e) {
  827. $('#msg').html('form ' + $(e.target).attr('id') + ' triggered bv.form.error event');
  828. })
  829. .on('error.form.bv', function(e) {
  830. $('#msg').html('form ' + $(e.target).attr('id') + ' triggered error.form.bv event');
  831. });
  832. this.bv = $('#eventForm2').data('bootstrapValidator');
  833. this.$email = this.bv.getFieldElements('email');
  834. });
  835. afterEach(function() {
  836. $('#eventForm2').bootstrapValidator('destroy').remove();
  837. });
  838. it('triggers bv.form.success', function() {
  839. this.$email.val('email@domain.com');
  840. this.bv.validate();
  841. expect($('#msg').html()).toEqual('form eventForm2 triggered bv.form.success event');
  842. });
  843. it('does not trigger success.form.bv', function() {
  844. this.$email.val('email@domain.com');
  845. this.bv.validate();
  846. expect($('#msg').html()).not.toEqual('form eventForm2 triggered success.form.bv event');
  847. });
  848. it('triggers bv.form.error', function() {
  849. spyOn(window, 'onerror');
  850. this.$email.val('this is"not\\allowed@example.com');
  851. this.bv.validate();
  852. expect($('#msg').html()).toEqual('form eventForm2 triggered bv.form.error event');
  853. expect(window.onerror).not.toHaveBeenCalled();
  854. });
  855. });
  856. describe('event field trigger with events changed', function() {
  857. beforeEach(function () {
  858. $.fn.bootstrapValidator.DEFAULT_OPTIONS = $.extend({}, $.fn.bootstrapValidator.DEFAULT_OPTIONS, {
  859. events: {
  860. formInit: 'init.form.bv',
  861. formError: 'bv.form.error',
  862. formSuccess: 'bv.form.success',
  863. fieldAdded: 'added.field.bv',
  864. fieldRemoved: 'removed.field.bv',
  865. fieldInit: 'init.field.bv',
  866. fieldError: 'bv.field.error',
  867. fieldSuccess: 'bv.field.success',
  868. fieldStatus: 'status.field.bv',
  869. validatorError: 'bv.validator.error',
  870. validatorSuccess: 'success.validator.bv'
  871. }
  872. });
  873. $([
  874. '<form class="form-horizontal" id="eventForm4">',
  875. '<div id="msg"></div>',
  876. '<div class="form-group">',
  877. '<input type="text" name="email" data-bv-emailaddress />',
  878. '</div>',
  879. '</form>'
  880. ].join('\n')).appendTo('body');
  881. $('#eventForm4')
  882. .bootstrapValidator()
  883. .on('success.field.bv', '[name="email"]', function(e, data) {
  884. $('#msg').html('triggered success.field.bv on ' + data.field);
  885. })
  886. .on('error.field.bv', '[name="email"]', function(e, data) {
  887. $('#msg').html('triggered error.field.bv on ' + data.field);
  888. })
  889. .on('bv.field.success', '[name="email"]', function(e, data) {
  890. $('#msg').html('triggered bv.field.success on ' + data.field);
  891. })
  892. .on('bv.field.error', '[name="email"]', function(e, data) {
  893. $('#msg').html('triggered bv.field.error on ' + data.field);
  894. });
  895. this.bv = $('#eventForm4').data('bootstrapValidator');
  896. this.$email = this.bv.getFieldElements('email');
  897. });
  898. afterEach(function() {
  899. $('#eventForm4').bootstrapValidator('destroy').remove();
  900. });
  901. it('triggers success.field.bv', function() {
  902. this.$email.val('email@domain.com');
  903. this.bv.validate();
  904. expect($('#msg').html()).not.toEqual('triggered success.field.bv on email');
  905. });
  906. it('does not trigger bv.field.success', function() {
  907. this.$email.val('email@domain.com');
  908. this.bv.validate();
  909. expect($('#msg').html()).toEqual('triggered bv.field.success on email');
  910. });
  911. it('does not trigger error.field.bv', function() {
  912. this.$email.val('Abc.example.com');
  913. this.bv.validate();
  914. expect($('#msg').html()).not.toEqual('triggered error.field.bv on email');
  915. });
  916. it('triggers bv.field.error', function() {
  917. spyOn(window, 'onerror');
  918. this.$email.val('Abc.example.com');
  919. this.bv.validate();
  920. expect($('#msg').html()).toEqual('triggered bv.field.error on email');
  921. expect(window.onerror).not.toHaveBeenCalled();
  922. });
  923. });
  924. // ---
  925. // Validator events
  926. // ---
  927. function onEmailAddressValidatorSuccess(e, data) {
  928. $('#msg').html(data.validator + ' validator passed');
  929. };
  930. function onEmailAddressValidatorError(e, data) {
  931. $('#msg').html(data.validator + ' validator did not pass');
  932. };
  933. describe('event validator declarative', function() {
  934. beforeEach(function() {
  935. $([
  936. '<form class="form-horizontal" id="eventForm">',
  937. '<div id="msg"></div>',
  938. '<div class="form-group">',
  939. '<input type="text" name="email" data-bv-emailaddress data-bv-emailaddress-onsuccess="onEmailAddressValidatorSuccess" data-bv-emailaddress-onerror="onEmailAddressValidatorError" />',
  940. '</div>',
  941. '</form>'
  942. ].join('\n')).appendTo('body');
  943. $('#eventForm').bootstrapValidator();
  944. this.bv = $('#eventForm').data('bootstrapValidator');
  945. this.$email = this.bv.getFieldElements('email');
  946. });
  947. afterEach(function() {
  948. $('#eventForm').bootstrapValidator('destroy').remove();
  949. });
  950. it('trigger data-bv-emailaddress-onsuccess', function() {
  951. this.$email.val('email@domain.com');
  952. this.bv.validate();
  953. expect($('#msg').html()).toEqual('emailAddress validator passed');
  954. });
  955. it('trigger data-bv-emailaddress-onerror', function() {
  956. this.$email.val('A@b@c@example.com');
  957. this.bv.validate();
  958. expect($('#msg').html()).toEqual('emailAddress validator did not pass');
  959. });
  960. });
  961. describe('event validator programmatically', function() {
  962. beforeEach(function() {
  963. $([
  964. '<form class="form-horizontal" id="eventForm">',
  965. '<div id="msg"></div>',
  966. '<div class="form-group">',
  967. '<input type="text" name="email" data-bv-emailaddress />',
  968. '</div>',
  969. '</form>'
  970. ].join('\n')).appendTo('body');
  971. $('#eventForm').bootstrapValidator({
  972. fields: {
  973. email: {
  974. validators: {
  975. emailAddress: {
  976. onSuccess: function(e, data) {
  977. $('#msg').html('emailAddress validator: onSuccess() called');
  978. },
  979. onError: function(e, data) {
  980. $('#msg').html('emailAddress validator: onError() called');
  981. },
  982. message: 'The email address is not valid'
  983. }
  984. }
  985. }
  986. }
  987. });
  988. this.bv = $('#eventForm').data('bootstrapValidator');
  989. this.$email = this.bv.getFieldElements('email');
  990. });
  991. afterEach(function() {
  992. $('#eventForm').bootstrapValidator('destroy').remove();
  993. });
  994. it('call onSuccess()', function() {
  995. this.$email.val('email@domain.com');
  996. this.bv.validate();
  997. expect($('#msg').html()).toEqual('emailAddress validator: onSuccess() called');
  998. });
  999. it('call onError()', function() {
  1000. this.$email.val('A@b@c@example.com');
  1001. this.bv.validate();
  1002. expect($('#msg').html()).toEqual('emailAddress validator: onError() called');
  1003. });
  1004. });
  1005. describe('excluded', function() {
  1006. beforeEach(function() {
  1007. $([
  1008. '<div class="container">',
  1009. '<form class="form-horizontal" id="excludedForm" data-bv-excluded="[name=\'email\']">',
  1010. '<div class="form-group">',
  1011. '<input type="text" name="username" required />',
  1012. '</div>',
  1013. '<div class="form-group">',
  1014. '<input type="text" name="email" required data-bv-emailaddress />',
  1015. '</div>',
  1016. '</form>',
  1017. '</div>'
  1018. ].join('')).appendTo('body');
  1019. $('#excludedForm').bootstrapValidator();
  1020. this.bv = $('#excludedForm').data('bootstrapValidator');
  1021. this.$username = this.bv.getFieldElements('username');
  1022. this.$email = this.bv.getFieldElements('email');
  1023. });
  1024. afterEach(function() {
  1025. $('#excludedForm').bootstrapValidator('destroy').parent().remove();
  1026. });
  1027. it('excluded form declarative', function() {
  1028. this.bv.validate();
  1029. expect(this.bv.isValid()).toEqual(false);
  1030. this.bv.resetForm();
  1031. this.$username.val('your_user_name');
  1032. this.$email.val('');
  1033. this.bv.validate();
  1034. expect(this.bv.isValid()).toBeTruthy();
  1035. });
  1036. it('excluded form programmatically', function() {
  1037. this.bv.destroy();
  1038. $('#excludedForm').removeAttr('data-bv-excluded');
  1039. $('#excludedForm').bootstrapValidator({
  1040. excluded: '[name="username"]'
  1041. });
  1042. this.bv = $('#excludedForm').data('bootstrapValidator');
  1043. this.$username = this.bv.getFieldElements('username');
  1044. this.$email = this.bv.getFieldElements('email');
  1045. this.$username.val('');
  1046. this.$email.val('invalid#email.com');
  1047. this.bv.validate();
  1048. expect(this.bv.isValid()).toEqual(false);
  1049. this.bv.resetForm();
  1050. this.$email.val('valid@email.com');
  1051. this.bv.validate();
  1052. expect(this.bv.isValid()).toBeTruthy();
  1053. });
  1054. it('excluded field declarative', function() {
  1055. this.bv.destroy();
  1056. $('#excludedForm').removeAttr('data-bv-excluded');
  1057. $('#excludedForm').find('[name="username"]').attr('data-bv-excluded', 'true');
  1058. $('#excludedForm').find('[name="email"]').attr('data-bv-excluded', 'false');
  1059. this.bv = $('#excludedForm').bootstrapValidator().data('bootstrapValidator');
  1060. this.$username = this.bv.getFieldElements('username');
  1061. this.$email = this.bv.getFieldElements('email');
  1062. this.$username.val('');
  1063. this.$email.val('');
  1064. this.bv.validate();
  1065. expect(this.bv.isValid()).toEqual(false);
  1066. this.bv.resetForm();
  1067. this.$email.val('invalid#email.com');
  1068. this.bv.validate();
  1069. expect(this.bv.isValid()).toEqual(false);
  1070. this.bv.resetForm();
  1071. this.$email.val('valid@email.com');
  1072. this.bv.validate();
  1073. expect(this.bv.isValid()).toBeTruthy();
  1074. });
  1075. it('excluded field programmatically true/false', function() {
  1076. this.bv.destroy();
  1077. $('#excludedForm').removeAttr('data-bv-excluded');
  1078. $('#excludedForm').bootstrapValidator({
  1079. fields: {
  1080. username: {
  1081. excluded: true
  1082. },
  1083. email: {
  1084. excluded: false
  1085. }
  1086. }
  1087. });
  1088. this.bv = $('#excludedForm').bootstrapValidator().data('bootstrapValidator');
  1089. this.$username = this.bv.getFieldElements('username');
  1090. this.$email = this.bv.getFieldElements('email');
  1091. this.$username.val('');
  1092. this.$email.val('');
  1093. this.bv.validate();
  1094. expect(this.bv.isValid()).toEqual(false);
  1095. this.bv.resetForm();
  1096. this.$email.val('invalid#email.com');
  1097. this.bv.validate();
  1098. expect(this.bv.isValid()).toEqual(false);
  1099. this.bv.resetForm();
  1100. this.$email.val('valid@email.com');
  1101. this.bv.validate();
  1102. expect(this.bv.isValid()).toBeTruthy();
  1103. });
  1104. it('excluded field programmatically "true"/"false"', function() {
  1105. this.bv.destroy();
  1106. $('#excludedForm').removeAttr('data-bv-excluded');
  1107. $('#excludedForm').bootstrapValidator({
  1108. fields: {
  1109. username: {
  1110. excluded: 'false'
  1111. },
  1112. email: {
  1113. excluded: 'true'
  1114. }
  1115. }
  1116. });
  1117. this.bv = $('#excludedForm').bootstrapValidator().data('bootstrapValidator');
  1118. this.$username = this.bv.getFieldElements('username');
  1119. this.$email = this.bv.getFieldElements('email');
  1120. this.$username.val('');
  1121. this.$email.val('valid@email.com');
  1122. this.bv.validate();
  1123. expect(this.bv.isValid()).toEqual(false);
  1124. this.bv.resetForm();
  1125. this.$username.val('your_user_name');
  1126. this.$email.val('invalid#email.com');
  1127. this.bv.validate();
  1128. expect(this.bv.isValid()).toBeTruthy();
  1129. });
  1130. });
  1131. describe('group option', function() {
  1132. beforeEach(function() {
  1133. $([
  1134. '<form id="groupForm" method="post" class="form-horizontal">',
  1135. '<div class="form-group">',
  1136. '<div class="firstNameGroup">',
  1137. '<label class="col-sm-2 control-label">First name</label>',
  1138. '<div class="col-sm-4">',
  1139. '<input type="text" class="form-control" name="firstName" />',
  1140. '</div>',
  1141. '</div>',
  1142. '<div class="lastNameGroup">',
  1143. '<label class="col-sm-2 control-label">Last name</label>',
  1144. '<div class="col-sm-4">',
  1145. '<input type="text" class="form-control" name="lastName" data-bv-group=".lastNameGroup" />',
  1146. '</div>',
  1147. '</div>',
  1148. '</div>',
  1149. '<div class="form-group">',
  1150. '<label class="col-sm-2 control-label">Username</label>',
  1151. '<div class="col-sm-5">',
  1152. '<input type="text" class="form-control" name="username" />',
  1153. '</div>',
  1154. '</div>',
  1155. '</form>'
  1156. ].join('')).appendTo('body');
  1157. $('#groupForm').bootstrapValidator({
  1158. fields: {
  1159. firstName: {
  1160. group: '.firstNameGroup',
  1161. validators: {
  1162. notEmpty: {
  1163. message: 'The first name is required and cannot be empty'
  1164. }
  1165. }
  1166. },
  1167. lastName: {
  1168. validators: {
  1169. notEmpty: {
  1170. message: 'The last name is required and cannot be empty'
  1171. }
  1172. }
  1173. },
  1174. username: {
  1175. validators: {
  1176. notEmpty: {
  1177. message: 'The username is required and cannot be empty'
  1178. },
  1179. stringLength: {
  1180. min: 6,
  1181. max: 30,
  1182. message: 'The username must be more than 6 and less than 30 characters long'
  1183. },
  1184. regexp: {
  1185. regexp: /^[a-zA-Z0-9_\.]+$/,
  1186. message: 'The username can only consist of alphabetical, number, dot and underscore'
  1187. }
  1188. }
  1189. }
  1190. }
  1191. });
  1192. this.bv = $('#groupForm').data('bootstrapValidator');
  1193. this.$firstName = this.bv.getFieldElements('firstName');
  1194. this.$lastName = this.bv.getFieldElements('lastName');
  1195. this.$username = this.bv.getFieldElements('username');
  1196. });
  1197. afterEach(function() {
  1198. $('#groupForm').bootstrapValidator('destroy').remove();
  1199. });
  1200. it('group default', function() {
  1201. this.$username.val('123@#$');
  1202. this.bv.validate();
  1203. expect(this.$username.parents('.form-group').hasClass('has-error')).toBeTruthy();
  1204. expect(this.$username.parents('.form-group').hasClass('has-success')).toEqual(false);
  1205. this.bv.resetForm();
  1206. this.$username.val('validUser.Name');
  1207. this.bv.validate();
  1208. expect(this.$username.parents('.form-group').hasClass('has-success')).toBeTruthy();
  1209. expect(this.$username.parents('.form-group').hasClass('has-error')).toEqual(false);
  1210. });
  1211. it('group programmatically', function() {
  1212. this.$firstName.val('');
  1213. this.bv.validate();
  1214. expect(this.$firstName.parents('.firstNameGroup').hasClass('has-error')).toBeTruthy();
  1215. expect(this.$firstName.parents('.firstNameGroup').hasClass('has-success')).toEqual(false);
  1216. expect(this.$firstName.parents('.form-group').hasClass('has-error')).toEqual(false);
  1217. });
  1218. it('group declarative', function() {
  1219. this.$firstName.val('First');
  1220. this.$lastName.val('Last');
  1221. this.bv.validate();
  1222. expect(this.$lastName.parents('.lastNameGroup').hasClass('has-success')).toBeTruthy();
  1223. expect(this.$lastName.parents('.lastNameGroup').hasClass('has-error')).toEqual(false);
  1224. expect(this.$lastName.parents('.form-group').hasClass('has-success')).toEqual(false);
  1225. expect(this.$lastName.parents('.form-group').hasClass('has-error')).toEqual(false);
  1226. });
  1227. });
  1228. describe('i18n', function() {
  1229. beforeEach(function() {
  1230. $([
  1231. '<form id="i18nForm" class="form-horizontal">',
  1232. '<div class="form-group">',
  1233. '<label class="col-lg-3 control-label">Full name</label>',
  1234. '<div class="col-lg-5">',
  1235. '<input type="text" class="form-control" name="fullName" />',
  1236. '</div>',
  1237. '</div>',
  1238. '<div class="form-group">',
  1239. '<label class="col-lg-3 control-label">Username</label>',
  1240. '<div class="col-lg-5">',
  1241. '<input type="text" class="form-control" name="username" />',
  1242. '</div>',
  1243. '</div>',
  1244. '<div class="form-group">',
  1245. '<label class="col-lg-3 control-label">Email address</label>',
  1246. '<div class="col-lg-5">',
  1247. '<input type="text" class="form-control" name="email" />',
  1248. '</div>',
  1249. '</div>',
  1250. '<div class="form-group">',
  1251. '<label class="col-lg-3 control-label">Password</label>',
  1252. '<div class="col-lg-5">',
  1253. '<input type="password" class="form-control" name="password" />',
  1254. '</div>',
  1255. '</div>',
  1256. '<div class="form-group">',
  1257. '<label class="col-lg-3 control-label">Retype password</label>',
  1258. '<div class="col-lg-5">',
  1259. '<input type="password" class="form-control" name="confirmPassword" />',
  1260. '</div>',
  1261. '</div>',
  1262. '<div class="form-group">',
  1263. '<label class="col-lg-3 control-label">Gender</label>',
  1264. '<div class="col-lg-5">',
  1265. '<div class="radio">',
  1266. '<label><input type="radio" name="gender" value="male" /> Male</label>',
  1267. '</div>',
  1268. '<div class="radio">',
  1269. '<label><input type="radio" name="gender" value="female" /> Female</label>',
  1270. '</div>',
  1271. '<div class="radio">',
  1272. '<label><input type="radio" name="gender" value="other" /> Other</label>',
  1273. '</div>',
  1274. '</div>',
  1275. '</div>',
  1276. '<div class="form-group">',
  1277. '<label class="col-lg-3 control-label">Age</label>',
  1278. '<div class="col-lg-3">',
  1279. '<input type="text" class="form-control" name="age" />',
  1280. '</div>',
  1281. '</div>',
  1282. '<div class="form-group">',
  1283. '<label class="col-lg-3 control-label">Website</label>',
  1284. '<div class="col-lg-5">',
  1285. '<input type="text" class="form-control" name="website" />',
  1286. '</div>',
  1287. '</div>',
  1288. '<div class="form-group">',
  1289. '<label class="col-lg-3 control-label">Phone number</label>',
  1290. '<div class="col-lg-5">',
  1291. '<input type="text" class="form-control" name="phoneNumber" />',
  1292. '</div>',
  1293. '</div>',
  1294. '<div class="form-group">',
  1295. '<label class="col-lg-3 control-label">Languages</label>',
  1296. '<div class="col-lg-5">',
  1297. '<div class="checkbox">',
  1298. '<label><input type="checkbox" name="languages[]" value="english" /> English</label>',
  1299. '</div>',
  1300. '<div class="checkbox">',
  1301. '<label><input type="checkbox" name="languages[]" value="french" /> French</label>',
  1302. '</div>',
  1303. '<div class="checkbox">',
  1304. '<label><input type="checkbox" name="languages[]" value="german" /> German</label>',
  1305. '</div>',
  1306. '<div class="checkbox">',
  1307. '<label><input type="checkbox" name="languages[]" value="russian" /> Russian</label>',
  1308. '</div>',
  1309. '<div class="checkbox">',
  1310. '<label><input type="checkbox" name="languages[]" value="other" /> Other</label>',
  1311. '</div>',
  1312. '</div>',
  1313. '</div>',
  1314. '<div class="form-group">',
  1315. '<label class="col-lg-3 control-label">Programming Languages</label>',
  1316. '<div class="col-lg-5">',
  1317. '<div class="checkbox">',
  1318. '<label><input type="checkbox" name="programs[]" value="net" /> .Net</label>',
  1319. '</div>',
  1320. '<div class="checkbox">',
  1321. '<label><input type="checkbox" name="programs[]" value="java" /> Java</label>',
  1322. '</div>',
  1323. '<div class="checkbox">',
  1324. '<label><input type="checkbox" name="programs[]" value="c" /> C/C++</label>',
  1325. '</div>',
  1326. '<div class="checkbox">',
  1327. '<label><input type="checkbox" name="programs[]" value="php" /> PHP</label>',
  1328. '</div>',
  1329. '<div class="checkbox">',
  1330. '<label><input type="checkbox" name="programs[]" value="perl" /> Perl</label>',
  1331. '</div>',
  1332. '<div class="checkbox">',
  1333. '<label><input type="checkbox" name="programs[]" value="ruby" /> Ruby</label>',
  1334. '</div>',
  1335. '<div class="checkbox">',
  1336. '<label><input type="checkbox" name="programs[]" value="python" /> Python</label>',
  1337. '</div>',
  1338. '<div class="checkbox">',
  1339. '<label><input type="checkbox" name="programs[]" value="javascript" /> Javascript</label>',
  1340. '</div>',
  1341. '</div>',
  1342. '</div>',
  1343. '</form>'
  1344. ].join('')).appendTo('body');
  1345. $('#i18nForm').bootstrapValidator({
  1346. feedbackIcons: {
  1347. valid: 'glyphicon glyphicon-ok',
  1348. invalid: 'glyphicon glyphicon-remove',
  1349. validating: 'glyphicon glyphicon-refresh'
  1350. },
  1351. fields: {
  1352. fullName: {
  1353. validators: {
  1354. notEmpty: {},
  1355. stringCase: {
  1356. 'case': 'upper'
  1357. }
  1358. }
  1359. },
  1360. username: {
  1361. validators: {
  1362. notEmpty: {},
  1363. stringLength: {
  1364. min: 6,
  1365. max: 20
  1366. },
  1367. regexp: {
  1368. regexp: /^[a-zA-Z0-9_\.]+$/
  1369. },
  1370. different: {
  1371. field: 'password'
  1372. }
  1373. }
  1374. },
  1375. email: {
  1376. validators: {
  1377. emailAddress: {}
  1378. }
  1379. },
  1380. password: {
  1381. validators: {
  1382. notEmpty: {},
  1383. identical: {
  1384. field: 'confirmPassword'
  1385. },
  1386. different: {
  1387. field: 'username'
  1388. }
  1389. }
  1390. },
  1391. confirmPassword: {
  1392. validators: {
  1393. notEmpty: {},
  1394. identical: {
  1395. field: 'password'
  1396. },
  1397. different: {
  1398. field: 'username'
  1399. }
  1400. }
  1401. },
  1402. age: {
  1403. validators: {
  1404. notEmpty: {},
  1405. digits: {},
  1406. greaterThan: {
  1407. value: 18
  1408. },
  1409. lessThan: {
  1410. value: 100
  1411. }
  1412. }
  1413. },
  1414. website: {
  1415. validators: {
  1416. notEmpty: {},
  1417. uri: {}
  1418. }
  1419. },
  1420. phoneNumber: {
  1421. validators: {
  1422. notEmpty: {},
  1423. digits: {},
  1424. phone: {
  1425. country: 'US'
  1426. }
  1427. }
  1428. },
  1429. gender: {
  1430. validators: {
  1431. notEmpty: {}
  1432. }
  1433. },
  1434. 'languages[]': {
  1435. validators: {
  1436. notEmpty: {}
  1437. }
  1438. },
  1439. 'programs[]': {
  1440. validators: {
  1441. choice: {
  1442. min: 2,
  1443. max: 4
  1444. }
  1445. }
  1446. }
  1447. }
  1448. });
  1449. this.bv = $('#i18nForm').data('bootstrapValidator');
  1450. this.$fullName = this.bv.getFieldElements('fullName');
  1451. this.$email = this.bv.getFieldElements('email');
  1452. this.$userName = this.bv.getFieldElements('username');
  1453. this.$password = this.bv.getFieldElements('password');
  1454. this.$confirm = this.bv.getFieldElements('confirmPassword');
  1455. this.$age = this.bv.getFieldElements('age');
  1456. this.$website = this.bv.getFieldElements('website');
  1457. this.$phone = this.bv.getFieldElements('phoneNumber');
  1458. this.$program = this.bv.getFieldElements('programs[]');
  1459. });
  1460. afterEach(function() {
  1461. $('#i18nForm').bootstrapValidator('destroy').remove();
  1462. });
  1463. it('default message', function() {
  1464. var format = $.fn.bootstrapValidator.helpers.format,
  1465. i18n = $.fn.bootstrapValidator.i18n;
  1466. this.bv.validate();
  1467. expect(this.bv.getMessages(this.$fullName, 'notEmpty')[0]).toEqual(i18n.notEmpty['default']);
  1468. this.$fullName.val('lowerName');
  1469. this.bv.revalidateField('fullName');
  1470. expect(this.bv.getMessages('fullName', 'stringCase')[0]).toEqual(i18n.stringCase.upper);
  1471. this.bv.resetForm();
  1472. this.$userName.val('123');
  1473. this.bv.validate();
  1474. expect(this.bv.getMessages('username', 'stringLength')[0]).toEqual(format(i18n.stringLength.between, [6, 20]));
  1475. this.bv.resetForm();
  1476. this.$userName.val('contain@#$');
  1477. this.bv.validate();
  1478. expect(this.bv.getMessages(this.$userName, 'regexp')[0]).toEqual(i18n.regexp['default']);
  1479. this.bv.resetForm();
  1480. this.$userName.val('validUserName');
  1481. this.$password.val('validUserName');
  1482. this.bv.validate();
  1483. expect(this.bv.getMessages('username', 'different')[0]).toEqual(i18n.different['default']);
  1484. this.bv.resetForm();
  1485. this.$email.val('A@b@c@example.com');
  1486. this.bv.validate();
  1487. expect(this.bv.getMessages(this.$email, 'emailAddress')[0]).toEqual(i18n.emailAddress['default']);
  1488. this.bv.resetForm();
  1489. this.$password.val('@S3cur3P@@w0rd');
  1490. this.$confirm.val('notMatch');
  1491. this.bv.validate();
  1492. expect(this.bv.getMessages('password', 'identical')[0]).toEqual(i18n.identical['default']);
  1493. this.bv.resetForm();
  1494. this.$age.val('notDigit');
  1495. this.bv.validate();
  1496. expect(this.bv.getMessages('age', 'digits')[0]).toEqual(i18n.digits['default']);
  1497. this.bv.resetForm();
  1498. this.$age.val(10);
  1499. this.bv.validate();
  1500. expect(this.bv.getMessages(this.$age, 'greaterThan')[0]).toEqual(format(i18n.greaterThan['default'], 18));
  1501. this.bv.resetForm();
  1502. this.$age.val(120);
  1503. this.bv.validate();
  1504. expect(this.bv.getMessages('age', 'lessThan')[0]).toEqual(format(i18n.lessThan['default'], 100));
  1505. this.bv.resetForm();
  1506. this.$website.val('http://invalidWebsite');
  1507. this.bv.validate();
  1508. expect(this.bv.getMessages('website', 'uri')[0]).toEqual(i18n.uri['default']);
  1509. this.bv.resetForm();
  1510. this.$phone.val('123456');
  1511. this.bv.validate();
  1512. expect(this.bv.getMessages('phoneNumber', 'phone')[0]).toEqual(format(i18n.phone.country, i18n.phone.countries['US']));
  1513. this.bv.resetForm();
  1514. this.$program.eq(0).prop('checked', 'checked');
  1515. this.bv.validate();
  1516. expect(this.bv.getMessages(this.$program, 'choice')[0]).toEqual(format(i18n.choice.between, [2, 4]));
  1517. this.bv.resetForm();
  1518. this.$program.prop('checked', 'checked');
  1519. this.bv.validate();
  1520. expect(this.bv.getMessages('programs[]', 'choice')[0]).toEqual(format(i18n.choice.between, [2, 4]));
  1521. });
  1522. });
  1523. describe('message', function() {
  1524. beforeEach(function() {
  1525. var html = [
  1526. '<div class="container">',
  1527. '<form class="form-horizontal" id="messageForm">',
  1528. '<div class="form-group">',
  1529. '<input type="password" class="form-control" name="password" placeholder="Enter secure password" />',
  1530. '</div>',
  1531. '</form>',
  1532. '</div>'
  1533. ].join('\n');
  1534. $(html).appendTo('body');
  1535. $('#messageForm').bootstrapValidator({
  1536. fields: {
  1537. password: {
  1538. validators: {
  1539. notEmpty: {
  1540. message: 'The password is required'
  1541. },
  1542. callback: {
  1543. callback: function(value, validator) {
  1544. // Check the password strength
  1545. if (value.length < 6) {
  1546. return {
  1547. valid: false,
  1548. message: 'The password must be more than 6 characters'
  1549. }
  1550. }
  1551. if (value === value.toLowerCase()) {
  1552. return {
  1553. valid: false,
  1554. message: 'The password must contain at least one upper case character'
  1555. }
  1556. }
  1557. if (value === value.toUpperCase()) {
  1558. return {
  1559. valid: false,
  1560. message: 'The password must contain at least one lower case character'
  1561. }
  1562. }
  1563. if (value.search(/[0-9]/) < 0) {
  1564. return {
  1565. valid: false,
  1566. message: 'The password must contain at least one digit'
  1567. }
  1568. }
  1569. return true;
  1570. }
  1571. }
  1572. }
  1573. }
  1574. }
  1575. });
  1576. this.bv = $('#messageForm').data('bootstrapValidator');
  1577. this.$password = this.bv.getFieldElements('password');
  1578. });
  1579. afterEach(function() {
  1580. $('#messageForm').bootstrapValidator('destroy').parent().remove();
  1581. });
  1582. it('update message from callback', function() {
  1583. this.bv.resetForm();
  1584. this.$password.val('123');
  1585. this.bv.validate();
  1586. expect(this.bv.getMessages('password', 'callback')[0]).toEqual('The password must be more than 6 characters');
  1587. this.bv.resetForm();
  1588. this.$password.val('no_upper_case!@#');
  1589. this.bv.validate();
  1590. expect(this.bv.getMessages('password', 'callback')[0]).toEqual('The password must contain at least one upper case character');
  1591. this.bv.resetForm();
  1592. this.$password.val('NO_LOWER_CASE123');
  1593. this.bv.validate();
  1594. expect(this.bv.getMessages('password', 'callback')[0]).toEqual('The password must contain at least one lower case character');
  1595. this.bv.resetForm();
  1596. this.$password.val('NoDigits!@#');
  1597. this.bv.validate();
  1598. expect(this.bv.getMessages('password', 'callback')[0]).toEqual('The password must contain at least one digit');
  1599. });
  1600. it('call updateMessage()', function() {
  1601. this.bv.updateStatus('password', this.bv.STATUS_INVALID, 'callback');
  1602. this.bv.updateMessage('password', 'callback', 'The password is weak');
  1603. expect(this.bv.getMessages('password', 'callback')[0]).toEqual('The password is weak');
  1604. this.bv.updateMessage(this.$password, 'callback', 'The password is not strong');
  1605. expect(this.bv.getMessages(this.$password, 'callback')[0]).toEqual('The password is not strong');
  1606. });
  1607. });
  1608. describe('verbose option', function() {
  1609. beforeEach(function() {
  1610. $([
  1611. '<form class="form-horizontal" id="verboseForm">',
  1612. '<div class="form-group">',
  1613. '<input type="text" name="fullName" class="form-control" ',
  1614. 'required data-bv-notempty-message="The full name is required and cannot be empty" ',
  1615. 'data-bv-regexp="true" data-bv-regexp-regexp="^[a-zA-Z\\s]+$" data-bv-regexp-message="The full name can only consist of alphabetical, number, and space" ',
  1616. 'data-bv-stringlength="true" data-bv-stringlength-min="8" data-bv-stringlength-max="40" data-bv-stringlength-message="The full name must be more than 8 and less than 40 characters long" ',
  1617. '/>',
  1618. '</div>',
  1619. '</form>'
  1620. ].join('\n')).appendTo('body');
  1621. // The order of validators are alphabetical:
  1622. // - notEmpty
  1623. // - regexp
  1624. // - stringLength
  1625. });
  1626. afterEach(function() {
  1627. $('#verboseForm').bootstrapValidator('destroy').remove();
  1628. });
  1629. it('set data-bv-verbose="false" for form', function() {
  1630. var bv = $('#verboseForm').attr('data-bv-verbose', 'false').bootstrapValidator().data('bootstrapValidator'),
  1631. $fullName = bv.getFieldElements('fullName'),
  1632. messages;
  1633. $fullName.val('');
  1634. bv.validate();
  1635. messages = bv.getMessages('fullName');
  1636. expect(messages.length).toEqual(1);
  1637. expect(messages[0]).toEqual($fullName.attr('data-bv-notempty-message'));
  1638. bv.resetForm();
  1639. $fullName.val('Special@#$');
  1640. bv.validate();
  1641. messages = bv.getMessages('fullName');
  1642. expect(messages.length).toEqual(1);
  1643. expect(messages[0]).toEqual($fullName.attr('data-bv-regexp-message'));
  1644. bv.resetForm();
  1645. $fullName.val('Full');
  1646. bv.validate();
  1647. messages = bv.getMessages('fullName');
  1648. expect(messages.length).toEqual(1);
  1649. expect(messages[0]).toEqual($fullName.attr('data-bv-stringlength-message'));
  1650. });
  1651. it('set data-bv-verbose="false" for field', function() {
  1652. var bv = $('#verboseForm')
  1653. .attr('data-bv-verbose', 'true')
  1654. .find('[name="fullName"]')
  1655. .attr('data-bv-verbose', 'false')
  1656. .end()
  1657. .bootstrapValidator().data('bootstrapValidator'),
  1658. $fullName = bv.getFieldElements('fullName'),
  1659. messages;
  1660. $fullName.val('');
  1661. bv.validate();
  1662. messages = bv.getMessages('fullName');
  1663. expect(messages.length).toEqual(1);
  1664. expect(messages[0]).toEqual($fullName.attr('data-bv-notempty-message'));
  1665. bv.resetForm();
  1666. $fullName.val('Special@#$');
  1667. bv.validate();
  1668. messages = bv.getMessages('fullName');
  1669. expect(messages.length).toEqual(1);
  1670. expect(messages[0]).toEqual($fullName.attr('data-bv-regexp-message'));
  1671. bv.resetForm();
  1672. $fullName.val('Full');
  1673. bv.validate();
  1674. messages = bv.getMessages('fullName');
  1675. expect(messages.length).toEqual(1);
  1676. expect(messages[0]).toEqual($fullName.attr('data-bv-stringlength-message'));
  1677. });
  1678. it('set verbose: "false" for form', function() {
  1679. var bv = $('#verboseForm').bootstrapValidator({ verbose: false }).data('bootstrapValidator'),
  1680. $fullName = bv.getFieldElements('fullName'),
  1681. messages;
  1682. $fullName.val('');
  1683. bv.validate();
  1684. messages = bv.getMessages('fullName');
  1685. expect(messages.length).toEqual(1);
  1686. expect(messages[0]).toEqual($fullName.attr('data-bv-notempty-message'));
  1687. bv.resetForm();
  1688. $fullName.val('Special@#$');
  1689. bv.validate();
  1690. messages = bv.getMessages('fullName');
  1691. expect(messages.length).toEqual(1);
  1692. expect(messages[0]).toEqual($fullName.attr('data-bv-regexp-message'));
  1693. bv.resetForm();
  1694. $fullName.val('Full');
  1695. bv.validate();
  1696. messages = bv.getMessages('fullName');
  1697. expect(messages.length).toEqual(1);
  1698. expect(messages[0]).toEqual($fullName.attr('data-bv-stringlength-message'));
  1699. });
  1700. it('set verbose: "false" for field', function() {
  1701. var bv = $('#verboseForm')
  1702. .attr('data-bv-verbose', 'true')
  1703. .bootstrapValidator({
  1704. verbose: true,
  1705. fields: {
  1706. fullName: {
  1707. verbose: false
  1708. }
  1709. }
  1710. })
  1711. .data('bootstrapValidator'),
  1712. $fullName = bv.getFieldElements('fullName'),
  1713. messages;
  1714. $fullName.val('');
  1715. bv.validate();
  1716. messages = bv.getMessages('fullName');
  1717. expect(messages.length).toEqual(1);
  1718. expect(messages[0]).toEqual($fullName.attr('data-bv-notempty-message'));
  1719. bv.resetForm();
  1720. $fullName.val('Special@#$');
  1721. bv.validate();
  1722. messages = bv.getMessages('fullName');
  1723. expect(messages.length).toEqual(1);
  1724. expect(messages[0]).toEqual($fullName.attr('data-bv-regexp-message'));
  1725. bv.resetForm();
  1726. $fullName.val('Full');
  1727. bv.validate();
  1728. messages = bv.getMessages('fullName');
  1729. expect(messages.length).toEqual(1);
  1730. expect(messages[0]).toEqual($fullName.attr('data-bv-stringlength-message'));
  1731. });
  1732. });
  1733. function betweenCompareMin() {
  1734. var compareTo = $('#betweenForm').find('[name="minAge"]').val();
  1735. $('#msgMin').html('betweenCompareMin() called; compare to ' + compareTo);
  1736. return compareTo;
  1737. };
  1738. function betweenCompareMax() {
  1739. var compareTo = $('#betweenForm').find('[name="maxAge"]').val();
  1740. $('#msgMax').html('betweenCompareMax() called; compare to ' + compareTo);
  1741. return compareTo;
  1742. };
  1743. TestSuite = $.extend({}, TestSuite, {
  1744. between: {
  1745. compareToMin: function(value, validator, $field) {
  1746. var compareTo = $('#betweenForm').find('[name="minAge"]').val();
  1747. $('#msgMin').html('TestSuite.between.compareToMin() called; compare to ' + compareTo);
  1748. return compareTo;
  1749. },
  1750. compareToMax: function(value, validator, $field) {
  1751. var compareTo = $('#betweenForm').find('[name="maxAge"]').val();
  1752. $('#msgMax').html('TestSuite.between.compareToMax() called; compare to ' + compareTo);
  1753. return compareTo;
  1754. }
  1755. }
  1756. });
  1757. describe('between', function() {
  1758. beforeEach(function() {
  1759. $([
  1760. '<form class="form-horizontal" id="betweenForm">',
  1761. '<div id="msgMin"></div>',
  1762. '<div id="msgMax"></div>',
  1763. '<div class="form-group">',
  1764. '<input type="text" name="minAge" />',
  1765. '</div>',
  1766. '<div class="form-group">',
  1767. '<input type="text" name="maxAge" />',
  1768. '</div>',
  1769. '<div class="form-group">',
  1770. '<input type="text" name="age" data-bv-between data-bv-between-min="18" data-bv-between-max="100" />',
  1771. '</div>',
  1772. '</form>'
  1773. ].join('\n')).appendTo('body');
  1774. $('#betweenForm').bootstrapValidator();
  1775. this.bv = $('#betweenForm').data('bootstrapValidator');
  1776. this.$minAge = this.bv.getFieldElements('minAge');
  1777. this.$maxAge = this.bv.getFieldElements('maxAge');
  1778. this.$age = this.bv.getFieldElements('age');
  1779. });
  1780. afterEach(function() {
  1781. $('#betweenForm').bootstrapValidator('destroy').remove();
  1782. });
  1783. it('not a number', function() {
  1784. this.$age.val('50abc');
  1785. this.bv.validate();
  1786. expect(this.bv.isValid()).toEqual(false);
  1787. });
  1788. it('compare to value', function() {
  1789. this.$age.val(10);
  1790. this.bv.validate();
  1791. expect(this.bv.isValid()).toEqual(false);
  1792. this.bv.resetForm();
  1793. this.$age.val(120);
  1794. this.bv.validate();
  1795. expect(this.bv.isValid()).toEqual(false);
  1796. this.bv.resetForm();
  1797. this.$age.val(30);
  1798. this.bv.validate();
  1799. expect(this.bv.isValid()).toBeTruthy();
  1800. });
  1801. it('compare to other field', function() {
  1802. this.$age.attr('data-bv-between-min', 'minAge')
  1803. .attr('data-bv-between-max', 'maxAge');
  1804. this.bv.destroy();
  1805. this.bv = $('#betweenForm').bootstrapValidator().data('bootstrapValidator');
  1806. this.$minAge.val(2);
  1807. this.$maxAge.val(10);
  1808. this.$age.val(5);
  1809. this.bv.validate();
  1810. expect(this.bv.isValid()).toBeTruthy();
  1811. this.bv.resetForm();
  1812. this.$minAge.val(20);
  1813. this.$maxAge.val(40);
  1814. this.$age.val(50);
  1815. this.bv.validate();
  1816. expect(this.bv.isValid()).toEqual(false);
  1817. expect(this.bv.getMessages('age', 'between')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.between['default'], [this.$minAge.val(), this.$maxAge.val()]));
  1818. });
  1819. it('compare to return value of a function', function() {
  1820. this.$age.attr('data-bv-between-min', 'betweenCompareMin')
  1821. .attr('data-bv-between-max', 'betweenCompareMax');
  1822. this.bv.destroy();
  1823. this.bv = $('#betweenForm').bootstrapValidator().data('bootstrapValidator');
  1824. this.$minAge.val(20);
  1825. this.$maxAge.val(30);
  1826. this.$age.val(18);
  1827. this.bv.validate();
  1828. expect($('#msgMin').html()).toEqual('betweenCompareMin() called; compare to 20');
  1829. expect($('#msgMax').html()).toEqual('betweenCompareMax() called; compare to 30');
  1830. expect(this.bv.isValid()).toEqual(false);
  1831. expect(this.bv.getMessages('age', 'between')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.between['default'], [this.$minAge.val(), this.$maxAge.val()]));
  1832. this.bv.resetForm();
  1833. this.$minAge.val(2);
  1834. this.$maxAge.val(10);
  1835. this.$age.val(6);
  1836. this.bv.validate();
  1837. expect($('#msgMin').html()).toEqual('betweenCompareMin() called; compare to 2');
  1838. expect($('#msgMax').html()).toEqual('betweenCompareMax() called; compare to 10');
  1839. expect(this.bv.isValid()).toBeTruthy();
  1840. });
  1841. it('compare to return value of a namespace function', function() {
  1842. this.$age.attr('data-bv-between-min', 'TestSuite.between.compareToMin')
  1843. .attr('data-bv-between-max', 'TestSuite.between.compareToMax');
  1844. this.bv.destroy();
  1845. this.bv = $('#betweenForm').bootstrapValidator().data('bootstrapValidator');
  1846. this.$minAge.val(20);
  1847. this.$maxAge.val(30);
  1848. this.$age.val(40);
  1849. this.bv.validate();
  1850. expect($('#msgMin').html()).toEqual('TestSuite.between.compareToMin() called; compare to 20');
  1851. expect($('#msgMax').html()).toEqual('TestSuite.between.compareToMax() called; compare to 30');
  1852. expect(this.bv.isValid()).toEqual(false);
  1853. expect(this.bv.getMessages('age', 'between')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.between['default'], [this.$minAge.val(), this.$maxAge.val()]));
  1854. this.bv.resetForm();
  1855. this.$minAge.val(2);
  1856. this.$maxAge.val(10);
  1857. this.$age.val(5);
  1858. this.bv.validate();
  1859. expect($('#msgMin').html()).toEqual('TestSuite.between.compareToMin() called; compare to 2');
  1860. expect($('#msgMax').html()).toEqual('TestSuite.between.compareToMax() called; compare to 10');
  1861. expect(this.bv.isValid()).toBeTruthy();
  1862. });
  1863. });
  1864. function validateCaptcha(value, validator, $field) {
  1865. var items = $('#captchaOperation').html().split(' '), sum = parseInt(items[0]) + parseInt(items[2]);
  1866. return value === sum + '';
  1867. };
  1868. describe('callback', function() {
  1869. beforeEach(function() {
  1870. $([
  1871. '<form class="form-horizontal" id="callbackForm">',
  1872. '<div class="form-group">',
  1873. '<label class="col-md-3 control-label" id="captchaOperation"></label>',
  1874. '<div class="col-md-2">',
  1875. '<input type="text" class="form-control" name="captcha" />',
  1876. '</div>',
  1877. '</div>',
  1878. '<div class="form-group">',
  1879. '<div class="col-md-2 col-md-offset-3">',
  1880. '<input type="text" class="form-control" name="declarativeCaptcha" data-bv-callback data-bv-callback-callback="validateCaptcha" />',
  1881. '</div>',
  1882. '</div>',
  1883. '</form>'
  1884. ].join('\n')).appendTo('body');
  1885. $('#callbackForm').bootstrapValidator({
  1886. fields: {
  1887. captcha: {
  1888. validators: {
  1889. callback: {
  1890. message: 'Wrong answer',
  1891. callback: function(value, validator, $field) {
  1892. return validateCaptcha(value, validator, $field);
  1893. }
  1894. }
  1895. }
  1896. }
  1897. }
  1898. });
  1899. this.bv = $('#callbackForm').data('bootstrapValidator');
  1900. this.$captcha = this.bv.getFieldElements('captcha');
  1901. this.$declarativeCaptcha = this.bv.getFieldElements('declarativeCaptcha');
  1902. });
  1903. afterEach(function() {
  1904. $('#callbackForm').bootstrapValidator('destroy').remove();
  1905. });
  1906. it('execute the callback', function() {
  1907. $('#captchaOperation').html('1 + 2');
  1908. this.$captcha.val('3');
  1909. this.bv.validate();
  1910. expect(this.bv.isValidField('captcha')).toBeTruthy();
  1911. this.bv.resetForm();
  1912. this.$captcha.val('5');
  1913. this.bv.validate();
  1914. expect(this.bv.isValidField('captcha')).toEqual(false);
  1915. });
  1916. it('callback declarative', function() {
  1917. $('#captchaOperation').html('10 + 20');
  1918. this.$declarativeCaptcha.val('40');
  1919. this.bv.validate();
  1920. expect(this.bv.isValidField('declarativeCaptcha')).toEqual(false);
  1921. this.bv.resetForm();
  1922. this.$declarativeCaptcha.val('30');
  1923. this.bv.validate();
  1924. expect(this.bv.isValidField('declarativeCaptcha')).toBeTruthy();
  1925. });
  1926. });
  1927. describe('creditCard', function() {
  1928. // Get the fake credit card number at http://www.getcreditcardnumbers.com/
  1929. beforeEach(function() {
  1930. var html = [
  1931. '<div class="container">',
  1932. '<form class="form-horizontal" id="ccForm">',
  1933. '<div class="form-group">',
  1934. '<input type="text" name="cc" data-bv-creditcard />',
  1935. '</div>',
  1936. '</form>',
  1937. '</div>'
  1938. ].join('\n');
  1939. $(html).appendTo('body');
  1940. $('#ccForm').bootstrapValidator();
  1941. this.bv = $('#ccForm').data('bootstrapValidator');
  1942. this.$creditCard = this.bv.getFieldElements('cc');
  1943. });
  1944. afterEach(function() {
  1945. $('#ccForm').bootstrapValidator('destroy').parent().remove();
  1946. });
  1947. it('accept spaces', function() {
  1948. this.$creditCard.val('5267 9789 9451 9654');
  1949. this.bv.validate();
  1950. expect(this.bv.isValidField('cc')).toBeTruthy();
  1951. });
  1952. it('accept dashes', function() {
  1953. this.$creditCard.val('6011-2649-6840-4521');
  1954. this.bv.validate();
  1955. expect(this.bv.isValidField('cc')).toBeTruthy();
  1956. });
  1957. it('invalid format', function() {
  1958. this.$creditCard.val('4539.1870.2954.3862');
  1959. this.bv.validate();
  1960. expect(this.bv.isValidField('cc')).toEqual(false);
  1961. });
  1962. it('American Express', function() {
  1963. this.$creditCard.val('340653705597107');
  1964. this.bv.validate();
  1965. expect(this.bv.isValidField('cc')).toBeTruthy();
  1966. });
  1967. it('American Express invalid length', function() {
  1968. this.$creditCard.val('3744148309166730');
  1969. this.bv.validate();
  1970. expect(this.bv.isValidField('cc')).toEqual(false);
  1971. });
  1972. it('American Express invalid prefix', function() {
  1973. this.$creditCard.val('356120148436654');
  1974. this.bv.validate();
  1975. expect(this.bv.isValidField('cc')).toEqual(false);
  1976. });
  1977. it('Diners Club', function() {
  1978. this.$creditCard.val('30130708434187');
  1979. this.bv.validate();
  1980. expect(this.bv.isValidField('cc')).toBeTruthy();
  1981. });
  1982. it('Diners Club (US)', function() {
  1983. this.$creditCard.val('5517479515603901');
  1984. this.bv.validate();
  1985. expect(this.bv.isValidField('cc')).toBeTruthy();
  1986. });
  1987. it('Discover', function() {
  1988. this.$creditCard.val('6011734674929094');
  1989. this.bv.validate();
  1990. expect(this.bv.isValidField('cc')).toBeTruthy();
  1991. });
  1992. it('JCB', function() {
  1993. this.$creditCard.val('3566002020360505');
  1994. this.bv.validate();
  1995. expect(this.bv.isValidField('cc')).toBeTruthy();
  1996. });
  1997. it('Laser', function() {
  1998. this.$creditCard.val('6304 9000 1774 0292 441');
  1999. this.bv.validate();
  2000. expect(this.bv.isValidField('cc')).toBeTruthy();
  2001. });
  2002. it('Maestro', function() {
  2003. this.$creditCard.val('6762835098779303');
  2004. this.bv.validate();
  2005. expect(this.bv.isValidField('cc')).toBeTruthy();
  2006. });
  2007. it('Mastercard', function() {
  2008. this.$creditCard.val('5303765013600904');
  2009. this.bv.validate();
  2010. expect(this.bv.isValidField('cc')).toBeTruthy();
  2011. });
  2012. it('Solo', function() {
  2013. this.$creditCard.val('6334580500000000');
  2014. this.bv.validate();
  2015. expect(this.bv.isValidField('cc')).toBeTruthy();
  2016. });
  2017. it('Visa', function() {
  2018. this.$creditCard.val('4929248980295542');
  2019. this.bv.validate();
  2020. expect(this.bv.isValidField('cc')).toBeTruthy();
  2021. });
  2022. it('Visa invalid check digit', function() {
  2023. this.$creditCard.val('4532599916257826');
  2024. this.bv.validate();
  2025. expect(this.bv.isValidField('cc')).toEqual(false);
  2026. });
  2027. });
  2028. describe('date', function() {
  2029. beforeEach(function() {
  2030. $([
  2031. '<form class="form-horizontal" id="dateForm">',
  2032. '<div id="msg"></div>',
  2033. '<div class="form-group">',
  2034. '<input type="text" name="date" data-bv-date />',
  2035. '</div>',
  2036. '</form>'
  2037. ].join('\n')).appendTo('body');
  2038. $('#dateForm').bootstrapValidator();
  2039. this.bv = $('#dateForm').data('bootstrapValidator');
  2040. this.$date = this.bv.getFieldElements('date');
  2041. });
  2042. afterEach(function() {
  2043. $('#dateForm').bootstrapValidator('destroy').remove();
  2044. });
  2045. it('YYYY/MM/DD', function() {
  2046. this.bv.updateOption('date', 'date', 'format', 'YYYY/MM/DD');
  2047. this.$date.val('2000/01/30');
  2048. this.bv.validate();
  2049. expect(this.bv.isValid()).toBeTruthy();
  2050. // Invalid year
  2051. this.bv.resetForm();
  2052. this.$date.val('100/10/20');
  2053. this.bv.validate();
  2054. expect(this.bv.isValid()).toEqual(false);
  2055. // Invalid month
  2056. this.bv.resetForm();
  2057. this.$date.val('2000/00/10');
  2058. this.bv.validate();
  2059. expect(this.bv.isValid()).toEqual(false);
  2060. this.bv.resetForm();
  2061. this.$date.val('2000/15/10');
  2062. this.bv.validate();
  2063. expect(this.bv.isValid()).toEqual(false);
  2064. // Invalid day
  2065. this.bv.resetForm();
  2066. this.$date.val('2000/03/00');
  2067. this.bv.validate();
  2068. expect(this.bv.isValid()).toEqual(false);
  2069. this.bv.resetForm();
  2070. this.$date.val('2000/10/32');
  2071. this.bv.validate();
  2072. expect(this.bv.isValid()).toEqual(false);
  2073. // Negative number
  2074. this.bv.resetForm();
  2075. this.$date.val('-2000/10/20');
  2076. this.bv.validate();
  2077. expect(this.bv.isValid()).toEqual(false);
  2078. this.bv.resetForm();
  2079. this.$date.val('2000/-10/20');
  2080. this.bv.validate();
  2081. expect(this.bv.isValid()).toEqual(false);
  2082. this.bv.resetForm();
  2083. this.$date.val('2000/10/-20');
  2084. this.bv.validate();
  2085. expect(this.bv.isValid()).toEqual(false);
  2086. // Consist invalid characters
  2087. // Issue #310
  2088. this.bv.resetForm();
  2089. this.$date.val('aaaa/');
  2090. this.bv.validate();
  2091. expect(this.bv.isValid()).toEqual(false);
  2092. this.bv.resetForm();
  2093. this.$date.val('2004df/1dd1/5ffg');
  2094. this.bv.validate();
  2095. expect(this.bv.isValid()).toEqual(false);
  2096. // Issue #475
  2097. this.bv.resetForm();
  2098. this.$date.val('2014/09');
  2099. this.bv.validate();
  2100. expect(this.bv.isValid()).toEqual(false);
  2101. this.bv.resetForm();
  2102. this.$date.val('2014/09/');
  2103. this.bv.validate();
  2104. expect(this.bv.isValid()).toEqual(false);
  2105. this.bv.resetForm();
  2106. this.$date.val('2014//15');
  2107. this.bv.validate();
  2108. expect(this.bv.isValid()).toEqual(false);
  2109. this.bv.resetForm();
  2110. this.$date.val('/09/15');
  2111. this.bv.validate();
  2112. expect(this.bv.isValid()).toEqual(false);
  2113. });
  2114. it('MM/DD/YYYY', function() {
  2115. this.bv.updateOption('date', 'date', 'format', 'MM/DD/YYYY');
  2116. this.$date.val('09/15/2020');
  2117. this.bv.validate();
  2118. expect(this.bv.isValid()).toBeTruthy();
  2119. this.bv.resetForm();
  2120. this.$date.val('09/15');
  2121. this.bv.validate();
  2122. expect(this.bv.isValid()).toEqual(false);
  2123. this.bv.resetForm();
  2124. this.$date.val('09/15/');
  2125. this.bv.validate();
  2126. expect(this.bv.isValid()).toEqual(false);
  2127. });
  2128. it('number of days in February', function() {
  2129. this.bv.updateOption('date', 'date', 'format', 'YYYY/MM/DD');
  2130. this.$date.val('2000/02/28');
  2131. this.bv.validate();
  2132. expect(this.bv.isValid()).toBeTruthy();
  2133. this.bv.resetForm();
  2134. this.$date.val('2000/02/29');
  2135. this.bv.validate();
  2136. expect(this.bv.isValid()).toBeTruthy();
  2137. this.bv.resetForm();
  2138. this.$date.val('2001/02/29');
  2139. this.bv.validate();
  2140. expect(this.bv.isValid()).toEqual(false);
  2141. });
  2142. // Issue #681
  2143. it('date, month, year are prefixed by zero', function() {
  2144. this.bv.updateOption('date', 'date', 'format', 'MM/DD/YYYY');
  2145. this.$date.val('0012/08/2014');
  2146. this.bv.validate();
  2147. expect(this.bv.isValid()).toEqual(false);
  2148. this.bv.resetForm();
  2149. this.$date.val('12/0008/2014');
  2150. this.bv.validate();
  2151. expect(this.bv.isValid()).toEqual(false);
  2152. this.bv.resetForm();
  2153. this.$date.val('12/08/002014');
  2154. this.bv.validate();
  2155. expect(this.bv.isValid()).toEqual(false);
  2156. this.bv.resetForm();
  2157. this.$date.val('12/08/2014');
  2158. this.bv.validate();
  2159. expect(this.bv.isValid()).toBeTruthy();
  2160. });
  2161. it('hours, minutes, seconds are prefixed by zero', function() {
  2162. this.bv.updateOption('date', 'date', 'format', 'YYYY/MM/DD h:m:s');
  2163. this.$date.val('2014/08/17 0007:30:00');
  2164. this.bv.validate();
  2165. expect(this.bv.isValid()).toEqual(false);
  2166. this.bv.resetForm();
  2167. this.$date.val('2014/08/17 07:030:00');
  2168. this.bv.validate();
  2169. expect(this.bv.isValid()).toEqual(false);
  2170. this.bv.resetForm();
  2171. this.$date.val('2014/08/17 07:30:0000');
  2172. this.bv.validate();
  2173. expect(this.bv.isValid()).toEqual(false);
  2174. this.bv.resetForm();
  2175. this.$date.val('2014/08/17 07:30:00');
  2176. this.bv.validate();
  2177. expect(this.bv.isValid()).toBeTruthy();
  2178. });
  2179. });
  2180. describe('ean', function() {
  2181. beforeEach(function() {
  2182. var html = [
  2183. '<div class="container">',
  2184. '<form class="form-horizontal" id="eanForm">',
  2185. '<div class="form-group">',
  2186. '<input type="text" name="ean" data-bv-ean />',
  2187. '</div>',
  2188. '</form>',
  2189. '</div>'
  2190. ].join('\n');
  2191. $(html).appendTo('body');
  2192. $('#eanForm').bootstrapValidator();
  2193. this.bv = $('#eanForm').data('bootstrapValidator');
  2194. this.$ean = this.bv.getFieldElements('ean');
  2195. });
  2196. afterEach(function() {
  2197. $('#eanForm').bootstrapValidator('destroy').parent().remove();
  2198. });
  2199. it('valid', function() {
  2200. var samples = ['73513537', '9780471117094', '4006381333931'];
  2201. for (var i in samples) {
  2202. this.$ean.val(samples[i]);
  2203. this.bv.validate();
  2204. expect(this.bv.isValidField('ean')).toBeTruthy();
  2205. }
  2206. });
  2207. it('contains only digits', function() {
  2208. this.$ean.val('123abcDEF!@#');
  2209. this.bv.validate();
  2210. expect(this.bv.isValidField('ean')).toEqual(false);
  2211. });
  2212. it('invalid length', function() {
  2213. this.$ean.val('1234567');
  2214. this.bv.validate();
  2215. expect(this.bv.isValidField('ean')).toEqual(false);
  2216. });
  2217. it('invalid check digit', function() {
  2218. this.$ean.val('73513536');
  2219. this.bv.validate();
  2220. expect(this.bv.isValidField('ean')).toEqual(false);
  2221. });
  2222. });
  2223. describe('emailAddress', function() {
  2224. beforeEach(function() {
  2225. $([
  2226. '<form class="form-horizontal" id="emailAddressForm">',
  2227. '<div id="msg"></div>',
  2228. '<div class="form-group">',
  2229. '<input type="text" name="email-address-or-addresses" data-bv-emailaddress />',
  2230. '</div>',
  2231. '</form>'
  2232. ].join('\n')).appendTo('body');
  2233. $('#emailAddressForm').bootstrapValidator();
  2234. this.bv = $('#emailAddressForm').data('bootstrapValidator');
  2235. this.$emailAddressOrAddresses = this.bv.getFieldElements('email-address-or-addresses');
  2236. });
  2237. afterEach(function() {
  2238. $('#emailAddressForm').bootstrapValidator('destroy').remove();
  2239. });
  2240. var validEmailAddresses = [
  2241. 'admin@mailserver1',
  2242. 'niceandsimple@example.com',
  2243. 'very.common@example.com',
  2244. 'a.little.lengthy.but.fine@dept.example.com',
  2245. 'disposable.style.email.with+symbol@example.com',
  2246. 'other.email-with-dash@example.com',
  2247. '"much.more unusual"@example.com',
  2248. '"very.unusual.@.unusual.com"@example.com',
  2249. '"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com',
  2250. '" "@example.org',
  2251. 'üñîçøðé@example.com'
  2252. ];
  2253. var invalidEmailAddresses = [
  2254. // "!#$%&'*+-/=?^_`{}|~@example.org", // This is actually passing validation; see https://github.com/nghuuphuoc/bootstrapvalidator/issues/673
  2255. 'üñîçøðé@üñîçøðé.com',
  2256. 'Abc.example.com',
  2257. 'A@b@c@example.com',
  2258. 'a"b(c)d,e:f;gi[j\k]l@example.com',
  2259. 'just"not"right@example.com',
  2260. 'this is"not\allowed@example.com',
  2261. 'this\ still\"not\\allowed@example.com'
  2262. ];
  2263. var validMultipleEmailAddressesForDefaultSeparators = [
  2264. 'niceandsimple@example.com,very.common@example.com',
  2265. 'niceandsimple@example.com;very.common@example.com',
  2266. 'niceandsimple@example.com;very.common@example.com,a.little.lengthy.but.fine@dept.example.com'
  2267. ];
  2268. var invalidMultipleEmailAddressesForDefaultSeparators = [
  2269. 'niceandsimple@example.com+very.common@example.com',
  2270. 'niceandsimple@example.com|very.common@example.com'
  2271. ];
  2272. var validMultipleEmailAddressesForCommaOrDollarSignSeparators = [
  2273. 'niceandsimple@example.com,very.common@example.com',
  2274. 'niceandsimple@example.com$very.common@example.com',
  2275. 'niceandsimple@example.com,very.common@example.com$a.little.lengthy.but.fine@dept.example.com'
  2276. ];
  2277. var invalidMultipleEmailAddressesForCommaOrDollarSignSeparators = [
  2278. 'niceandsimple@example.com;very.common@example.com',
  2279. 'niceandsimple@example.com;very.common@example.com,a.little.lengthy.but.fine@dept.example.com'
  2280. ];
  2281. it('Valid email addresses (multiple=false)', function() {
  2282. var that = this;
  2283. $.each(validEmailAddresses, function(index, emailAddress) {
  2284. that.bv.resetForm();
  2285. that.$emailAddressOrAddresses.val(emailAddress);
  2286. that.bv.validate();
  2287. expect(that.bv.isValid()).toBeTruthy();
  2288. });
  2289. });
  2290. it('Invalid email addresses (multiple=false)', function() {
  2291. var that = this;
  2292. var addresses = invalidEmailAddresses
  2293. .concat(validMultipleEmailAddressesForDefaultSeparators)
  2294. .concat(invalidMultipleEmailAddressesForDefaultSeparators)
  2295. .concat(validMultipleEmailAddressesForCommaOrDollarSignSeparators)
  2296. .concat(invalidMultipleEmailAddressesForCommaOrDollarSignSeparators);
  2297. $.each(addresses, function(index, emailAddress) {
  2298. that.bv.resetForm();
  2299. that.$emailAddressOrAddresses.val(emailAddress);
  2300. that.bv.validate();
  2301. expect(that.bv.isValid()).toEqual(false);
  2302. });
  2303. });
  2304. it('Invalid email addresses (multiple=false,separator=/[,\$]/)', function() {
  2305. var that = this;
  2306. that.bv.updateOption('email-address-or-addresses', 'emailAddress', 'separator', /[,;]/);
  2307. var addresses = invalidEmailAddresses
  2308. .concat(validMultipleEmailAddressesForDefaultSeparators)
  2309. .concat(invalidMultipleEmailAddressesForDefaultSeparators)
  2310. .concat(validMultipleEmailAddressesForCommaOrDollarSignSeparators)
  2311. .concat(invalidMultipleEmailAddressesForCommaOrDollarSignSeparators);
  2312. $.each(addresses, function(index, emailAddress) {
  2313. that.bv.resetForm();
  2314. that.$emailAddressOrAddresses.val(emailAddress);
  2315. that.bv.validate();
  2316. expect(that.bv.isValid()).toEqual(false);
  2317. });
  2318. });
  2319. it('Valid email addresses (multiple=true)', function() {
  2320. var that = this;
  2321. that.bv.updateOption('email-address-or-addresses', 'emailAddress', 'multiple', true);
  2322. var addresses = validEmailAddresses
  2323. .concat(validMultipleEmailAddressesForDefaultSeparators);
  2324. $.each(addresses, function(index, emailAddress) {
  2325. that.bv.resetForm();
  2326. that.$emailAddressOrAddresses.val(emailAddress);
  2327. that.bv.validate();
  2328. expect(that.bv.isValid()).toBeTruthy();
  2329. });
  2330. });
  2331. it('Invalid email addresses (multiple=true)', function() {
  2332. var that = this;
  2333. that.bv.updateOption('email-address-or-addresses', 'emailAddress', 'multiple', true);
  2334. var addresses = invalidEmailAddresses
  2335. .concat(invalidMultipleEmailAddressesForDefaultSeparators);
  2336. $.each(addresses, function(index, emailAddress) {
  2337. that.bv.resetForm();
  2338. that.$emailAddressOrAddresses.val(emailAddress);
  2339. that.bv.validate();
  2340. expect(that.bv.isValid()).toEqual(false);
  2341. });
  2342. });
  2343. it('Valid email addresses (multiple=true,separator=/[,\$]/)', function() {
  2344. var that = this;
  2345. that.bv.updateOption('email-address-or-addresses', 'emailAddress', 'multiple', true);
  2346. that.bv.updateOption('email-address-or-addresses', 'emailAddress', 'separator', /[,\$]/);
  2347. var addresses = validEmailAddresses
  2348. .concat(validMultipleEmailAddressesForCommaOrDollarSignSeparators);
  2349. $.each(addresses, function(index, emailAddress) {
  2350. that.bv.resetForm();
  2351. that.$emailAddressOrAddresses.val(emailAddress);
  2352. that.bv.validate();
  2353. expect(that.bv.isValid()).toBeTruthy();
  2354. });
  2355. });
  2356. it('Invalid email addresses (multiple=true,separator=/[,\$]/)', function() {
  2357. var that = this;
  2358. that.bv.updateOption('email-address-or-addresses', 'emailAddress', 'multiple', true);
  2359. that.bv.updateOption('email-address-or-addresses', 'emailAddress', 'separator', /[,\$]/);
  2360. var addresses = invalidEmailAddresses
  2361. .concat(invalidMultipleEmailAddressesForCommaOrDollarSignSeparators);
  2362. $.each(addresses, function(index, emailAddress) {
  2363. that.bv.resetForm();
  2364. that.$emailAddressOrAddresses.val(emailAddress);
  2365. that.bv.validate();
  2366. expect(that.bv.isValid()).toEqual(false);
  2367. });
  2368. });
  2369. });
  2370. function greaterThanCompare() {
  2371. var compareTo = $('#greaterThanForm').find('[name="minAge"]').val();
  2372. $('#msg').html('greaterThanCompare() called; compare to ' + compareTo);
  2373. return compareTo;
  2374. };
  2375. TestSuite = $.extend({}, TestSuite, {
  2376. greaterThan: {
  2377. compareTo: function(value, validator, $field) {
  2378. var compareTo = $('#greaterThanForm').find('[name="minAge"]').val();
  2379. $('#msg').html('TestSuite.greaterThan.compareTo() called; compare to ' + compareTo);
  2380. return compareTo;
  2381. }
  2382. }
  2383. });
  2384. describe('greaterThan', function() {
  2385. beforeEach(function() {
  2386. $([
  2387. '<form class="form-horizontal" id="greaterThanForm">',
  2388. '<div id="msg"></div>',
  2389. '<div class="form-group">',
  2390. '<input type="text" name="minAge" />',
  2391. '</div>',
  2392. '<div class="form-group">',
  2393. '<input type="text" name="age" data-bv-greaterthan data-bv-greaterthan-value="18" />',
  2394. '</div>',
  2395. '</form>'
  2396. ].join('\n')).appendTo('body');
  2397. $('#greaterThanForm').bootstrapValidator();
  2398. this.bv = $('#greaterThanForm').data('bootstrapValidator');
  2399. this.$minAge = this.bv.getFieldElements('minAge');
  2400. this.$age = this.bv.getFieldElements('age');
  2401. });
  2402. afterEach(function() {
  2403. $('#greaterThanForm').bootstrapValidator('destroy').remove();
  2404. });
  2405. it('not a number', function() {
  2406. this.$age.val('20abc');
  2407. this.bv.validate();
  2408. expect(this.bv.isValid()).toEqual(false);
  2409. });
  2410. it('compare to value', function() {
  2411. this.$age.val(10);
  2412. this.bv.validate();
  2413. expect(this.bv.isValid()).toEqual(false);
  2414. this.bv.resetForm();
  2415. this.$age.val(20);
  2416. this.bv.validate();
  2417. expect(this.bv.isValid()).toBeTruthy();
  2418. });
  2419. it('compare to other field', function() {
  2420. this.$age.attr('data-bv-greaterthan-value', 'minAge');
  2421. this.bv.destroy();
  2422. this.bv = $('#greaterThanForm').bootstrapValidator().data('bootstrapValidator');
  2423. this.$minAge.val(10);
  2424. this.$age.val(20);
  2425. this.bv.validate();
  2426. expect(this.bv.isValid()).toBeTruthy();
  2427. this.bv.resetForm();
  2428. this.$minAge.val(20);
  2429. this.$age.val(10);
  2430. this.bv.validate();
  2431. expect(this.bv.isValid()).toEqual(false);
  2432. expect(this.bv.getMessages('age', 'greaterThan')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.greaterThan['default'], this.$minAge.val()));
  2433. });
  2434. it('compare to return value of a function', function() {
  2435. this.$age.attr('data-bv-greaterthan-value', 'greaterThanCompare');
  2436. this.bv.destroy();
  2437. this.bv = $('#greaterThanForm').bootstrapValidator().data('bootstrapValidator');
  2438. this.$minAge.val(20);
  2439. this.$age.val(18);
  2440. this.bv.validate();
  2441. expect($('#msg').html()).toEqual('greaterThanCompare() called; compare to 20');
  2442. expect(this.bv.isValid()).toEqual(false);
  2443. expect(this.bv.getMessages('age', 'greaterThan')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.greaterThan['default'], this.$minAge.val()));
  2444. this.bv.resetForm();
  2445. this.$minAge.val(18);
  2446. this.$age.val(20);
  2447. this.bv.validate();
  2448. expect($('#msg').html()).toEqual('greaterThanCompare() called; compare to 18');
  2449. expect(this.bv.isValid()).toBeTruthy();
  2450. });
  2451. it('compare to return value of a namespace function', function() {
  2452. this.$age.attr('data-bv-greaterthan-value', 'TestSuite.greaterThan.compareTo');
  2453. this.bv.destroy();
  2454. this.bv = $('#greaterThanForm').bootstrapValidator().data('bootstrapValidator');
  2455. this.$minAge.val(20);
  2456. this.$age.val(18);
  2457. this.bv.validate();
  2458. expect($('#msg').html()).toEqual('TestSuite.greaterThan.compareTo() called; compare to 20');
  2459. expect(this.bv.isValid()).toEqual(false);
  2460. expect(this.bv.getMessages('age', 'greaterThan')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.greaterThan['default'], this.$minAge.val()));
  2461. this.bv.resetForm();
  2462. this.$minAge.val(18);
  2463. this.$age.val(20);
  2464. this.bv.validate();
  2465. expect($('#msg').html()).toEqual('TestSuite.greaterThan.compareTo() called; compare to 18');
  2466. expect(this.bv.isValid()).toBeTruthy();
  2467. });
  2468. });
  2469. describe('hslColor', function() {
  2470. beforeEach(function() {
  2471. var html = [
  2472. '<div class="container">',
  2473. '<form class="form-horizontal" id="rbgColorForm">',
  2474. '<div class="form-group">',
  2475. '<input type="text" name="hsl" data-bv-hslcolor />',
  2476. '</div>',
  2477. '</form>',
  2478. '</div>'
  2479. ].join('\n');
  2480. $(html).appendTo('body');
  2481. $('#rbgColorForm').bootstrapValidator();
  2482. this.bv = $('#rbgColorForm').data('bootstrapValidator');
  2483. this.$hslColor = this.bv.getFieldElements('hsl');
  2484. });
  2485. afterEach(function() {
  2486. $('#rbgColorForm').bootstrapValidator('destroy').parent().remove();
  2487. });
  2488. it('accept hsl()', function() {
  2489. this.$hslColor.val('hsl(120,50%,50%)');
  2490. this.bv.validate();
  2491. expect(this.bv.isValidField('hsl')).toBeTruthy();
  2492. });
  2493. it('accept spaces around values', function() {
  2494. this.$hslColor.val('hsl( 120 , 50% , 50% )');
  2495. this.bv.validate();
  2496. expect(this.bv.isValidField('hsl')).toBeTruthy();
  2497. });
  2498. it('accept multiple spaces around values', function() {
  2499. this.$hslColor.val('hsl( 120, 50%, 50% )');
  2500. this.bv.validate();
  2501. expect(this.bv.isValidField('hsl')).toBeTruthy();
  2502. });
  2503. it('accept negative hue value', function() {
  2504. this.$hslColor.val('hsl(-120,50%,50%)');
  2505. this.bv.validate();
  2506. expect(this.bv.isValidField('hsl')).toBeTruthy();
  2507. });
  2508. it('accept hue values larger than 360', function() {
  2509. this.$hslColor.val('hsl(480,50%,50%)');
  2510. this.bv.validate();
  2511. expect(this.bv.isValidField('hsl')).toBeTruthy();
  2512. });
  2513. it('reject negative saturation value', function() {
  2514. this.$hslColor.val('hsl(10,-50%,50%)');
  2515. this.bv.validate();
  2516. expect(this.bv.isValidField('hsl')).toEqual(false);
  2517. });
  2518. it('reject negative lightness', function() {
  2519. this.$hslColor.val('hsl(10,50%,-50%)');
  2520. this.bv.validate();
  2521. expect(this.bv.isValidField('hsl')).toEqual(false);
  2522. });
  2523. it('require hsl()', function() {
  2524. this.$hslColor.val('120,50%,50%');
  2525. this.bv.validate();
  2526. expect(this.bv.isValidField('hsl')).toEqual(false);
  2527. });
  2528. it('reject percentages above 100%', function() {
  2529. this.$hslColor.val('hsl(120,100%,101%)');
  2530. this.bv.validate();
  2531. expect(this.bv.isValidField('hsl')).toEqual(false);
  2532. });
  2533. it('reject space between hsl and (', function() {
  2534. this.$hslColor.val('hsl (120,50%,50%)');
  2535. this.bv.validate();
  2536. expect(this.bv.isValidField('hsl')).toEqual(false);
  2537. });
  2538. it('reject leading space', function() {
  2539. this.$hslColor.val(' hsl(120,50%,50%)');
  2540. this.bv.validate();
  2541. expect(this.bv.isValidField('hsl')).toEqual(false);
  2542. });
  2543. it('reject trailing space', function() {
  2544. this.$hslColor.val('hsl(120,50%,50%) ');
  2545. this.bv.validate();
  2546. expect(this.bv.isValidField('hsl')).toEqual(false);
  2547. });
  2548. it('reject percentages in hue value', function() {
  2549. this.$hslColor.val('hsl(50%, 50%, 100%)');
  2550. this.bv.validate();
  2551. expect(this.bv.isValidField('hsl')).toEqual(false);
  2552. });
  2553. it('reject integers in saturation value', function() {
  2554. this.$hslColor.val('hsl(120, 50, 100%)');
  2555. this.bv.validate();
  2556. expect(this.bv.isValidField('hsl')).toEqual(false);
  2557. });
  2558. it('reject integers in lightness value', function() {
  2559. this.$hslColor.val('hsl(120, 50%, 100)');
  2560. this.bv.validate();
  2561. expect(this.bv.isValidField('hsl')).toEqual(false);
  2562. });
  2563. });
  2564. describe('iban', function() {
  2565. beforeEach(function() {
  2566. $([
  2567. '<form class="form-horizontal" id="ibanForm">',
  2568. '<div class="form-group">',
  2569. '<select class="form-control" name="country">',
  2570. '<option value="AD">Andorra</option>',
  2571. '<option value="AE">United Arab Emirates</option>',
  2572. '<option value="AL">Albania</option>',
  2573. '<option value="AO">Angola</option>',
  2574. '<option value="AT">Austria</option>',
  2575. '<option value="AZ">Azerbaijan</option>',
  2576. '<option value="BA">Bosnia and Herzegovina</option>',
  2577. '<option value="BE">Belgium</option>',
  2578. '<option value="BF">Burkina Faso</option>',
  2579. '<option value="BG">Bulgaria</option>',
  2580. '<option value="BH">Bahrain</option>',
  2581. '<option value="BI">Burundi</option>',
  2582. '<option value="BJ">Benin</option>',
  2583. '<option value="BR">Brazil</option>',
  2584. '<option value="CH">Switzerland</option>',
  2585. '<option value="CM">Cameroon</option>',
  2586. '<option value="CR">Costa Rica</option>',
  2587. '<option value="CV">Cape Verde</option>',
  2588. '<option value="CY">Cyprus</option>',
  2589. '<option value="CZ">Czech Republic</option>',
  2590. '<option value="DE">Germany</option>',
  2591. '<option value="DK">Denmark</option>',
  2592. '<option value="DO">Dominican Republic</option>',
  2593. '<option value="DZ">Algeria</option>',
  2594. '<option value="EE">Estonia</option>',
  2595. '<option value="ES">Spain</option>',
  2596. '<option value="FI">Finland</option>',
  2597. '<option value="FO">Faroe Islands</option>',
  2598. '<option value="FR">France</option>',
  2599. '<option value="GB">United Kingdom</option>',
  2600. '<option value="GE">Georgia</option>',
  2601. '<option value="GI">Gibraltar</option>',
  2602. '<option value="GL">Greenland</option>',
  2603. '<option value="GR">Greece</option>',
  2604. '<option value="GT">Guatemala</option>',
  2605. '<option value="HR">Croatia</option>',
  2606. '<option value="HU">Hungary</option>',
  2607. '<option value="IE">Ireland</option>',
  2608. '<option value="IL">Israel</option>',
  2609. '<option value="IR">Iran</option>',
  2610. '<option value="IS">Iceland</option>',
  2611. '<option value="IT">Italy</option>',
  2612. '<option value="JO">Jordan</option>',
  2613. '<option value="KW">Kuwait</option>',
  2614. '<option value="KZ">Kazakhstan</option>',
  2615. '<option value="LB">Lebanon</option>',
  2616. '<option value="LI">Liechtenstein</option>',
  2617. '<option value="LT">Lithuania</option>',
  2618. '<option value="LU">Luxembourg</option>',
  2619. '<option value="LV">Latvia</option>',
  2620. '<option value="MC">Monaco</option>',
  2621. '<option value="MD">Moldova</option>',
  2622. '<option value="ME">Montenegro</option>',
  2623. '<option value="MG">Madagascar</option>',
  2624. '<option value="MK">Macedonia</option>',
  2625. '<option value="ML">Mali</option>',
  2626. '<option value="MR">Mauritania</option>',
  2627. '<option value="MT">Malta</option>',
  2628. '<option value="MU">Mauritius</option>',
  2629. '<option value="MZ">Mozambique</option>',
  2630. '<option value="NL">Netherlands</option>',
  2631. '<option value="NO">Norway</option>',
  2632. '<option value="PK">Pakistan</option>',
  2633. '<option value="PL">Poland</option>',
  2634. '<option value="PS">Palestinian</option>',
  2635. '<option value="PT">Portugal</option>',
  2636. '<option value="QA">Qatar</option>',
  2637. '<option value="RO">Romania</option>',
  2638. '<option value="RS">Serbia</option>',
  2639. '<option value="SA">Saudi Arabia</option>',
  2640. '<option value="SE">Sweden</option>',
  2641. '<option value="SE">Slovenia</option>',
  2642. '<option value="SK">Slovakia</option>',
  2643. '<option value="SM">San Marino</option>',
  2644. '<option value="SN">Senegal</option>',
  2645. '<option value="TN">Tunisia</option>',
  2646. '<option value="TR">Turkey</option>',
  2647. '<option value="VG">Virgin Islands, British</option>',
  2648. '</select>',
  2649. '</div>',
  2650. '<div class="form-group">',
  2651. '<input type="text" name="iban" data-bv-iban />',
  2652. '</div>',
  2653. '</form>'
  2654. ].join('\n')).appendTo('body');
  2655. $('#ibanForm').bootstrapValidator();
  2656. this.bv = $('#ibanForm').data('bootstrapValidator');
  2657. this.$country = this.bv.getFieldElements('country');
  2658. this.$iban = this.bv.getFieldElements('iban');
  2659. });
  2660. afterEach(function() {
  2661. $('#ibanForm').bootstrapValidator('destroy').remove();
  2662. });
  2663. it('not supported country', function() {
  2664. this.$iban.val('US123456789');
  2665. this.bv.validate();
  2666. expect(this.bv.isValidField('iban')).toEqual(false);
  2667. });
  2668. it('dynamic country', function() {
  2669. this.$iban.attr('data-bv-iban-country', 'country');
  2670. this.bv.destroy();
  2671. this.bv = $('#ibanForm').bootstrapValidator().data('bootstrapValidator');
  2672. this.$country.val('AT');
  2673. this.$iban.val('AT611904300234573201');
  2674. this.bv.validate();
  2675. expect(this.bv.isValid()).toBeTruthy();
  2676. this.bv.resetForm();
  2677. this.$country.val('BG');
  2678. this.$iban.val('HR1210010051863000160');
  2679. this.bv.validate();
  2680. expect(this.bv.isValid()).toEqual(false);
  2681. });
  2682. it('Albania', function() {
  2683. this.$iban.val('AL47212110090000000235698741');
  2684. this.bv.validate();
  2685. expect(this.bv.isValidField('iban')).toBeTruthy();
  2686. });
  2687. it('Algeria', function() {
  2688. this.$iban.val('DZ4000400174401001050486');
  2689. this.bv.validate();
  2690. expect(this.bv.isValidField('iban')).toBeTruthy();
  2691. });
  2692. it('Andorra', function() {
  2693. this.$iban.val('AD1200012030200359100100');
  2694. this.bv.validate();
  2695. expect(this.bv.isValidField('iban')).toBeTruthy();
  2696. });
  2697. it('Angola', function() {
  2698. this.$iban.val('AO06000600000100037131174');
  2699. this.bv.validate();
  2700. expect(this.bv.isValidField('iban')).toBeTruthy();
  2701. });
  2702. it('Austria', function() {
  2703. this.$iban.val('AT611904300234573201');
  2704. this.bv.validate();
  2705. expect(this.bv.isValidField('iban')).toBeTruthy();
  2706. });
  2707. it('Azerbaijan', function() {
  2708. this.$iban.val('AZ21NABZ00000000137010001944');
  2709. this.bv.validate();
  2710. expect(this.bv.isValidField('iban')).toBeTruthy();
  2711. });
  2712. it('Bahrain', function() {
  2713. this.$iban.val('BH29BMAG1299123456BH00');
  2714. this.bv.validate();
  2715. expect(this.bv.isValidField('iban')).toBeTruthy();
  2716. });
  2717. it('Belgium', function() {
  2718. this.$iban.val('BE68539007547034');
  2719. this.bv.validate();
  2720. expect(this.bv.isValidField('iban')).toBeTruthy();
  2721. });
  2722. it('Benin', function() {
  2723. this.$iban.val('BJ11B00610100400271101192591');
  2724. this.bv.validate();
  2725. expect(this.bv.isValidField('iban')).toBeTruthy();
  2726. });
  2727. it('Brazil', function() {
  2728. this.$iban.val('BR9700360305000010009795493P1');
  2729. this.bv.validate();
  2730. expect(this.bv.isValidField('iban')).toBeTruthy();
  2731. });
  2732. it('Bulgaria', function() {
  2733. this.$iban.val('BG80BNBG96611020345678');
  2734. this.bv.validate();
  2735. expect(this.bv.isValidField('iban')).toBeTruthy();
  2736. });
  2737. it('Burkina Faso', function() {
  2738. this.$iban.val('BF1030134020015400945000643');
  2739. this.bv.validate();
  2740. expect(this.bv.isValidField('iban')).toBeTruthy();
  2741. });
  2742. it('Burundi', function() {
  2743. this.$iban.val('BI43201011067444');
  2744. this.bv.validate();
  2745. expect(this.bv.isValidField('iban')).toBeTruthy();
  2746. });
  2747. it('Cameroon', function() {
  2748. this.$iban.val('CM2110003001000500000605306');
  2749. this.bv.validate();
  2750. expect(this.bv.isValidField('iban')).toBeTruthy();
  2751. });
  2752. it('Cape Verde', function() {
  2753. this.$iban.val('CV64000300004547069110176');
  2754. this.bv.validate();
  2755. expect(this.bv.isValidField('iban')).toBeTruthy();
  2756. });
  2757. it('Costa Rica', function() {
  2758. this.$iban.val('CR0515202001026284066');
  2759. this.bv.validate();
  2760. expect(this.bv.isValidField('iban')).toBeTruthy();
  2761. });
  2762. it('Croatia', function() {
  2763. this.$iban.val('HR1210010051863000160');
  2764. this.bv.validate();
  2765. expect(this.bv.isValidField('iban')).toBeTruthy();
  2766. });
  2767. it('Cyprus', function() {
  2768. this.$iban.val('CY17002001280000001200527600');
  2769. this.bv.validate();
  2770. expect(this.bv.isValidField('iban')).toBeTruthy();
  2771. });
  2772. it('Czech Republic', function() {
  2773. this.$iban.val('CZ6508000000192000145399');
  2774. this.bv.validate();
  2775. expect(this.bv.isValidField('iban')).toBeTruthy();
  2776. });
  2777. it('Denmark', function() {
  2778. this.$iban.val('DK5000400440116243');
  2779. this.bv.validate();
  2780. expect(this.bv.isValidField('iban')).toBeTruthy();
  2781. });
  2782. it('Dominican Republic', function() {
  2783. this.$iban.val('DO28BAGR00000001212453611324');
  2784. this.bv.validate();
  2785. expect(this.bv.isValidField('iban')).toBeTruthy();
  2786. });
  2787. it('Estonia', function() {
  2788. this.$iban.val('EE382200221020145685');
  2789. this.bv.validate();
  2790. expect(this.bv.isValidField('iban')).toBeTruthy();
  2791. });
  2792. it('Faroe Islands', function() {
  2793. this.$iban.val('FO1464600009692713');
  2794. this.bv.validate();
  2795. expect(this.bv.isValidField('iban')).toBeTruthy();
  2796. });
  2797. it('Finland', function() {
  2798. this.$iban.val('FI2112345600000785');
  2799. this.bv.validate();
  2800. expect(this.bv.isValidField('iban')).toBeTruthy();
  2801. });
  2802. it('France', function() {
  2803. this.$iban.val('FR1420041010050500013M02606');
  2804. this.bv.validate();
  2805. expect(this.bv.isValidField('iban')).toBeTruthy();
  2806. });
  2807. it('Guatemala', function() {
  2808. this.$iban.val('GT82TRAJ01020000001210029690');
  2809. this.bv.validate();
  2810. expect(this.bv.isValidField('iban')).toBeTruthy();
  2811. });
  2812. it('Georgia', function() {
  2813. this.$iban.val('GE29NB0000000101904917');
  2814. this.bv.validate();
  2815. expect(this.bv.isValidField('iban')).toBeTruthy();
  2816. });
  2817. it('Germany', function() {
  2818. this.$iban.val('DE89370400440532013000');
  2819. this.bv.validate();
  2820. expect(this.bv.isValidField('iban')).toBeTruthy();
  2821. });
  2822. it('Gibraltar', function() {
  2823. this.$iban.val('GI75NWBK000000007099453');
  2824. this.bv.validate();
  2825. expect(this.bv.isValidField('iban')).toBeTruthy();
  2826. });
  2827. it('Greece', function() {
  2828. this.$iban.val('GR1601101250000000012300695');
  2829. this.bv.validate();
  2830. expect(this.bv.isValidField('iban')).toBeTruthy();
  2831. });
  2832. it('Greenland', function() {
  2833. this.$iban.val('GL8964710001000206');
  2834. this.bv.validate();
  2835. expect(this.bv.isValidField('iban')).toBeTruthy();
  2836. });
  2837. it('Hungary', function() {
  2838. this.$iban.val('HU42117730161111101800000000');
  2839. this.bv.validate();
  2840. expect(this.bv.isValidField('iban')).toBeTruthy();
  2841. });
  2842. it('Iceland', function() {
  2843. this.$iban.val('IS140159260076545510730339');
  2844. this.bv.validate();
  2845. expect(this.bv.isValidField('iban')).toBeTruthy();
  2846. });
  2847. it('Iran', function() {
  2848. this.$iban.val('IR580540105180021273113007');
  2849. this.bv.validate();
  2850. expect(this.bv.isValidField('iban')).toBeTruthy();
  2851. });
  2852. it('Ireland', function() {
  2853. this.$iban.val('IE29AIBK93115212345678');
  2854. this.bv.validate();
  2855. expect(this.bv.isValidField('iban')).toBeTruthy();
  2856. });
  2857. it('Israel', function() {
  2858. this.$iban.val('IL620108000000099999999');
  2859. this.bv.validate();
  2860. expect(this.bv.isValidField('iban')).toBeTruthy();
  2861. });
  2862. it('Italy', function() {
  2863. this.$iban.val('IT60X0542811101000000123456');
  2864. this.bv.validate();
  2865. expect(this.bv.isValidField('iban')).toBeTruthy();
  2866. });
  2867. it('Ivory Coast', function() {
  2868. this.$iban.val('CI05A00060174100178530011852');
  2869. this.bv.validate();
  2870. expect(this.bv.isValidField('iban')).toBeTruthy();
  2871. });
  2872. it('Jordan', function() {
  2873. this.$iban.val('JO94CBJO0010000000000131000302');
  2874. this.bv.validate();
  2875. expect(this.bv.isValidField('iban')).toBeTruthy();
  2876. });
  2877. it('Kazakhstan', function() {
  2878. this.$iban.val('KZ176010251000042993');
  2879. this.bv.validate();
  2880. expect(this.bv.isValidField('iban')).toBeTruthy();
  2881. });
  2882. it('Kuwait', function() {
  2883. this.$iban.val('KW74NBOK0000000000001000372151');
  2884. this.bv.validate();
  2885. expect(this.bv.isValidField('iban')).toBeTruthy();
  2886. });
  2887. it('Latvia', function() {
  2888. this.$iban.val('LV80BANK0000435195001');
  2889. this.bv.validate();
  2890. expect(this.bv.isValidField('iban')).toBeTruthy();
  2891. });
  2892. it('Lebanon', function() {
  2893. this.$iban.val('LB30099900000001001925579115');
  2894. this.bv.validate();
  2895. expect(this.bv.isValidField('iban')).toBeTruthy();
  2896. });
  2897. it('Liechtenstein', function() {
  2898. this.$iban.val('LI21088100002324013AA');
  2899. this.bv.validate();
  2900. expect(this.bv.isValidField('iban')).toBeTruthy();
  2901. });
  2902. it('Lithuania', function() {
  2903. this.$iban.val('LT121000011101001000');
  2904. this.bv.validate();
  2905. expect(this.bv.isValidField('iban')).toBeTruthy();
  2906. });
  2907. it('Luxembourg', function() {
  2908. this.$iban.val('LU280019400644750000');
  2909. this.bv.validate();
  2910. expect(this.bv.isValidField('iban')).toBeTruthy();
  2911. });
  2912. it('Macedonia', function() {
  2913. this.$iban.val('MK07300000000042425');
  2914. this.bv.validate();
  2915. expect(this.bv.isValidField('iban')).toBeTruthy();
  2916. });
  2917. it('Madagascar', function() {
  2918. this.$iban.val('MG4600005030010101914016056');
  2919. this.bv.validate();
  2920. expect(this.bv.isValidField('iban')).toBeTruthy();
  2921. });
  2922. it('Malta', function() {
  2923. this.$iban.val('MT84MALT011000012345MTLCAST001S');
  2924. this.bv.validate();
  2925. expect(this.bv.isValidField('iban')).toBeTruthy();
  2926. });
  2927. it('Mauritania', function() {
  2928. this.$iban.val('MR1300012000010000002037372');
  2929. this.bv.validate();
  2930. expect(this.bv.isValidField('iban')).toBeTruthy();
  2931. });
  2932. it('Mauritius', function() {
  2933. this.$iban.val('MU17BOMM0101101030300200000MUR');
  2934. this.bv.validate();
  2935. expect(this.bv.isValidField('iban')).toBeTruthy();
  2936. });
  2937. it('Mali', function() {
  2938. this.$iban.val('ML03D00890170001002120000447');
  2939. this.bv.validate();
  2940. expect(this.bv.isValidField('iban')).toBeTruthy();
  2941. });
  2942. it('Moldova', function() {
  2943. this.$iban.val('MD24AG000225100013104168');
  2944. this.bv.validate();
  2945. expect(this.bv.isValidField('iban')).toBeTruthy();
  2946. });
  2947. it('Monaco', function() {
  2948. this.$iban.val('MC5813488000010051108001292');
  2949. this.bv.validate();
  2950. expect(this.bv.isValidField('iban')).toBeTruthy();
  2951. });
  2952. it('Montenegro', function() {
  2953. this.$iban.val('ME25505000012345678951');
  2954. this.bv.validate();
  2955. expect(this.bv.isValidField('iban')).toBeTruthy();
  2956. });
  2957. it('Mozambique', function() {
  2958. this.$iban.val('MZ59000100000011834194157');
  2959. this.bv.validate();
  2960. expect(this.bv.isValidField('iban')).toBeTruthy();
  2961. });
  2962. it('Netherlands', function() {
  2963. this.$iban.val('NL91ABNA0417164300');
  2964. this.bv.validate();
  2965. expect(this.bv.isValidField('iban')).toBeTruthy();
  2966. });
  2967. it('Norway', function() {
  2968. this.$iban.val('NO9386011117947');
  2969. this.bv.validate();
  2970. expect(this.bv.isValidField('iban')).toBeTruthy();
  2971. });
  2972. it('Pakistan', function() {
  2973. this.$iban.val('PK24SCBL0000001171495101');
  2974. this.bv.validate();
  2975. expect(this.bv.isValidField('iban')).toBeTruthy();
  2976. });
  2977. it('Palestine', function() {
  2978. this.$iban.val('PS92PALS000000000400123456702');
  2979. this.bv.validate();
  2980. expect(this.bv.isValidField('iban')).toBeTruthy();
  2981. });
  2982. it('Poland', function() {
  2983. this.$iban.val('PL27114020040000300201355387');
  2984. this.bv.validate();
  2985. expect(this.bv.isValidField('iban')).toBeTruthy();
  2986. });
  2987. it('Portugal', function() {
  2988. this.$iban.val('PT50000201231234567890154');
  2989. this.bv.validate();
  2990. expect(this.bv.isValidField('iban')).toBeTruthy();
  2991. });
  2992. it('Qatar', function() {
  2993. this.$iban.val('QA58DOHB00001234567890ABCDEFG');
  2994. this.bv.validate();
  2995. expect(this.bv.isValidField('iban')).toBeTruthy();
  2996. });
  2997. it('Romania', function() {
  2998. this.$iban.val('RO49AAAA1B31007593840000');
  2999. this.bv.validate();
  3000. expect(this.bv.isValidField('iban')).toBeTruthy();
  3001. });
  3002. it('San Marino', function() {
  3003. this.$iban.val('SM86U0322509800000000270100');
  3004. this.bv.validate();
  3005. expect(this.bv.isValidField('iban')).toBeTruthy();
  3006. });
  3007. it('Saudi Arabia', function() {
  3008. this.$iban.val('SA0380000000608010167519');
  3009. this.bv.validate();
  3010. expect(this.bv.isValidField('iban')).toBeTruthy();
  3011. });
  3012. it('Senegal', function() {
  3013. this.$iban.val('SN12K00100152000025690007542');
  3014. this.bv.validate();
  3015. expect(this.bv.isValidField('iban')).toBeTruthy();
  3016. });
  3017. it('Serbia', function() {
  3018. this.$iban.val('RS35260005601001611379');
  3019. this.bv.validate();
  3020. expect(this.bv.isValidField('iban')).toBeTruthy();
  3021. });
  3022. it('Slovakia', function() {
  3023. this.$iban.val('SK3112000000198742637541');
  3024. this.bv.validate();
  3025. expect(this.bv.isValidField('iban')).toBeTruthy();
  3026. });
  3027. it('Slovenia', function() {
  3028. this.$iban.val('SI56191000000123438');
  3029. this.bv.validate();
  3030. expect(this.bv.isValidField('iban')).toBeTruthy();
  3031. });
  3032. it('Spain', function() {
  3033. this.$iban.val('ES9121000418450200051332');
  3034. this.bv.validate();
  3035. expect(this.bv.isValidField('iban')).toBeTruthy();
  3036. });
  3037. it('Sweden', function() {
  3038. this.$iban.val('SE3550000000054910000003');
  3039. this.bv.validate();
  3040. expect(this.bv.isValidField('iban')).toBeTruthy();
  3041. });
  3042. it('Switzerland', function() {
  3043. this.$iban.val('CH9300762011623852957');
  3044. this.bv.validate();
  3045. expect(this.bv.isValidField('iban')).toBeTruthy();
  3046. });
  3047. it('Tunisia', function() {
  3048. this.$iban.val('TN5914207207100707129648');
  3049. this.bv.validate();
  3050. expect(this.bv.isValidField('iban')).toBeTruthy();
  3051. });
  3052. it('Turkey', function() {
  3053. this.$iban.val('TR330006100519786457841326');
  3054. this.bv.validate();
  3055. expect(this.bv.isValidField('iban')).toBeTruthy();
  3056. });
  3057. it('United Arab Emirates', function() {
  3058. this.$iban.val('AE260211000000230064016');
  3059. this.bv.validate();
  3060. expect(this.bv.isValidField('iban')).toBeTruthy();
  3061. });
  3062. it('United Kingdom', function() {
  3063. this.$iban.val('GB29NWBK60161331926819');
  3064. this.bv.validate();
  3065. expect(this.bv.isValidField('iban')).toBeTruthy();
  3066. });
  3067. it('Virgin Islands, British', function() {
  3068. this.$iban.val('VG96VPVG0000012345678901');
  3069. this.bv.validate();
  3070. expect(this.bv.isValidField('iban')).toBeTruthy();
  3071. });
  3072. it('invalid checksum', function() {
  3073. this.$iban.val('TR330006100519786457841325');
  3074. this.bv.validate();
  3075. expect(this.bv.isValidField('iban')).toEqual(false);
  3076. });
  3077. });
  3078. describe('id', function() {
  3079. beforeEach(function() {
  3080. $([
  3081. '<form class="form-horizontal" id="idForm">',
  3082. '<div class="form-group">',
  3083. '<select class="form-control" name="country">',
  3084. '<option value="BA">Bosnia and Herzegovina</option>',
  3085. '<option value="BG">Bulgaria</option>',
  3086. '<option value="BR">Brazil</option>',
  3087. '<option value="CH">Switzerland</option>',
  3088. '<option value="CL">Chile</option>',
  3089. '<option value="CN">China</option>',
  3090. '<option value="CZ">Czech</option>',
  3091. '<option value="DK">Denmark</option>',
  3092. '<option value="EE">Estonia</option>',
  3093. '<option value="ES">Spain</option>',
  3094. '<option value="FI">Finland</option>',
  3095. '<option value="HR">Croatia</option>',
  3096. '<option value="IE">Ireland</option>',
  3097. '<option value="IS">Iceland</option>',
  3098. '<option value="LT">Lithuania</option>',
  3099. '<option value="LV">Latvia</option>',
  3100. '<option value="ME">Montenegro</option>',
  3101. '<option value="MK">Macedonia</option>',
  3102. '<option value="NL">Netherlands</option>',
  3103. '<option value="RO">Romania</option>',
  3104. '<option value="RS">Serbia</option>',
  3105. '<option value="SE">Sweden</option>',
  3106. '<option value="SI">Slovenia</option>',
  3107. '<option value="SK">Slovakia</option>',
  3108. '<option value="SM">San Marino</option>',
  3109. '<option value="TH">Thailand</option>',
  3110. '<option value="ZA">South Africa</option>',
  3111. '</select>',
  3112. '</div>',
  3113. '<div class="form-group">',
  3114. '<input class="form-control" type="text" name="id" data-bv-id />',
  3115. '</div>',
  3116. '</form>'
  3117. ].join('\n')).appendTo('body');
  3118. $('#idForm').bootstrapValidator();
  3119. /**
  3120. * @type {BootstrapValidator}
  3121. */
  3122. this.bv = $('#idForm').data('bootstrapValidator');
  3123. this.$country = this.bv.getFieldElements('country');
  3124. this.$id = this.bv.getFieldElements('id');
  3125. });
  3126. afterEach(function() {
  3127. $('#idForm').bootstrapValidator('destroy').remove();
  3128. });
  3129. it('dynamic country', function() {
  3130. this.$id.attr('data-bv-id-country', 'country');
  3131. this.bv.destroy();
  3132. this.bv = $('#idForm').bootstrapValidator().data('bootstrapValidator');
  3133. this.$country.val('BG');
  3134. this.$id.val('7552010005');
  3135. this.bv.validate();
  3136. expect(this.bv.isValid()).toBeTruthy();
  3137. this.bv.resetForm();
  3138. this.$country.val('BR');
  3139. this.$id.val('231.002.999-00');
  3140. this.bv.validate();
  3141. expect(this.bv.isValid()).toEqual(false);
  3142. });
  3143. it('Bulgarian national identification number (EGN)', function() {
  3144. this.bv.updateOption('id', 'id', 'country', 'BG');
  3145. // Valid samples
  3146. var validSamples = ['7523169263', '8032056031', '803205 603 1', '8001010008', '7501020018', '7552010005', '7542011030'];
  3147. for (var i in validSamples) {
  3148. this.bv.resetForm();
  3149. this.$id.val(validSamples[i]);
  3150. this.bv.validate();
  3151. expect(this.bv.isValid()).toBeTruthy();
  3152. }
  3153. // Invalid samples
  3154. var invalidSamples = ['8019010008'];
  3155. for (i in invalidSamples) {
  3156. this.bv.resetForm();
  3157. this.$id.val(invalidSamples[i]);
  3158. this.bv.validate();
  3159. expect(this.bv.isValid()).toEqual(false);
  3160. }
  3161. });
  3162. it('Brazilian national identification number (CPF)', function() {
  3163. this.bv.updateOption('id', 'id', 'country', 'BR');
  3164. // Valid samples
  3165. var validSamples = ['39053344705', '390.533.447-05', '111.444.777-35'];
  3166. for (var i in validSamples) {
  3167. this.bv.resetForm();
  3168. this.$id.val(validSamples[i]);
  3169. this.bv.validate();
  3170. expect(this.bv.isValid()).toBeTruthy();
  3171. }
  3172. // Invalid samples
  3173. var invalidSamples = ['231.002.999-00'];
  3174. for (i in invalidSamples) {
  3175. this.bv.resetForm();
  3176. this.$id.val(invalidSamples[i]);
  3177. this.bv.validate();
  3178. expect(this.bv.isValid()).toEqual(false);
  3179. }
  3180. });
  3181. it('Swiss Social Security Number (AHV-Nr/No AVS)', function() {
  3182. this.bv.updateOption('id', 'id', 'country', 'CH');
  3183. // Valid samples
  3184. var validSamples = ['756.1234.5678.95', '7561234567895'];
  3185. for (var i in validSamples) {
  3186. this.bv.resetForm();
  3187. this.$id.val(validSamples[i]);
  3188. this.bv.validate();
  3189. expect(this.bv.isValid()).toBeTruthy();
  3190. }
  3191. });
  3192. it('Chilean national identification number (RUN/RUT)', function() {
  3193. this.bv.updateOption('id', 'id', 'country', 'CL');
  3194. // Valid samples
  3195. var validSamples = ['76086428-5', '22060449-7', '12531909-2','12937893-K','12937893-k'];
  3196. for (var i in validSamples) {
  3197. this.bv.resetForm();
  3198. this.$id.val(validSamples[i]);
  3199. this.bv.validate();
  3200. expect(this.bv.isValid()).toBeTruthy();
  3201. }
  3202. });
  3203. // #793
  3204. it('Chinese citizen identification number', function() {
  3205. this.bv.updateOption('id', 'id', 'country', 'CN');
  3206. // Valid samples
  3207. var validSamples = ['450202201409072332', '22011219930407001X', '110108601017023'];
  3208. for (var i in validSamples) {
  3209. this.bv.resetForm();
  3210. this.$id.val(validSamples[i]);
  3211. this.bv.validate();
  3212. expect(this.bv.isValid()).toBeTruthy();
  3213. }
  3214. // Invalid samples
  3215. var invalidSamples = ['999999199304070016', '220112190002290016', '220112199304070019', '999999601017023', '110108999999023'];
  3216. for (i in invalidSamples) {
  3217. this.bv.resetForm();
  3218. this.$id.val(invalidSamples[i]);
  3219. this.bv.validate();
  3220. expect(this.bv.isValid()).toEqual(false);
  3221. }
  3222. });
  3223. it('Czech national identification number (RC)', function() {
  3224. this.bv.updateOption('id', 'id', 'country', 'CZ');
  3225. // Valid samples
  3226. var validSamples = ['7103192745', '991231123'];
  3227. for (var i in validSamples) {
  3228. this.bv.resetForm();
  3229. this.$id.val(validSamples[i]);
  3230. this.bv.validate();
  3231. expect(this.bv.isValid()).toBeTruthy();
  3232. }
  3233. // Invalid samples
  3234. var invalidSamples = ['1103492745', '590312123'];
  3235. for (i in invalidSamples) {
  3236. this.bv.resetForm();
  3237. this.$id.val(invalidSamples[i]);
  3238. this.bv.validate();
  3239. expect(this.bv.isValid()).toEqual(false);
  3240. }
  3241. });
  3242. it('Danish Personal Identification number (CPR)', function() {
  3243. this.bv.updateOption('id', 'id', 'country', 'DK');
  3244. // Valid samples
  3245. var validSamples = ['2110625629', '211062-5629'];
  3246. for (var i in validSamples) {
  3247. this.bv.resetForm();
  3248. this.$id.val(validSamples[i]);
  3249. this.bv.validate();
  3250. expect(this.bv.isValid()).toBeTruthy();
  3251. }
  3252. // Invalid samples
  3253. var invalidSamples = ['511062-5629'];
  3254. for (i in invalidSamples) {
  3255. this.bv.resetForm();
  3256. this.$id.val(invalidSamples[i]);
  3257. this.bv.validate();
  3258. expect(this.bv.isValid()).toEqual(false);
  3259. }
  3260. });
  3261. it('Estonian Personal Identification Code (isikukood)', function() {
  3262. this.bv.updateOption('id', 'id', 'country', 'EE');
  3263. // Valid samples
  3264. var validSamples = ['37605030299'];
  3265. for (var i in validSamples) {
  3266. this.bv.resetForm();
  3267. this.$id.val(validSamples[i]);
  3268. this.bv.validate();
  3269. expect(this.bv.isValid()).toBeTruthy();
  3270. }
  3271. });
  3272. it('Spanish personal identity code (DNI/NIE)', function() {
  3273. this.bv.updateOption('id', 'id', 'country', 'ES');
  3274. // Valid samples
  3275. var validSamples = ['54362315K', '54362315-K', 'X2482300W', 'X-2482300W', 'X-2482300-W'];
  3276. for (var i in validSamples) {
  3277. this.bv.resetForm();
  3278. this.$id.val(validSamples[i]);
  3279. this.bv.validate();
  3280. expect(this.bv.isValid()).toBeTruthy();
  3281. }
  3282. // Invalid samples
  3283. var invalidSamples = ['54362315Z', 'X-2482300A'];
  3284. for (i in invalidSamples) {
  3285. this.bv.resetForm();
  3286. this.$id.val(invalidSamples[i]);
  3287. this.bv.validate();
  3288. expect(this.bv.isValid()).toEqual(false);
  3289. }
  3290. });
  3291. it('Finnish Personal Identity Code (HETU)', function() {
  3292. this.bv.updateOption('id', 'id', 'country', 'FI');
  3293. // Valid samples
  3294. var validSamples = ['311280-888Y', '131052-308T'];
  3295. for (var i in validSamples) {
  3296. this.bv.resetForm();
  3297. this.$id.val(validSamples[i]);
  3298. this.bv.validate();
  3299. expect(this.bv.isValid()).toBeTruthy();
  3300. }
  3301. // Invalid samples
  3302. var invalidSamples = ['131052-308U', '310252-308Y'];
  3303. for (i in invalidSamples) {
  3304. this.bv.resetForm();
  3305. this.$id.val(invalidSamples[i]);
  3306. this.bv.validate();
  3307. expect(this.bv.isValid()).toEqual(false);
  3308. }
  3309. });
  3310. it('Croatian personal identification number (OIB)', function() {
  3311. this.bv.updateOption('id', 'id', 'country', 'HR');
  3312. // Valid samples
  3313. var validSamples = ['33392005961'];
  3314. for (var i in validSamples) {
  3315. this.bv.resetForm();
  3316. this.$id.val(validSamples[i]);
  3317. this.bv.validate();
  3318. expect(this.bv.isValid()).toBeTruthy();
  3319. }
  3320. // Invalid samples
  3321. var invalidSamples = ['33392005962'];
  3322. for (i in invalidSamples) {
  3323. this.bv.resetForm();
  3324. this.$id.val(invalidSamples[i]);
  3325. this.bv.validate();
  3326. expect(this.bv.isValid()).toEqual(false);
  3327. }
  3328. });
  3329. it('Irish Personal Public Service Number (PPS)', function() {
  3330. this.bv.updateOption('id', 'id', 'country', 'IE');
  3331. // Valid samples
  3332. var validSamples = ['6433435F', '6433435FT', '6433435FW', '6433435OA', '6433435IH', '1234567TW', '1234567FA'];
  3333. for (var i in validSamples) {
  3334. this.bv.resetForm();
  3335. this.$id.val(validSamples[i]);
  3336. this.bv.validate();
  3337. expect(this.bv.isValid()).toBeTruthy();
  3338. }
  3339. // Invalid samples
  3340. var invalidSamples = ['6433435E', '6433435VH'];
  3341. for (i in invalidSamples) {
  3342. this.bv.resetForm();
  3343. this.$id.val(invalidSamples[i]);
  3344. this.bv.validate();
  3345. expect(this.bv.isValid()).toEqual(false);
  3346. }
  3347. });
  3348. it('Iceland national identification number (Kennitala)', function() {
  3349. this.bv.updateOption('id', 'id', 'country', 'IS');
  3350. // Valid samples
  3351. var validSamples = ['120174-3399', '1201743399', '0902862349'];
  3352. for (var i in validSamples) {
  3353. this.bv.resetForm();
  3354. this.$id.val(validSamples[i]);
  3355. this.bv.validate();
  3356. expect(this.bv.isValid()).toBeTruthy();
  3357. }
  3358. });
  3359. it('Lithuanian Personal Code (Asmens kodas)', function() {
  3360. this.bv.updateOption('id', 'id', 'country', 'LT');
  3361. // Valid samples
  3362. var validSamples = ['38703181745'];
  3363. for (var i in validSamples) {
  3364. this.bv.resetForm();
  3365. this.$id.val(validSamples[i]);
  3366. this.bv.validate();
  3367. expect(this.bv.isValid()).toBeTruthy();
  3368. }
  3369. // Invalid samples
  3370. var invalidSamples = ['38703181746', '78703181745', '38703421745'];
  3371. for (i in invalidSamples) {
  3372. this.bv.resetForm();
  3373. this.$id.val(invalidSamples[i]);
  3374. this.bv.validate();
  3375. expect(this.bv.isValid()).toEqual(false);
  3376. }
  3377. });
  3378. it('Latvian Personal Code (Personas kods)', function() {
  3379. this.bv.updateOption('id', 'id', 'country', 'LV');
  3380. // Valid samples
  3381. var validSamples = ['161175-19997', '16117519997'];
  3382. for (var i in validSamples) {
  3383. this.bv.resetForm();
  3384. this.$id.val(validSamples[i]);
  3385. this.bv.validate();
  3386. expect(this.bv.isValid()).toBeTruthy();
  3387. }
  3388. // Invalid samples
  3389. var invalidSamples = ['161375-19997'];
  3390. for (i in invalidSamples) {
  3391. this.bv.resetForm();
  3392. this.$id.val(invalidSamples[i]);
  3393. this.bv.validate();
  3394. expect(this.bv.isValid()).toEqual(false);
  3395. }
  3396. });
  3397. it('Dutch national identification number (BSN)', function() {
  3398. this.bv.updateOption('id', 'id', 'country', 'NL');
  3399. // Valid samples
  3400. var validSamples = ['111222333', '941331490', '9413.31.490'];
  3401. for (var i in validSamples) {
  3402. this.bv.resetForm();
  3403. this.$id.val(validSamples[i]);
  3404. this.bv.validate();
  3405. expect(this.bv.isValid()).toBeTruthy();
  3406. }
  3407. // Invalid samples
  3408. var invalidSamples = ['111252333'];
  3409. for (i in invalidSamples) {
  3410. this.bv.resetForm();
  3411. this.$id.val(invalidSamples[i]);
  3412. this.bv.validate();
  3413. expect(this.bv.isValid()).toEqual(false);
  3414. }
  3415. });
  3416. it('Romanian numerical personal code (CNP)', function() {
  3417. this.bv.updateOption('id', 'id', 'country', 'RO');
  3418. // Valid samples
  3419. var validSamples = ['1630615123457', '1800101221144'];
  3420. for (var i in validSamples) {
  3421. this.bv.resetForm();
  3422. this.$id.val(validSamples[i]);
  3423. this.bv.validate();
  3424. expect(this.bv.isValid()).toBeTruthy();
  3425. }
  3426. // Invalid samples
  3427. var invalidSamples = ['8800101221144', '1632215123457', '1630615123458'];
  3428. for (i in invalidSamples) {
  3429. this.bv.resetForm();
  3430. this.$id.val(invalidSamples[i]);
  3431. this.bv.validate();
  3432. expect(this.bv.isValid()).toEqual(false);
  3433. }
  3434. });
  3435. it('Swedish personal identity number (personnummer)', function() {
  3436. this.bv.updateOption('id', 'id', 'country', 'SE');
  3437. // Valid samples
  3438. var validSamples = ['8112289874', '811228-9874', '811228+9874'];
  3439. for (var i in validSamples) {
  3440. this.bv.resetForm();
  3441. this.$id.val(validSamples[i]);
  3442. this.bv.validate();
  3443. expect(this.bv.isValid()).toBeTruthy();
  3444. }
  3445. // Invalid samples
  3446. var invalidSamples = ['811228-9873'];
  3447. for (i in invalidSamples) {
  3448. this.bv.resetForm();
  3449. this.$id.val(invalidSamples[i]);
  3450. this.bv.validate();
  3451. expect(this.bv.isValid()).toEqual(false);
  3452. }
  3453. });
  3454. it('Slovak national identifier number (RC)', function() {
  3455. this.bv.updateOption('id', 'id', 'country', 'SK');
  3456. // Valid samples
  3457. var validSamples = ['7103192745', '991231123'];
  3458. for (var i in validSamples) {
  3459. this.bv.resetForm();
  3460. this.$id.val(validSamples[i]);
  3461. this.bv.validate();
  3462. expect(this.bv.isValid()).toBeTruthy();
  3463. }
  3464. // Invalid samples
  3465. var invalidSamples = ['7103192746', '1103492745'];
  3466. for (i in invalidSamples) {
  3467. this.bv.resetForm();
  3468. this.$id.val(invalidSamples[i]);
  3469. this.bv.validate();
  3470. expect(this.bv.isValid()).toEqual(false);
  3471. }
  3472. });
  3473. it('South African ID', function() {
  3474. this.bv.updateOption('id', 'id', 'country', 'ZA');
  3475. // Valid samples
  3476. var validSamples = ['8001015009087'];
  3477. for (var i in validSamples) {
  3478. this.bv.resetForm();
  3479. this.$id.val(validSamples[i]);
  3480. this.bv.validate();
  3481. expect(this.bv.isValid()).toBeTruthy();
  3482. }
  3483. // Invalid samples
  3484. var invalidSamples = ['8001015009287', '8001015009086'];
  3485. for (i in invalidSamples) {
  3486. this.bv.resetForm();
  3487. this.$id.val(invalidSamples[i]);
  3488. this.bv.validate();
  3489. expect(this.bv.isValid()).toEqual(false);
  3490. }
  3491. });
  3492. it('Thailand citizen number', function() {
  3493. this.bv.updateOption('id', 'id', 'country', 'TH');
  3494. // Valid samples
  3495. var validSamples = ['7145620509547', '3688699975685', '2368719339716'];
  3496. for (var i in validSamples) {
  3497. this.bv.resetForm();
  3498. this.$id.val(validSamples[i]);
  3499. this.bv.validate();
  3500. expect(this.bv.isValid()).toBeTruthy();
  3501. }
  3502. // Invalid samples
  3503. var invalidSamples = ['1100800092310'];
  3504. for (i in invalidSamples) {
  3505. this.bv.resetForm();
  3506. this.$id.val(invalidSamples[i]);
  3507. this.bv.validate();
  3508. expect(this.bv.isValid()).toEqual(false);
  3509. }
  3510. });
  3511. });
  3512. describe('imo', function() {
  3513. beforeEach(function() {
  3514. $([
  3515. '<form class="form-horizontal" id="imoForm">',
  3516. '<div class="form-group">',
  3517. '<input type="text" name="imo" data-bv-imo />',
  3518. '</div>',
  3519. '</form>'
  3520. ].join('\n')).appendTo('body');
  3521. $('#imoForm').bootstrapValidator();
  3522. this.bv = $('#imoForm').data('bootstrapValidator');
  3523. this.$imo = this.bv.getFieldElements('imo');
  3524. });
  3525. afterEach(function() {
  3526. $('#imoForm').bootstrapValidator('destroy').remove();
  3527. });
  3528. it('Valid IMO (upper)', function() {
  3529. this.bv.resetForm();
  3530. this.$imo.val('IMO 9074729');
  3531. this.bv.validate();
  3532. expect(this.bv.isValid()).toBeTruthy();
  3533. });
  3534. it('Valid IMO (lower)', function() {
  3535. this.bv.resetForm();
  3536. this.$imo.val('imo 9074729');
  3537. this.bv.validate();
  3538. expect(this.bv.isValid()).toBeTruthy();
  3539. });
  3540. it('Invalid IMO (bad format)', function() {
  3541. this.bv.resetForm();
  3542. this.$imo.val('9074729');
  3543. this.bv.validate();
  3544. expect(this.bv.isValid()).toBeFalsy();
  3545. });
  3546. it('Invalid IMO (bad check digit)', function() {
  3547. this.bv.resetForm();
  3548. this.$imo.val('IMO 9074728');
  3549. this.bv.validate();
  3550. expect(this.bv.isValid()).toBeFalsy();
  3551. });
  3552. });
  3553. describe('ip', function() {
  3554. beforeEach(function() {
  3555. $([
  3556. '<form class="form-horizontal" id="ipForm">',
  3557. '<div class="form-group">',
  3558. '<input type="text" name="ipv4" data-bv-ip data-bv-ip-ipv6="false" />',
  3559. '</div>',
  3560. '<div class="form-group">',
  3561. '<input type="text" name="ipv6" data-bv-ip data-bv-ip-ipv4="false" />',
  3562. '</div>',
  3563. '<div class="form-group">',
  3564. '<input type="text" name="both" data-bv-ip />',
  3565. '</div>',
  3566. '</form>'
  3567. ].join('\n')).appendTo('body');
  3568. $('#ipForm').bootstrapValidator();
  3569. this.bv = $('#ipForm').data('bootstrapValidator');
  3570. this.$ipv4 = this.bv.getFieldElements('ipv4');
  3571. this.$ipv6 = this.bv.getFieldElements('ipv6');
  3572. this.$both = this.bv.getFieldElements('both');
  3573. });
  3574. afterEach(function() {
  3575. $('#ipForm').bootstrapValidator('destroy').remove();
  3576. });
  3577. it('Valid ipv4', function() {
  3578. this.$ipv4.val('0.0.0.0');
  3579. this.bv.validate();
  3580. expect(this.bv.isValid()).toBeTruthy();
  3581. this.bv.resetForm();
  3582. this.$ipv4.val('192.168.1.1');
  3583. this.bv.validate();
  3584. expect(this.bv.isValid()).toBeTruthy();
  3585. this.bv.resetForm();
  3586. this.$ipv4.val('255.255.255.255');
  3587. this.bv.validate();
  3588. expect(this.bv.isValid()).toBeTruthy();
  3589. });
  3590. it('Invalid ipv4', function() {
  3591. this.$ipv4.val('10.168.0001.100'); // extra 0 not allowed
  3592. this.bv.validate();
  3593. expect(this.bv.isValid()).toEqual(false);
  3594. this.bv.resetForm();
  3595. this.$ipv4.val('0.0.0.256'); // 256 not allowed, max is 255
  3596. this.bv.validate();
  3597. expect(this.bv.isValid()).toEqual(false);
  3598. this.bv.resetForm();
  3599. this.$ipv4.val('256.255.255.255'); // max is 255.255.255.255
  3600. this.bv.validate();
  3601. expect(this.bv.isValid()).toEqual(false);
  3602. this.bv.resetForm();
  3603. this.$ipv4.val('192.168. 224.0'); // internal space
  3604. this.bv.validate();
  3605. expect(this.bv.isValid()).toEqual(false);
  3606. this.bv.resetForm();
  3607. this.$ipv4.val('192.168.224.0 1'); // junk after valid address
  3608. this.bv.validate();
  3609. expect(this.bv.isValid()).toEqual(false);
  3610. });
  3611. it('Valid ipv6', function() {
  3612. this.$ipv6.val('0000:0000:0000:0000:0000:0000:0000:0000');
  3613. this.bv.validate();
  3614. expect(this.bv.isValid()).toBeTruthy();
  3615. this.bv.resetForm();
  3616. this.$ipv6.val('fe00::1');
  3617. this.bv.validate();
  3618. expect(this.bv.isValid()).toBeTruthy();
  3619. this.bv.resetForm();
  3620. this.$ipv6.val('fe80::217:f2ff:fe07:ed62');
  3621. this.bv.validate();
  3622. expect(this.bv.isValid()).toBeTruthy();
  3623. this.bv.resetForm();
  3624. this.$ipv6.val('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff');
  3625. this.bv.validate();
  3626. expect(this.bv.isValid()).toBeTruthy();
  3627. });
  3628. it('Invalid ipv6', function() {
  3629. this.$ipv6.val('02001:0000:1234:0000:0000:C1C0:ABCD:0876'); // extra 0 not allowed
  3630. this.bv.validate();
  3631. expect(this.bv.isValid()).toEqual(false);
  3632. this.bv.resetForm();
  3633. this.$ipv6.val('2001:0000:1234:0000:00001:C1C0:ABCD:0876'); // extra 0 not allowed
  3634. this.bv.validate();
  3635. expect(this.bv.isValid()).toEqual(false);
  3636. this.bv.resetForm();
  3637. this.$ipv6.val('2001:0000:1234: 0000:0000:C1C0:ABCD:0876'); // internal space
  3638. this.bv.validate();
  3639. expect(this.bv.isValid()).toEqual(false);
  3640. this.bv.resetForm();
  3641. this.$ipv6.val('2001:0000:1234:0000:0000:C1C0:ABCD:0876 0'); // junk after valid address
  3642. this.bv.validate();
  3643. expect(this.bv.isValid()).toEqual(false);
  3644. this.bv.resetForm();
  3645. this.$ipv6.val('3ffe:0b00:0000:0001:0000:0000:000a'); // seven segment
  3646. this.bv.validate();
  3647. expect(this.bv.isValid()).toEqual(false);
  3648. this.bv.resetForm();
  3649. this.$ipv6.val('FF02:0000:0000:0000:0000:0000:0000:0000:0001'); // nine segment
  3650. this.bv.validate();
  3651. expect(this.bv.isValid()).toEqual(false);
  3652. this.bv.resetForm();
  3653. this.$ipv6.val('::1111:2222:3333:4444:5555:6666::'); // double "::"
  3654. this.bv.validate();
  3655. expect(this.bv.isValid()).toEqual(false);
  3656. this.bv.resetForm();
  3657. this.$ipv6.val('3ffe:b00::1::a'); // double "::"
  3658. this.bv.validate();
  3659. expect(this.bv.isValid()).toEqual(false);
  3660. });
  3661. it('Both', function() {
  3662. this.$both.val('255.255.255.255'); // valid
  3663. this.bv.validate();
  3664. expect(this.bv.isValid()).toBeTruthy();
  3665. this.bv.resetForm();
  3666. this.$both.val('256.0.0.0'); // 256 not allowed, max is 255
  3667. this.bv.validate();
  3668. expect(this.bv.isValid()).toEqual(false);
  3669. this.bv.resetForm();
  3670. this.$both.val('2001:0db8:0000:85a3:0000:0000:ac1f:8001'); // valid
  3671. this.bv.validate();
  3672. expect(this.bv.isValid()).toBeTruthy();
  3673. this.bv.resetForm();
  3674. this.$both.val('2001:0000:1234:0000:0000:C1C0:ABCD:0876 0'); // junk after valid address
  3675. this.bv.validate();
  3676. expect(this.bv.isValid()).toEqual(false);
  3677. });
  3678. });
  3679. describe('isbn', function() {
  3680. beforeEach(function() {
  3681. var html = [
  3682. '<div class="container">',
  3683. '<form class="form-horizontal" id="isbnForm">',
  3684. '<div class="form-group">',
  3685. '<input type="text" name="isbn" data-bv-isbn />',
  3686. '</div>',
  3687. '</form>',
  3688. '</div>'
  3689. ].join('\n');
  3690. $(html).appendTo('body');
  3691. $('#isbnForm').bootstrapValidator();
  3692. this.bv = $('#isbnForm').data('bootstrapValidator');
  3693. this.$isbn = this.bv.getFieldElements('isbn');
  3694. });
  3695. afterEach(function() {
  3696. $('#isbnForm').bootstrapValidator('destroy').parent().remove();
  3697. });
  3698. it('isbn10 hyphen', function() {
  3699. var samples = ['99921-58-10-7', '9971-5-0210-0', '960-425-059-0', '80-902734-1-6'];
  3700. for (var i in samples) {
  3701. this.$isbn.val(samples[i]);
  3702. this.bv.validate();
  3703. expect(this.bv.isValidField('isbn')).toBeTruthy();
  3704. }
  3705. });
  3706. it('isbn10 space', function() {
  3707. var samples = ['85 359 0277 5', '1 84356 028 3', '0 684 84328 5', '0 85131 041 9', '0 943396 04 2'];
  3708. for (var i in samples) {
  3709. this.$isbn.val(samples[i]);
  3710. this.bv.validate();
  3711. expect(this.bv.isValidField('isbn')).toBeTruthy();
  3712. }
  3713. });
  3714. it('isbn10 hyphen with X', function() {
  3715. var samples = ['0-8044-2957-X', '0-9752298-0-X'];
  3716. for (var i in samples) {
  3717. this.$isbn.val(samples[i]);
  3718. this.bv.validate();
  3719. expect(this.bv.isValidField('isbn')).toBeTruthy();
  3720. }
  3721. });
  3722. it('isbn10 invalid check digit', function() {
  3723. this.$isbn.val('99921-58-10-6');
  3724. this.bv.validate();
  3725. expect(this.bv.isValidField('isbn')).toEqual(false);
  3726. });
  3727. it('isbn13', function() {
  3728. this.$isbn.val('978-0-306-40615-7');
  3729. this.bv.validate();
  3730. expect(this.bv.isValidField('isbn')).toBeTruthy();
  3731. });
  3732. it('isbn13 invalid check digit', function() {
  3733. this.$isbn.val('978-0-306-40615-6');
  3734. this.bv.validate();
  3735. expect(this.bv.isValidField('isbn')).toEqual(false);
  3736. });
  3737. });
  3738. describe('isin', function() {
  3739. beforeEach(function() {
  3740. var html = [
  3741. '<div class="container">',
  3742. '<form class="form-horizontal" id="isinForm">',
  3743. '<div class="form-group">',
  3744. '<input type="text" name="isin" data-bv-isin />',
  3745. '</div>',
  3746. '</form>',
  3747. '</div>'
  3748. ].join('\n');
  3749. $(html).appendTo('body');
  3750. $('#isinForm').bootstrapValidator();
  3751. this.bv = $('#isinForm').data('bootstrapValidator');
  3752. this.$isin = this.bv.getFieldElements('isin');
  3753. });
  3754. afterEach(function() {
  3755. $('#isinForm').bootstrapValidator('destroy').parent().remove();
  3756. });
  3757. it('valid', function() {
  3758. var samples = ['US0378331005', 'AU0000XVGZA3', 'GB0002634946'];
  3759. for (var i in samples) {
  3760. this.$isin.val(samples[i]);
  3761. this.bv.validate();
  3762. expect(this.bv.isValidField('isin')).toBeTruthy();
  3763. }
  3764. });
  3765. it('invalid country code', function() {
  3766. this.$isin.val('AA0000XVGZA3');
  3767. this.bv.validate();
  3768. expect(this.bv.isValidField('isin')).toEqual(false);
  3769. });
  3770. it('contains only digits and alphabet', function() {
  3771. this.$isin.val('US12345ABC@#$');
  3772. this.bv.validate();
  3773. expect(this.bv.isValidField('isin')).toEqual(false);
  3774. });
  3775. it('invalid length', function() {
  3776. this.$isin.val('US1234567');
  3777. this.bv.validate();
  3778. expect(this.bv.isValidField('isin')).toEqual(false);
  3779. });
  3780. it('invalid check digit', function() {
  3781. this.$isin.val('US0378331004');
  3782. this.bv.validate();
  3783. expect(this.bv.isValidField('isin')).toEqual(false);
  3784. });
  3785. });
  3786. describe('ismn', function() {
  3787. beforeEach(function() {
  3788. var html = [
  3789. '<div class="container">',
  3790. '<form class="form-horizontal" id="ismnForm">',
  3791. '<div class="form-group">',
  3792. '<input type="text" name="ismn" data-bv-ismn />',
  3793. '</div>',
  3794. '</form>',
  3795. '</div>'
  3796. ].join('\n');
  3797. $(html).appendTo('body');
  3798. $('#ismnForm').bootstrapValidator();
  3799. this.bv = $('#ismnForm').data('bootstrapValidator');
  3800. this.$ismn = this.bv.getFieldElements('ismn');
  3801. });
  3802. afterEach(function() {
  3803. $('#ismnForm').bootstrapValidator('destroy').parent().remove();
  3804. });
  3805. it('valid start with M', function() {
  3806. this.$ismn.val('M230671187');
  3807. this.bv.validate();
  3808. expect(this.bv.isValidField('ismn')).toBeTruthy();
  3809. });
  3810. it('valid start with 979', function() {
  3811. this.$ismn.val('9790060115615');
  3812. this.bv.validate();
  3813. expect(this.bv.isValidField('ismn')).toBeTruthy();
  3814. });
  3815. it('valid contains spaces', function() {
  3816. this.$ismn.val('979 0 3452 4680 5');
  3817. this.bv.validate();
  3818. expect(this.bv.isValidField('ismn')).toBeTruthy();
  3819. });
  3820. it('valid contains dashes', function() {
  3821. this.$ismn.val('979-0-0601-1561-5');
  3822. this.bv.validate();
  3823. expect(this.bv.isValidField('ismn')).toBeTruthy();
  3824. });
  3825. it('invalid format', function() {
  3826. this.$ismn.val('N123456789');
  3827. this.bv.validate();
  3828. expect(this.bv.isValidField('ismn')).toEqual(false);
  3829. });
  3830. it('invalid check digit', function() {
  3831. this.$ismn.val('9790060115614');
  3832. this.bv.validate();
  3833. expect(this.bv.isValidField('ismn')).toEqual(false);
  3834. });
  3835. });
  3836. describe('issn', function() {
  3837. beforeEach(function() {
  3838. var html = [
  3839. '<div class="container">',
  3840. '<form class="form-horizontal" id="issnForm">',
  3841. '<div class="form-group">',
  3842. '<input type="text" name="issn" data-bv-issn />',
  3843. '</div>',
  3844. '</form>',
  3845. '</div>'
  3846. ].join('\n');
  3847. $(html).appendTo('body');
  3848. $('#issnForm').bootstrapValidator();
  3849. this.bv = $('#issnForm').data('bootstrapValidator');
  3850. this.$issn = this.bv.getFieldElements('issn');
  3851. });
  3852. afterEach(function() {
  3853. $('#issnForm').bootstrapValidator('destroy').parent().remove();
  3854. });
  3855. it('valid', function() {
  3856. var samples = ['0378-5955', '0024-9319', '0032-1478'];
  3857. for (var i in samples) {
  3858. this.$issn.val(samples[i]);
  3859. this.bv.validate();
  3860. expect(this.bv.isValidField('issn')).toBeTruthy();
  3861. }
  3862. });
  3863. it('not contains hyphen', function() {
  3864. this.$issn.val('03785955');
  3865. this.bv.validate();
  3866. expect(this.bv.isValidField('issn')).toEqual(false);
  3867. });
  3868. it('contains only digits, X', function() {
  3869. this.$issn.val('1234-566A');
  3870. this.bv.validate();
  3871. expect(this.bv.isValidField('issn')).toEqual(false);
  3872. });
  3873. it('invalid check sum', function() {
  3874. this.$issn.val('0032-147X');
  3875. this.bv.validate();
  3876. expect(this.bv.isValidField('issn')).toEqual(false);
  3877. });
  3878. });
  3879. function lessThanCompare() {
  3880. var compareTo = $('#lessThanForm').find('[name="maxAge"]').val();
  3881. $('#msg').html('lessThanCompare() called; compare to ' + compareTo);
  3882. return compareTo;
  3883. };
  3884. TestSuite = $.extend({}, TestSuite, {
  3885. lessThan: {
  3886. compareTo: function(value, validator, $field) {
  3887. var compareTo = $('#lessThanForm').find('[name="maxAge"]').val();
  3888. $('#msg').html('TestSuite.lessThan.compareTo() called; compare to ' + compareTo);
  3889. return compareTo;
  3890. }
  3891. }
  3892. });
  3893. describe('lessThan', function() {
  3894. beforeEach(function() {
  3895. $([
  3896. '<form class="form-horizontal" id="lessThanForm">',
  3897. '<div id="msg"></div>',
  3898. '<div class="form-group">',
  3899. '<input type="text" name="maxAge" />',
  3900. '</div>',
  3901. '<div class="form-group">',
  3902. '<input type="text" name="age" data-bv-lessthan data-bv-lessthan-value="100" />',
  3903. '</div>',
  3904. '</form>'
  3905. ].join('\n')).appendTo('body');
  3906. $('#lessThanForm').bootstrapValidator();
  3907. this.bv = $('#lessThanForm').data('bootstrapValidator');
  3908. this.$maxAge = this.bv.getFieldElements('maxAge');
  3909. this.$age = this.bv.getFieldElements('age');
  3910. });
  3911. afterEach(function() {
  3912. $('#lessThanForm').bootstrapValidator('destroy').remove();
  3913. });
  3914. it('not a number', function() {
  3915. this.$age.val('20abc');
  3916. this.bv.validate();
  3917. expect(this.bv.isValid()).toEqual(false);
  3918. });
  3919. it('compare to value', function() {
  3920. this.$age.val(120);
  3921. this.bv.validate();
  3922. expect(this.bv.isValid()).toEqual(false);
  3923. this.bv.resetForm();
  3924. this.$age.val(30);
  3925. this.bv.validate();
  3926. expect(this.bv.isValid()).toBeTruthy();
  3927. });
  3928. it('compare to other field', function() {
  3929. this.$age.attr('data-bv-lessthan-value', 'maxAge');
  3930. this.bv.destroy();
  3931. this.bv = $('#lessThanForm').bootstrapValidator().data('bootstrapValidator');
  3932. this.$maxAge.val(40);
  3933. this.$age.val(20);
  3934. this.bv.validate();
  3935. expect(this.bv.isValid()).toBeTruthy();
  3936. this.bv.resetForm();
  3937. this.$maxAge.val(20);
  3938. this.$age.val(30);
  3939. this.bv.validate();
  3940. expect(this.bv.isValid()).toEqual(false);
  3941. expect(this.bv.getMessages('age', 'lessThan')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.lessThan['default'], this.$maxAge.val()));
  3942. });
  3943. it('compare to return value of a function', function() {
  3944. this.$age.attr('data-bv-lessthan-value', 'lessThanCompare');
  3945. this.bv.destroy();
  3946. this.bv = $('#lessThanForm').bootstrapValidator().data('bootstrapValidator');
  3947. this.$maxAge.val(50);
  3948. this.$age.val(60);
  3949. this.bv.validate();
  3950. expect($('#msg').html()).toEqual('lessThanCompare() called; compare to 50');
  3951. expect(this.bv.isValid()).toEqual(false);
  3952. expect(this.bv.getMessages('age', 'lessThan')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.lessThan['default'], this.$maxAge.val()));
  3953. this.bv.resetForm();
  3954. this.$maxAge.val(60);
  3955. this.$age.val(30);
  3956. this.bv.validate();
  3957. expect($('#msg').html()).toEqual('lessThanCompare() called; compare to 60');
  3958. expect(this.bv.isValid()).toBeTruthy();
  3959. });
  3960. it('compare to return value of a namespace function', function() {
  3961. this.$age.attr('data-bv-lessthan-value', 'TestSuite.lessThan.compareTo');
  3962. this.bv.destroy();
  3963. this.bv = $('#lessThanForm').bootstrapValidator().data('bootstrapValidator');
  3964. this.$maxAge.val(50);
  3965. this.$age.val(60);
  3966. this.bv.validate();
  3967. expect($('#msg').html()).toEqual('TestSuite.lessThan.compareTo() called; compare to 50');
  3968. expect(this.bv.isValid()).toEqual(false);
  3969. expect(this.bv.getMessages('age', 'lessThan')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.lessThan['default'], this.$maxAge.val()));
  3970. this.bv.resetForm();
  3971. this.$maxAge.val(60);
  3972. this.$age.val(30);
  3973. this.bv.validate();
  3974. expect($('#msg').html()).toEqual('TestSuite.lessThan.compareTo() called; compare to 60');
  3975. expect(this.bv.isValid()).toBeTruthy();
  3976. });
  3977. });
  3978. describe('meid', function() {
  3979. beforeEach(function() {
  3980. $([
  3981. '<form class="form-horizontal" id="meidForm">',
  3982. '<div class="form-group">',
  3983. '<input type="text" name="meid" data-bv-meid />',
  3984. '</div>',
  3985. '</form>'
  3986. ].join('\n')).appendTo('body');
  3987. $('#meidForm').bootstrapValidator();
  3988. this.bv = $('#meidForm').data('bootstrapValidator');
  3989. this.$meid = this.bv.getFieldElements('meid');
  3990. });
  3991. afterEach(function() {
  3992. $('#meidForm').bootstrapValidator('destroy').remove();
  3993. });
  3994. it('Valid MEID (14 hex, check digit)', function() {
  3995. this.bv.resetForm();
  3996. this.$meid.val('A00000049259B16');
  3997. this.bv.validate();
  3998. expect(this.bv.isValid()).toBeTruthy();
  3999. });
  4000. it('Valid MEID (14 hex, dashes, check digit)', function() {
  4001. this.bv.resetForm();
  4002. this.$meid.val('A0-000004-9259B1-6');
  4003. this.bv.validate();
  4004. expect(this.bv.isValid()).toBeTruthy();
  4005. });
  4006. it('Valid MEID (14 hex, spaces, check digit)', function() {
  4007. this.bv.resetForm();
  4008. this.$meid.val('A0 000004 9259B1 6');
  4009. this.bv.validate();
  4010. expect(this.bv.isValid()).toBeTruthy();
  4011. });
  4012. it('Valid MEID (18 dec, check digit)', function() {
  4013. this.bv.resetForm();
  4014. this.$meid.val('2936087365007037100');
  4015. this.bv.validate();
  4016. expect(this.bv.isValid()).toBeTruthy();
  4017. });
  4018. it('Valid MEID (18 dec, dashes, check digit)', function() {
  4019. this.bv.resetForm();
  4020. this.$meid.val('29360-87365-0070-3710-0');
  4021. this.bv.validate();
  4022. expect(this.bv.isValid()).toBeTruthy();
  4023. });
  4024. it('Valid MEID (18 dec, spaces, check digit)', function() {
  4025. this.bv.resetForm();
  4026. this.$meid.val('29360 87365 0070 3710 0');
  4027. this.bv.validate();
  4028. expect(this.bv.isValid()).toBeTruthy();
  4029. });
  4030. it('Valid MEID (14 hex)', function() {
  4031. this.bv.resetForm();
  4032. this.$meid.val('AF0123450ABCDE');
  4033. this.bv.validate();
  4034. expect(this.bv.isValid()).toBeTruthy();
  4035. });
  4036. it('Valid MEID (14 hex, dashes)', function() {
  4037. this.bv.resetForm();
  4038. this.$meid.val('AF-012345-0ABCDE');
  4039. this.bv.validate();
  4040. expect(this.bv.isValid()).toBeTruthy();
  4041. });
  4042. it('Valid MEID (14 hex, spaces)', function() {
  4043. this.bv.resetForm();
  4044. this.$meid.val('AF 012345 0ABCDE');
  4045. this.bv.validate();
  4046. expect(this.bv.isValid()).toBeTruthy();
  4047. });
  4048. it('Valid MEID (18 dec)', function() {
  4049. this.bv.resetForm();
  4050. this.$meid.val('293608736500703710');
  4051. this.bv.validate();
  4052. expect(this.bv.isValid()).toBeTruthy();
  4053. });
  4054. it('Valid MEID (18 dec, dashes)', function() {
  4055. this.bv.resetForm();
  4056. this.$meid.val('29360-87365-0070-3710');
  4057. this.bv.validate();
  4058. expect(this.bv.isValid()).toBeTruthy();
  4059. });
  4060. it('Valid MEID (18 dec, spaces)', function() {
  4061. this.bv.resetForm();
  4062. this.$meid.val('29360 87365 0070 3710');
  4063. this.bv.validate();
  4064. expect(this.bv.isValid()).toBeTruthy();
  4065. });
  4066. it('Invalid MEID (14 hex, bad check digit)', function() {
  4067. this.bv.resetForm();
  4068. this.$meid.val('A00000049259B15');
  4069. this.bv.validate();
  4070. expect(this.bv.isValid()).toBeFalsy();
  4071. });
  4072. it('Invalid MEID (13 hex)', function() {
  4073. this.bv.resetForm();
  4074. this.$meid.val('A00000049259B');
  4075. this.bv.validate();
  4076. expect(this.bv.isValid()).toBeFalsy();
  4077. });
  4078. it('Invalid MEID (18 dec, bad check digit)', function() {
  4079. this.bv.resetForm();
  4080. this.$meid.val('2936087365007037101');
  4081. this.bv.validate();
  4082. expect(this.bv.isValid()).toBeFalsy();
  4083. });
  4084. it('Invalid MEID (17 dec)', function() {
  4085. this.bv.resetForm();
  4086. this.$meid.val('29360873650070371');
  4087. this.bv.validate();
  4088. expect(this.bv.isValid()).toBeFalsy();
  4089. });
  4090. });
  4091. describe('phone', function() {
  4092. beforeEach(function() {
  4093. $([
  4094. '<form class="form-horizontal" id="phoneForm">',
  4095. '<div class="form-group">',
  4096. '<select class="form-control" name="country">',
  4097. '<option value="BR">Brazil</option>',
  4098. '<option value="CN">China</option>',
  4099. '<option value="DK">Denmark</option>',
  4100. '<option value="ES">Spain</option>',
  4101. '<option value="FR">France</option>',
  4102. '<option value="GB">United Kingdom</option>',
  4103. '<option value="MA">Morocco</option>',
  4104. '<option value="PK">Pakistan</option>',
  4105. '<option value="RO">Romania</option>',
  4106. '<option value="RU">Russia</option>',
  4107. '<option value="TH">Thailand</option>',
  4108. '<option value="US">USA</option>',
  4109. '<option value="VE">Venezuela</option>',
  4110. '</select>',
  4111. '</div>',
  4112. '<div class="form-group">',
  4113. '<input type="text" name="phone" data-bv-phone />',
  4114. '</div>',
  4115. '</form>',
  4116. ].join('\n')).appendTo('body');
  4117. $('#phoneForm').bootstrapValidator();
  4118. /**
  4119. * @type {BootstrapValidator}
  4120. */
  4121. this.bv = $('#phoneForm').data('bootstrapValidator');
  4122. this.$country = this.bv.getFieldElements('country');
  4123. this.$phone = this.bv.getFieldElements('phone');
  4124. });
  4125. afterEach(function() {
  4126. $('#phoneForm').bootstrapValidator('destroy').remove();
  4127. });
  4128. it('dynamic country', function() {
  4129. this.$phone.attr('data-bv-phone-country', 'country');
  4130. this.bv.destroy();
  4131. this.bv = $('#phoneForm').bootstrapValidator().data('bootstrapValidator');
  4132. this.$country.val('BR');
  4133. this.$phone.val('16920894635');
  4134. this.bv.validate();
  4135. expect(this.bv.isValid()).toBeTruthy();
  4136. this.bv.resetForm();
  4137. this.$country.val('FR');
  4138. this.$phone.val('0644444444');
  4139. this.bv.validate();
  4140. expect(this.bv.isValid()).toBeTruthy();
  4141. this.bv.resetForm();
  4142. this.$country.val('GB');
  4143. this.$phone.val('012345678900');
  4144. this.bv.validate();
  4145. expect(this.bv.isValid()).toBeFalsy();
  4146. });
  4147. it('Brazil phone number', function() {
  4148. this.bv.updateOption('phone', 'phone', 'country', 'BR');
  4149. // Valid samples
  4150. var validSamples = [
  4151. '0800.000.00.00', '0800-000-00-00', '0800 000 00 00', '0800-00-00-00', '0800.00.00.00', '0800 00 00 00',
  4152. '0800-000-0000', '0800 000 0000', '0800.000.0000', '08000000000',
  4153. '1692089-4635', '16920894635', '16992089-4635', '16 99202-4635', '(16)99202-4635', '(16)92089-4635',
  4154. '(16) 92089-4635', '(15) 4343-4343', '+55 15 3702-7523', '(+55) 15 3702-7523', '(+55)1537027523',
  4155. '(+55)(15)3702-7523', '(+55) 15 3702-7523', '(+55) 15 99202-7523', '99202-4635', '(16) 9208-4635'
  4156. ];
  4157. for (var i in validSamples) {
  4158. this.bv.resetForm();
  4159. this.$phone.val(validSamples[i]);
  4160. this.bv.validate();
  4161. expect(this.bv.isValid()).toBeTruthy();
  4162. }
  4163. });
  4164. it('China phone number', function() {
  4165. this.bv.updateOption('phone', 'phone', 'country', 'CN');
  4166. // Valid samples
  4167. var validSamples = [
  4168. '18911111111', '189 1111 1111', '189-1111-1111', '0086-18911111111', '+86-18911111111',
  4169. '86-18911111111', '0086 18911111111', '+86 18911111111', '86 18911111111', '0086 189-1111-1111',
  4170. '+86 189-1111-1111', '86 189-1111-1111', '02011111111', '020-11111111', '020 11111111',
  4171. '020 1111 1111', '020-1111-1111', '0086 020 82803159', '0086-020-82803159', '0086-020-82803159',
  4172. '+86 20 61302222-8866', '+86 20 6130-2222-8866', '+86 10 59081185'
  4173. ];
  4174. for (var i in validSamples) {
  4175. this.bv.resetForm();
  4176. this.$phone.val(validSamples[i]);
  4177. this.bv.validate();
  4178. expect(this.bv.isValid()).toBeTruthy();
  4179. }
  4180. });
  4181. it('France phone number', function() {
  4182. this.bv.updateOption('phone', 'phone', 'country', 'FR');
  4183. // Valid samples
  4184. var validSamples = [
  4185. // National formats
  4186. '0644444444', '06 44 44 44 44', '06-44-44-44-44', '06.44.44.44.44',
  4187. // International formats
  4188. '+33644444444', '+336.44.44.44.44', '+33 6.44.44.44.44', '0033644444444', '00336.44.44.44.44',
  4189. '0033 6.44.44.44.44',
  4190. // Some times
  4191. '+33(0)644444444', '+33 (0) 644444444'
  4192. ];
  4193. for (var i in validSamples) {
  4194. this.bv.resetForm();
  4195. this.$phone.val(validSamples[i]);
  4196. this.bv.validate();
  4197. expect(this.bv.isValid()).toBeTruthy();
  4198. }
  4199. // Invalid samples
  4200. var invalidSamples = [
  4201. // The separator between pairs of digits is not the same
  4202. '06 44.44-44.44', '06 44 44-44.44', '06 44 44-4444', '06 44 44-4444',
  4203. // Too many digits
  4204. '06444444444444',
  4205. // Missing leading 0
  4206. '6644444444',
  4207. // Too much non-numeric characters
  4208. '06 44.44-44.44', '+33 (0) 644444444',
  4209. // Bad parenthesis
  4210. '(0)644444444',
  4211. // Bad separator after the international prefix
  4212. '+33-(0)-644444444', '+33 (0)-644444444', '+33-(0) 644444444',
  4213. // Trailing separator
  4214. '06.44.44.44.44.'
  4215. ];
  4216. for (i in invalidSamples) {
  4217. this.bv.resetForm();
  4218. this.$phone.val(invalidSamples[i]);
  4219. this.bv.validate();
  4220. expect(this.bv.isValid()).toEqual(false);
  4221. }
  4222. });
  4223. it('United Kingdom phone number', function() {
  4224. this.bv.updateOption('phone', 'phone', 'country', 'GB');
  4225. // Valid samples
  4226. var validSamples = [
  4227. // National formats
  4228. '01611234567', '0161 123 4567', '(0161) 123 4567', '0161-123-4567',
  4229. // International formats
  4230. '+44 161 123 4567', '+441611234567', '+44(0)161234567', '00 44 161 1234567', '(011) 44 161 234567', '0161-158-5587',
  4231. // Extensions
  4232. '0161 123 4567 ext. 123', '01611234567x123', '+44161234567x123', '+44 (0) 161 1234567 ext 123'
  4233. ];
  4234. for (var i in validSamples) {
  4235. this.bv.resetForm();
  4236. this.$phone.val(validSamples[i]);
  4237. this.bv.validate();
  4238. expect(this.bv.isValid()).toBeTruthy();
  4239. }
  4240. // Invalid samples
  4241. var invalidSamples = [
  4242. '012345678900', // Too many digits
  4243. '1611234567', // Missing trunk
  4244. '012345678', // Not enough digits
  4245. '123 4567', // Missing area code
  4246. '061 123 4567' // Invalid area code
  4247. ];
  4248. for (i in invalidSamples) {
  4249. this.bv.resetForm();
  4250. this.$phone.val(invalidSamples[i]);
  4251. this.bv.validate();
  4252. expect(this.bv.isValid()).toEqual(false);
  4253. }
  4254. });
  4255. it('Morocco phone number', function() {
  4256. this.bv.updateOption('phone', 'phone', 'country', 'MA');
  4257. // Valid samples
  4258. var validSamples = [
  4259. // National formats
  4260. '0644444444', '0610245896', '0630548564', '06 44 44 44 44', '06-44-44-44-44', '06.44.44.44.44', '06 44.44-44.44',
  4261. '0528254856', '0535484541', '05 28 44 44 44', '05-28-44.44.44', '05.28.44.44.44', '05 28.44-44.44',
  4262. // International formats
  4263. '+212644444444', '+2126.44.44.44.44', '+212 6.44.44.44.44', '00212644444444', '002126.44.44.44.44', '00212 6.44.44.44.44',
  4264. // Some times
  4265. '+212(0)644444444', '+212 (0) 644444444'
  4266. ];
  4267. for (var i in validSamples) {
  4268. this.bv.resetForm();
  4269. this.$phone.val(validSamples[i]);
  4270. this.bv.validate();
  4271. expect(this.bv.isValid()).toBeTruthy();
  4272. }
  4273. // Invalid samples
  4274. var invalidSamples = [
  4275. '0625468961', '0512548632', '0542564896', // Not a valid phone numbers
  4276. '06444444444444', // Too many digits
  4277. '6644444444', // Missing leading 0
  4278. '06 44.44-44.44', '+212 (0) 644444444', // Too much non-numeric characters
  4279. '(0)644444444' // Bad parenthesis
  4280. ];
  4281. for (i in invalidSamples) {
  4282. this.bv.resetForm();
  4283. this.$phone.val(invalidSamples[i]);
  4284. this.bv.validate();
  4285. expect(this.bv.isValid()).toEqual(false);
  4286. }
  4287. });
  4288. it('Pakistan phone number', function() {
  4289. this.bv.updateOption('phone', 'phone', 'country', 'PK');
  4290. // Valid samples
  4291. var validSamples = ['03336527366'];
  4292. for (var i in validSamples) {
  4293. this.bv.resetForm();
  4294. this.$phone.val(validSamples[i]);
  4295. this.bv.validate();
  4296. expect(this.bv.isValid()).toBeTruthy();
  4297. }
  4298. });
  4299. it('Romania phone number', function() {
  4300. this.bv.updateOption('phone', 'phone', 'country', 'RO');
  4301. // Valid samples
  4302. var validSamples = [
  4303. '+40213-564-864', '+40213.564.864', '+40213 564 864', '0213-564-864',
  4304. '0213564864', '0313564864',
  4305. '0720512346', '0730512346', '0740512346', '0750512346', '+40750512346', '+40750.512.346',
  4306. '0760512346', '0770512346', '0780512346'
  4307. ];
  4308. for (var i in validSamples) {
  4309. this.bv.resetForm();
  4310. this.$phone.val(validSamples[i]);
  4311. this.bv.validate();
  4312. expect(this.bv.isValid()).toBeTruthy();
  4313. }
  4314. // Invalid samples
  4315. var invalidSamples = [
  4316. '0213/564/864', // Invalid separator
  4317. '0413564864', // Invalid land line number (The valid one should be +402, +403 or inside country 02 - 03)
  4318. '0790512346' // Invalid mobile phone number (The valid one is 072xxxxxxx - 078xxxxxxx)
  4319. ];
  4320. for (i in invalidSamples) {
  4321. this.bv.resetForm();
  4322. this.$phone.val(invalidSamples[i]);
  4323. this.bv.validate();
  4324. expect(this.bv.isValid()).toEqual(false);
  4325. }
  4326. });
  4327. it('Russia phone number', function() {
  4328. this.bv.updateOption('phone', 'phone', 'country', 'RU');
  4329. // Valid samples
  4330. var validSamples = ['+7(911)976-91-04'];
  4331. for (var i in validSamples) {
  4332. this.bv.resetForm();
  4333. this.$phone.val(validSamples[i]);
  4334. this.bv.validate();
  4335. expect(this.bv.isValid()).toBeTruthy();
  4336. }
  4337. });
  4338. });
  4339. describe('rgbColor', function() {
  4340. beforeEach(function() {
  4341. var html = [
  4342. '<div class="container">',
  4343. '<form class="form-horizontal" id="rbgColorForm">',
  4344. '<div class="form-group">',
  4345. '<input type="text" name="rc" data-bv-rgbcolor />',
  4346. '</div>',
  4347. '</form>',
  4348. '</div>'
  4349. ].join('\n');
  4350. $(html).appendTo('body');
  4351. $('#rbgColorForm').bootstrapValidator();
  4352. this.bv = $('#rbgColorForm').data('bootstrapValidator');
  4353. this.$rgbColor = this.bv.getFieldElements('rc');
  4354. });
  4355. afterEach(function() {
  4356. $('#rbgColorForm').bootstrapValidator('destroy').parent().remove();
  4357. });
  4358. it('accept rgb()', function() {
  4359. this.$rgbColor.val('rgb(255,255,255)');
  4360. this.bv.validate();
  4361. expect(this.bv.isValidField('rc')).toBeTruthy();
  4362. });
  4363. it('accept spaces around numeric values', function() {
  4364. this.$rgbColor.val('rgb( 255 , 255 , 255 )');
  4365. this.bv.validate();
  4366. expect(this.bv.isValidField('rc')).toBeTruthy();
  4367. });
  4368. it('accept multiple spaces around numeric values', function() {
  4369. this.$rgbColor.val('rgb( 255, 255, 255 )');
  4370. this.bv.validate();
  4371. expect(this.bv.isValidField('rc')).toBeTruthy();
  4372. });
  4373. it('accept interger values', function() {
  4374. this.$rgbColor.val('rgb(255,255,255)');
  4375. this.bv.validate();
  4376. expect(this.bv.isValidField('rc')).toBeTruthy();
  4377. });
  4378. it('accept percent values', function() {
  4379. this.$rgbColor.val('rgb(100%,100%,100%)');
  4380. this.bv.validate();
  4381. expect(this.bv.isValidField('rc')).toBeTruthy();
  4382. });
  4383. it('reject mixed intergers and percentile input', function() {
  4384. this.$rgbColor.val('rgb(255,255,100%)');
  4385. this.bv.validate();
  4386. expect(this.bv.isValidField('rc')).toEqual(false);
  4387. });
  4388. it('reject negative integers', function() {
  4389. this.$rgbColor.val('rgb(-10,255,255)');
  4390. this.bv.validate();
  4391. expect(this.bv.isValidField('rc')).toEqual(false);
  4392. });
  4393. it('reject negative percentages', function() {
  4394. this.$rgbColor.val('rgb(-10%,100%,100%)');
  4395. this.bv.validate();
  4396. expect(this.bv.isValidField('rc')).toEqual(false);
  4397. });
  4398. it('require rgb()', function() {
  4399. this.$rgbColor.val('255,255,255');
  4400. this.bv.validate();
  4401. expect(this.bv.isValidField('rc')).toEqual(false);
  4402. });
  4403. it('reject intergers above 255', function() {
  4404. this.$rgbColor.val('rgb(255,255,256)');
  4405. this.bv.validate();
  4406. expect(this.bv.isValidField('rc')).toEqual(false);
  4407. });
  4408. it('reject percentages above 100%', function() {
  4409. this.$rgbColor.val('rgb(100%,100%,101%)');
  4410. this.bv.validate();
  4411. expect(this.bv.isValidField('rc')).toEqual(false);
  4412. });
  4413. it('reject space between rgb and (', function() {
  4414. this.$rgbColor.val('rgb (255,255,255)');
  4415. this.bv.validate();
  4416. expect(this.bv.isValidField('rc')).toEqual(false);
  4417. });
  4418. it('reject leading space', function() {
  4419. this.$rgbColor.val(' rgb(255,255,255)');
  4420. this.bv.validate();
  4421. expect(this.bv.isValidField('rc')).toEqual(false);
  4422. });
  4423. it('reject trailing space', function() {
  4424. this.$rgbColor.val('rgb(255,255,255) ');
  4425. this.bv.validate();
  4426. expect(this.bv.isValidField('rc')).toEqual(false);
  4427. });
  4428. });
  4429. describe('rgbaColor', function() {
  4430. beforeEach(function() {
  4431. var html = [
  4432. '<div class="container">',
  4433. '<form class="form-horizontal" id="rbgColorForm">',
  4434. '<div class="form-group">',
  4435. '<input type="text" name="rgba" data-bv-rgbacolor />',
  4436. '</div>',
  4437. '</form>',
  4438. '</div>'
  4439. ].join('\n');
  4440. $(html).appendTo('body');
  4441. $('#rbgColorForm').bootstrapValidator();
  4442. this.bv = $('#rbgColorForm').data('bootstrapValidator');
  4443. this.$rgbaColor = this.bv.getFieldElements('rgba');
  4444. });
  4445. afterEach(function() {
  4446. $('#rbgColorForm').bootstrapValidator('destroy').parent().remove();
  4447. });
  4448. it('accept rgba()', function() {
  4449. this.$rgbaColor.val('rgba(255,255,255,1)');
  4450. this.bv.validate();
  4451. expect(this.bv.isValidField('rgba')).toBeTruthy();
  4452. });
  4453. it('accept spaces around numeric values', function() {
  4454. this.$rgbaColor.val('rgba( 255 , 255 , 255 , 1 )');
  4455. this.bv.validate();
  4456. expect(this.bv.isValidField('rgba')).toBeTruthy();
  4457. });
  4458. it('accept multiple spaces around numeric values', function() {
  4459. this.$rgbaColor.val('rgba( 255 , 255 , 255 , 1 )');
  4460. this.bv.validate();
  4461. expect(this.bv.isValidField('rgba')).toBeTruthy();
  4462. });
  4463. it('accept interger values', function() {
  4464. this.$rgbaColor.val('rgba(255,255,255,1)');
  4465. this.bv.validate();
  4466. expect(this.bv.isValidField('rgba')).toBeTruthy();
  4467. });
  4468. it('accept percent values', function() {
  4469. this.$rgbaColor.val('rgba(100%,100%,100%,1)');
  4470. this.bv.validate();
  4471. expect(this.bv.isValidField('rgba')).toBeTruthy();
  4472. });
  4473. it('accept integer alpha channel value of 0', function() {
  4474. this.$rgbaColor.val('rgba(255,255,255,0)');
  4475. this.bv.validate();
  4476. expect(this.bv.isValidField('rgba')).toBeTruthy();
  4477. });
  4478. it('accept integer alpha channel value of 1', function() {
  4479. this.$rgbaColor.val('rgba(255,255,255,1)');
  4480. this.bv.validate();
  4481. expect(this.bv.isValidField('rgba')).toBeTruthy();
  4482. });
  4483. it('accept floating alpha channel with leading 0', function() {
  4484. this.$rgbaColor.val('rgba(255,255,255,0.5)');
  4485. this.bv.validate();
  4486. expect(this.bv.isValidField('rgba')).toBeTruthy();
  4487. });
  4488. it('accept floating alpha channel without leading 0', function() {
  4489. this.$rgbaColor.val('rgba(255,255,255,.5)');
  4490. this.bv.validate();
  4491. expect(this.bv.isValidField('rgba')).toBeTruthy();
  4492. });
  4493. it('accept floating alpha channel with more than 1 decimal place', function() {
  4494. this.$rgbaColor.val('rgba(255,255,255,.524141)');
  4495. this.bv.validate();
  4496. expect(this.bv.isValidField('rgba')).toBeTruthy();
  4497. });
  4498. it('accept integer alpha channel value of 0 with percentage rgb values', function() {
  4499. this.$rgbaColor.val('rgba(100%,100%,100%,0)');
  4500. this.bv.validate();
  4501. expect(this.bv.isValidField('rgba')).toBeTruthy();
  4502. });
  4503. it('accept integer alpha channel value of 1 with percentage rgb values', function() {
  4504. this.$rgbaColor.val('rgba(100%,100%,100%,1)');
  4505. this.bv.validate();
  4506. expect(this.bv.isValidField('rgba')).toBeTruthy();
  4507. });
  4508. it('accept floating alpha channel with leading 0 with percentage rgb values', function() {
  4509. this.$rgbaColor.val('rgba(100%,100%,100%,0.5)');
  4510. this.bv.validate();
  4511. expect(this.bv.isValidField('rgba')).toBeTruthy();
  4512. });
  4513. it('accept floating alpha channel without leading 0 with percentage rgb values', function() {
  4514. this.$rgbaColor.val('rgba(100%,100%,100%,.5)');
  4515. this.bv.validate();
  4516. expect(this.bv.isValidField('rgba')).toBeTruthy();
  4517. });
  4518. it('accept floating alpha channel with more than 1 decimal place with percentage rgb values', function() {
  4519. this.$rgbaColor.val('rgba(100%,100%,100%,.524141)');
  4520. this.bv.validate();
  4521. expect(this.bv.isValidField('rgba')).toBeTruthy();
  4522. });
  4523. it('reject percentage value for alpha channel', function() {
  4524. this.$rgbaColor.val('rgba(100%,100%,100%,50%)');
  4525. this.bv.validate();
  4526. expect(this.bv.isValidField('rgba')).toEqual(false);
  4527. });
  4528. it('reject integers larger than 1 for alpha channel', function() {
  4529. this.$rgbaColor.val('rgba(255,255,255,2)');
  4530. this.bv.validate();
  4531. expect(this.bv.isValidField('rgba')).toEqual(false);
  4532. });
  4533. it('reject negative integers for alpha channel', function() {
  4534. this.$rgbaColor.val('rgba(255,255,255,-1)');
  4535. this.bv.validate();
  4536. expect(this.bv.isValidField('rgba')).toEqual(false);
  4537. });
  4538. it('reject floats larger than 1 for alpha channel', function() {
  4539. this.$rgbaColor.val('rgba(255,255,255,1.000000000001)');
  4540. this.bv.validate();
  4541. expect(this.bv.isValidField('rgba')).toEqual(false);
  4542. });
  4543. it('reject negative floats for alpha channel', function() {
  4544. this.$rgbaColor.val('rgba(255,255,255,-0.5)');
  4545. this.bv.validate();
  4546. expect(this.bv.isValidField('rgba')).toEqual(false);
  4547. });
  4548. it('reject more floats larger than 1 for alpha channel', function() {
  4549. this.$rgbaColor.val('rgba(255,255,255,2.3)');
  4550. this.bv.validate();
  4551. expect(this.bv.isValidField('rgba')).toEqual(false);
  4552. });
  4553. it('reject mixed intergers and percentile input', function() {
  4554. this.$rgbaColor.val('rgba(255,255,100%,1)');
  4555. this.bv.validate();
  4556. expect(this.bv.isValidField('rgba')).toEqual(false);
  4557. });
  4558. it('reject negative integers', function() {
  4559. this.$rgbaColor.val('rgba(-10,255,255,1)');
  4560. this.bv.validate();
  4561. expect(this.bv.isValidField('rgba')).toEqual(false);
  4562. });
  4563. it('reject negative percentages', function() {
  4564. this.$rgbaColor.val('rgba(-10%,100%,100%,1)');
  4565. this.bv.validate();
  4566. expect(this.bv.isValidField('rgba')).toEqual(false);
  4567. });
  4568. it('require rgba()', function() {
  4569. this.$rgbaColor.val('255,255,255,1');
  4570. this.bv.validate();
  4571. expect(this.bv.isValidField('rgba')).toEqual(false);
  4572. });
  4573. it('reject intergers above 255', function() {
  4574. this.$rgbaColor.val('rgba(255,255,256),1');
  4575. this.bv.validate();
  4576. expect(this.bv.isValidField('rgba')).toEqual(false);
  4577. });
  4578. it('reject percentages above 100%', function() {
  4579. this.$rgbaColor.val('rgba(100%,100%,101%,1)');
  4580. this.bv.validate();
  4581. expect(this.bv.isValidField('rgba')).toEqual(false);
  4582. });
  4583. it('reject space between rgba and (', function() {
  4584. this.$rgbaColor.val('rgba (255,255,255,1)');
  4585. this.bv.validate();
  4586. expect(this.bv.isValidField('rgba')).toEqual(false);
  4587. });
  4588. it('reject leading space', function() {
  4589. this.$rgbaColor.val(' rgba(255,255,255,1)');
  4590. this.bv.validate();
  4591. expect(this.bv.isValidField('rgba')).toEqual(false);
  4592. });
  4593. it('reject trailing space', function() {
  4594. this.$rgbaColor.val('rgba(255,255,255,1) ');
  4595. this.bv.validate();
  4596. expect(this.bv.isValidField('rgba')).toEqual(false);
  4597. });
  4598. });
  4599. describe('uri', function() {
  4600. beforeEach(function() {
  4601. $([
  4602. '<form class="form-horizontal" id="uriForm">',
  4603. '<div id="msg"></div>',
  4604. '<div class="form-group">',
  4605. '<input type="text" name="uri" data-bv-uri />',
  4606. '</div>',
  4607. '</form>'
  4608. ].join('\n')).appendTo('body');
  4609. $('#uriForm').bootstrapValidator();
  4610. this.bv = $('#uriForm').data('bootstrapValidator');
  4611. this.$uri = this.bv.getFieldElements('uri');
  4612. });
  4613. afterEach(function() {
  4614. $('#uriForm').bootstrapValidator('destroy').remove();
  4615. });
  4616. var validGlobalURIs = [
  4617. 'http://foo.com/blah_blah',
  4618. 'http://foo.com/blah_blah',
  4619. 'http://foo.com/blah_blah/',
  4620. 'http://foo.com/blah_blah_(wikipedia)',
  4621. 'http://foo.com/blah_blah_(wikipedia)_(again)',
  4622. 'http://www.example.com/wpstyle/?p=364',
  4623. 'https://www.example.com/foo/?bar=baz&inga=42&quux',
  4624. 'http://✪df.ws/123',
  4625. 'http://userid:password@example.com:8080',
  4626. 'http://userid:password@example.com:8080/',
  4627. 'http://userid@example.com',
  4628. 'http://userid@example.com/',
  4629. 'http://userid@example.com:8080',
  4630. 'http://userid@example.com:8080/',
  4631. 'http://userid:password@example.com',
  4632. 'http://userid:password@example.com/',
  4633. 'http://142.42.1.1/',
  4634. 'http://142.42.1.1:8080/',
  4635. 'http://➡.ws/䨹',
  4636. 'http://⌘.ws',
  4637. 'http://⌘.ws/',
  4638. 'http://foo.com/blah_(wikipedia)#cite-1',
  4639. 'http://foo.com/blah_(wikipedia)_blah#cite-1',
  4640. 'http://foo.com/unicode_(✪)_in_parens',
  4641. 'http://foo.com/(something)?after=parens',
  4642. 'http://☺.damowmow.com/',
  4643. 'http://code.google.com/events/#&product=browser',
  4644. 'http://j.mp',
  4645. 'ftp://foo.bar/baz',
  4646. 'http://foo.bar/?q=Test%20URL-encoded%20stuff',
  4647. 'http://مثال.إختبار',
  4648. 'http://例子.测试',
  4649. 'http://उदाहरण.परीक्षा',
  4650. "http://-.~_!$&'()*+,;=:%40:80%2f::::::@example.com",
  4651. 'http://1337.net',
  4652. 'http://a.b-c.de',
  4653. 'http://223.255.255.254'
  4654. ];
  4655. var invalidGlobalURIs = [
  4656. 'http://',
  4657. 'http://.',
  4658. 'http://..',
  4659. 'http://../',
  4660. 'http://?',
  4661. 'http://??',
  4662. 'http://??/',
  4663. 'http://#',
  4664. 'http://##',
  4665. 'http://##/',
  4666. 'http://foo.bar?q=Spaces should be encoded',
  4667. '//',
  4668. '//a',
  4669. '///a',
  4670. '///',
  4671. 'http:///a',
  4672. 'foo.com',
  4673. 'rdar://1234',
  4674. 'h://test',
  4675. 'http:// shouldfail.com',
  4676. ':// should fail',
  4677. 'http://foo.bar/foo(bar)baz quux',
  4678. 'ftps://foo.bar/',
  4679. 'http://-error-.invalid/',
  4680. 'http://a.b--c.de/',
  4681. 'http://-a.b.co',
  4682. 'http://a.b-.co',
  4683. 'http://.www.foo.bar/',
  4684. 'http://www.foo.bar./',
  4685. 'http://.www.foo.bar./'
  4686. ];
  4687. var localURIs = [
  4688. 'http://intranetsite',
  4689. 'http://intranetsite/test',
  4690. 'http://intranetsite:80',
  4691. 'http://intranetsite:80/test',
  4692. 'http://user:pass@intranetsite',
  4693. 'http://user:pass@intranetsite/test',
  4694. 'http://user:pass@intranetsite:80',
  4695. 'http://user:pass@intranetsite:80/test',
  4696. 'http://10.1.1.0',
  4697. 'http://10.1.1.255',
  4698. 'http://10.1.1.1',
  4699. 'http://10.1.1.254',
  4700. 'http://127.0.0.1',
  4701. 'http://192.168.0.1',
  4702. 'http://0.0.0.0',
  4703. 'http://224.1.1.1',
  4704. 'http://1.1.1.1.1',
  4705. 'http://123.123.123',
  4706. 'http://3628126748'
  4707. ];
  4708. it('Valid URIs (allowLocal=false)', function() {
  4709. var me = this;
  4710. $.each(validGlobalURIs, function(index, uri) {
  4711. me.bv.resetForm();
  4712. me.$uri.val(uri);
  4713. me.bv.validate();
  4714. expect(me.bv.isValid()).toBeTruthy();
  4715. });
  4716. });
  4717. it('Invalid URIs (allowLocal=false)', function() {
  4718. var me = this;
  4719. $.each(invalidGlobalURIs.concat(localURIs), function(index, uri) {
  4720. me.bv.resetForm();
  4721. me.$uri.val(uri);
  4722. me.bv.validate();
  4723. expect(me.bv.isValid()).toEqual(false);
  4724. });
  4725. });
  4726. it('Valid URIs (allowLocal=true)', function() {
  4727. var me = this;
  4728. me.bv.updateOption('uri', 'uri', 'allowLocal', true);
  4729. $.each(validGlobalURIs.concat(localURIs), function(index, uri) {
  4730. me.bv.resetForm();
  4731. me.$uri.val(uri);
  4732. me.bv.validate();
  4733. expect(me.bv.isValid()).toBeTruthy();
  4734. });
  4735. });
  4736. it('Invalid URIs (allowLocal=true)', function() {
  4737. var me = this;
  4738. me.bv.updateOption('uri', 'uri', 'allowLocal', true);
  4739. $.each(invalidGlobalURIs, function(index, uri) {
  4740. me.bv.resetForm();
  4741. me.$uri.val(uri);
  4742. me.bv.validate();
  4743. expect(me.bv.isValid()).toEqual(false);
  4744. });
  4745. });
  4746. });
  4747. describe('vat', function() {
  4748. beforeEach(function() {
  4749. $([
  4750. '<form class="form-horizontal" id="vatForm">',
  4751. '<div class="form-group">',
  4752. '<select class="form-control" name="country">',
  4753. '<option value="AT">Austria</option>',
  4754. '<option value="BE">Belgium</option>',
  4755. '<option value="BG">Bulgaria</option>',
  4756. '<option value="HR">Croatia</option>',
  4757. '<option value="CY">Cyprus</option>',
  4758. '<option value="CZ">Czech Republic</option>',
  4759. '<option value="DK">Denmark</option>',
  4760. '<option value="EE">Estonia</option>',
  4761. '<option value="FI">Finland</option>',
  4762. '<option value="FR">France</option>',
  4763. '<option value="DE">Germany</option>',
  4764. '<option value="GR">Greece</option>',
  4765. '<option value="HU">Hungary</option>',
  4766. '<option value="IE">Ireland</option>',
  4767. '<option value="IS">Iceland</option>',
  4768. '<option value="IT">Italy</option>',
  4769. '<option value="LV">Latvia</option>',
  4770. '<option value="LT">Lithuania</option>',
  4771. '<option value="LU">Luxembourg</option>',
  4772. '<option value="MT">Malta</option>',
  4773. '<option value="NL">Netherlands</option>',
  4774. '<option value="NO">Norway</option>',
  4775. '<option value="PL">Poland</option>',
  4776. '<option value="PT">Portugal</option>',
  4777. '<option value="RO">Romania</option>',
  4778. '<option value="RU">Russia</option>',
  4779. '<option value="RS">Serbia</option>',
  4780. '<option value="SK">Slovakia</option>',
  4781. '<option value="SI">Slovenia</option>',
  4782. '<option value="ES">Spain</option>',
  4783. '<option value="SE">Sweden</option>',
  4784. '<option value="CH">Switzerland</option>',
  4785. '<option value="GB">United Kingdom</option>',
  4786. '<option value="VE">Venezuela</option>',
  4787. '<option value="ZA">South Africa</option>',
  4788. '</select>',
  4789. '</div>',
  4790. '<div class="form-group">',
  4791. '<input type="text" name="vat" data-bv-vat />',
  4792. '</div>',
  4793. '</form>',
  4794. ].join('\n')).appendTo('body');
  4795. $('#vatForm').bootstrapValidator();
  4796. /**
  4797. * @type {BootstrapValidator}
  4798. */
  4799. this.bv = $('#vatForm').data('bootstrapValidator');
  4800. this.$country = this.bv.getFieldElements('country');
  4801. this.$vat = this.bv.getFieldElements('vat');
  4802. });
  4803. afterEach(function() {
  4804. $('#vatForm').bootstrapValidator('destroy').remove();
  4805. });
  4806. it('dynamic country', function() {
  4807. this.$vat.attr('data-bv-vat-country', 'country');
  4808. this.bv.destroy();
  4809. this.bv = $('#vatForm').bootstrapValidator().data('bootstrapValidator');
  4810. this.$country.val('AT');
  4811. this.$vat.val('ATU13585627');
  4812. this.bv.validate();
  4813. expect(this.bv.isValid()).toBeTruthy();
  4814. this.bv.resetForm();
  4815. this.$country.val('BG');
  4816. this.$vat.val('BE0428759497');
  4817. this.bv.validate();
  4818. expect(this.bv.isValid()).toEqual(false);
  4819. this.bv.resetForm();
  4820. this.$country.val('BE');
  4821. this.$vat.val('BE431150351');
  4822. this.bv.validate();
  4823. expect(this.bv.isValid()).toEqual(false);
  4824. });
  4825. it('Austrian VAT number', function() {
  4826. this.bv.updateOption('vat', 'vat', 'country', 'AT');
  4827. // Valid samples
  4828. var validSamples = ['ATU13585627', 'U13585627'];
  4829. for (var i in validSamples) {
  4830. this.bv.resetForm();
  4831. this.$vat.val(validSamples[i]);
  4832. this.bv.validate();
  4833. expect(this.bv.isValid()).toBeTruthy();
  4834. }
  4835. // Invalid samples
  4836. var invalidSamples = ['ATU13585626', 'U13585626'];
  4837. for (i in invalidSamples) {
  4838. this.bv.resetForm();
  4839. this.$vat.val(invalidSamples[i]);
  4840. this.bv.validate();
  4841. expect(this.bv.isValid()).toEqual(false);
  4842. }
  4843. });
  4844. it('Belgian VAT number', function() {
  4845. this.bv.updateOption('vat', 'vat', 'country', 'BE');
  4846. // Valid samples
  4847. var validSamples = ['BE0428759497', '0428759497'];
  4848. for (var i in validSamples) {
  4849. this.bv.resetForm();
  4850. this.$vat.val(validSamples[i]);
  4851. this.bv.validate();
  4852. expect(this.bv.isValid()).toBeTruthy();
  4853. }
  4854. // Invalid samples
  4855. var invalidSamples = ['BE431150351', '431150351'];
  4856. for (i in invalidSamples) {
  4857. this.bv.resetForm();
  4858. this.$vat.val(invalidSamples[i]);
  4859. this.bv.validate();
  4860. expect(this.bv.isValid()).toEqual(false);
  4861. }
  4862. });
  4863. it('Bulgarian VAT number', function() {
  4864. this.bv.updateOption('vat', 'vat', 'country', 'BG');
  4865. // Valid samples
  4866. var validSamples = ['BG175074752', 'BG7523169263', 'BG8032056031', 'BG7542011030', 'BG7111042925', '175074752', '7523169263', '8032056031'];
  4867. for (var i in validSamples) {
  4868. this.bv.resetForm();
  4869. this.$vat.val(validSamples[i]);
  4870. this.bv.validate();
  4871. expect(this.bv.isValid()).toBeTruthy();
  4872. }
  4873. // Invalid samples
  4874. var invalidSamples = ['BG175074753', 'BG7552A10004', 'BG7111042922', '175074753', '7552A10004'];
  4875. for (i in invalidSamples) {
  4876. this.bv.resetForm();
  4877. this.$vat.val(invalidSamples[i]);
  4878. this.bv.validate();
  4879. expect(this.bv.isValid()).toEqual(false);
  4880. }
  4881. });
  4882. it('Cypriot VAT number', function() {
  4883. this.bv.updateOption('vat', 'vat', 'country', 'CY');
  4884. // Valid samples
  4885. var validSamples = ['CY10259033P', '10259033P'];
  4886. for (var i in validSamples) {
  4887. this.bv.resetForm();
  4888. this.$vat.val(validSamples[i]);
  4889. this.bv.validate();
  4890. expect(this.bv.isValid()).toBeTruthy();
  4891. }
  4892. // Invalid samples
  4893. var invalidSamples = ['CY10259033Z', '10259033Z'];
  4894. for (i in invalidSamples) {
  4895. this.bv.resetForm();
  4896. this.$vat.val(invalidSamples[i]);
  4897. this.bv.validate();
  4898. expect(this.bv.isValid()).toEqual(false);
  4899. }
  4900. });
  4901. it('Czech Republic VAT number', function() {
  4902. this.bv.updateOption('vat', 'vat', 'country', 'CZ');
  4903. // Valid samples
  4904. var validSamples = ['CZ25123891', 'CZ7103192745', 'CZ991231123', 'CZ640903926', '25123891', '7103192745'];
  4905. for (var i in validSamples) {
  4906. this.bv.resetForm();
  4907. this.$vat.val(validSamples[i]);
  4908. this.bv.validate();
  4909. expect(this.bv.isValid()).toBeTruthy();
  4910. }
  4911. // Invalid samples
  4912. var invalidSamples = ['CZ25123890', 'CZ1103492745', 'CZ590312123', '25123890', '1103492745'];
  4913. for (i in invalidSamples) {
  4914. this.bv.resetForm();
  4915. this.$vat.val(invalidSamples[i]);
  4916. this.bv.validate();
  4917. expect(this.bv.isValid()).toEqual(false);
  4918. }
  4919. });
  4920. it('German VAT number', function() {
  4921. this.bv.updateOption('vat', 'vat', 'country', 'DE');
  4922. // Valid samples
  4923. var validSamples = ['DE136695976', '136695976'];
  4924. for (var i in validSamples) {
  4925. this.bv.resetForm();
  4926. this.$vat.val(validSamples[i]);
  4927. this.bv.validate();
  4928. expect(this.bv.isValid()).toBeTruthy();
  4929. }
  4930. // Invalid samples
  4931. var invalidSamples = ['DE136695978', '136695978'];
  4932. for (i in invalidSamples) {
  4933. this.bv.resetForm();
  4934. this.$vat.val(invalidSamples[i]);
  4935. this.bv.validate();
  4936. expect(this.bv.isValid()).toEqual(false);
  4937. }
  4938. });
  4939. it('Danish VAT number', function() {
  4940. this.bv.updateOption('vat', 'vat', 'country', 'DK');
  4941. // Valid samples
  4942. var validSamples = ['DK13585628', '13585628'];
  4943. for (var i in validSamples) {
  4944. this.bv.resetForm();
  4945. this.$vat.val(validSamples[i]);
  4946. this.bv.validate();
  4947. expect(this.bv.isValid()).toBeTruthy();
  4948. }
  4949. // Invalid samples
  4950. var invalidSamples = ['DK13585627', '13585627'];
  4951. for (i in invalidSamples) {
  4952. this.bv.resetForm();
  4953. this.$vat.val(invalidSamples[i]);
  4954. this.bv.validate();
  4955. expect(this.bv.isValid()).toEqual(false);
  4956. }
  4957. });
  4958. it('Estonian VAT number', function() {
  4959. this.bv.updateOption('vat', 'vat', 'country', 'EE');
  4960. // Valid samples
  4961. var validSamples = ['EE100931558', 'EE100594102', '100931558', '100594102'];
  4962. for (var i in validSamples) {
  4963. this.bv.resetForm();
  4964. this.$vat.val(validSamples[i]);
  4965. this.bv.validate();
  4966. expect(this.bv.isValid()).toBeTruthy();
  4967. }
  4968. // Invalid samples
  4969. var invalidSamples = ['EE100594103', '100594103'];
  4970. for (i in invalidSamples) {
  4971. this.bv.resetForm();
  4972. this.$vat.val(invalidSamples[i]);
  4973. this.bv.validate();
  4974. expect(this.bv.isValid()).toEqual(false);
  4975. }
  4976. });
  4977. it('Spanish VAT number (NIF)', function() {
  4978. this.bv.updateOption('vat', 'vat', 'country', 'ES');
  4979. // Valid samples
  4980. var validSamples = ['ES54362315K', 'ESX2482300W', 'ESX5253868R', 'ESM1234567L', 'ESJ99216582', 'ESB58378431', 'ESB64717838', '54362315K', 'X2482300W', 'X5253868R', 'M1234567L', 'J99216582'];
  4981. for (var i in validSamples) {
  4982. this.bv.resetForm();
  4983. this.$vat.val(validSamples[i]);
  4984. this.bv.validate();
  4985. expect(this.bv.isValid()).toBeTruthy();
  4986. }
  4987. // Invalid samples
  4988. var invalidSamples = ['ES54362315Z', 'ESX2482300A', 'ESJ99216583', '54362315Z', 'X2482300A'];
  4989. for (i in invalidSamples) {
  4990. this.bv.resetForm();
  4991. this.$vat.val(invalidSamples[i]);
  4992. this.bv.validate();
  4993. expect(this.bv.isValid()).toEqual(false);
  4994. }
  4995. });
  4996. it('Finnish VAT number', function() {
  4997. this.bv.updateOption('vat', 'vat', 'country', 'FI');
  4998. // Valid samples
  4999. var validSamples = ['FI20774740', '20774740'];
  5000. for (var i in validSamples) {
  5001. this.bv.resetForm();
  5002. this.$vat.val(validSamples[i]);
  5003. this.bv.validate();
  5004. expect(this.bv.isValid()).toBeTruthy();
  5005. }
  5006. // Invalid samples
  5007. var invalidSamples = ['FI20774741', '20774741'];
  5008. for (i in invalidSamples) {
  5009. this.bv.resetForm();
  5010. this.$vat.val(invalidSamples[i]);
  5011. this.bv.validate();
  5012. expect(this.bv.isValid()).toEqual(false);
  5013. }
  5014. });
  5015. it('French VAT number (TVA)', function() {
  5016. this.bv.updateOption('vat', 'vat', 'country', 'FR');
  5017. // Valid samples
  5018. var validSamples = ['FR40303265045', 'FR23334175221', 'FRK7399859412', 'FR4Z123456782', '40303265045', '23334175221', 'K7399859412'];
  5019. for (var i in validSamples) {
  5020. this.bv.resetForm();
  5021. this.$vat.val(validSamples[i]);
  5022. this.bv.validate();
  5023. expect(this.bv.isValid()).toBeTruthy();
  5024. }
  5025. // Invalid samples
  5026. var invalidSamples = ['FR84323140391', '84323140391'];
  5027. for (i in invalidSamples) {
  5028. this.bv.resetForm();
  5029. this.$vat.val(invalidSamples[i]);
  5030. this.bv.validate();
  5031. expect(this.bv.isValid()).toEqual(false);
  5032. }
  5033. });
  5034. it('United Kingdom VAT number', function() {
  5035. this.bv.updateOption('vat', 'vat', 'country', 'GB');
  5036. // Valid samples
  5037. var validSamples = ['GB980780684', '980780684'];
  5038. for (var i in validSamples) {
  5039. this.bv.resetForm();
  5040. this.$vat.val(validSamples[i]);
  5041. this.bv.validate();
  5042. expect(this.bv.isValid()).toBeTruthy();
  5043. }
  5044. // Invalid samples
  5045. var invalidSamples = ['GB802311781', '802311781'];
  5046. for (i in invalidSamples) {
  5047. this.bv.resetForm();
  5048. this.$vat.val(invalidSamples[i]);
  5049. this.bv.validate();
  5050. expect(this.bv.isValid()).toEqual(false);
  5051. }
  5052. });
  5053. it('Greek VAT number', function() {
  5054. this.bv.updateOption('vat', 'vat', 'country', 'GR');
  5055. // Valid samples
  5056. var validSamples = ['GR023456780', 'EL094259216', '023456780', '094259216'];
  5057. for (var i in validSamples) {
  5058. this.bv.resetForm();
  5059. this.$vat.val(validSamples[i]);
  5060. this.bv.validate();
  5061. expect(this.bv.isValid()).toBeTruthy();
  5062. }
  5063. // Invalid samples
  5064. var invalidSamples = ['GR123456781', '123456781'];
  5065. for (i in invalidSamples) {
  5066. this.bv.resetForm();
  5067. this.$vat.val(invalidSamples[i]);
  5068. this.bv.validate();
  5069. expect(this.bv.isValid()).toEqual(false);
  5070. }
  5071. });
  5072. it('Hungarian VAT number', function() {
  5073. this.bv.updateOption('vat', 'vat', 'country', 'HU');
  5074. // Valid samples
  5075. var validSamples = ['HU12892312', '12892312'];
  5076. for (var i in validSamples) {
  5077. this.bv.resetForm();
  5078. this.$vat.val(validSamples[i]);
  5079. this.bv.validate();
  5080. expect(this.bv.isValid()).toBeTruthy();
  5081. }
  5082. // Invalid samples
  5083. var invalidSamples = ['HU12892313', '12892313'];
  5084. for (i in invalidSamples) {
  5085. this.bv.resetForm();
  5086. this.$vat.val(invalidSamples[i]);
  5087. this.bv.validate();
  5088. expect(this.bv.isValid()).toEqual(false);
  5089. }
  5090. });
  5091. it('Croatian VAT number', function() {
  5092. this.bv.updateOption('vat', 'vat', 'country', 'HR');
  5093. // Valid samples
  5094. var validSamples = ['HR33392005961', '33392005961'];
  5095. for (var i in validSamples) {
  5096. this.bv.resetForm();
  5097. this.$vat.val(validSamples[i]);
  5098. this.bv.validate();
  5099. expect(this.bv.isValid()).toBeTruthy();
  5100. }
  5101. // Invalid samples
  5102. var invalidSamples = ['HR33392005962', '33392005962'];
  5103. for (i in invalidSamples) {
  5104. this.bv.resetForm();
  5105. this.$vat.val(invalidSamples[i]);
  5106. this.bv.validate();
  5107. expect(this.bv.isValid()).toEqual(false);
  5108. }
  5109. });
  5110. it('Irish VAT number', function() {
  5111. this.bv.updateOption('vat', 'vat', 'country', 'IE');
  5112. // Valid samples
  5113. var validSamples = ['IE6433435F', 'IE6433435OA', 'IE8D79739I', '6433435F', '6433435OA', '8D79739I'];
  5114. for (var i in validSamples) {
  5115. this.bv.resetForm();
  5116. this.$vat.val(validSamples[i]);
  5117. this.bv.validate();
  5118. expect(this.bv.isValid()).toBeTruthy();
  5119. }
  5120. // Invalid samples
  5121. var invalidSamples = ['IE8D79738J', '8D79738J'];
  5122. for (i in invalidSamples) {
  5123. this.bv.resetForm();
  5124. this.$vat.val(invalidSamples[i]);
  5125. this.bv.validate();
  5126. expect(this.bv.isValid()).toEqual(false);
  5127. }
  5128. });
  5129. it('Iceland VAT (VSK) number', function() {
  5130. this.bv.updateOption('vat', 'vat', 'country', 'IS');
  5131. // Valid samples
  5132. var validSamples = ['IS11111', 'IS111111', '11111', '111111'];
  5133. for (var i in validSamples) {
  5134. this.bv.resetForm();
  5135. this.$vat.val(validSamples[i]);
  5136. this.bv.validate();
  5137. expect(this.bv.isValid()).toBeTruthy();
  5138. }
  5139. // Invalid samples
  5140. var invalidSamples = ['IS1234567', 'IS123456ABC', '1234567', '123456ABC'];
  5141. for (i in invalidSamples) {
  5142. this.bv.resetForm();
  5143. this.$vat.val(invalidSamples[i]);
  5144. this.bv.validate();
  5145. expect(this.bv.isValid()).toEqual(false);
  5146. }
  5147. });
  5148. it('Italian VAT number', function() {
  5149. this.bv.updateOption('vat', 'vat', 'country', 'IT');
  5150. // Valid samples
  5151. var validSamples = ['IT00743110157', '00743110157'];
  5152. for (var i in validSamples) {
  5153. this.bv.resetForm();
  5154. this.$vat.val(validSamples[i]);
  5155. this.bv.validate();
  5156. expect(this.bv.isValid()).toBeTruthy();
  5157. }
  5158. // Invalid samples
  5159. var invalidSamples = ['IT00743110158', '00743110158'];
  5160. for (i in invalidSamples) {
  5161. this.bv.resetForm();
  5162. this.$vat.val(invalidSamples[i]);
  5163. this.bv.validate();
  5164. expect(this.bv.isValid()).toEqual(false);
  5165. }
  5166. });
  5167. it('Lithuanian VAT number', function() {
  5168. this.bv.updateOption('vat', 'vat', 'country', 'LT');
  5169. // Valid samples
  5170. var validSamples = ['LT119511515', 'LT100001919017', 'LT100004801610', '119511515', '100001919017', '100004801610'];
  5171. for (var i in validSamples) {
  5172. this.bv.resetForm();
  5173. this.$vat.val(validSamples[i]);
  5174. this.bv.validate();
  5175. expect(this.bv.isValid()).toBeTruthy();
  5176. }
  5177. // Invalid samples
  5178. var invalidSamples = ['LT100001919018', '100001919018'];
  5179. for (i in invalidSamples) {
  5180. this.bv.resetForm();
  5181. this.$vat.val(invalidSamples[i]);
  5182. this.bv.validate();
  5183. expect(this.bv.isValid()).toEqual(false);
  5184. }
  5185. });
  5186. it('Luxembourg VAT number', function() {
  5187. this.bv.updateOption('vat', 'vat', 'country', 'LU');
  5188. // Valid samples
  5189. var validSamples = ['LU15027442', '15027442'];
  5190. for (var i in validSamples) {
  5191. this.bv.resetForm();
  5192. this.$vat.val(validSamples[i]);
  5193. this.bv.validate();
  5194. expect(this.bv.isValid()).toBeTruthy();
  5195. }
  5196. // Invalid samples
  5197. var invalidSamples = ['LU15027443', '15027443'];
  5198. for (i in invalidSamples) {
  5199. this.bv.resetForm();
  5200. this.$vat.val(invalidSamples[i]);
  5201. this.bv.validate();
  5202. expect(this.bv.isValid()).toEqual(false);
  5203. }
  5204. });
  5205. it('Latvian VAT number', function() {
  5206. this.bv.updateOption('vat', 'vat', 'country', 'LV');
  5207. // Valid samples
  5208. var validSamples = ['LV40003521600', 'LV16117519997', '40003521600', '16117519997'];
  5209. for (var i in validSamples) {
  5210. this.bv.resetForm();
  5211. this.$vat.val(validSamples[i]);
  5212. this.bv.validate();
  5213. expect(this.bv.isValid()).toBeTruthy();
  5214. }
  5215. // Invalid samples
  5216. var invalidSamples = ['LV40003521601', 'LV16137519997', '40003521601', '16137519997'];
  5217. for (i in invalidSamples) {
  5218. this.bv.resetForm();
  5219. this.$vat.val(invalidSamples[i]);
  5220. this.bv.validate();
  5221. expect(this.bv.isValid()).toEqual(false);
  5222. }
  5223. });
  5224. it('Maltese VAT number', function() {
  5225. this.bv.updateOption('vat', 'vat', 'country', 'MT');
  5226. // Valid samples
  5227. var validSamples = ['MT11679112', '11679112'];
  5228. for (var i in validSamples) {
  5229. this.bv.resetForm();
  5230. this.$vat.val(validSamples[i]);
  5231. this.bv.validate();
  5232. expect(this.bv.isValid()).toBeTruthy();
  5233. }
  5234. // Invalid samples
  5235. var invalidSamples = ['MT11679113', '11679113'];
  5236. for (i in invalidSamples) {
  5237. this.bv.resetForm();
  5238. this.$vat.val(invalidSamples[i]);
  5239. this.bv.validate();
  5240. expect(this.bv.isValid()).toEqual(false);
  5241. }
  5242. });
  5243. it('Dutch VAT number', function() {
  5244. this.bv.updateOption('vat', 'vat', 'country', 'NL');
  5245. // Valid samples
  5246. var validSamples = ['NL004495445B01', '004495445B01'];
  5247. for (var i in validSamples) {
  5248. this.bv.resetForm();
  5249. this.$vat.val(validSamples[i]);
  5250. this.bv.validate();
  5251. expect(this.bv.isValid()).toBeTruthy();
  5252. }
  5253. // Invalid samples
  5254. var invalidSamples = ['NL123456789B90', '123456789B90'];
  5255. for (i in invalidSamples) {
  5256. this.bv.resetForm();
  5257. this.$vat.val(invalidSamples[i]);
  5258. this.bv.validate();
  5259. expect(this.bv.isValid()).toEqual(false);
  5260. }
  5261. });
  5262. it('Polish VAT number', function() {
  5263. this.bv.updateOption('vat', 'vat', 'country', 'PL');
  5264. // Valid samples
  5265. var validSamples = ['PL8567346215', '8567346215'];
  5266. for (var i in validSamples) {
  5267. this.bv.resetForm();
  5268. this.$vat.val(validSamples[i]);
  5269. this.bv.validate();
  5270. expect(this.bv.isValid()).toBeTruthy();
  5271. }
  5272. // Invalid samples
  5273. var invalidSamples = ['PL8567346216', '8567346216'];
  5274. for (i in invalidSamples) {
  5275. this.bv.resetForm();
  5276. this.$vat.val(invalidSamples[i]);
  5277. this.bv.validate();
  5278. expect(this.bv.isValid()).toEqual(false);
  5279. }
  5280. });
  5281. it('Portuguese VAT number', function() {
  5282. this.bv.updateOption('vat', 'vat', 'country', 'PT');
  5283. // Valid samples
  5284. var validSamples = ['PT501964843', '501964843'];
  5285. for (var i in validSamples) {
  5286. this.bv.resetForm();
  5287. this.$vat.val(validSamples[i]);
  5288. this.bv.validate();
  5289. expect(this.bv.isValid()).toBeTruthy();
  5290. }
  5291. // Invalid samples
  5292. var invalidSamples = ['PT501964842', '501964842'];
  5293. for (i in invalidSamples) {
  5294. this.bv.resetForm();
  5295. this.$vat.val(invalidSamples[i]);
  5296. this.bv.validate();
  5297. expect(this.bv.isValid()).toEqual(false);
  5298. }
  5299. });
  5300. it('Romanian VAT number', function() {
  5301. this.bv.updateOption('vat', 'vat', 'country', 'RO');
  5302. // Valid samples
  5303. var validSamples = ['RO18547290', '18547290'];
  5304. for (var i in validSamples) {
  5305. this.bv.resetForm();
  5306. this.$vat.val(validSamples[i]);
  5307. this.bv.validate();
  5308. expect(this.bv.isValid()).toBeTruthy();
  5309. }
  5310. // Invalid samples
  5311. var invalidSamples = ['RO18547291', '18547291'];
  5312. for (i in invalidSamples) {
  5313. this.bv.resetForm();
  5314. this.$vat.val(invalidSamples[i]);
  5315. this.bv.validate();
  5316. expect(this.bv.isValid()).toEqual(false);
  5317. }
  5318. });
  5319. it('Russian VAT number', function() {
  5320. this.bv.updateOption('vat', 'vat', 'country', 'RU');
  5321. // Valid samples
  5322. var validSamples = ['RU7805145876', 'RU781300557475', '7805145876', '781300557475'];
  5323. for (var i in validSamples) {
  5324. this.bv.resetForm();
  5325. this.$vat.val(validSamples[i]);
  5326. this.bv.validate();
  5327. expect(this.bv.isValid()).toBeTruthy();
  5328. }
  5329. // Invalid samples
  5330. var invalidSamples = ['RU7805145877', 'RU781300557474', '7805145877', '781300557474'];
  5331. for (i in invalidSamples) {
  5332. this.bv.resetForm();
  5333. this.$vat.val(invalidSamples[i]);
  5334. this.bv.validate();
  5335. expect(this.bv.isValid()).toEqual(false);
  5336. }
  5337. });
  5338. it('Swedish VAT number', function() {
  5339. this.bv.updateOption('vat', 'vat', 'country', 'SE');
  5340. // Valid samples
  5341. var validSamples = ['SE123456789701', '123456789701'];
  5342. for (var i in validSamples) {
  5343. this.bv.resetForm();
  5344. this.$vat.val(validSamples[i]);
  5345. this.bv.validate();
  5346. expect(this.bv.isValid()).toBeTruthy();
  5347. }
  5348. // Invalid samples
  5349. var invalidSamples = ['SE123456789101', '123456789101'];
  5350. for (i in invalidSamples) {
  5351. this.bv.resetForm();
  5352. this.$vat.val(invalidSamples[i]);
  5353. this.bv.validate();
  5354. expect(this.bv.isValid()).toEqual(false);
  5355. }
  5356. });
  5357. it('Slovenian VAT number', function() {
  5358. this.bv.updateOption('vat', 'vat', 'country', 'SI');
  5359. // Valid samples
  5360. var validSamples = ['SI50223054', '50223054'];
  5361. for (var i in validSamples) {
  5362. this.bv.resetForm();
  5363. this.$vat.val(validSamples[i]);
  5364. this.bv.validate();
  5365. expect(this.bv.isValid()).toBeTruthy();
  5366. }
  5367. // Invalid samples
  5368. var invalidSamples = ['SI50223055', '50223055'];
  5369. for (i in invalidSamples) {
  5370. this.bv.resetForm();
  5371. this.$vat.val(invalidSamples[i]);
  5372. this.bv.validate();
  5373. expect(this.bv.isValid()).toEqual(false);
  5374. }
  5375. });
  5376. it('Slovak VAT number', function() {
  5377. this.bv.updateOption('vat', 'vat', 'country', 'SK');
  5378. // Valid samples
  5379. var validSamples = ['SK2022749619', '2022749619'];
  5380. for (var i in validSamples) {
  5381. this.bv.resetForm();
  5382. this.$vat.val(validSamples[i]);
  5383. this.bv.validate();
  5384. expect(this.bv.isValid()).toBeTruthy();
  5385. }
  5386. // Invalid samples
  5387. var invalidSamples = ['SK2022749618', '2022749618'];
  5388. for (i in invalidSamples) {
  5389. this.bv.resetForm();
  5390. this.$vat.val(invalidSamples[i]);
  5391. this.bv.validate();
  5392. expect(this.bv.isValid()).toEqual(false);
  5393. }
  5394. });
  5395. it('South African VAT number', function() {
  5396. this.bv.updateOption('vat', 'vat', 'country', 'ZA');
  5397. // Valid samples
  5398. var validSamples = ['ZA4012345678', '4012345678'];
  5399. for (var i in validSamples) {
  5400. this.bv.resetForm();
  5401. this.$vat.val(validSamples[i]);
  5402. this.bv.validate();
  5403. expect(this.bv.isValid()).toBeTruthy();
  5404. }
  5405. // Invalid samples
  5406. var invalidSamples = ['ZA40123456789', 'ZA0123456789', '40123456789', '0123456789'];
  5407. for (i in invalidSamples) {
  5408. this.bv.resetForm();
  5409. this.$vat.val(invalidSamples[i]);
  5410. this.bv.validate();
  5411. expect(this.bv.isValid()).toEqual(false);
  5412. }
  5413. });
  5414. it('Venezuelan VAT number (RIF)', function() {
  5415. this.bv.updateOption('vat', 'vat', 'country', 'VE');
  5416. // Valid samples
  5417. var validSamples = ['VEJ309272292', 'VEV242818101', 'VEJ000126518', 'VEJ000458324', 'J309272292', 'V242818101', 'J000126518', 'J000458324'];
  5418. for (var i in validSamples) {
  5419. this.bv.resetForm();
  5420. this.$vat.val(validSamples[i]);
  5421. this.bv.validate();
  5422. expect(this.bv.isValid()).toBeTruthy();
  5423. }
  5424. // Invalid samples
  5425. var invalidSamples = ['VEJ309272293', 'VEV242818100', 'J000126519', 'J000458323'];
  5426. for (i in invalidSamples) {
  5427. this.bv.resetForm();
  5428. this.$vat.val(invalidSamples[i]);
  5429. this.bv.validate();
  5430. expect(this.bv.isValid()).toEqual(false);
  5431. }
  5432. });
  5433. });
  5434. function getCountryCode(value, validator, $field) {
  5435. $('#msg').html('getCountryCode() called');
  5436. return validator.getFieldElements('country').val();
  5437. };
  5438. TestSuite = $.extend({}, TestSuite, {
  5439. ZipCode: {
  5440. getCountryCode: function(value, validator, $field) {
  5441. $('#msg').html('TestSuite.ZipCode.getCountryCode() called');
  5442. return validator.getFieldElements('country').val();
  5443. }
  5444. }
  5445. });
  5446. describe('zipCode', function() {
  5447. beforeEach(function() {
  5448. $([
  5449. '<form class="form-horizontal" id="zipCodeForm">',
  5450. '<div id="msg"></div>',
  5451. '<div class="form-group">',
  5452. '<label class="col-md-3 control-label">Country:</label>',
  5453. '<div class="col-md-2">',
  5454. '<select class="form-control" name="country">',
  5455. '<option value="">Select a country</option>',
  5456. '<option value="US">United States</option>',
  5457. '<option value="CA">Canada</option>',
  5458. '<option value="DK">Denmark</option>',
  5459. '<option value="IT">Italy</option>',
  5460. '<option value="NL">Netherlands</option>',
  5461. '<option value="SE">Sweden</option>',
  5462. '<option value="GB">United Kingdom</option>',
  5463. '</select>',
  5464. '</div>',
  5465. '</div>',
  5466. '<div class="form-group">',
  5467. '<label class="col-md-3 control-label">Zipcode</label>',
  5468. '<div class="col-md-2">',
  5469. '<input type="text" class="form-control" name="zc" data-bv-zipcode data-bv-zipcode-country="US" />',
  5470. '</div>',
  5471. '</div>',
  5472. '</form>'
  5473. ].join('\n')).appendTo('body');
  5474. $('#zipCodeForm').bootstrapValidator();
  5475. /**
  5476. * @type {BootstrapValidator}
  5477. */
  5478. this.bv = $('#zipCodeForm').data('bootstrapValidator');
  5479. this.$country = this.bv.getFieldElements('country');
  5480. this.$zipCode = this.bv.getFieldElements('zc');
  5481. });
  5482. afterEach(function() {
  5483. $('#zipCodeForm').bootstrapValidator('destroy').remove();
  5484. });
  5485. it('country code US', function() {
  5486. this.$zipCode.val('12345');
  5487. this.bv.validate();
  5488. expect(this.bv.isValid()).toBeTruthy();
  5489. this.bv.resetForm();
  5490. this.$zipCode.val('123');
  5491. this.bv.validate();
  5492. expect(this.bv.isValid()).toEqual(false);
  5493. });
  5494. it('country code updateOption()', function() {
  5495. // Check IT postal code
  5496. this.bv.updateOption('zc', 'zipCode', 'country', 'IT');
  5497. this.$zipCode.val('1234');
  5498. this.bv.validate();
  5499. expect(this.bv.isValid()).toEqual(false);
  5500. this.bv.resetForm();
  5501. this.$zipCode.val('IT-12345');
  5502. this.bv.validate();
  5503. expect(this.bv.isValid()).toBeTruthy();
  5504. // Check United Kingdom postal code
  5505. this.bv.updateOption('zc', 'zipCode', 'country', 'GB');
  5506. var validUkSamples = ['EC1A 1BB', 'W1A 1HQ', 'M1 1AA', 'B33 8TH', 'CR2 6XH', 'DN55 1PT', 'AI-2640', 'ASCN 1ZZ', 'GIR 0AA'];
  5507. for (var i in validUkSamples) {
  5508. this.bv.resetForm();
  5509. this.$zipCode.val(validUkSamples[i]);
  5510. this.bv.validate();
  5511. expect(this.bv.isValid()).toBeTruthy();
  5512. }
  5513. });
  5514. it('country code other field declarative', function() {
  5515. this.$zipCode.attr('data-bv-zipcode-country', 'country');
  5516. // Need to destroy the plugin instance ...
  5517. $('#zipCodeForm').bootstrapValidator('destroy');
  5518. // ... and re-create it
  5519. this.bv = $('#zipCodeForm').bootstrapValidator().data('bootstrapValidator');
  5520. this.$country.val('IT');
  5521. this.bv.resetForm();
  5522. this.$zipCode.val('1234');
  5523. this.bv.validate();
  5524. expect(this.bv.isValid()).toEqual(false);
  5525. this.bv.resetForm();
  5526. this.$zipCode.val('I-12345');
  5527. this.bv.validate();
  5528. expect(this.bv.isValid()).toBeTruthy();
  5529. });
  5530. it('country code callback declarative function', function() {
  5531. this.$zipCode.attr('data-bv-zipcode-country', 'getCountryCode');
  5532. $('#zipCodeForm').bootstrapValidator('destroy');
  5533. this.bv = $('#zipCodeForm').bootstrapValidator().data('bootstrapValidator');
  5534. this.$country.val('NL');
  5535. this.$zipCode.val('0123');
  5536. this.bv.validate();
  5537. expect($('#msg').html()).toEqual('getCountryCode() called');
  5538. expect(this.bv.isValid()).toEqual(false);
  5539. });
  5540. it('country code callback declarative function()', function() {
  5541. this.$zipCode.attr('data-bv-zipcode-country', 'getCountryCode()');
  5542. $('#zipCodeForm').bootstrapValidator('destroy');
  5543. this.bv = $('#zipCodeForm').bootstrapValidator().data('bootstrapValidator');
  5544. this.$country.val('NL');
  5545. this.$zipCode.val('1234 ab');
  5546. this.bv.validate();
  5547. expect($('#msg').html()).toEqual('getCountryCode() called');
  5548. expect(this.bv.isValid()).toBeTruthy();
  5549. });
  5550. it('country code callback declarative A.B.C', function() {
  5551. this.$zipCode.attr('data-bv-zipcode-country', 'TestSuite.ZipCode.getCountryCode');
  5552. $('#zipCodeForm').bootstrapValidator('destroy');
  5553. this.bv = $('#zipCodeForm').bootstrapValidator().data('bootstrapValidator');
  5554. this.$country.val('DK');
  5555. this.$zipCode.val('DK 123');
  5556. this.bv.validate();
  5557. expect($('#msg').html()).toEqual('TestSuite.ZipCode.getCountryCode() called');
  5558. expect(this.bv.isValid()).toEqual(false);
  5559. });
  5560. it('country code callback declarative A.B.C()', function() {
  5561. this.$zipCode.attr('data-bv-zipcode-country', 'TestSuite.ZipCode.getCountryCode()');
  5562. $('#zipCodeForm').bootstrapValidator('destroy');
  5563. this.bv = $('#zipCodeForm').bootstrapValidator().data('bootstrapValidator');
  5564. this.$country.val('DK');
  5565. this.$zipCode.val('DK-1234');
  5566. this.bv.validate();
  5567. expect($('#msg').html()).toEqual('TestSuite.ZipCode.getCountryCode() called');
  5568. expect(this.bv.isValid()).toBeTruthy();
  5569. });
  5570. it('country code callback programmatically', function() {
  5571. this.$zipCode.removeAttr('data-bv-zipcode-country');
  5572. $('#zipCodeForm').bootstrapValidator('destroy');
  5573. this.bv = $('#zipCodeForm')
  5574. .bootstrapValidator({
  5575. fields: {
  5576. zc: {
  5577. validators: {
  5578. zipCode: {
  5579. country: function(value, validator, $field) {
  5580. return getCountryCode(value, validator, $field);
  5581. }
  5582. }
  5583. }
  5584. }
  5585. }
  5586. })
  5587. .data('bootstrapValidator');
  5588. this.$country.val('SE');
  5589. this.bv.resetForm();
  5590. this.$zipCode.val('S-567 8');
  5591. this.bv.validate();
  5592. expect($('#msg').html()).toEqual('getCountryCode() called');
  5593. expect(this.bv.isValid()).toEqual(false);
  5594. this.bv.resetForm();
  5595. this.$zipCode.val('S-12345');
  5596. this.bv.validate();
  5597. expect($('#msg').html()).toEqual('getCountryCode() called');
  5598. expect(this.bv.isValid()).toBeTruthy();
  5599. });
  5600. it('not supported country code', function() {
  5601. this.$zipCode.attr('data-bv-zipcode-country', 'NOT_SUPPORTED');
  5602. $('#zipCodeForm').bootstrapValidator('destroy');
  5603. this.bv = $('#zipCodeForm').bootstrapValidator().data('bootstrapValidator');
  5604. this.bv.resetForm();
  5605. this.$zipCode.val('1234');
  5606. this.bv.validate();
  5607. expect(this.bv.isValid()).toEqual(false);
  5608. expect(this.bv.getMessages(this.$zipCode, 'zipCode')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.zipCode.countryNotSupported, 'NOT_SUPPORTED'));
  5609. });
  5610. });