spec.js 263 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485
  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('color', function() {
  1928. beforeEach(function() {
  1929. var html = [
  1930. '<div class="container">',
  1931. '<form class="form-horizontal" id="colorForm">',
  1932. '<div class="form-group">',
  1933. '<input type="text" name="color" data-bv-color />',
  1934. '<input type="text" name="colorMultiple" data-bv-color />',
  1935. '<input type="text" name="colorHex" data-bv-color />',
  1936. '<input type="text" name="colorRgb" data-bv-color />',
  1937. '<input type="text" name="colorRgba" data-bv-color />',
  1938. '<input type="text" name="colorHsl" data-bv-color />',
  1939. '<input type="text" name="colorHsla" data-bv-color />',
  1940. '<input type="text" name="colorKeyword" data-bv-color />',
  1941. '</div>',
  1942. '</form>',
  1943. '</div>'
  1944. ].join('\n');
  1945. $(html).appendTo('body');
  1946. $('#colorForm').bootstrapValidator({
  1947. colorMultiple: {
  1948. validators: {
  1949. color: {
  1950. type: [
  1951. "hex",
  1952. "rgb"
  1953. ]
  1954. }
  1955. }
  1956. },
  1957. colorHex: {
  1958. validators: {
  1959. color: {
  1960. type: ["hex"]
  1961. }
  1962. }
  1963. },
  1964. colorRgb: {
  1965. validators: {
  1966. color: {
  1967. type: ["rgb"]
  1968. }
  1969. }
  1970. },
  1971. colorRgba: {
  1972. validators: {
  1973. color: {
  1974. type: ["rgba"]
  1975. }
  1976. }
  1977. },
  1978. colorHsl: {
  1979. validators: {
  1980. color: {
  1981. type: ["hsl"]
  1982. }
  1983. }
  1984. },
  1985. colorHsla: {
  1986. validators: {
  1987. color: {
  1988. type: ["hsla"]
  1989. }
  1990. }
  1991. },
  1992. colorKeyword: {
  1993. validators: {
  1994. color: {
  1995. type: ["keyword"]
  1996. }
  1997. }
  1998. }
  1999. });
  2000. this.bv = $('#colorForm').data('bootstrapValidator');
  2001. this.$color = this.bv.getFieldElements('color');
  2002. this.$colorHex = this.bv.getFieldElements('colorHex');
  2003. this.$colorRgb = this.bv.getFieldElements('colorRgb');
  2004. this.$colorRgba = this.bv.getFieldElements('colorRgba');
  2005. this.$colorHsl = this.bv.getFieldElements('colorHsl');
  2006. this.$colorHsla = this.bv.getFieldElements('colorHsla');
  2007. this.$colorKeyword = this.bv.getFieldElements('colorKeyword');
  2008. });
  2009. afterEach(function() {
  2010. $('#colorForm').bootstrapValidator('destroy').parent().remove();
  2011. });
  2012. /* First run all color tests in the color field*/
  2013. // Start hsla() tests
  2014. it('hsla(): accept hsla()', function() {
  2015. this.$color.val('hsla(120,50%,50%,1)');
  2016. this.bv.validate();
  2017. expect(this.bv.isValidField('color')).toBeTruthy();
  2018. });
  2019. it('hsla(): accept spaces around values', function() {
  2020. this.$color.val('hsla( 120 , 50% , 50%, 1 )');
  2021. this.bv.validate();
  2022. expect(this.bv.isValidField('color')).toBeTruthy();
  2023. });
  2024. it('hsla(): accept multiple spaces around values', function() {
  2025. this.$color.val('hsla( 120, 50%, 50% , 1 )');
  2026. this.bv.validate();
  2027. expect(this.bv.isValidField('color')).toBeTruthy();
  2028. });
  2029. it('hsla(): accept negative hue value', function() {
  2030. this.$color.val('hsla(-120,50%,50%,1)');
  2031. this.bv.validate();
  2032. expect(this.bv.isValidField('color')).toBeTruthy();
  2033. });
  2034. it('hsla(): accept hue values larger than 360', function() {
  2035. this.$color.val('hsla(480,50%,50%,1)');
  2036. this.bv.validate();
  2037. expect(this.bv.isValidField('color')).toBeTruthy();
  2038. });
  2039. it('hsla(): accept integer alpha channel value of 0', function() {
  2040. this.$color.val('hsla(120,50%,100%,0)');
  2041. this.bv.validate();
  2042. expect(this.bv.isValidField('color')).toBeTruthy();
  2043. });
  2044. it('hsla(): accept integer alpha channel value of 1', function() {
  2045. this.$color.val('hsla(120,50%,100%,1)');
  2046. this.bv.validate();
  2047. expect(this.bv.isValidField('color')).toBeTruthy();
  2048. });
  2049. it('hsla(): accept floating alpha channel with leading 0', function() {
  2050. this.$color.val('hsla(120,50%,100%,0.5)');
  2051. this.bv.validate();
  2052. expect(this.bv.isValidField('color')).toBeTruthy();
  2053. });
  2054. it('hsla(): accept floating alpha channel without leading 0', function() {
  2055. this.$color.val('hsla(120,50%,100%,.5)');
  2056. this.bv.validate();
  2057. expect(this.bv.isValidField('color')).toBeTruthy();
  2058. });
  2059. it('hsla(): accept floating alpha channel with more than 1 decimal place', function() {
  2060. this.$color.val('hsla(120,50%,100%,.524141)');
  2061. this.bv.validate();
  2062. expect(this.bv.isValidField('color')).toBeTruthy();
  2063. });
  2064. it('hsla(): reject percentage value for alpha channel', function() {
  2065. this.$color.val('hsla(120,50%,100%,50%)');
  2066. this.bv.validate();
  2067. expect(this.bv.isValidField('color')).toEqual(false);
  2068. });
  2069. it('hsla(): reject integers larger than 1 for alpha channel', function() {
  2070. this.$color.val('hsla(120,50%,100%,2)');
  2071. this.bv.validate();
  2072. expect(this.bv.isValidField('color')).toEqual(false);
  2073. });
  2074. it('hsla(): reject negative integers for alpha channel', function() {
  2075. this.$color.val('hsla(120,50%,100%,-1)');
  2076. this.bv.validate();
  2077. expect(this.bv.isValidField('color')).toEqual(false);
  2078. });
  2079. it('hsla(): reject floats larger than 1 for alpha channel', function() {
  2080. this.$color.val('hsla(120,50%,100%,1.000000000001)');
  2081. this.bv.validate();
  2082. expect(this.bv.isValidField('color')).toEqual(false);
  2083. });
  2084. it('hsla(): reject negative floats for alpha channel', function() {
  2085. this.$color.val('hsla(120,50%,100%,-0.5)');
  2086. this.bv.validate();
  2087. expect(this.bv.isValidField('color')).toEqual(false);
  2088. });
  2089. it('hsla(): reject more floats larger than 1 for alpha channel', function() {
  2090. this.$color.val('hsla(120,50%,100%,2.3)');
  2091. this.bv.validate();
  2092. expect(this.bv.isValidField('color')).toEqual(false);
  2093. });
  2094. it('hsla(): reject negative saturation value', function() {
  2095. this.$color.val('hsla(10,-50%,50%,1)');
  2096. this.bv.validate();
  2097. expect(this.bv.isValidField('color')).toEqual(false);
  2098. });
  2099. it('hsla(): reject negative lightness', function() {
  2100. this.$color.val('hsla(10,50%,-50%,1)');
  2101. this.bv.validate();
  2102. expect(this.bv.isValidField('color')).toEqual(false);
  2103. });
  2104. it('hsla(): require hsla()', function() {
  2105. this.$color.val('120,50%,50%,1');
  2106. this.bv.validate();
  2107. expect(this.bv.isValidField('color')).toEqual(false);
  2108. });
  2109. it('hsla(): reject percentages above 100%', function() {
  2110. this.$color.val('hsla(120,100%,101%,1)');
  2111. this.bv.validate();
  2112. expect(this.bv.isValidField('color')).toEqual(false);
  2113. });
  2114. it('hsla(): reject space between hsla and (', function() {
  2115. this.$color.val('hsla (120,50%,50%,1)');
  2116. this.bv.validate();
  2117. expect(this.bv.isValidField('color')).toEqual(false);
  2118. });
  2119. it('hsla(): reject leading space', function() {
  2120. this.$color.val(' hsla(120,50%,50%,1)');
  2121. this.bv.validate();
  2122. expect(this.bv.isValidField('color')).toEqual(false);
  2123. });
  2124. it('hsla(): reject trailing space', function() {
  2125. this.$color.val('hsla(120,50%,50%,1) ');
  2126. this.bv.validate();
  2127. expect(this.bv.isValidField('color')).toEqual(false);
  2128. });
  2129. it('hsla(): reject percentages in hue value', function() {
  2130. this.$color.val('hsla(50%, 50%, 100%,1)');
  2131. this.bv.validate();
  2132. expect(this.bv.isValidField('color')).toEqual(false);
  2133. });
  2134. it('hsla(): reject integers in saturation value', function() {
  2135. this.$color.val('hsla(120, 50, 100%,1)');
  2136. this.bv.validate();
  2137. expect(this.bv.isValidField('color')).toEqual(false);
  2138. });
  2139. it('hsla(): reject integers in lightness value', function() {
  2140. this.$color.val('hsla(120, 50%, 100,1)');
  2141. this.bv.validate();
  2142. expect(this.bv.isValidField('color')).toEqual(false);
  2143. });
  2144. // Start hsl() tests
  2145. it('hsl(): accept hsl()', function() {
  2146. this.$color.val('hsl(120,50%,50%)');
  2147. this.bv.validate();
  2148. expect(this.bv.isValidField('color')).toBeTruthy();
  2149. });
  2150. it('hsl(): accept spaces around values', function() {
  2151. this.$color.val('hsl( 120 , 50% , 50% )');
  2152. this.bv.validate();
  2153. expect(this.bv.isValidField('color')).toBeTruthy();
  2154. });
  2155. it('hsl(): accept multiple spaces around values', function() {
  2156. this.$color.val('hsl( 120, 50%, 50% )');
  2157. this.bv.validate();
  2158. expect(this.bv.isValidField('color')).toBeTruthy();
  2159. });
  2160. it('hsl(): accept negative hue value', function() {
  2161. this.$color.val('hsl(-120,50%,50%)');
  2162. this.bv.validate();
  2163. expect(this.bv.isValidField('color')).toBeTruthy();
  2164. });
  2165. it('hsl(): accept hue values larger than 360', function() {
  2166. this.$color.val('hsl(480,50%,50%)');
  2167. this.bv.validate();
  2168. expect(this.bv.isValidField('color')).toBeTruthy();
  2169. });
  2170. it('hsl(): reject negative saturation value', function() {
  2171. this.$color.val('hsl(10,-50%,50%)');
  2172. this.bv.validate();
  2173. expect(this.bv.isValidField('color')).toEqual(false);
  2174. });
  2175. it('hsl(): reject negative lightness', function() {
  2176. this.$color.val('hsl(10,50%,-50%)');
  2177. this.bv.validate();
  2178. expect(this.bv.isValidField('color')).toEqual(false);
  2179. });
  2180. it('hsl(): require hsl()', function() {
  2181. this.$color.val('120,50%,50%');
  2182. this.bv.validate();
  2183. expect(this.bv.isValidField('color')).toEqual(false);
  2184. });
  2185. it('hsl(): reject percentages above 100%', function() {
  2186. this.$color.val('hsl(120,100%,101%)');
  2187. this.bv.validate();
  2188. expect(this.bv.isValidField('color')).toEqual(false);
  2189. });
  2190. it('hsl(): reject space between hsl and (', function() {
  2191. this.$color.val('hsl (120,50%,50%)');
  2192. this.bv.validate();
  2193. expect(this.bv.isValidField('color')).toEqual(false);
  2194. });
  2195. it('hsl(): reject leading space', function() {
  2196. this.$color.val(' hsl(120,50%,50%)');
  2197. this.bv.validate();
  2198. expect(this.bv.isValidField('color')).toEqual(false);
  2199. });
  2200. it('hsl(): reject trailing space', function() {
  2201. this.$color.val('hsl(120,50%,50%) ');
  2202. this.bv.validate();
  2203. expect(this.bv.isValidField('color')).toEqual(false);
  2204. });
  2205. it('hsl(): reject percentages in hue value', function() {
  2206. this.$color.val('hsl(50%, 50%, 100%)');
  2207. this.bv.validate();
  2208. expect(this.bv.isValidField('color')).toEqual(false);
  2209. });
  2210. it('hsl(): reject integers in saturation value', function() {
  2211. this.$color.val('hsl(120, 50, 100%)');
  2212. this.bv.validate();
  2213. expect(this.bv.isValidField('color')).toEqual(false);
  2214. });
  2215. it('hsl(): reject integers in lightness value', function() {
  2216. this.$color.val('hsl(120, 50%, 100)');
  2217. this.bv.validate();
  2218. expect(this.bv.isValidField('color')).toEqual(false);
  2219. });
  2220. // Start keyword test
  2221. it('keyword: accept transparent', function() {
  2222. this.$color.val('transparent');
  2223. this.bv.validate();
  2224. expect(this.bv.isValidField('color')).toBeTruthy();
  2225. });
  2226. it('keyword: accept blueviolet', function() {
  2227. this.$color.val('transparent');
  2228. this.bv.validate();
  2229. expect(this.bv.isValidField('color')).toBeTruthy();
  2230. });
  2231. it('keyword: reject combined keywords', function() {
  2232. this.$color.val('blueviolet red');
  2233. this.bv.validate();
  2234. expect(this.bv.isValidField('color')).toEqual(false);
  2235. });
  2236. it('keyword: reject shady', function() {
  2237. this.$color.val('shady');
  2238. this.bv.validate();
  2239. expect(this.bv.isValidField('color')).toEqual(false);
  2240. });
  2241. it('keyword: reject blueish', function() {
  2242. this.$color.val('blueish');
  2243. this.bv.validate();
  2244. expect(this.bv.isValidField('color')).toEqual(false);
  2245. });
  2246. // Start rgba() test
  2247. it('rgba(): accept rgba()', function() {
  2248. this.$color.val('rgba(255,255,255,1)');
  2249. this.bv.validate();
  2250. expect(this.bv.isValidField('color')).toBeTruthy();
  2251. });
  2252. it('rgba(): accept spaces around numeric values', function() {
  2253. this.$color.val('rgba( 255 , 255 , 255 , 1 )');
  2254. this.bv.validate();
  2255. expect(this.bv.isValidField('color')).toBeTruthy();
  2256. });
  2257. it('rgba(): accept multiple spaces around numeric values', function() {
  2258. this.$color.val('rgba( 255 , 255 , 255 , 1 )');
  2259. this.bv.validate();
  2260. expect(this.bv.isValidField('color')).toBeTruthy();
  2261. });
  2262. it('rgba(): accept interger values', function() {
  2263. this.$color.val('rgba(255,255,255,1)');
  2264. this.bv.validate();
  2265. expect(this.bv.isValidField('color')).toBeTruthy();
  2266. });
  2267. it('rgba(): accept percent values', function() {
  2268. this.$color.val('rgba(100%,100%,100%,1)');
  2269. this.bv.validate();
  2270. expect(this.bv.isValidField('color')).toBeTruthy();
  2271. });
  2272. it('rgba(): accept integer alpha channel value of 0', function() {
  2273. this.$color.val('rgba(255,255,255,0)');
  2274. this.bv.validate();
  2275. expect(this.bv.isValidField('color')).toBeTruthy();
  2276. });
  2277. it('rgba(): accept integer alpha channel value of 1', function() {
  2278. this.$color.val('rgba(255,255,255,1)');
  2279. this.bv.validate();
  2280. expect(this.bv.isValidField('color')).toBeTruthy();
  2281. });
  2282. it('rgba(): accept floating alpha channel with leading 0', function() {
  2283. this.$color.val('rgba(255,255,255,0.5)');
  2284. this.bv.validate();
  2285. expect(this.bv.isValidField('color')).toBeTruthy();
  2286. });
  2287. it('rgba(): accept floating alpha channel without leading 0', function() {
  2288. this.$color.val('rgba(255,255,255,.5)');
  2289. this.bv.validate();
  2290. expect(this.bv.isValidField('color')).toBeTruthy();
  2291. });
  2292. it('rgba(): accept floating alpha channel with more than 1 decimal place', function() {
  2293. this.$color.val('rgba(255,255,255,.524141)');
  2294. this.bv.validate();
  2295. expect(this.bv.isValidField('color')).toBeTruthy();
  2296. });
  2297. it('rgba(): accept integer alpha channel value of 0 with percentage rgb values', function() {
  2298. this.$color.val('rgba(100%,100%,100%,0)');
  2299. this.bv.validate();
  2300. expect(this.bv.isValidField('color')).toBeTruthy();
  2301. });
  2302. it('rgba(): accept integer alpha channel value of 1 with percentage rgb values', function() {
  2303. this.$color.val('rgba(100%,100%,100%,1)');
  2304. this.bv.validate();
  2305. expect(this.bv.isValidField('color')).toBeTruthy();
  2306. });
  2307. it('rgba(): accept floating alpha channel with leading 0 with percentage rgb values', function() {
  2308. this.$color.val('rgba(100%,100%,100%,0.5)');
  2309. this.bv.validate();
  2310. expect(this.bv.isValidField('color')).toBeTruthy();
  2311. });
  2312. it('rgba(): accept floating alpha channel without leading 0 with percentage rgb values', function() {
  2313. this.$color.val('rgba(100%,100%,100%,.5)');
  2314. this.bv.validate();
  2315. expect(this.bv.isValidField('color')).toBeTruthy();
  2316. });
  2317. it('rgba(): accept floating alpha channel with more than 1 decimal place with percentage rgb values', function() {
  2318. this.$color.val('rgba(100%,100%,100%,.524141)');
  2319. this.bv.validate();
  2320. expect(this.bv.isValidField('color')).toBeTruthy();
  2321. });
  2322. it('rgba(): reject percentage value for alpha channel', function() {
  2323. this.$color.val('rgba(100%,100%,100%,50%)');
  2324. this.bv.validate();
  2325. expect(this.bv.isValidField('color')).toEqual(false);
  2326. });
  2327. it('rgba(): reject integers larger than 1 for alpha channel', function() {
  2328. this.$color.val('rgba(255,255,255,2)');
  2329. this.bv.validate();
  2330. expect(this.bv.isValidField('color')).toEqual(false);
  2331. });
  2332. it('rgba(): reject negative integers for alpha channel', function() {
  2333. this.$color.val('rgba(255,255,255,-1)');
  2334. this.bv.validate();
  2335. expect(this.bv.isValidField('color')).toEqual(false);
  2336. });
  2337. it('rgba(): reject floats larger than 1 for alpha channel', function() {
  2338. this.$color.val('rgba(255,255,255,1.000000000001)');
  2339. this.bv.validate();
  2340. expect(this.bv.isValidField('color')).toEqual(false);
  2341. });
  2342. it('rgba(): reject negative floats for alpha channel', function() {
  2343. this.$color.val('rgba(255,255,255,-0.5)');
  2344. this.bv.validate();
  2345. expect(this.bv.isValidField('color')).toEqual(false);
  2346. });
  2347. it('rgba(): reject more floats larger than 1 for alpha channel', function() {
  2348. this.$color.val('rgba(255,255,255,2.3)');
  2349. this.bv.validate();
  2350. expect(this.bv.isValidField('color')).toEqual(false);
  2351. });
  2352. it('rgba(): reject mixed intergers and percentile input', function() {
  2353. this.$color.val('rgba(255,255,100%,1)');
  2354. this.bv.validate();
  2355. expect(this.bv.isValidField('color')).toEqual(false);
  2356. });
  2357. it('rgba(): reject negative integers', function() {
  2358. this.$color.val('rgba(-10,255,255,1)');
  2359. this.bv.validate();
  2360. expect(this.bv.isValidField('color')).toEqual(false);
  2361. });
  2362. it('rgba(): reject negative percentages', function() {
  2363. this.$color.val('rgba(-10%,100%,100%,1)');
  2364. this.bv.validate();
  2365. expect(this.bv.isValidField('color')).toEqual(false);
  2366. });
  2367. it('rgba(): require rgba()', function() {
  2368. this.$color.val('255,255,255,1');
  2369. this.bv.validate();
  2370. expect(this.bv.isValidField('color')).toEqual(false);
  2371. });
  2372. it('rgba(): reject intergers above 255', function() {
  2373. this.$color.val('rgba(255,255,256),1');
  2374. this.bv.validate();
  2375. expect(this.bv.isValidField('color')).toEqual(false);
  2376. });
  2377. it('rgba(): reject percentages above 100%', function() {
  2378. this.$color.val('rgba(100%,100%,101%,1)');
  2379. this.bv.validate();
  2380. expect(this.bv.isValidField('color')).toEqual(false);
  2381. });
  2382. it('rgba(): reject space between rgba and (', function() {
  2383. this.$color.val('rgba (255,255,255,1)');
  2384. this.bv.validate();
  2385. expect(this.bv.isValidField('color')).toEqual(false);
  2386. });
  2387. it('rgba(): reject leading space', function() {
  2388. this.$color.val(' rgba(255,255,255,1)');
  2389. this.bv.validate();
  2390. expect(this.bv.isValidField('color')).toEqual(false);
  2391. });
  2392. it('rgba(): reject trailing space', function() {
  2393. this.$color.val('rgba(255,255,255,1) ');
  2394. this.bv.validate();
  2395. expect(this.bv.isValidField('color')).toEqual(false);
  2396. });
  2397. // Start rgb() test
  2398. it('rgb(): accept rgb()', function() {
  2399. this.$color.val('rgb(255,255,255)');
  2400. this.bv.validate();
  2401. expect(this.bv.isValidField('color')).toBeTruthy();
  2402. });
  2403. it('rgb(): accept spaces around numeric values', function() {
  2404. this.$color.val('rgb( 255 , 255 , 255 )');
  2405. this.bv.validate();
  2406. expect(this.bv.isValidField('color')).toBeTruthy();
  2407. });
  2408. it('rgb(): accept multiple spaces around numeric values', function() {
  2409. this.$color.val('rgb( 255, 255, 255 )');
  2410. this.bv.validate();
  2411. expect(this.bv.isValidField('color')).toBeTruthy();
  2412. });
  2413. it('rgb(): accept interger values', function() {
  2414. this.$color.val('rgb(255,255,255)');
  2415. this.bv.validate();
  2416. expect(this.bv.isValidField('color')).toBeTruthy();
  2417. });
  2418. it('rgb(): accept percent values', function() {
  2419. this.$color.val('rgb(100%,100%,100%)');
  2420. this.bv.validate();
  2421. expect(this.bv.isValidField('color')).toBeTruthy();
  2422. });
  2423. it('rgb(): reject mixed intergers and percentile input', function() {
  2424. this.$color.val('rgb(255,255,100%)');
  2425. this.bv.validate();
  2426. expect(this.bv.isValidField('color')).toEqual(false);
  2427. });
  2428. it('rgb(): reject negative integers', function() {
  2429. this.$color.val('rgb(-10,255,255)');
  2430. this.bv.validate();
  2431. expect(this.bv.isValidField('color')).toEqual(false);
  2432. });
  2433. it('rgb(): reject negative percentages', function() {
  2434. this.$color.val('rgb(-10%,100%,100%)');
  2435. this.bv.validate();
  2436. expect(this.bv.isValidField('color')).toEqual(false);
  2437. });
  2438. it('rgb(): require rgb()', function() {
  2439. this.$color.val('255,255,255');
  2440. this.bv.validate();
  2441. expect(this.bv.isValidField('color')).toEqual(false);
  2442. });
  2443. it('rgb(): reject intergers above 255', function() {
  2444. this.$color.val('rgb(255,255,256)');
  2445. this.bv.validate();
  2446. expect(this.bv.isValidField('color')).toEqual(false);
  2447. });
  2448. it('rgb(): reject percentages above 100%', function() {
  2449. this.$color.val('rgb(100%,100%,101%)');
  2450. this.bv.validate();
  2451. expect(this.bv.isValidField('color')).toEqual(false);
  2452. });
  2453. it('rgb(): reject space between rgb and (', function() {
  2454. this.$color.val('rgb (255,255,255)');
  2455. this.bv.validate();
  2456. expect(this.bv.isValidField('color')).toEqual(false);
  2457. });
  2458. it('rgb(): reject leading space', function() {
  2459. this.$color.val(' rgb(255,255,255)');
  2460. this.bv.validate();
  2461. expect(this.bv.isValidField('color')).toEqual(false);
  2462. });
  2463. it('rgb(): reject trailing space', function() {
  2464. this.$color.val('rgb(255,255,255) ');
  2465. this.bv.validate();
  2466. expect(this.bv.isValidField('color')).toEqual(false);
  2467. });
  2468. /* Run individual tests */
  2469. it('Individual field keyword: accept keyword', function() {
  2470. this.$colorKeyword.val('blue');
  2471. this.bv.validate();
  2472. expect(this.bv.isValidField('colorKeyword')).toBeTruthy();
  2473. });
  2474. it('Individual field keyword: reject rgb', function() {
  2475. this.$colorKeyword.val('rgb(255,255,255)');
  2476. this.bv.validate();
  2477. expect(this.bv.isValidField('colorKeyword')).toBeTruthy();
  2478. });
  2479. it('Individual field hex: accept 6 char hex', function() {
  2480. this.$colorHex.val('#0000FF');
  2481. this.bv.validate();
  2482. expect(this.bv.isValidField('colorHex')).toBeTruthy();
  2483. });
  2484. it('Individual field hex: reject keyword', function() {
  2485. this.$colorHex.val('blue');
  2486. this.bv.validate();
  2487. expect(this.bv.isValidField('colorHex')).toBeTruthy();
  2488. });
  2489. it('Individual field rgb(): accept rgb()', function() {
  2490. this.$colorRgb.val('rgb(255,255,255)');
  2491. this.bv.validate();
  2492. expect(this.bv.isValidField('colorRgb')).toBeTruthy();
  2493. });
  2494. it('Individual field rgb(): reject hex', function() {
  2495. this.$colorRgb.val('#0000FF');
  2496. this.bv.validate();
  2497. expect(this.bv.isValidField('colorRgb')).toBeTruthy();
  2498. });
  2499. it('Individual field rgba(): accept rgba()', function() {
  2500. this.$colorRgba.val('rgba(255,255,255,1)');
  2501. this.bv.validate();
  2502. expect(this.bv.isValidField('colorRgba')).toBeTruthy();
  2503. });
  2504. it('Individual field rgba(): reject rgb()', function() {
  2505. this.$colorRgba.val('rgb(255,255,255)');
  2506. this.bv.validate();
  2507. expect(this.bv.isValidField('colorRgba')).toBeTruthy();
  2508. });
  2509. it('Individual field hsl(): accept hsl()', function() {
  2510. this.$colorHsl.val('hsl(120,50%,50%)');
  2511. this.bv.validate();
  2512. expect(this.bv.isValidField('colorHsl')).toBeTruthy();
  2513. });
  2514. it('Individual field hsl(): reject rgba()', function() {
  2515. this.$colorHsl.val('rgba(255,255,255,1)');
  2516. this.bv.validate();
  2517. expect(this.bv.isValidField('colorHsl')).toBeTruthy();
  2518. });
  2519. it('Individual field hsla(): accept hsla()', function() {
  2520. this.$colorHsla.val('hsla(120,50%,50%,1)');
  2521. this.bv.validate();
  2522. expect(this.bv.isValidField('colorHsla')).toBeTruthy();
  2523. });
  2524. it('Individual field hsla(): reject hsl()', function() {
  2525. this.$colorHsla.val('hsl(120,50%,50%)');
  2526. this.bv.validate();
  2527. expect(this.bv.isValidField('colorHsla')).toBeTruthy();
  2528. });
  2529. });
  2530. describe('creditCard', function() {
  2531. // Get the fake credit card number at http://www.getcreditcardnumbers.com/
  2532. beforeEach(function() {
  2533. var html = [
  2534. '<div class="container">',
  2535. '<form class="form-horizontal" id="ccForm">',
  2536. '<div class="form-group">',
  2537. '<input type="text" name="cc" data-bv-creditcard />',
  2538. '</div>',
  2539. '</form>',
  2540. '</div>'
  2541. ].join('\n');
  2542. $(html).appendTo('body');
  2543. $('#ccForm').bootstrapValidator();
  2544. this.bv = $('#ccForm').data('bootstrapValidator');
  2545. this.$creditCard = this.bv.getFieldElements('cc');
  2546. });
  2547. afterEach(function() {
  2548. $('#ccForm').bootstrapValidator('destroy').parent().remove();
  2549. });
  2550. it('accept spaces', function() {
  2551. this.$creditCard.val('5267 9789 9451 9654');
  2552. this.bv.validate();
  2553. expect(this.bv.isValidField('cc')).toBeTruthy();
  2554. });
  2555. it('accept dashes', function() {
  2556. this.$creditCard.val('6011-2649-6840-4521');
  2557. this.bv.validate();
  2558. expect(this.bv.isValidField('cc')).toBeTruthy();
  2559. });
  2560. it('invalid format', function() {
  2561. this.$creditCard.val('4539.1870.2954.3862');
  2562. this.bv.validate();
  2563. expect(this.bv.isValidField('cc')).toEqual(false);
  2564. });
  2565. it('American Express', function() {
  2566. this.$creditCard.val('340653705597107');
  2567. this.bv.validate();
  2568. expect(this.bv.isValidField('cc')).toBeTruthy();
  2569. });
  2570. it('American Express invalid length', function() {
  2571. this.$creditCard.val('3744148309166730');
  2572. this.bv.validate();
  2573. expect(this.bv.isValidField('cc')).toEqual(false);
  2574. });
  2575. it('American Express invalid prefix', function() {
  2576. this.$creditCard.val('356120148436654');
  2577. this.bv.validate();
  2578. expect(this.bv.isValidField('cc')).toEqual(false);
  2579. });
  2580. it('Diners Club', function() {
  2581. this.$creditCard.val('30130708434187');
  2582. this.bv.validate();
  2583. expect(this.bv.isValidField('cc')).toBeTruthy();
  2584. });
  2585. it('Diners Club (US)', function() {
  2586. this.$creditCard.val('5517479515603901');
  2587. this.bv.validate();
  2588. expect(this.bv.isValidField('cc')).toBeTruthy();
  2589. });
  2590. it('Discover', function() {
  2591. this.$creditCard.val('6011734674929094');
  2592. this.bv.validate();
  2593. expect(this.bv.isValidField('cc')).toBeTruthy();
  2594. });
  2595. it('JCB', function() {
  2596. this.$creditCard.val('3566002020360505');
  2597. this.bv.validate();
  2598. expect(this.bv.isValidField('cc')).toBeTruthy();
  2599. });
  2600. it('Laser', function() {
  2601. this.$creditCard.val('6304 9000 1774 0292 441');
  2602. this.bv.validate();
  2603. expect(this.bv.isValidField('cc')).toBeTruthy();
  2604. });
  2605. it('Maestro', function() {
  2606. this.$creditCard.val('6762835098779303');
  2607. this.bv.validate();
  2608. expect(this.bv.isValidField('cc')).toBeTruthy();
  2609. });
  2610. it('Mastercard', function() {
  2611. this.$creditCard.val('5303765013600904');
  2612. this.bv.validate();
  2613. expect(this.bv.isValidField('cc')).toBeTruthy();
  2614. });
  2615. it('Solo', function() {
  2616. this.$creditCard.val('6334580500000000');
  2617. this.bv.validate();
  2618. expect(this.bv.isValidField('cc')).toBeTruthy();
  2619. });
  2620. it('Visa', function() {
  2621. this.$creditCard.val('4929248980295542');
  2622. this.bv.validate();
  2623. expect(this.bv.isValidField('cc')).toBeTruthy();
  2624. });
  2625. it('Visa invalid check digit', function() {
  2626. this.$creditCard.val('4532599916257826');
  2627. this.bv.validate();
  2628. expect(this.bv.isValidField('cc')).toEqual(false);
  2629. });
  2630. });
  2631. describe('date', function() {
  2632. beforeEach(function() {
  2633. $([
  2634. '<form class="form-horizontal" id="dateForm">',
  2635. '<div id="msg"></div>',
  2636. '<div class="form-group">',
  2637. '<input type="text" name="date" data-bv-date />',
  2638. '</div>',
  2639. '</form>'
  2640. ].join('\n')).appendTo('body');
  2641. $('#dateForm').bootstrapValidator();
  2642. this.bv = $('#dateForm').data('bootstrapValidator');
  2643. this.$date = this.bv.getFieldElements('date');
  2644. });
  2645. afterEach(function() {
  2646. $('#dateForm').bootstrapValidator('destroy').remove();
  2647. });
  2648. it('YYYY/MM/DD', function() {
  2649. this.bv.updateOption('date', 'date', 'format', 'YYYY/MM/DD');
  2650. this.$date.val('2000/01/30');
  2651. this.bv.validate();
  2652. expect(this.bv.isValid()).toBeTruthy();
  2653. // Invalid year
  2654. this.bv.resetForm();
  2655. this.$date.val('100/10/20');
  2656. this.bv.validate();
  2657. expect(this.bv.isValid()).toEqual(false);
  2658. // Invalid month
  2659. this.bv.resetForm();
  2660. this.$date.val('2000/00/10');
  2661. this.bv.validate();
  2662. expect(this.bv.isValid()).toEqual(false);
  2663. this.bv.resetForm();
  2664. this.$date.val('2000/15/10');
  2665. this.bv.validate();
  2666. expect(this.bv.isValid()).toEqual(false);
  2667. // Invalid day
  2668. this.bv.resetForm();
  2669. this.$date.val('2000/03/00');
  2670. this.bv.validate();
  2671. expect(this.bv.isValid()).toEqual(false);
  2672. this.bv.resetForm();
  2673. this.$date.val('2000/10/32');
  2674. this.bv.validate();
  2675. expect(this.bv.isValid()).toEqual(false);
  2676. // Negative number
  2677. this.bv.resetForm();
  2678. this.$date.val('-2000/10/20');
  2679. this.bv.validate();
  2680. expect(this.bv.isValid()).toEqual(false);
  2681. this.bv.resetForm();
  2682. this.$date.val('2000/-10/20');
  2683. this.bv.validate();
  2684. expect(this.bv.isValid()).toEqual(false);
  2685. this.bv.resetForm();
  2686. this.$date.val('2000/10/-20');
  2687. this.bv.validate();
  2688. expect(this.bv.isValid()).toEqual(false);
  2689. // Consist invalid characters
  2690. // Issue #310
  2691. this.bv.resetForm();
  2692. this.$date.val('aaaa/');
  2693. this.bv.validate();
  2694. expect(this.bv.isValid()).toEqual(false);
  2695. this.bv.resetForm();
  2696. this.$date.val('2004df/1dd1/5ffg');
  2697. this.bv.validate();
  2698. expect(this.bv.isValid()).toEqual(false);
  2699. // Issue #475
  2700. this.bv.resetForm();
  2701. this.$date.val('2014/09');
  2702. this.bv.validate();
  2703. expect(this.bv.isValid()).toEqual(false);
  2704. this.bv.resetForm();
  2705. this.$date.val('2014/09/');
  2706. this.bv.validate();
  2707. expect(this.bv.isValid()).toEqual(false);
  2708. this.bv.resetForm();
  2709. this.$date.val('2014//15');
  2710. this.bv.validate();
  2711. expect(this.bv.isValid()).toEqual(false);
  2712. this.bv.resetForm();
  2713. this.$date.val('/09/15');
  2714. this.bv.validate();
  2715. expect(this.bv.isValid()).toEqual(false);
  2716. });
  2717. it('MM/DD/YYYY', function() {
  2718. this.bv.updateOption('date', 'date', 'format', 'MM/DD/YYYY');
  2719. this.$date.val('09/15/2020');
  2720. this.bv.validate();
  2721. expect(this.bv.isValid()).toBeTruthy();
  2722. this.bv.resetForm();
  2723. this.$date.val('09/15');
  2724. this.bv.validate();
  2725. expect(this.bv.isValid()).toEqual(false);
  2726. this.bv.resetForm();
  2727. this.$date.val('09/15/');
  2728. this.bv.validate();
  2729. expect(this.bv.isValid()).toEqual(false);
  2730. });
  2731. it('number of days in February', function() {
  2732. this.bv.updateOption('date', 'date', 'format', 'YYYY/MM/DD');
  2733. this.$date.val('2000/02/28');
  2734. this.bv.validate();
  2735. expect(this.bv.isValid()).toBeTruthy();
  2736. this.bv.resetForm();
  2737. this.$date.val('2000/02/29');
  2738. this.bv.validate();
  2739. expect(this.bv.isValid()).toBeTruthy();
  2740. this.bv.resetForm();
  2741. this.$date.val('2001/02/29');
  2742. this.bv.validate();
  2743. expect(this.bv.isValid()).toEqual(false);
  2744. });
  2745. // Issue #681
  2746. it('date, month, year are prefixed by zero', function() {
  2747. this.bv.updateOption('date', 'date', 'format', 'MM/DD/YYYY');
  2748. this.$date.val('0012/08/2014');
  2749. this.bv.validate();
  2750. expect(this.bv.isValid()).toEqual(false);
  2751. this.bv.resetForm();
  2752. this.$date.val('12/0008/2014');
  2753. this.bv.validate();
  2754. expect(this.bv.isValid()).toEqual(false);
  2755. this.bv.resetForm();
  2756. this.$date.val('12/08/002014');
  2757. this.bv.validate();
  2758. expect(this.bv.isValid()).toEqual(false);
  2759. this.bv.resetForm();
  2760. this.$date.val('12/08/2014');
  2761. this.bv.validate();
  2762. expect(this.bv.isValid()).toBeTruthy();
  2763. });
  2764. it('hours, minutes, seconds are prefixed by zero', function() {
  2765. this.bv.updateOption('date', 'date', 'format', 'YYYY/MM/DD h:m:s');
  2766. this.$date.val('2014/08/17 0007:30:00');
  2767. this.bv.validate();
  2768. expect(this.bv.isValid()).toEqual(false);
  2769. this.bv.resetForm();
  2770. this.$date.val('2014/08/17 07:030:00');
  2771. this.bv.validate();
  2772. expect(this.bv.isValid()).toEqual(false);
  2773. this.bv.resetForm();
  2774. this.$date.val('2014/08/17 07:30:0000');
  2775. this.bv.validate();
  2776. expect(this.bv.isValid()).toEqual(false);
  2777. this.bv.resetForm();
  2778. this.$date.val('2014/08/17 07:30:00');
  2779. this.bv.validate();
  2780. expect(this.bv.isValid()).toBeTruthy();
  2781. });
  2782. });
  2783. describe('ean', function() {
  2784. beforeEach(function() {
  2785. var html = [
  2786. '<div class="container">',
  2787. '<form class="form-horizontal" id="eanForm">',
  2788. '<div class="form-group">',
  2789. '<input type="text" name="ean" data-bv-ean />',
  2790. '</div>',
  2791. '</form>',
  2792. '</div>'
  2793. ].join('\n');
  2794. $(html).appendTo('body');
  2795. $('#eanForm').bootstrapValidator();
  2796. this.bv = $('#eanForm').data('bootstrapValidator');
  2797. this.$ean = this.bv.getFieldElements('ean');
  2798. });
  2799. afterEach(function() {
  2800. $('#eanForm').bootstrapValidator('destroy').parent().remove();
  2801. });
  2802. it('valid', function() {
  2803. var samples = ['73513537', '9780471117094', '4006381333931'];
  2804. for (var i in samples) {
  2805. this.$ean.val(samples[i]);
  2806. this.bv.validate();
  2807. expect(this.bv.isValidField('ean')).toBeTruthy();
  2808. }
  2809. });
  2810. it('contains only digits', function() {
  2811. this.$ean.val('123abcDEF!@#');
  2812. this.bv.validate();
  2813. expect(this.bv.isValidField('ean')).toEqual(false);
  2814. });
  2815. it('invalid length', function() {
  2816. this.$ean.val('1234567');
  2817. this.bv.validate();
  2818. expect(this.bv.isValidField('ean')).toEqual(false);
  2819. });
  2820. it('invalid check digit', function() {
  2821. this.$ean.val('73513536');
  2822. this.bv.validate();
  2823. expect(this.bv.isValidField('ean')).toEqual(false);
  2824. });
  2825. });
  2826. describe('emailAddress', function() {
  2827. beforeEach(function() {
  2828. $([
  2829. '<form class="form-horizontal" id="emailAddressForm">',
  2830. '<div id="msg"></div>',
  2831. '<div class="form-group">',
  2832. '<input type="text" name="email-address-or-addresses" data-bv-emailaddress />',
  2833. '</div>',
  2834. '</form>'
  2835. ].join('\n')).appendTo('body');
  2836. $('#emailAddressForm').bootstrapValidator();
  2837. this.bv = $('#emailAddressForm').data('bootstrapValidator');
  2838. this.$emailAddressOrAddresses = this.bv.getFieldElements('email-address-or-addresses');
  2839. });
  2840. afterEach(function() {
  2841. $('#emailAddressForm').bootstrapValidator('destroy').remove();
  2842. });
  2843. var validEmailAddresses = [
  2844. 'admin@mailserver1',
  2845. 'niceandsimple@example.com',
  2846. 'very.common@example.com',
  2847. 'a.little.lengthy.but.fine@dept.example.com',
  2848. 'disposable.style.email.with+symbol@example.com',
  2849. 'other.email-with-dash@example.com',
  2850. '"much.more unusual"@example.com',
  2851. '"very.unusual.@.unusual.com"@example.com',
  2852. '"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com',
  2853. '" "@example.org',
  2854. 'üñîçøðé@example.com'
  2855. ];
  2856. var invalidEmailAddresses = [
  2857. // "!#$%&'*+-/=?^_`{}|~@example.org", // This is actually passing validation; see https://github.com/nghuuphuoc/bootstrapvalidator/issues/673
  2858. 'üñîçøðé@üñîçøðé.com',
  2859. 'Abc.example.com',
  2860. 'A@b@c@example.com',
  2861. 'a"b(c)d,e:f;gi[j\k]l@example.com',
  2862. 'just"not"right@example.com',
  2863. 'this is"not\allowed@example.com',
  2864. 'this\ still\"not\\allowed@example.com'
  2865. ];
  2866. var validMultipleEmailAddressesForDefaultSeparators = [
  2867. 'niceandsimple@example.com,very.common@example.com',
  2868. 'niceandsimple@example.com;very.common@example.com',
  2869. 'niceandsimple@example.com;very.common@example.com,a.little.lengthy.but.fine@dept.example.com'
  2870. ];
  2871. var invalidMultipleEmailAddressesForDefaultSeparators = [
  2872. 'niceandsimple@example.com+very.common@example.com',
  2873. 'niceandsimple@example.com|very.common@example.com'
  2874. ];
  2875. var validMultipleEmailAddressesForCommaOrDollarSignSeparators = [
  2876. 'niceandsimple@example.com,very.common@example.com',
  2877. 'niceandsimple@example.com$very.common@example.com',
  2878. 'niceandsimple@example.com,very.common@example.com$a.little.lengthy.but.fine@dept.example.com'
  2879. ];
  2880. var invalidMultipleEmailAddressesForCommaOrDollarSignSeparators = [
  2881. 'niceandsimple@example.com;very.common@example.com',
  2882. 'niceandsimple@example.com;very.common@example.com,a.little.lengthy.but.fine@dept.example.com'
  2883. ];
  2884. it('Valid email addresses (multiple=false)', function() {
  2885. var that = this;
  2886. $.each(validEmailAddresses, function(index, emailAddress) {
  2887. that.bv.resetForm();
  2888. that.$emailAddressOrAddresses.val(emailAddress);
  2889. that.bv.validate();
  2890. expect(that.bv.isValid()).toBeTruthy();
  2891. });
  2892. });
  2893. it('Invalid email addresses (multiple=false)', function() {
  2894. var that = this;
  2895. var addresses = invalidEmailAddresses
  2896. .concat(validMultipleEmailAddressesForDefaultSeparators)
  2897. .concat(invalidMultipleEmailAddressesForDefaultSeparators)
  2898. .concat(validMultipleEmailAddressesForCommaOrDollarSignSeparators)
  2899. .concat(invalidMultipleEmailAddressesForCommaOrDollarSignSeparators);
  2900. $.each(addresses, function(index, emailAddress) {
  2901. that.bv.resetForm();
  2902. that.$emailAddressOrAddresses.val(emailAddress);
  2903. that.bv.validate();
  2904. expect(that.bv.isValid()).toEqual(false);
  2905. });
  2906. });
  2907. it('Invalid email addresses (multiple=false,separator=/[,\$]/)', function() {
  2908. var that = this;
  2909. that.bv.updateOption('email-address-or-addresses', 'emailAddress', 'separator', /[,;]/);
  2910. var addresses = invalidEmailAddresses
  2911. .concat(validMultipleEmailAddressesForDefaultSeparators)
  2912. .concat(invalidMultipleEmailAddressesForDefaultSeparators)
  2913. .concat(validMultipleEmailAddressesForCommaOrDollarSignSeparators)
  2914. .concat(invalidMultipleEmailAddressesForCommaOrDollarSignSeparators);
  2915. $.each(addresses, function(index, emailAddress) {
  2916. that.bv.resetForm();
  2917. that.$emailAddressOrAddresses.val(emailAddress);
  2918. that.bv.validate();
  2919. expect(that.bv.isValid()).toEqual(false);
  2920. });
  2921. });
  2922. it('Valid email addresses (multiple=true)', function() {
  2923. var that = this;
  2924. that.bv.updateOption('email-address-or-addresses', 'emailAddress', 'multiple', true);
  2925. var addresses = validEmailAddresses
  2926. .concat(validMultipleEmailAddressesForDefaultSeparators);
  2927. $.each(addresses, function(index, emailAddress) {
  2928. that.bv.resetForm();
  2929. that.$emailAddressOrAddresses.val(emailAddress);
  2930. that.bv.validate();
  2931. expect(that.bv.isValid()).toBeTruthy();
  2932. });
  2933. });
  2934. it('Invalid email addresses (multiple=true)', function() {
  2935. var that = this;
  2936. that.bv.updateOption('email-address-or-addresses', 'emailAddress', 'multiple', true);
  2937. var addresses = invalidEmailAddresses
  2938. .concat(invalidMultipleEmailAddressesForDefaultSeparators);
  2939. $.each(addresses, function(index, emailAddress) {
  2940. that.bv.resetForm();
  2941. that.$emailAddressOrAddresses.val(emailAddress);
  2942. that.bv.validate();
  2943. expect(that.bv.isValid()).toEqual(false);
  2944. });
  2945. });
  2946. it('Valid email addresses (multiple=true,separator=/[,\$]/)', function() {
  2947. var that = this;
  2948. that.bv.updateOption('email-address-or-addresses', 'emailAddress', 'multiple', true);
  2949. that.bv.updateOption('email-address-or-addresses', 'emailAddress', 'separator', /[,\$]/);
  2950. var addresses = validEmailAddresses
  2951. .concat(validMultipleEmailAddressesForCommaOrDollarSignSeparators);
  2952. $.each(addresses, function(index, emailAddress) {
  2953. that.bv.resetForm();
  2954. that.$emailAddressOrAddresses.val(emailAddress);
  2955. that.bv.validate();
  2956. expect(that.bv.isValid()).toBeTruthy();
  2957. });
  2958. });
  2959. it('Invalid email addresses (multiple=true,separator=/[,\$]/)', function() {
  2960. var that = this;
  2961. that.bv.updateOption('email-address-or-addresses', 'emailAddress', 'multiple', true);
  2962. that.bv.updateOption('email-address-or-addresses', 'emailAddress', 'separator', /[,\$]/);
  2963. var addresses = invalidEmailAddresses
  2964. .concat(invalidMultipleEmailAddressesForCommaOrDollarSignSeparators);
  2965. $.each(addresses, function(index, emailAddress) {
  2966. that.bv.resetForm();
  2967. that.$emailAddressOrAddresses.val(emailAddress);
  2968. that.bv.validate();
  2969. expect(that.bv.isValid()).toEqual(false);
  2970. });
  2971. });
  2972. });
  2973. function greaterThanCompare() {
  2974. var compareTo = $('#greaterThanForm').find('[name="minAge"]').val();
  2975. $('#msg').html('greaterThanCompare() called; compare to ' + compareTo);
  2976. return compareTo;
  2977. };
  2978. TestSuite = $.extend({}, TestSuite, {
  2979. greaterThan: {
  2980. compareTo: function(value, validator, $field) {
  2981. var compareTo = $('#greaterThanForm').find('[name="minAge"]').val();
  2982. $('#msg').html('TestSuite.greaterThan.compareTo() called; compare to ' + compareTo);
  2983. return compareTo;
  2984. }
  2985. }
  2986. });
  2987. describe('greaterThan', function() {
  2988. beforeEach(function() {
  2989. $([
  2990. '<form class="form-horizontal" id="greaterThanForm">',
  2991. '<div id="msg"></div>',
  2992. '<div class="form-group">',
  2993. '<input type="text" name="minAge" />',
  2994. '</div>',
  2995. '<div class="form-group">',
  2996. '<input type="text" name="age" data-bv-greaterthan data-bv-greaterthan-value="18" />',
  2997. '</div>',
  2998. '</form>'
  2999. ].join('\n')).appendTo('body');
  3000. $('#greaterThanForm').bootstrapValidator();
  3001. this.bv = $('#greaterThanForm').data('bootstrapValidator');
  3002. this.$minAge = this.bv.getFieldElements('minAge');
  3003. this.$age = this.bv.getFieldElements('age');
  3004. });
  3005. afterEach(function() {
  3006. $('#greaterThanForm').bootstrapValidator('destroy').remove();
  3007. });
  3008. it('not a number', function() {
  3009. this.$age.val('20abc');
  3010. this.bv.validate();
  3011. expect(this.bv.isValid()).toEqual(false);
  3012. });
  3013. it('compare to value', function() {
  3014. this.$age.val(10);
  3015. this.bv.validate();
  3016. expect(this.bv.isValid()).toEqual(false);
  3017. this.bv.resetForm();
  3018. this.$age.val(20);
  3019. this.bv.validate();
  3020. expect(this.bv.isValid()).toBeTruthy();
  3021. });
  3022. it('compare to other field', function() {
  3023. this.$age.attr('data-bv-greaterthan-value', 'minAge');
  3024. this.bv.destroy();
  3025. this.bv = $('#greaterThanForm').bootstrapValidator().data('bootstrapValidator');
  3026. this.$minAge.val(10);
  3027. this.$age.val(20);
  3028. this.bv.validate();
  3029. expect(this.bv.isValid()).toBeTruthy();
  3030. this.bv.resetForm();
  3031. this.$minAge.val(20);
  3032. this.$age.val(10);
  3033. this.bv.validate();
  3034. expect(this.bv.isValid()).toEqual(false);
  3035. expect(this.bv.getMessages('age', 'greaterThan')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.greaterThan['default'], this.$minAge.val()));
  3036. });
  3037. it('compare to return value of a function', function() {
  3038. this.$age.attr('data-bv-greaterthan-value', 'greaterThanCompare');
  3039. this.bv.destroy();
  3040. this.bv = $('#greaterThanForm').bootstrapValidator().data('bootstrapValidator');
  3041. this.$minAge.val(20);
  3042. this.$age.val(18);
  3043. this.bv.validate();
  3044. expect($('#msg').html()).toEqual('greaterThanCompare() called; compare to 20');
  3045. expect(this.bv.isValid()).toEqual(false);
  3046. expect(this.bv.getMessages('age', 'greaterThan')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.greaterThan['default'], this.$minAge.val()));
  3047. this.bv.resetForm();
  3048. this.$minAge.val(18);
  3049. this.$age.val(20);
  3050. this.bv.validate();
  3051. expect($('#msg').html()).toEqual('greaterThanCompare() called; compare to 18');
  3052. expect(this.bv.isValid()).toBeTruthy();
  3053. });
  3054. it('compare to return value of a namespace function', function() {
  3055. this.$age.attr('data-bv-greaterthan-value', 'TestSuite.greaterThan.compareTo');
  3056. this.bv.destroy();
  3057. this.bv = $('#greaterThanForm').bootstrapValidator().data('bootstrapValidator');
  3058. this.$minAge.val(20);
  3059. this.$age.val(18);
  3060. this.bv.validate();
  3061. expect($('#msg').html()).toEqual('TestSuite.greaterThan.compareTo() called; compare to 20');
  3062. expect(this.bv.isValid()).toEqual(false);
  3063. expect(this.bv.getMessages('age', 'greaterThan')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.greaterThan['default'], this.$minAge.val()));
  3064. this.bv.resetForm();
  3065. this.$minAge.val(18);
  3066. this.$age.val(20);
  3067. this.bv.validate();
  3068. expect($('#msg').html()).toEqual('TestSuite.greaterThan.compareTo() called; compare to 18');
  3069. expect(this.bv.isValid()).toBeTruthy();
  3070. });
  3071. });
  3072. describe('hslColor', function() {
  3073. beforeEach(function() {
  3074. var html = [
  3075. '<div class="container">',
  3076. '<form class="form-horizontal" id="hslColorForm">',
  3077. '<div class="form-group">',
  3078. '<input type="text" name="hsl" data-bv-hslcolor />',
  3079. '</div>',
  3080. '</form>',
  3081. '</div>'
  3082. ].join('\n');
  3083. $(html).appendTo('body');
  3084. $('#hslColorForm').bootstrapValidator();
  3085. this.bv = $('#hslColorForm').data('bootstrapValidator');
  3086. this.$hslColor = this.bv.getFieldElements('hsl');
  3087. });
  3088. afterEach(function() {
  3089. $('#hslColorForm').bootstrapValidator('destroy').parent().remove();
  3090. });
  3091. it('accept hsl()', function() {
  3092. this.$hslColor.val('hsl(120,50%,50%)');
  3093. this.bv.validate();
  3094. expect(this.bv.isValidField('hsl')).toBeTruthy();
  3095. });
  3096. it('accept spaces around values', function() {
  3097. this.$hslColor.val('hsl( 120 , 50% , 50% )');
  3098. this.bv.validate();
  3099. expect(this.bv.isValidField('hsl')).toBeTruthy();
  3100. });
  3101. it('accept multiple spaces around values', function() {
  3102. this.$hslColor.val('hsl( 120, 50%, 50% )');
  3103. this.bv.validate();
  3104. expect(this.bv.isValidField('hsl')).toBeTruthy();
  3105. });
  3106. it('accept negative hue value', function() {
  3107. this.$hslColor.val('hsl(-120,50%,50%)');
  3108. this.bv.validate();
  3109. expect(this.bv.isValidField('hsl')).toBeTruthy();
  3110. });
  3111. it('accept hue values larger than 360', function() {
  3112. this.$hslColor.val('hsl(480,50%,50%)');
  3113. this.bv.validate();
  3114. expect(this.bv.isValidField('hsl')).toBeTruthy();
  3115. });
  3116. it('reject negative saturation value', function() {
  3117. this.$hslColor.val('hsl(10,-50%,50%)');
  3118. this.bv.validate();
  3119. expect(this.bv.isValidField('hsl')).toEqual(false);
  3120. });
  3121. it('reject negative lightness', function() {
  3122. this.$hslColor.val('hsl(10,50%,-50%)');
  3123. this.bv.validate();
  3124. expect(this.bv.isValidField('hsl')).toEqual(false);
  3125. });
  3126. it('require hsl()', function() {
  3127. this.$hslColor.val('120,50%,50%');
  3128. this.bv.validate();
  3129. expect(this.bv.isValidField('hsl')).toEqual(false);
  3130. });
  3131. it('reject percentages above 100%', function() {
  3132. this.$hslColor.val('hsl(120,100%,101%)');
  3133. this.bv.validate();
  3134. expect(this.bv.isValidField('hsl')).toEqual(false);
  3135. });
  3136. it('reject space between hsl and (', function() {
  3137. this.$hslColor.val('hsl (120,50%,50%)');
  3138. this.bv.validate();
  3139. expect(this.bv.isValidField('hsl')).toEqual(false);
  3140. });
  3141. it('reject leading space', function() {
  3142. this.$hslColor.val(' hsl(120,50%,50%)');
  3143. this.bv.validate();
  3144. expect(this.bv.isValidField('hsl')).toEqual(false);
  3145. });
  3146. it('reject trailing space', function() {
  3147. this.$hslColor.val('hsl(120,50%,50%) ');
  3148. this.bv.validate();
  3149. expect(this.bv.isValidField('hsl')).toEqual(false);
  3150. });
  3151. it('reject percentages in hue value', function() {
  3152. this.$hslColor.val('hsl(50%, 50%, 100%)');
  3153. this.bv.validate();
  3154. expect(this.bv.isValidField('hsl')).toEqual(false);
  3155. });
  3156. it('reject integers in saturation value', function() {
  3157. this.$hslColor.val('hsl(120, 50, 100%)');
  3158. this.bv.validate();
  3159. expect(this.bv.isValidField('hsl')).toEqual(false);
  3160. });
  3161. it('reject integers in lightness value', function() {
  3162. this.$hslColor.val('hsl(120, 50%, 100)');
  3163. this.bv.validate();
  3164. expect(this.bv.isValidField('hsl')).toEqual(false);
  3165. });
  3166. });
  3167. describe('hslaColor', function() {
  3168. beforeEach(function() {
  3169. var html = [
  3170. '<div class="container">',
  3171. '<form class="form-horizontal" id="hslaColorForm">',
  3172. '<div class="form-group">',
  3173. '<input type="text" name="hsla" data-bv-hslacolor />',
  3174. '</div>',
  3175. '</form>',
  3176. '</div>'
  3177. ].join('\n');
  3178. $(html).appendTo('body');
  3179. $('#hslaColorForm').bootstrapValidator();
  3180. this.bv = $('#hslaColorForm').data('bootstrapValidator');
  3181. this.$hslaColor = this.bv.getFieldElements('hsla');
  3182. });
  3183. afterEach(function() {
  3184. $('#hslaColorForm').bootstrapValidator('destroy').parent().remove();
  3185. });
  3186. it('accept hsla()', function() {
  3187. this.$hslaColor.val('hsla(120,50%,50%,1)');
  3188. this.bv.validate();
  3189. expect(this.bv.isValidField('hsla')).toBeTruthy();
  3190. });
  3191. it('accept spaces around values', function() {
  3192. this.$hslaColor.val('hsla( 120 , 50% , 50%, 1 )');
  3193. this.bv.validate();
  3194. expect(this.bv.isValidField('hsla')).toBeTruthy();
  3195. });
  3196. it('accept multiple spaces around values', function() {
  3197. this.$hslaColor.val('hsla( 120, 50%, 50% , 1 )');
  3198. this.bv.validate();
  3199. expect(this.bv.isValidField('hsla')).toBeTruthy();
  3200. });
  3201. it('accept negative hue value', function() {
  3202. this.$hslaColor.val('hsla(-120,50%,50%,1)');
  3203. this.bv.validate();
  3204. expect(this.bv.isValidField('hsla')).toBeTruthy();
  3205. });
  3206. it('accept hue values larger than 360', function() {
  3207. this.$hslaColor.val('hsla(480,50%,50%,1)');
  3208. this.bv.validate();
  3209. expect(this.bv.isValidField('hsla')).toBeTruthy();
  3210. });
  3211. it('accept integer alpha channel value of 0', function() {
  3212. this.$hslaColor.val('hsla(120,50%,100%,0)');
  3213. this.bv.validate();
  3214. expect(this.bv.isValidField('hsla')).toBeTruthy();
  3215. });
  3216. it('accept integer alpha channel value of 1', function() {
  3217. this.$hslaColor.val('hsla(120,50%,100%,1)');
  3218. this.bv.validate();
  3219. expect(this.bv.isValidField('hsla')).toBeTruthy();
  3220. });
  3221. it('accept floating alpha channel with leading 0', function() {
  3222. this.$hslaColor.val('hsla(120,50%,100%,0.5)');
  3223. this.bv.validate();
  3224. expect(this.bv.isValidField('hsla')).toBeTruthy();
  3225. });
  3226. it('accept floating alpha channel without leading 0', function() {
  3227. this.$hslaColor.val('hsla(120,50%,100%,.5)');
  3228. this.bv.validate();
  3229. expect(this.bv.isValidField('hsla')).toBeTruthy();
  3230. });
  3231. it('accept floating alpha channel with more than 1 decimal place', function() {
  3232. this.$hslaColor.val('hsla(120,50%,100%,.524141)');
  3233. this.bv.validate();
  3234. expect(this.bv.isValidField('hsla')).toBeTruthy();
  3235. });
  3236. it('reject percentage value for alpha channel', function() {
  3237. this.$hslaColor.val('hsla(120,50%,100%,50%)');
  3238. this.bv.validate();
  3239. expect(this.bv.isValidField('hsla')).toEqual(false);
  3240. });
  3241. it('reject integers larger than 1 for alpha channel', function() {
  3242. this.$hslaColor.val('hsla(120,50%,100%,2)');
  3243. this.bv.validate();
  3244. expect(this.bv.isValidField('hsla')).toEqual(false);
  3245. });
  3246. it('reject negative integers for alpha channel', function() {
  3247. this.$hslaColor.val('hsla(120,50%,100%,-1)');
  3248. this.bv.validate();
  3249. expect(this.bv.isValidField('hsla')).toEqual(false);
  3250. });
  3251. it('reject floats larger than 1 for alpha channel', function() {
  3252. this.$hslaColor.val('hsla(120,50%,100%,1.000000000001)');
  3253. this.bv.validate();
  3254. expect(this.bv.isValidField('hsla')).toEqual(false);
  3255. });
  3256. it('reject negative floats for alpha channel', function() {
  3257. this.$hslaColor.val('hsla(120,50%,100%,-0.5)');
  3258. this.bv.validate();
  3259. expect(this.bv.isValidField('hsla')).toEqual(false);
  3260. });
  3261. it('reject more floats larger than 1 for alpha channel', function() {
  3262. this.$hslaColor.val('hsla(120,50%,100%,2.3)');
  3263. this.bv.validate();
  3264. expect(this.bv.isValidField('hsla')).toEqual(false);
  3265. });
  3266. it('reject negative saturation value', function() {
  3267. this.$hslaColor.val('hsla(10,-50%,50%,1)');
  3268. this.bv.validate();
  3269. expect(this.bv.isValidField('hsla')).toEqual(false);
  3270. });
  3271. it('reject negative lightness', function() {
  3272. this.$hslaColor.val('hsla(10,50%,-50%,1)');
  3273. this.bv.validate();
  3274. expect(this.bv.isValidField('hsla')).toEqual(false);
  3275. });
  3276. it('require hsla()', function() {
  3277. this.$hslaColor.val('120,50%,50%,1');
  3278. this.bv.validate();
  3279. expect(this.bv.isValidField('hsla')).toEqual(false);
  3280. });
  3281. it('reject percentages above 100%', function() {
  3282. this.$hslaColor.val('hsla(120,100%,101%,1)');
  3283. this.bv.validate();
  3284. expect(this.bv.isValidField('hsla')).toEqual(false);
  3285. });
  3286. it('reject space between hsla and (', function() {
  3287. this.$hslaColor.val('hsla (120,50%,50%,1)');
  3288. this.bv.validate();
  3289. expect(this.bv.isValidField('hsla')).toEqual(false);
  3290. });
  3291. it('reject leading space', function() {
  3292. this.$hslaColor.val(' hsla(120,50%,50%,1)');
  3293. this.bv.validate();
  3294. expect(this.bv.isValidField('hsla')).toEqual(false);
  3295. });
  3296. it('reject trailing space', function() {
  3297. this.$hslaColor.val('hsla(120,50%,50%,1) ');
  3298. this.bv.validate();
  3299. expect(this.bv.isValidField('hsla')).toEqual(false);
  3300. });
  3301. it('reject percentages in hue value', function() {
  3302. this.$hslaColor.val('hsla(50%, 50%, 100%,1)');
  3303. this.bv.validate();
  3304. expect(this.bv.isValidField('hsla')).toEqual(false);
  3305. });
  3306. it('reject integers in saturation value', function() {
  3307. this.$hslaColor.val('hsla(120, 50, 100%,1)');
  3308. this.bv.validate();
  3309. expect(this.bv.isValidField('hsla')).toEqual(false);
  3310. });
  3311. it('reject integers in lightness value', function() {
  3312. this.$hslaColor.val('hsla(120, 50%, 100,1)');
  3313. this.bv.validate();
  3314. expect(this.bv.isValidField('hsla')).toEqual(false);
  3315. });
  3316. });
  3317. describe('iban', function() {
  3318. beforeEach(function() {
  3319. $([
  3320. '<form class="form-horizontal" id="ibanForm">',
  3321. '<div class="form-group">',
  3322. '<select class="form-control" name="country">',
  3323. '<option value="AD">Andorra</option>',
  3324. '<option value="AE">United Arab Emirates</option>',
  3325. '<option value="AL">Albania</option>',
  3326. '<option value="AO">Angola</option>',
  3327. '<option value="AT">Austria</option>',
  3328. '<option value="AZ">Azerbaijan</option>',
  3329. '<option value="BA">Bosnia and Herzegovina</option>',
  3330. '<option value="BE">Belgium</option>',
  3331. '<option value="BF">Burkina Faso</option>',
  3332. '<option value="BG">Bulgaria</option>',
  3333. '<option value="BH">Bahrain</option>',
  3334. '<option value="BI">Burundi</option>',
  3335. '<option value="BJ">Benin</option>',
  3336. '<option value="BR">Brazil</option>',
  3337. '<option value="CH">Switzerland</option>',
  3338. '<option value="CM">Cameroon</option>',
  3339. '<option value="CR">Costa Rica</option>',
  3340. '<option value="CV">Cape Verde</option>',
  3341. '<option value="CY">Cyprus</option>',
  3342. '<option value="CZ">Czech Republic</option>',
  3343. '<option value="DE">Germany</option>',
  3344. '<option value="DK">Denmark</option>',
  3345. '<option value="DO">Dominican Republic</option>',
  3346. '<option value="DZ">Algeria</option>',
  3347. '<option value="EE">Estonia</option>',
  3348. '<option value="ES">Spain</option>',
  3349. '<option value="FI">Finland</option>',
  3350. '<option value="FO">Faroe Islands</option>',
  3351. '<option value="FR">France</option>',
  3352. '<option value="GB">United Kingdom</option>',
  3353. '<option value="GE">Georgia</option>',
  3354. '<option value="GI">Gibraltar</option>',
  3355. '<option value="GL">Greenland</option>',
  3356. '<option value="GR">Greece</option>',
  3357. '<option value="GT">Guatemala</option>',
  3358. '<option value="HR">Croatia</option>',
  3359. '<option value="HU">Hungary</option>',
  3360. '<option value="IE">Ireland</option>',
  3361. '<option value="IL">Israel</option>',
  3362. '<option value="IR">Iran</option>',
  3363. '<option value="IS">Iceland</option>',
  3364. '<option value="IT">Italy</option>',
  3365. '<option value="JO">Jordan</option>',
  3366. '<option value="KW">Kuwait</option>',
  3367. '<option value="KZ">Kazakhstan</option>',
  3368. '<option value="LB">Lebanon</option>',
  3369. '<option value="LI">Liechtenstein</option>',
  3370. '<option value="LT">Lithuania</option>',
  3371. '<option value="LU">Luxembourg</option>',
  3372. '<option value="LV">Latvia</option>',
  3373. '<option value="MC">Monaco</option>',
  3374. '<option value="MD">Moldova</option>',
  3375. '<option value="ME">Montenegro</option>',
  3376. '<option value="MG">Madagascar</option>',
  3377. '<option value="MK">Macedonia</option>',
  3378. '<option value="ML">Mali</option>',
  3379. '<option value="MR">Mauritania</option>',
  3380. '<option value="MT">Malta</option>',
  3381. '<option value="MU">Mauritius</option>',
  3382. '<option value="MZ">Mozambique</option>',
  3383. '<option value="NL">Netherlands</option>',
  3384. '<option value="NO">Norway</option>',
  3385. '<option value="PK">Pakistan</option>',
  3386. '<option value="PL">Poland</option>',
  3387. '<option value="PS">Palestinian</option>',
  3388. '<option value="PT">Portugal</option>',
  3389. '<option value="QA">Qatar</option>',
  3390. '<option value="RO">Romania</option>',
  3391. '<option value="RS">Serbia</option>',
  3392. '<option value="SA">Saudi Arabia</option>',
  3393. '<option value="SE">Sweden</option>',
  3394. '<option value="SE">Slovenia</option>',
  3395. '<option value="SK">Slovakia</option>',
  3396. '<option value="SM">San Marino</option>',
  3397. '<option value="SN">Senegal</option>',
  3398. '<option value="TN">Tunisia</option>',
  3399. '<option value="TR">Turkey</option>',
  3400. '<option value="VG">Virgin Islands, British</option>',
  3401. '</select>',
  3402. '</div>',
  3403. '<div class="form-group">',
  3404. '<input type="text" name="iban" data-bv-iban />',
  3405. '</div>',
  3406. '</form>'
  3407. ].join('\n')).appendTo('body');
  3408. $('#ibanForm').bootstrapValidator();
  3409. this.bv = $('#ibanForm').data('bootstrapValidator');
  3410. this.$country = this.bv.getFieldElements('country');
  3411. this.$iban = this.bv.getFieldElements('iban');
  3412. });
  3413. afterEach(function() {
  3414. $('#ibanForm').bootstrapValidator('destroy').remove();
  3415. });
  3416. it('not supported country', function() {
  3417. this.$iban.val('US123456789');
  3418. this.bv.validate();
  3419. expect(this.bv.isValidField('iban')).toEqual(false);
  3420. });
  3421. it('dynamic country', function() {
  3422. this.$iban.attr('data-bv-iban-country', 'country');
  3423. this.bv.destroy();
  3424. this.bv = $('#ibanForm').bootstrapValidator().data('bootstrapValidator');
  3425. this.$country.val('AT');
  3426. this.$iban.val('AT611904300234573201');
  3427. this.bv.validate();
  3428. expect(this.bv.isValid()).toBeTruthy();
  3429. this.bv.resetForm();
  3430. this.$country.val('BG');
  3431. this.$iban.val('HR1210010051863000160');
  3432. this.bv.validate();
  3433. expect(this.bv.isValid()).toEqual(false);
  3434. });
  3435. it('Albania', function() {
  3436. this.$iban.val('AL47212110090000000235698741');
  3437. this.bv.validate();
  3438. expect(this.bv.isValidField('iban')).toBeTruthy();
  3439. });
  3440. it('Algeria', function() {
  3441. this.$iban.val('DZ4000400174401001050486');
  3442. this.bv.validate();
  3443. expect(this.bv.isValidField('iban')).toBeTruthy();
  3444. });
  3445. it('Andorra', function() {
  3446. this.$iban.val('AD1200012030200359100100');
  3447. this.bv.validate();
  3448. expect(this.bv.isValidField('iban')).toBeTruthy();
  3449. });
  3450. it('Angola', function() {
  3451. this.$iban.val('AO06000600000100037131174');
  3452. this.bv.validate();
  3453. expect(this.bv.isValidField('iban')).toBeTruthy();
  3454. });
  3455. it('Austria', function() {
  3456. this.$iban.val('AT611904300234573201');
  3457. this.bv.validate();
  3458. expect(this.bv.isValidField('iban')).toBeTruthy();
  3459. });
  3460. it('Azerbaijan', function() {
  3461. this.$iban.val('AZ21NABZ00000000137010001944');
  3462. this.bv.validate();
  3463. expect(this.bv.isValidField('iban')).toBeTruthy();
  3464. });
  3465. it('Bahrain', function() {
  3466. this.$iban.val('BH29BMAG1299123456BH00');
  3467. this.bv.validate();
  3468. expect(this.bv.isValidField('iban')).toBeTruthy();
  3469. });
  3470. it('Belgium', function() {
  3471. this.$iban.val('BE68539007547034');
  3472. this.bv.validate();
  3473. expect(this.bv.isValidField('iban')).toBeTruthy();
  3474. });
  3475. it('Benin', function() {
  3476. this.$iban.val('BJ11B00610100400271101192591');
  3477. this.bv.validate();
  3478. expect(this.bv.isValidField('iban')).toBeTruthy();
  3479. });
  3480. it('Brazil', function() {
  3481. this.$iban.val('BR9700360305000010009795493P1');
  3482. this.bv.validate();
  3483. expect(this.bv.isValidField('iban')).toBeTruthy();
  3484. });
  3485. it('Bulgaria', function() {
  3486. this.$iban.val('BG80BNBG96611020345678');
  3487. this.bv.validate();
  3488. expect(this.bv.isValidField('iban')).toBeTruthy();
  3489. });
  3490. it('Burkina Faso', function() {
  3491. this.$iban.val('BF1030134020015400945000643');
  3492. this.bv.validate();
  3493. expect(this.bv.isValidField('iban')).toBeTruthy();
  3494. });
  3495. it('Burundi', function() {
  3496. this.$iban.val('BI43201011067444');
  3497. this.bv.validate();
  3498. expect(this.bv.isValidField('iban')).toBeTruthy();
  3499. });
  3500. it('Cameroon', function() {
  3501. this.$iban.val('CM2110003001000500000605306');
  3502. this.bv.validate();
  3503. expect(this.bv.isValidField('iban')).toBeTruthy();
  3504. });
  3505. it('Cape Verde', function() {
  3506. this.$iban.val('CV64000300004547069110176');
  3507. this.bv.validate();
  3508. expect(this.bv.isValidField('iban')).toBeTruthy();
  3509. });
  3510. it('Costa Rica', function() {
  3511. this.$iban.val('CR0515202001026284066');
  3512. this.bv.validate();
  3513. expect(this.bv.isValidField('iban')).toBeTruthy();
  3514. });
  3515. it('Croatia', function() {
  3516. this.$iban.val('HR1210010051863000160');
  3517. this.bv.validate();
  3518. expect(this.bv.isValidField('iban')).toBeTruthy();
  3519. });
  3520. it('Cyprus', function() {
  3521. this.$iban.val('CY17002001280000001200527600');
  3522. this.bv.validate();
  3523. expect(this.bv.isValidField('iban')).toBeTruthy();
  3524. });
  3525. it('Czech Republic', function() {
  3526. this.$iban.val('CZ6508000000192000145399');
  3527. this.bv.validate();
  3528. expect(this.bv.isValidField('iban')).toBeTruthy();
  3529. });
  3530. it('Denmark', function() {
  3531. this.$iban.val('DK5000400440116243');
  3532. this.bv.validate();
  3533. expect(this.bv.isValidField('iban')).toBeTruthy();
  3534. });
  3535. it('Dominican Republic', function() {
  3536. this.$iban.val('DO28BAGR00000001212453611324');
  3537. this.bv.validate();
  3538. expect(this.bv.isValidField('iban')).toBeTruthy();
  3539. });
  3540. it('Estonia', function() {
  3541. this.$iban.val('EE382200221020145685');
  3542. this.bv.validate();
  3543. expect(this.bv.isValidField('iban')).toBeTruthy();
  3544. });
  3545. it('Faroe Islands', function() {
  3546. this.$iban.val('FO1464600009692713');
  3547. this.bv.validate();
  3548. expect(this.bv.isValidField('iban')).toBeTruthy();
  3549. });
  3550. it('Finland', function() {
  3551. this.$iban.val('FI2112345600000785');
  3552. this.bv.validate();
  3553. expect(this.bv.isValidField('iban')).toBeTruthy();
  3554. });
  3555. it('France', function() {
  3556. this.$iban.val('FR1420041010050500013M02606');
  3557. this.bv.validate();
  3558. expect(this.bv.isValidField('iban')).toBeTruthy();
  3559. });
  3560. it('Guatemala', function() {
  3561. this.$iban.val('GT82TRAJ01020000001210029690');
  3562. this.bv.validate();
  3563. expect(this.bv.isValidField('iban')).toBeTruthy();
  3564. });
  3565. it('Georgia', function() {
  3566. this.$iban.val('GE29NB0000000101904917');
  3567. this.bv.validate();
  3568. expect(this.bv.isValidField('iban')).toBeTruthy();
  3569. });
  3570. it('Germany', function() {
  3571. this.$iban.val('DE89370400440532013000');
  3572. this.bv.validate();
  3573. expect(this.bv.isValidField('iban')).toBeTruthy();
  3574. });
  3575. it('Gibraltar', function() {
  3576. this.$iban.val('GI75NWBK000000007099453');
  3577. this.bv.validate();
  3578. expect(this.bv.isValidField('iban')).toBeTruthy();
  3579. });
  3580. it('Greece', function() {
  3581. this.$iban.val('GR1601101250000000012300695');
  3582. this.bv.validate();
  3583. expect(this.bv.isValidField('iban')).toBeTruthy();
  3584. });
  3585. it('Greenland', function() {
  3586. this.$iban.val('GL8964710001000206');
  3587. this.bv.validate();
  3588. expect(this.bv.isValidField('iban')).toBeTruthy();
  3589. });
  3590. it('Hungary', function() {
  3591. this.$iban.val('HU42117730161111101800000000');
  3592. this.bv.validate();
  3593. expect(this.bv.isValidField('iban')).toBeTruthy();
  3594. });
  3595. it('Iceland', function() {
  3596. this.$iban.val('IS140159260076545510730339');
  3597. this.bv.validate();
  3598. expect(this.bv.isValidField('iban')).toBeTruthy();
  3599. });
  3600. it('Iran', function() {
  3601. this.$iban.val('IR580540105180021273113007');
  3602. this.bv.validate();
  3603. expect(this.bv.isValidField('iban')).toBeTruthy();
  3604. });
  3605. it('Ireland', function() {
  3606. this.$iban.val('IE29AIBK93115212345678');
  3607. this.bv.validate();
  3608. expect(this.bv.isValidField('iban')).toBeTruthy();
  3609. });
  3610. it('Israel', function() {
  3611. this.$iban.val('IL620108000000099999999');
  3612. this.bv.validate();
  3613. expect(this.bv.isValidField('iban')).toBeTruthy();
  3614. });
  3615. it('Italy', function() {
  3616. this.$iban.val('IT60X0542811101000000123456');
  3617. this.bv.validate();
  3618. expect(this.bv.isValidField('iban')).toBeTruthy();
  3619. });
  3620. it('Ivory Coast', function() {
  3621. this.$iban.val('CI05A00060174100178530011852');
  3622. this.bv.validate();
  3623. expect(this.bv.isValidField('iban')).toBeTruthy();
  3624. });
  3625. it('Jordan', function() {
  3626. this.$iban.val('JO94CBJO0010000000000131000302');
  3627. this.bv.validate();
  3628. expect(this.bv.isValidField('iban')).toBeTruthy();
  3629. });
  3630. it('Kazakhstan', function() {
  3631. this.$iban.val('KZ176010251000042993');
  3632. this.bv.validate();
  3633. expect(this.bv.isValidField('iban')).toBeTruthy();
  3634. });
  3635. it('Kuwait', function() {
  3636. this.$iban.val('KW74NBOK0000000000001000372151');
  3637. this.bv.validate();
  3638. expect(this.bv.isValidField('iban')).toBeTruthy();
  3639. });
  3640. it('Latvia', function() {
  3641. this.$iban.val('LV80BANK0000435195001');
  3642. this.bv.validate();
  3643. expect(this.bv.isValidField('iban')).toBeTruthy();
  3644. });
  3645. it('Lebanon', function() {
  3646. this.$iban.val('LB30099900000001001925579115');
  3647. this.bv.validate();
  3648. expect(this.bv.isValidField('iban')).toBeTruthy();
  3649. });
  3650. it('Liechtenstein', function() {
  3651. this.$iban.val('LI21088100002324013AA');
  3652. this.bv.validate();
  3653. expect(this.bv.isValidField('iban')).toBeTruthy();
  3654. });
  3655. it('Lithuania', function() {
  3656. this.$iban.val('LT121000011101001000');
  3657. this.bv.validate();
  3658. expect(this.bv.isValidField('iban')).toBeTruthy();
  3659. });
  3660. it('Luxembourg', function() {
  3661. this.$iban.val('LU280019400644750000');
  3662. this.bv.validate();
  3663. expect(this.bv.isValidField('iban')).toBeTruthy();
  3664. });
  3665. it('Macedonia', function() {
  3666. this.$iban.val('MK07300000000042425');
  3667. this.bv.validate();
  3668. expect(this.bv.isValidField('iban')).toBeTruthy();
  3669. });
  3670. it('Madagascar', function() {
  3671. this.$iban.val('MG4600005030010101914016056');
  3672. this.bv.validate();
  3673. expect(this.bv.isValidField('iban')).toBeTruthy();
  3674. });
  3675. it('Malta', function() {
  3676. this.$iban.val('MT84MALT011000012345MTLCAST001S');
  3677. this.bv.validate();
  3678. expect(this.bv.isValidField('iban')).toBeTruthy();
  3679. });
  3680. it('Mauritania', function() {
  3681. this.$iban.val('MR1300012000010000002037372');
  3682. this.bv.validate();
  3683. expect(this.bv.isValidField('iban')).toBeTruthy();
  3684. });
  3685. it('Mauritius', function() {
  3686. this.$iban.val('MU17BOMM0101101030300200000MUR');
  3687. this.bv.validate();
  3688. expect(this.bv.isValidField('iban')).toBeTruthy();
  3689. });
  3690. it('Mali', function() {
  3691. this.$iban.val('ML03D00890170001002120000447');
  3692. this.bv.validate();
  3693. expect(this.bv.isValidField('iban')).toBeTruthy();
  3694. });
  3695. it('Moldova', function() {
  3696. this.$iban.val('MD24AG000225100013104168');
  3697. this.bv.validate();
  3698. expect(this.bv.isValidField('iban')).toBeTruthy();
  3699. });
  3700. it('Monaco', function() {
  3701. this.$iban.val('MC5813488000010051108001292');
  3702. this.bv.validate();
  3703. expect(this.bv.isValidField('iban')).toBeTruthy();
  3704. });
  3705. it('Montenegro', function() {
  3706. this.$iban.val('ME25505000012345678951');
  3707. this.bv.validate();
  3708. expect(this.bv.isValidField('iban')).toBeTruthy();
  3709. });
  3710. it('Mozambique', function() {
  3711. this.$iban.val('MZ59000100000011834194157');
  3712. this.bv.validate();
  3713. expect(this.bv.isValidField('iban')).toBeTruthy();
  3714. });
  3715. it('Netherlands', function() {
  3716. this.$iban.val('NL91ABNA0417164300');
  3717. this.bv.validate();
  3718. expect(this.bv.isValidField('iban')).toBeTruthy();
  3719. });
  3720. it('Norway', function() {
  3721. this.$iban.val('NO9386011117947');
  3722. this.bv.validate();
  3723. expect(this.bv.isValidField('iban')).toBeTruthy();
  3724. });
  3725. it('Pakistan', function() {
  3726. this.$iban.val('PK24SCBL0000001171495101');
  3727. this.bv.validate();
  3728. expect(this.bv.isValidField('iban')).toBeTruthy();
  3729. });
  3730. it('Palestine', function() {
  3731. this.$iban.val('PS92PALS000000000400123456702');
  3732. this.bv.validate();
  3733. expect(this.bv.isValidField('iban')).toBeTruthy();
  3734. });
  3735. it('Poland', function() {
  3736. this.$iban.val('PL27114020040000300201355387');
  3737. this.bv.validate();
  3738. expect(this.bv.isValidField('iban')).toBeTruthy();
  3739. });
  3740. it('Portugal', function() {
  3741. this.$iban.val('PT50000201231234567890154');
  3742. this.bv.validate();
  3743. expect(this.bv.isValidField('iban')).toBeTruthy();
  3744. });
  3745. it('Qatar', function() {
  3746. this.$iban.val('QA58DOHB00001234567890ABCDEFG');
  3747. this.bv.validate();
  3748. expect(this.bv.isValidField('iban')).toBeTruthy();
  3749. });
  3750. it('Romania', function() {
  3751. this.$iban.val('RO49AAAA1B31007593840000');
  3752. this.bv.validate();
  3753. expect(this.bv.isValidField('iban')).toBeTruthy();
  3754. });
  3755. it('San Marino', function() {
  3756. this.$iban.val('SM86U0322509800000000270100');
  3757. this.bv.validate();
  3758. expect(this.bv.isValidField('iban')).toBeTruthy();
  3759. });
  3760. it('Saudi Arabia', function() {
  3761. this.$iban.val('SA0380000000608010167519');
  3762. this.bv.validate();
  3763. expect(this.bv.isValidField('iban')).toBeTruthy();
  3764. });
  3765. it('Senegal', function() {
  3766. this.$iban.val('SN12K00100152000025690007542');
  3767. this.bv.validate();
  3768. expect(this.bv.isValidField('iban')).toBeTruthy();
  3769. });
  3770. it('Serbia', function() {
  3771. this.$iban.val('RS35260005601001611379');
  3772. this.bv.validate();
  3773. expect(this.bv.isValidField('iban')).toBeTruthy();
  3774. });
  3775. it('Slovakia', function() {
  3776. this.$iban.val('SK3112000000198742637541');
  3777. this.bv.validate();
  3778. expect(this.bv.isValidField('iban')).toBeTruthy();
  3779. });
  3780. it('Slovenia', function() {
  3781. this.$iban.val('SI56191000000123438');
  3782. this.bv.validate();
  3783. expect(this.bv.isValidField('iban')).toBeTruthy();
  3784. });
  3785. it('Spain', function() {
  3786. this.$iban.val('ES9121000418450200051332');
  3787. this.bv.validate();
  3788. expect(this.bv.isValidField('iban')).toBeTruthy();
  3789. });
  3790. it('Sweden', function() {
  3791. this.$iban.val('SE3550000000054910000003');
  3792. this.bv.validate();
  3793. expect(this.bv.isValidField('iban')).toBeTruthy();
  3794. });
  3795. it('Switzerland', function() {
  3796. this.$iban.val('CH9300762011623852957');
  3797. this.bv.validate();
  3798. expect(this.bv.isValidField('iban')).toBeTruthy();
  3799. });
  3800. it('Tunisia', function() {
  3801. this.$iban.val('TN5914207207100707129648');
  3802. this.bv.validate();
  3803. expect(this.bv.isValidField('iban')).toBeTruthy();
  3804. });
  3805. it('Turkey', function() {
  3806. this.$iban.val('TR330006100519786457841326');
  3807. this.bv.validate();
  3808. expect(this.bv.isValidField('iban')).toBeTruthy();
  3809. });
  3810. it('United Arab Emirates', function() {
  3811. this.$iban.val('AE260211000000230064016');
  3812. this.bv.validate();
  3813. expect(this.bv.isValidField('iban')).toBeTruthy();
  3814. });
  3815. it('United Kingdom', function() {
  3816. this.$iban.val('GB29NWBK60161331926819');
  3817. this.bv.validate();
  3818. expect(this.bv.isValidField('iban')).toBeTruthy();
  3819. });
  3820. it('Virgin Islands, British', function() {
  3821. this.$iban.val('VG96VPVG0000012345678901');
  3822. this.bv.validate();
  3823. expect(this.bv.isValidField('iban')).toBeTruthy();
  3824. });
  3825. it('invalid checksum', function() {
  3826. this.$iban.val('TR330006100519786457841325');
  3827. this.bv.validate();
  3828. expect(this.bv.isValidField('iban')).toEqual(false);
  3829. });
  3830. });
  3831. describe('id', function() {
  3832. beforeEach(function() {
  3833. $([
  3834. '<form class="form-horizontal" id="idForm">',
  3835. '<div class="form-group">',
  3836. '<select class="form-control" name="country">',
  3837. '<option value="BA">Bosnia and Herzegovina</option>',
  3838. '<option value="BG">Bulgaria</option>',
  3839. '<option value="BR">Brazil</option>',
  3840. '<option value="CH">Switzerland</option>',
  3841. '<option value="CL">Chile</option>',
  3842. '<option value="CN">China</option>',
  3843. '<option value="CZ">Czech</option>',
  3844. '<option value="DK">Denmark</option>',
  3845. '<option value="EE">Estonia</option>',
  3846. '<option value="ES">Spain</option>',
  3847. '<option value="FI">Finland</option>',
  3848. '<option value="HR">Croatia</option>',
  3849. '<option value="IE">Ireland</option>',
  3850. '<option value="IS">Iceland</option>',
  3851. '<option value="LT">Lithuania</option>',
  3852. '<option value="LV">Latvia</option>',
  3853. '<option value="ME">Montenegro</option>',
  3854. '<option value="MK">Macedonia</option>',
  3855. '<option value="NL">Netherlands</option>',
  3856. '<option value="RO">Romania</option>',
  3857. '<option value="RS">Serbia</option>',
  3858. '<option value="SE">Sweden</option>',
  3859. '<option value="SI">Slovenia</option>',
  3860. '<option value="SK">Slovakia</option>',
  3861. '<option value="SM">San Marino</option>',
  3862. '<option value="TH">Thailand</option>',
  3863. '<option value="ZA">South Africa</option>',
  3864. '</select>',
  3865. '</div>',
  3866. '<div class="form-group">',
  3867. '<input class="form-control" type="text" name="id" data-bv-id />',
  3868. '</div>',
  3869. '</form>'
  3870. ].join('\n')).appendTo('body');
  3871. $('#idForm').bootstrapValidator();
  3872. /**
  3873. * @type {BootstrapValidator}
  3874. */
  3875. this.bv = $('#idForm').data('bootstrapValidator');
  3876. this.$country = this.bv.getFieldElements('country');
  3877. this.$id = this.bv.getFieldElements('id');
  3878. });
  3879. afterEach(function() {
  3880. $('#idForm').bootstrapValidator('destroy').remove();
  3881. });
  3882. it('dynamic country', function() {
  3883. this.$id.attr('data-bv-id-country', 'country');
  3884. this.bv.destroy();
  3885. this.bv = $('#idForm').bootstrapValidator().data('bootstrapValidator');
  3886. this.$country.val('BG');
  3887. this.$id.val('7552010005');
  3888. this.bv.validate();
  3889. expect(this.bv.isValid()).toBeTruthy();
  3890. this.bv.resetForm();
  3891. this.$country.val('BR');
  3892. this.$id.val('231.002.999-00');
  3893. this.bv.validate();
  3894. expect(this.bv.isValid()).toEqual(false);
  3895. });
  3896. it('Bulgarian national identification number (EGN)', function() {
  3897. this.bv.updateOption('id', 'id', 'country', 'BG');
  3898. // Valid samples
  3899. var validSamples = ['7523169263', '8032056031', '803205 603 1', '8001010008', '7501020018', '7552010005', '7542011030'];
  3900. for (var i in validSamples) {
  3901. this.bv.resetForm();
  3902. this.$id.val(validSamples[i]);
  3903. this.bv.validate();
  3904. expect(this.bv.isValid()).toBeTruthy();
  3905. }
  3906. // Invalid samples
  3907. var invalidSamples = ['8019010008'];
  3908. for (i in invalidSamples) {
  3909. this.bv.resetForm();
  3910. this.$id.val(invalidSamples[i]);
  3911. this.bv.validate();
  3912. expect(this.bv.isValid()).toEqual(false);
  3913. }
  3914. });
  3915. it('Brazilian national identification number (CPF)', function() {
  3916. this.bv.updateOption('id', 'id', 'country', 'BR');
  3917. // Valid samples
  3918. var validSamples = ['39053344705', '390.533.447-05', '111.444.777-35'];
  3919. for (var i in validSamples) {
  3920. this.bv.resetForm();
  3921. this.$id.val(validSamples[i]);
  3922. this.bv.validate();
  3923. expect(this.bv.isValid()).toBeTruthy();
  3924. }
  3925. // Invalid samples
  3926. var invalidSamples = ['231.002.999-00'];
  3927. for (i in invalidSamples) {
  3928. this.bv.resetForm();
  3929. this.$id.val(invalidSamples[i]);
  3930. this.bv.validate();
  3931. expect(this.bv.isValid()).toEqual(false);
  3932. }
  3933. });
  3934. it('Swiss Social Security Number (AHV-Nr/No AVS)', function() {
  3935. this.bv.updateOption('id', 'id', 'country', 'CH');
  3936. // Valid samples
  3937. var validSamples = ['756.1234.5678.95', '7561234567895'];
  3938. for (var i in validSamples) {
  3939. this.bv.resetForm();
  3940. this.$id.val(validSamples[i]);
  3941. this.bv.validate();
  3942. expect(this.bv.isValid()).toBeTruthy();
  3943. }
  3944. });
  3945. it('Chilean national identification number (RUN/RUT)', function() {
  3946. this.bv.updateOption('id', 'id', 'country', 'CL');
  3947. // Valid samples
  3948. var validSamples = ['76086428-5', '22060449-7', '12531909-2','12937893-K','12937893-k'];
  3949. for (var i in validSamples) {
  3950. this.bv.resetForm();
  3951. this.$id.val(validSamples[i]);
  3952. this.bv.validate();
  3953. expect(this.bv.isValid()).toBeTruthy();
  3954. }
  3955. });
  3956. // #793
  3957. it('Chinese citizen identification number', function() {
  3958. this.bv.updateOption('id', 'id', 'country', 'CN');
  3959. // Valid samples
  3960. var validSamples = ['450202201409072332', '22011219930407001X', '110108601017023'];
  3961. for (var i in validSamples) {
  3962. this.bv.resetForm();
  3963. this.$id.val(validSamples[i]);
  3964. this.bv.validate();
  3965. expect(this.bv.isValid()).toBeTruthy();
  3966. }
  3967. // Invalid samples
  3968. var invalidSamples = ['999999199304070016', '220112190002290016', '220112199304070019', '999999601017023', '110108999999023'];
  3969. for (i in invalidSamples) {
  3970. this.bv.resetForm();
  3971. this.$id.val(invalidSamples[i]);
  3972. this.bv.validate();
  3973. expect(this.bv.isValid()).toEqual(false);
  3974. }
  3975. });
  3976. it('Czech national identification number (RC)', function() {
  3977. this.bv.updateOption('id', 'id', 'country', 'CZ');
  3978. // Valid samples
  3979. var validSamples = ['7103192745', '991231123'];
  3980. for (var i in validSamples) {
  3981. this.bv.resetForm();
  3982. this.$id.val(validSamples[i]);
  3983. this.bv.validate();
  3984. expect(this.bv.isValid()).toBeTruthy();
  3985. }
  3986. // Invalid samples
  3987. var invalidSamples = ['1103492745', '590312123'];
  3988. for (i in invalidSamples) {
  3989. this.bv.resetForm();
  3990. this.$id.val(invalidSamples[i]);
  3991. this.bv.validate();
  3992. expect(this.bv.isValid()).toEqual(false);
  3993. }
  3994. });
  3995. it('Danish Personal Identification number (CPR)', function() {
  3996. this.bv.updateOption('id', 'id', 'country', 'DK');
  3997. // Valid samples
  3998. var validSamples = ['2110625629', '211062-5629'];
  3999. for (var i in validSamples) {
  4000. this.bv.resetForm();
  4001. this.$id.val(validSamples[i]);
  4002. this.bv.validate();
  4003. expect(this.bv.isValid()).toBeTruthy();
  4004. }
  4005. // Invalid samples
  4006. var invalidSamples = ['511062-5629'];
  4007. for (i in invalidSamples) {
  4008. this.bv.resetForm();
  4009. this.$id.val(invalidSamples[i]);
  4010. this.bv.validate();
  4011. expect(this.bv.isValid()).toEqual(false);
  4012. }
  4013. });
  4014. it('Estonian Personal Identification Code (isikukood)', function() {
  4015. this.bv.updateOption('id', 'id', 'country', 'EE');
  4016. // Valid samples
  4017. var validSamples = ['37605030299'];
  4018. for (var i in validSamples) {
  4019. this.bv.resetForm();
  4020. this.$id.val(validSamples[i]);
  4021. this.bv.validate();
  4022. expect(this.bv.isValid()).toBeTruthy();
  4023. }
  4024. });
  4025. it('Spanish personal identity code (DNI/NIE)', function() {
  4026. this.bv.updateOption('id', 'id', 'country', 'ES');
  4027. // Valid samples
  4028. var validSamples = ['54362315K', '54362315-K', 'X2482300W', 'X-2482300W', 'X-2482300-W'];
  4029. for (var i in validSamples) {
  4030. this.bv.resetForm();
  4031. this.$id.val(validSamples[i]);
  4032. this.bv.validate();
  4033. expect(this.bv.isValid()).toBeTruthy();
  4034. }
  4035. // Invalid samples
  4036. var invalidSamples = ['54362315Z', 'X-2482300A'];
  4037. for (i in invalidSamples) {
  4038. this.bv.resetForm();
  4039. this.$id.val(invalidSamples[i]);
  4040. this.bv.validate();
  4041. expect(this.bv.isValid()).toEqual(false);
  4042. }
  4043. });
  4044. it('Finnish Personal Identity Code (HETU)', function() {
  4045. this.bv.updateOption('id', 'id', 'country', 'FI');
  4046. // Valid samples
  4047. var validSamples = ['311280-888Y', '131052-308T'];
  4048. for (var i in validSamples) {
  4049. this.bv.resetForm();
  4050. this.$id.val(validSamples[i]);
  4051. this.bv.validate();
  4052. expect(this.bv.isValid()).toBeTruthy();
  4053. }
  4054. // Invalid samples
  4055. var invalidSamples = ['131052-308U', '310252-308Y'];
  4056. for (i in invalidSamples) {
  4057. this.bv.resetForm();
  4058. this.$id.val(invalidSamples[i]);
  4059. this.bv.validate();
  4060. expect(this.bv.isValid()).toEqual(false);
  4061. }
  4062. });
  4063. it('Croatian personal identification number (OIB)', function() {
  4064. this.bv.updateOption('id', 'id', 'country', 'HR');
  4065. // Valid samples
  4066. var validSamples = ['33392005961'];
  4067. for (var i in validSamples) {
  4068. this.bv.resetForm();
  4069. this.$id.val(validSamples[i]);
  4070. this.bv.validate();
  4071. expect(this.bv.isValid()).toBeTruthy();
  4072. }
  4073. // Invalid samples
  4074. var invalidSamples = ['33392005962'];
  4075. for (i in invalidSamples) {
  4076. this.bv.resetForm();
  4077. this.$id.val(invalidSamples[i]);
  4078. this.bv.validate();
  4079. expect(this.bv.isValid()).toEqual(false);
  4080. }
  4081. });
  4082. it('Irish Personal Public Service Number (PPS)', function() {
  4083. this.bv.updateOption('id', 'id', 'country', 'IE');
  4084. // Valid samples
  4085. var validSamples = ['6433435F', '6433435FT', '6433435FW', '6433435OA', '6433435IH', '1234567TW', '1234567FA'];
  4086. for (var i in validSamples) {
  4087. this.bv.resetForm();
  4088. this.$id.val(validSamples[i]);
  4089. this.bv.validate();
  4090. expect(this.bv.isValid()).toBeTruthy();
  4091. }
  4092. // Invalid samples
  4093. var invalidSamples = ['6433435E', '6433435VH'];
  4094. for (i in invalidSamples) {
  4095. this.bv.resetForm();
  4096. this.$id.val(invalidSamples[i]);
  4097. this.bv.validate();
  4098. expect(this.bv.isValid()).toEqual(false);
  4099. }
  4100. });
  4101. it('Iceland national identification number (Kennitala)', function() {
  4102. this.bv.updateOption('id', 'id', 'country', 'IS');
  4103. // Valid samples
  4104. var validSamples = ['120174-3399', '1201743399', '0902862349'];
  4105. for (var i in validSamples) {
  4106. this.bv.resetForm();
  4107. this.$id.val(validSamples[i]);
  4108. this.bv.validate();
  4109. expect(this.bv.isValid()).toBeTruthy();
  4110. }
  4111. });
  4112. it('Lithuanian Personal Code (Asmens kodas)', function() {
  4113. this.bv.updateOption('id', 'id', 'country', 'LT');
  4114. // Valid samples
  4115. var validSamples = ['38703181745'];
  4116. for (var i in validSamples) {
  4117. this.bv.resetForm();
  4118. this.$id.val(validSamples[i]);
  4119. this.bv.validate();
  4120. expect(this.bv.isValid()).toBeTruthy();
  4121. }
  4122. // Invalid samples
  4123. var invalidSamples = ['38703181746', '78703181745', '38703421745'];
  4124. for (i in invalidSamples) {
  4125. this.bv.resetForm();
  4126. this.$id.val(invalidSamples[i]);
  4127. this.bv.validate();
  4128. expect(this.bv.isValid()).toEqual(false);
  4129. }
  4130. });
  4131. it('Latvian Personal Code (Personas kods)', function() {
  4132. this.bv.updateOption('id', 'id', 'country', 'LV');
  4133. // Valid samples
  4134. var validSamples = ['161175-19997', '16117519997'];
  4135. for (var i in validSamples) {
  4136. this.bv.resetForm();
  4137. this.$id.val(validSamples[i]);
  4138. this.bv.validate();
  4139. expect(this.bv.isValid()).toBeTruthy();
  4140. }
  4141. // Invalid samples
  4142. var invalidSamples = ['161375-19997'];
  4143. for (i in invalidSamples) {
  4144. this.bv.resetForm();
  4145. this.$id.val(invalidSamples[i]);
  4146. this.bv.validate();
  4147. expect(this.bv.isValid()).toEqual(false);
  4148. }
  4149. });
  4150. it('Dutch national identification number (BSN)', function() {
  4151. this.bv.updateOption('id', 'id', 'country', 'NL');
  4152. // Valid samples
  4153. var validSamples = ['111222333', '941331490', '9413.31.490'];
  4154. for (var i in validSamples) {
  4155. this.bv.resetForm();
  4156. this.$id.val(validSamples[i]);
  4157. this.bv.validate();
  4158. expect(this.bv.isValid()).toBeTruthy();
  4159. }
  4160. // Invalid samples
  4161. var invalidSamples = ['111252333'];
  4162. for (i in invalidSamples) {
  4163. this.bv.resetForm();
  4164. this.$id.val(invalidSamples[i]);
  4165. this.bv.validate();
  4166. expect(this.bv.isValid()).toEqual(false);
  4167. }
  4168. });
  4169. it('Romanian numerical personal code (CNP)', function() {
  4170. this.bv.updateOption('id', 'id', 'country', 'RO');
  4171. // Valid samples
  4172. var validSamples = ['1630615123457', '1800101221144'];
  4173. for (var i in validSamples) {
  4174. this.bv.resetForm();
  4175. this.$id.val(validSamples[i]);
  4176. this.bv.validate();
  4177. expect(this.bv.isValid()).toBeTruthy();
  4178. }
  4179. // Invalid samples
  4180. var invalidSamples = ['8800101221144', '1632215123457', '1630615123458'];
  4181. for (i in invalidSamples) {
  4182. this.bv.resetForm();
  4183. this.$id.val(invalidSamples[i]);
  4184. this.bv.validate();
  4185. expect(this.bv.isValid()).toEqual(false);
  4186. }
  4187. });
  4188. it('Swedish personal identity number (personnummer)', function() {
  4189. this.bv.updateOption('id', 'id', 'country', 'SE');
  4190. // Valid samples
  4191. var validSamples = ['8112289874', '811228-9874', '811228+9874'];
  4192. for (var i in validSamples) {
  4193. this.bv.resetForm();
  4194. this.$id.val(validSamples[i]);
  4195. this.bv.validate();
  4196. expect(this.bv.isValid()).toBeTruthy();
  4197. }
  4198. // Invalid samples
  4199. var invalidSamples = ['811228-9873'];
  4200. for (i in invalidSamples) {
  4201. this.bv.resetForm();
  4202. this.$id.val(invalidSamples[i]);
  4203. this.bv.validate();
  4204. expect(this.bv.isValid()).toEqual(false);
  4205. }
  4206. });
  4207. it('Slovak national identifier number (RC)', function() {
  4208. this.bv.updateOption('id', 'id', 'country', 'SK');
  4209. // Valid samples
  4210. var validSamples = ['7103192745', '991231123'];
  4211. for (var i in validSamples) {
  4212. this.bv.resetForm();
  4213. this.$id.val(validSamples[i]);
  4214. this.bv.validate();
  4215. expect(this.bv.isValid()).toBeTruthy();
  4216. }
  4217. // Invalid samples
  4218. var invalidSamples = ['7103192746', '1103492745'];
  4219. for (i in invalidSamples) {
  4220. this.bv.resetForm();
  4221. this.$id.val(invalidSamples[i]);
  4222. this.bv.validate();
  4223. expect(this.bv.isValid()).toEqual(false);
  4224. }
  4225. });
  4226. it('South African ID', function() {
  4227. this.bv.updateOption('id', 'id', 'country', 'ZA');
  4228. // Valid samples
  4229. var validSamples = ['8001015009087'];
  4230. for (var i in validSamples) {
  4231. this.bv.resetForm();
  4232. this.$id.val(validSamples[i]);
  4233. this.bv.validate();
  4234. expect(this.bv.isValid()).toBeTruthy();
  4235. }
  4236. // Invalid samples
  4237. var invalidSamples = ['8001015009287', '8001015009086'];
  4238. for (i in invalidSamples) {
  4239. this.bv.resetForm();
  4240. this.$id.val(invalidSamples[i]);
  4241. this.bv.validate();
  4242. expect(this.bv.isValid()).toEqual(false);
  4243. }
  4244. });
  4245. it('Thailand citizen number', function() {
  4246. this.bv.updateOption('id', 'id', 'country', 'TH');
  4247. // Valid samples
  4248. var validSamples = ['7145620509547', '3688699975685', '2368719339716'];
  4249. for (var i in validSamples) {
  4250. this.bv.resetForm();
  4251. this.$id.val(validSamples[i]);
  4252. this.bv.validate();
  4253. expect(this.bv.isValid()).toBeTruthy();
  4254. }
  4255. // Invalid samples
  4256. var invalidSamples = ['1100800092310'];
  4257. for (i in invalidSamples) {
  4258. this.bv.resetForm();
  4259. this.$id.val(invalidSamples[i]);
  4260. this.bv.validate();
  4261. expect(this.bv.isValid()).toEqual(false);
  4262. }
  4263. });
  4264. });
  4265. describe('imo', function() {
  4266. beforeEach(function() {
  4267. $([
  4268. '<form class="form-horizontal" id="imoForm">',
  4269. '<div class="form-group">',
  4270. '<input type="text" name="imo" data-bv-imo />',
  4271. '</div>',
  4272. '</form>'
  4273. ].join('\n')).appendTo('body');
  4274. $('#imoForm').bootstrapValidator();
  4275. this.bv = $('#imoForm').data('bootstrapValidator');
  4276. this.$imo = this.bv.getFieldElements('imo');
  4277. });
  4278. afterEach(function() {
  4279. $('#imoForm').bootstrapValidator('destroy').remove();
  4280. });
  4281. it('Valid IMO (upper)', function() {
  4282. this.bv.resetForm();
  4283. this.$imo.val('IMO 9074729');
  4284. this.bv.validate();
  4285. expect(this.bv.isValid()).toBeTruthy();
  4286. });
  4287. it('Valid IMO (lower)', function() {
  4288. this.bv.resetForm();
  4289. this.$imo.val('imo 9074729');
  4290. this.bv.validate();
  4291. expect(this.bv.isValid()).toBeTruthy();
  4292. });
  4293. it('Invalid IMO (bad format)', function() {
  4294. this.bv.resetForm();
  4295. this.$imo.val('9074729');
  4296. this.bv.validate();
  4297. expect(this.bv.isValid()).toBeFalsy();
  4298. });
  4299. it('Invalid IMO (bad check digit)', function() {
  4300. this.bv.resetForm();
  4301. this.$imo.val('IMO 9074728');
  4302. this.bv.validate();
  4303. expect(this.bv.isValid()).toBeFalsy();
  4304. });
  4305. });
  4306. describe('ip', function() {
  4307. beforeEach(function() {
  4308. $([
  4309. '<form class="form-horizontal" id="ipForm">',
  4310. '<div class="form-group">',
  4311. '<input type="text" name="ipv4" data-bv-ip data-bv-ip-ipv6="false" />',
  4312. '</div>',
  4313. '<div class="form-group">',
  4314. '<input type="text" name="ipv6" data-bv-ip data-bv-ip-ipv4="false" />',
  4315. '</div>',
  4316. '<div class="form-group">',
  4317. '<input type="text" name="both" data-bv-ip />',
  4318. '</div>',
  4319. '</form>'
  4320. ].join('\n')).appendTo('body');
  4321. $('#ipForm').bootstrapValidator();
  4322. this.bv = $('#ipForm').data('bootstrapValidator');
  4323. this.$ipv4 = this.bv.getFieldElements('ipv4');
  4324. this.$ipv6 = this.bv.getFieldElements('ipv6');
  4325. this.$both = this.bv.getFieldElements('both');
  4326. });
  4327. afterEach(function() {
  4328. $('#ipForm').bootstrapValidator('destroy').remove();
  4329. });
  4330. it('Valid ipv4', function() {
  4331. this.$ipv4.val('0.0.0.0');
  4332. this.bv.validate();
  4333. expect(this.bv.isValid()).toBeTruthy();
  4334. this.bv.resetForm();
  4335. this.$ipv4.val('192.168.1.1');
  4336. this.bv.validate();
  4337. expect(this.bv.isValid()).toBeTruthy();
  4338. this.bv.resetForm();
  4339. this.$ipv4.val('255.255.255.255');
  4340. this.bv.validate();
  4341. expect(this.bv.isValid()).toBeTruthy();
  4342. });
  4343. it('Invalid ipv4', function() {
  4344. this.$ipv4.val('10.168.0001.100'); // extra 0 not allowed
  4345. this.bv.validate();
  4346. expect(this.bv.isValid()).toEqual(false);
  4347. this.bv.resetForm();
  4348. this.$ipv4.val('0.0.0.256'); // 256 not allowed, max is 255
  4349. this.bv.validate();
  4350. expect(this.bv.isValid()).toEqual(false);
  4351. this.bv.resetForm();
  4352. this.$ipv4.val('256.255.255.255'); // max is 255.255.255.255
  4353. this.bv.validate();
  4354. expect(this.bv.isValid()).toEqual(false);
  4355. this.bv.resetForm();
  4356. this.$ipv4.val('192.168. 224.0'); // internal space
  4357. this.bv.validate();
  4358. expect(this.bv.isValid()).toEqual(false);
  4359. this.bv.resetForm();
  4360. this.$ipv4.val('192.168.224.0 1'); // junk after valid address
  4361. this.bv.validate();
  4362. expect(this.bv.isValid()).toEqual(false);
  4363. });
  4364. it('Valid ipv6', function() {
  4365. this.$ipv6.val('0000:0000:0000:0000:0000:0000:0000:0000');
  4366. this.bv.validate();
  4367. expect(this.bv.isValid()).toBeTruthy();
  4368. this.bv.resetForm();
  4369. this.$ipv6.val('fe00::1');
  4370. this.bv.validate();
  4371. expect(this.bv.isValid()).toBeTruthy();
  4372. this.bv.resetForm();
  4373. this.$ipv6.val('fe80::217:f2ff:fe07:ed62');
  4374. this.bv.validate();
  4375. expect(this.bv.isValid()).toBeTruthy();
  4376. this.bv.resetForm();
  4377. this.$ipv6.val('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff');
  4378. this.bv.validate();
  4379. expect(this.bv.isValid()).toBeTruthy();
  4380. });
  4381. it('Invalid ipv6', function() {
  4382. this.$ipv6.val('02001:0000:1234:0000:0000:C1C0:ABCD:0876'); // extra 0 not allowed
  4383. this.bv.validate();
  4384. expect(this.bv.isValid()).toEqual(false);
  4385. this.bv.resetForm();
  4386. this.$ipv6.val('2001:0000:1234:0000:00001:C1C0:ABCD:0876'); // extra 0 not allowed
  4387. this.bv.validate();
  4388. expect(this.bv.isValid()).toEqual(false);
  4389. this.bv.resetForm();
  4390. this.$ipv6.val('2001:0000:1234: 0000:0000:C1C0:ABCD:0876'); // internal space
  4391. this.bv.validate();
  4392. expect(this.bv.isValid()).toEqual(false);
  4393. this.bv.resetForm();
  4394. this.$ipv6.val('2001:0000:1234:0000:0000:C1C0:ABCD:0876 0'); // junk after valid address
  4395. this.bv.validate();
  4396. expect(this.bv.isValid()).toEqual(false);
  4397. this.bv.resetForm();
  4398. this.$ipv6.val('3ffe:0b00:0000:0001:0000:0000:000a'); // seven segment
  4399. this.bv.validate();
  4400. expect(this.bv.isValid()).toEqual(false);
  4401. this.bv.resetForm();
  4402. this.$ipv6.val('FF02:0000:0000:0000:0000:0000:0000:0000:0001'); // nine segment
  4403. this.bv.validate();
  4404. expect(this.bv.isValid()).toEqual(false);
  4405. this.bv.resetForm();
  4406. this.$ipv6.val('::1111:2222:3333:4444:5555:6666::'); // double "::"
  4407. this.bv.validate();
  4408. expect(this.bv.isValid()).toEqual(false);
  4409. this.bv.resetForm();
  4410. this.$ipv6.val('3ffe:b00::1::a'); // double "::"
  4411. this.bv.validate();
  4412. expect(this.bv.isValid()).toEqual(false);
  4413. });
  4414. it('Both', function() {
  4415. this.$both.val('255.255.255.255'); // valid
  4416. this.bv.validate();
  4417. expect(this.bv.isValid()).toBeTruthy();
  4418. this.bv.resetForm();
  4419. this.$both.val('256.0.0.0'); // 256 not allowed, max is 255
  4420. this.bv.validate();
  4421. expect(this.bv.isValid()).toEqual(false);
  4422. this.bv.resetForm();
  4423. this.$both.val('2001:0db8:0000:85a3:0000:0000:ac1f:8001'); // valid
  4424. this.bv.validate();
  4425. expect(this.bv.isValid()).toBeTruthy();
  4426. this.bv.resetForm();
  4427. this.$both.val('2001:0000:1234:0000:0000:C1C0:ABCD:0876 0'); // junk after valid address
  4428. this.bv.validate();
  4429. expect(this.bv.isValid()).toEqual(false);
  4430. });
  4431. });
  4432. describe('isbn', function() {
  4433. beforeEach(function() {
  4434. var html = [
  4435. '<div class="container">',
  4436. '<form class="form-horizontal" id="isbnForm">',
  4437. '<div class="form-group">',
  4438. '<input type="text" name="isbn" data-bv-isbn />',
  4439. '</div>',
  4440. '</form>',
  4441. '</div>'
  4442. ].join('\n');
  4443. $(html).appendTo('body');
  4444. $('#isbnForm').bootstrapValidator();
  4445. this.bv = $('#isbnForm').data('bootstrapValidator');
  4446. this.$isbn = this.bv.getFieldElements('isbn');
  4447. });
  4448. afterEach(function() {
  4449. $('#isbnForm').bootstrapValidator('destroy').parent().remove();
  4450. });
  4451. it('isbn10 hyphen', function() {
  4452. var samples = ['99921-58-10-7', '9971-5-0210-0', '960-425-059-0', '80-902734-1-6'];
  4453. for (var i in samples) {
  4454. this.$isbn.val(samples[i]);
  4455. this.bv.validate();
  4456. expect(this.bv.isValidField('isbn')).toBeTruthy();
  4457. }
  4458. });
  4459. it('isbn10 space', function() {
  4460. var samples = ['85 359 0277 5', '1 84356 028 3', '0 684 84328 5', '0 85131 041 9', '0 943396 04 2'];
  4461. for (var i in samples) {
  4462. this.$isbn.val(samples[i]);
  4463. this.bv.validate();
  4464. expect(this.bv.isValidField('isbn')).toBeTruthy();
  4465. }
  4466. });
  4467. it('isbn10 hyphen with X', function() {
  4468. var samples = ['0-8044-2957-X', '0-9752298-0-X'];
  4469. for (var i in samples) {
  4470. this.$isbn.val(samples[i]);
  4471. this.bv.validate();
  4472. expect(this.bv.isValidField('isbn')).toBeTruthy();
  4473. }
  4474. });
  4475. it('isbn10 invalid check digit', function() {
  4476. this.$isbn.val('99921-58-10-6');
  4477. this.bv.validate();
  4478. expect(this.bv.isValidField('isbn')).toEqual(false);
  4479. });
  4480. it('isbn13', function() {
  4481. this.$isbn.val('978-0-306-40615-7');
  4482. this.bv.validate();
  4483. expect(this.bv.isValidField('isbn')).toBeTruthy();
  4484. });
  4485. it('isbn13 invalid check digit', function() {
  4486. this.$isbn.val('978-0-306-40615-6');
  4487. this.bv.validate();
  4488. expect(this.bv.isValidField('isbn')).toEqual(false);
  4489. });
  4490. });
  4491. describe('isin', function() {
  4492. beforeEach(function() {
  4493. var html = [
  4494. '<div class="container">',
  4495. '<form class="form-horizontal" id="isinForm">',
  4496. '<div class="form-group">',
  4497. '<input type="text" name="isin" data-bv-isin />',
  4498. '</div>',
  4499. '</form>',
  4500. '</div>'
  4501. ].join('\n');
  4502. $(html).appendTo('body');
  4503. $('#isinForm').bootstrapValidator();
  4504. this.bv = $('#isinForm').data('bootstrapValidator');
  4505. this.$isin = this.bv.getFieldElements('isin');
  4506. });
  4507. afterEach(function() {
  4508. $('#isinForm').bootstrapValidator('destroy').parent().remove();
  4509. });
  4510. it('valid', function() {
  4511. var samples = ['US0378331005', 'AU0000XVGZA3', 'GB0002634946'];
  4512. for (var i in samples) {
  4513. this.$isin.val(samples[i]);
  4514. this.bv.validate();
  4515. expect(this.bv.isValidField('isin')).toBeTruthy();
  4516. }
  4517. });
  4518. it('invalid country code', function() {
  4519. this.$isin.val('AA0000XVGZA3');
  4520. this.bv.validate();
  4521. expect(this.bv.isValidField('isin')).toEqual(false);
  4522. });
  4523. it('contains only digits and alphabet', function() {
  4524. this.$isin.val('US12345ABC@#$');
  4525. this.bv.validate();
  4526. expect(this.bv.isValidField('isin')).toEqual(false);
  4527. });
  4528. it('invalid length', function() {
  4529. this.$isin.val('US1234567');
  4530. this.bv.validate();
  4531. expect(this.bv.isValidField('isin')).toEqual(false);
  4532. });
  4533. it('invalid check digit', function() {
  4534. this.$isin.val('US0378331004');
  4535. this.bv.validate();
  4536. expect(this.bv.isValidField('isin')).toEqual(false);
  4537. });
  4538. });
  4539. describe('ismn', function() {
  4540. beforeEach(function() {
  4541. var html = [
  4542. '<div class="container">',
  4543. '<form class="form-horizontal" id="ismnForm">',
  4544. '<div class="form-group">',
  4545. '<input type="text" name="ismn" data-bv-ismn />',
  4546. '</div>',
  4547. '</form>',
  4548. '</div>'
  4549. ].join('\n');
  4550. $(html).appendTo('body');
  4551. $('#ismnForm').bootstrapValidator();
  4552. this.bv = $('#ismnForm').data('bootstrapValidator');
  4553. this.$ismn = this.bv.getFieldElements('ismn');
  4554. });
  4555. afterEach(function() {
  4556. $('#ismnForm').bootstrapValidator('destroy').parent().remove();
  4557. });
  4558. it('valid start with M', function() {
  4559. this.$ismn.val('M230671187');
  4560. this.bv.validate();
  4561. expect(this.bv.isValidField('ismn')).toBeTruthy();
  4562. });
  4563. it('valid start with 979', function() {
  4564. this.$ismn.val('9790060115615');
  4565. this.bv.validate();
  4566. expect(this.bv.isValidField('ismn')).toBeTruthy();
  4567. });
  4568. it('valid contains spaces', function() {
  4569. this.$ismn.val('979 0 3452 4680 5');
  4570. this.bv.validate();
  4571. expect(this.bv.isValidField('ismn')).toBeTruthy();
  4572. });
  4573. it('valid contains dashes', function() {
  4574. this.$ismn.val('979-0-0601-1561-5');
  4575. this.bv.validate();
  4576. expect(this.bv.isValidField('ismn')).toBeTruthy();
  4577. });
  4578. it('invalid format', function() {
  4579. this.$ismn.val('N123456789');
  4580. this.bv.validate();
  4581. expect(this.bv.isValidField('ismn')).toEqual(false);
  4582. });
  4583. it('invalid check digit', function() {
  4584. this.$ismn.val('9790060115614');
  4585. this.bv.validate();
  4586. expect(this.bv.isValidField('ismn')).toEqual(false);
  4587. });
  4588. });
  4589. describe('issn', function() {
  4590. beforeEach(function() {
  4591. var html = [
  4592. '<div class="container">',
  4593. '<form class="form-horizontal" id="issnForm">',
  4594. '<div class="form-group">',
  4595. '<input type="text" name="issn" data-bv-issn />',
  4596. '</div>',
  4597. '</form>',
  4598. '</div>'
  4599. ].join('\n');
  4600. $(html).appendTo('body');
  4601. $('#issnForm').bootstrapValidator();
  4602. this.bv = $('#issnForm').data('bootstrapValidator');
  4603. this.$issn = this.bv.getFieldElements('issn');
  4604. });
  4605. afterEach(function() {
  4606. $('#issnForm').bootstrapValidator('destroy').parent().remove();
  4607. });
  4608. it('valid', function() {
  4609. var samples = ['0378-5955', '0024-9319', '0032-1478'];
  4610. for (var i in samples) {
  4611. this.$issn.val(samples[i]);
  4612. this.bv.validate();
  4613. expect(this.bv.isValidField('issn')).toBeTruthy();
  4614. }
  4615. });
  4616. it('not contains hyphen', function() {
  4617. this.$issn.val('03785955');
  4618. this.bv.validate();
  4619. expect(this.bv.isValidField('issn')).toEqual(false);
  4620. });
  4621. it('contains only digits, X', function() {
  4622. this.$issn.val('1234-566A');
  4623. this.bv.validate();
  4624. expect(this.bv.isValidField('issn')).toEqual(false);
  4625. });
  4626. it('invalid check sum', function() {
  4627. this.$issn.val('0032-147X');
  4628. this.bv.validate();
  4629. expect(this.bv.isValidField('issn')).toEqual(false);
  4630. });
  4631. });
  4632. describe('keywordColor', function() {
  4633. beforeEach(function() {
  4634. var html = [
  4635. '<div class="container">',
  4636. '<form class="form-horizontal" id="keywordColorForm">',
  4637. '<div class="form-group">',
  4638. '<input type="text" name="keyword" data-bv-keywordcolor />',
  4639. '</div>',
  4640. '</form>',
  4641. '</div>'
  4642. ].join('\n');
  4643. $(html).appendTo('body');
  4644. $('#keywordColorForm').bootstrapValidator();
  4645. this.bv = $('#keywordColorForm').data('bootstrapValidator');
  4646. this.$keywordColor = this.bv.getFieldElements('keyword');
  4647. });
  4648. afterEach(function() {
  4649. $('#keywordColorForm').bootstrapValidator('destroy').parent().remove();
  4650. });
  4651. it('accept transparent', function() {
  4652. this.$keywordColor.val('transparent');
  4653. this.bv.validate();
  4654. expect(this.bv.isValidField('keyword')).toBeTruthy();
  4655. });
  4656. it('accept blueviolet', function() {
  4657. this.$keywordColor.val('transparent');
  4658. this.bv.validate();
  4659. expect(this.bv.isValidField('keyword')).toBeTruthy();
  4660. });
  4661. it('reject combined keywords', function() {
  4662. this.$keywordColor.val('blueviolet red');
  4663. this.bv.validate();
  4664. expect(this.bv.isValidField('keyword')).toEqual(false);
  4665. });
  4666. it('reject shady', function() {
  4667. this.$keywordColor.val('shady');
  4668. this.bv.validate();
  4669. expect(this.bv.isValidField('keyword')).toEqual(false);
  4670. });
  4671. it('reject blueish', function() {
  4672. this.$keywordColor.val('blueish');
  4673. this.bv.validate();
  4674. expect(this.bv.isValidField('keyword')).toEqual(false);
  4675. });
  4676. });
  4677. function lessThanCompare() {
  4678. var compareTo = $('#lessThanForm').find('[name="maxAge"]').val();
  4679. $('#msg').html('lessThanCompare() called; compare to ' + compareTo);
  4680. return compareTo;
  4681. };
  4682. TestSuite = $.extend({}, TestSuite, {
  4683. lessThan: {
  4684. compareTo: function(value, validator, $field) {
  4685. var compareTo = $('#lessThanForm').find('[name="maxAge"]').val();
  4686. $('#msg').html('TestSuite.lessThan.compareTo() called; compare to ' + compareTo);
  4687. return compareTo;
  4688. }
  4689. }
  4690. });
  4691. describe('lessThan', function() {
  4692. beforeEach(function() {
  4693. $([
  4694. '<form class="form-horizontal" id="lessThanForm">',
  4695. '<div id="msg"></div>',
  4696. '<div class="form-group">',
  4697. '<input type="text" name="maxAge" />',
  4698. '</div>',
  4699. '<div class="form-group">',
  4700. '<input type="text" name="age" data-bv-lessthan data-bv-lessthan-value="100" />',
  4701. '</div>',
  4702. '</form>'
  4703. ].join('\n')).appendTo('body');
  4704. $('#lessThanForm').bootstrapValidator();
  4705. this.bv = $('#lessThanForm').data('bootstrapValidator');
  4706. this.$maxAge = this.bv.getFieldElements('maxAge');
  4707. this.$age = this.bv.getFieldElements('age');
  4708. });
  4709. afterEach(function() {
  4710. $('#lessThanForm').bootstrapValidator('destroy').remove();
  4711. });
  4712. it('not a number', function() {
  4713. this.$age.val('20abc');
  4714. this.bv.validate();
  4715. expect(this.bv.isValid()).toEqual(false);
  4716. });
  4717. it('compare to value', function() {
  4718. this.$age.val(120);
  4719. this.bv.validate();
  4720. expect(this.bv.isValid()).toEqual(false);
  4721. this.bv.resetForm();
  4722. this.$age.val(30);
  4723. this.bv.validate();
  4724. expect(this.bv.isValid()).toBeTruthy();
  4725. });
  4726. it('compare to other field', function() {
  4727. this.$age.attr('data-bv-lessthan-value', 'maxAge');
  4728. this.bv.destroy();
  4729. this.bv = $('#lessThanForm').bootstrapValidator().data('bootstrapValidator');
  4730. this.$maxAge.val(40);
  4731. this.$age.val(20);
  4732. this.bv.validate();
  4733. expect(this.bv.isValid()).toBeTruthy();
  4734. this.bv.resetForm();
  4735. this.$maxAge.val(20);
  4736. this.$age.val(30);
  4737. this.bv.validate();
  4738. expect(this.bv.isValid()).toEqual(false);
  4739. expect(this.bv.getMessages('age', 'lessThan')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.lessThan['default'], this.$maxAge.val()));
  4740. });
  4741. it('compare to return value of a function', function() {
  4742. this.$age.attr('data-bv-lessthan-value', 'lessThanCompare');
  4743. this.bv.destroy();
  4744. this.bv = $('#lessThanForm').bootstrapValidator().data('bootstrapValidator');
  4745. this.$maxAge.val(50);
  4746. this.$age.val(60);
  4747. this.bv.validate();
  4748. expect($('#msg').html()).toEqual('lessThanCompare() called; compare to 50');
  4749. expect(this.bv.isValid()).toEqual(false);
  4750. expect(this.bv.getMessages('age', 'lessThan')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.lessThan['default'], this.$maxAge.val()));
  4751. this.bv.resetForm();
  4752. this.$maxAge.val(60);
  4753. this.$age.val(30);
  4754. this.bv.validate();
  4755. expect($('#msg').html()).toEqual('lessThanCompare() called; compare to 60');
  4756. expect(this.bv.isValid()).toBeTruthy();
  4757. });
  4758. it('compare to return value of a namespace function', function() {
  4759. this.$age.attr('data-bv-lessthan-value', 'TestSuite.lessThan.compareTo');
  4760. this.bv.destroy();
  4761. this.bv = $('#lessThanForm').bootstrapValidator().data('bootstrapValidator');
  4762. this.$maxAge.val(50);
  4763. this.$age.val(60);
  4764. this.bv.validate();
  4765. expect($('#msg').html()).toEqual('TestSuite.lessThan.compareTo() called; compare to 50');
  4766. expect(this.bv.isValid()).toEqual(false);
  4767. expect(this.bv.getMessages('age', 'lessThan')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.lessThan['default'], this.$maxAge.val()));
  4768. this.bv.resetForm();
  4769. this.$maxAge.val(60);
  4770. this.$age.val(30);
  4771. this.bv.validate();
  4772. expect($('#msg').html()).toEqual('TestSuite.lessThan.compareTo() called; compare to 60');
  4773. expect(this.bv.isValid()).toBeTruthy();
  4774. });
  4775. });
  4776. describe('meid', function() {
  4777. beforeEach(function() {
  4778. $([
  4779. '<form class="form-horizontal" id="meidForm">',
  4780. '<div class="form-group">',
  4781. '<input type="text" name="meid" data-bv-meid />',
  4782. '</div>',
  4783. '</form>'
  4784. ].join('\n')).appendTo('body');
  4785. $('#meidForm').bootstrapValidator();
  4786. this.bv = $('#meidForm').data('bootstrapValidator');
  4787. this.$meid = this.bv.getFieldElements('meid');
  4788. });
  4789. afterEach(function() {
  4790. $('#meidForm').bootstrapValidator('destroy').remove();
  4791. });
  4792. it('Valid MEID (14 hex, check digit)', function() {
  4793. this.bv.resetForm();
  4794. this.$meid.val('A00000049259B16');
  4795. this.bv.validate();
  4796. expect(this.bv.isValid()).toBeTruthy();
  4797. });
  4798. it('Valid MEID (14 hex, dashes, check digit)', function() {
  4799. this.bv.resetForm();
  4800. this.$meid.val('A0-000004-9259B1-6');
  4801. this.bv.validate();
  4802. expect(this.bv.isValid()).toBeTruthy();
  4803. });
  4804. it('Valid MEID (14 hex, spaces, check digit)', function() {
  4805. this.bv.resetForm();
  4806. this.$meid.val('A0 000004 9259B1 6');
  4807. this.bv.validate();
  4808. expect(this.bv.isValid()).toBeTruthy();
  4809. });
  4810. it('Valid MEID (18 dec, check digit)', function() {
  4811. this.bv.resetForm();
  4812. this.$meid.val('2936087365007037100');
  4813. this.bv.validate();
  4814. expect(this.bv.isValid()).toBeTruthy();
  4815. });
  4816. it('Valid MEID (18 dec, dashes, check digit)', function() {
  4817. this.bv.resetForm();
  4818. this.$meid.val('29360-87365-0070-3710-0');
  4819. this.bv.validate();
  4820. expect(this.bv.isValid()).toBeTruthy();
  4821. });
  4822. it('Valid MEID (18 dec, spaces, check digit)', function() {
  4823. this.bv.resetForm();
  4824. this.$meid.val('29360 87365 0070 3710 0');
  4825. this.bv.validate();
  4826. expect(this.bv.isValid()).toBeTruthy();
  4827. });
  4828. it('Valid MEID (14 hex)', function() {
  4829. this.bv.resetForm();
  4830. this.$meid.val('AF0123450ABCDE');
  4831. this.bv.validate();
  4832. expect(this.bv.isValid()).toBeTruthy();
  4833. });
  4834. it('Valid MEID (14 hex, dashes)', function() {
  4835. this.bv.resetForm();
  4836. this.$meid.val('AF-012345-0ABCDE');
  4837. this.bv.validate();
  4838. expect(this.bv.isValid()).toBeTruthy();
  4839. });
  4840. it('Valid MEID (14 hex, spaces)', function() {
  4841. this.bv.resetForm();
  4842. this.$meid.val('AF 012345 0ABCDE');
  4843. this.bv.validate();
  4844. expect(this.bv.isValid()).toBeTruthy();
  4845. });
  4846. it('Valid MEID (18 dec)', function() {
  4847. this.bv.resetForm();
  4848. this.$meid.val('293608736500703710');
  4849. this.bv.validate();
  4850. expect(this.bv.isValid()).toBeTruthy();
  4851. });
  4852. it('Valid MEID (18 dec, dashes)', function() {
  4853. this.bv.resetForm();
  4854. this.$meid.val('29360-87365-0070-3710');
  4855. this.bv.validate();
  4856. expect(this.bv.isValid()).toBeTruthy();
  4857. });
  4858. it('Valid MEID (18 dec, spaces)', function() {
  4859. this.bv.resetForm();
  4860. this.$meid.val('29360 87365 0070 3710');
  4861. this.bv.validate();
  4862. expect(this.bv.isValid()).toBeTruthy();
  4863. });
  4864. it('Invalid MEID (14 hex, bad check digit)', function() {
  4865. this.bv.resetForm();
  4866. this.$meid.val('A00000049259B15');
  4867. this.bv.validate();
  4868. expect(this.bv.isValid()).toBeFalsy();
  4869. });
  4870. it('Invalid MEID (13 hex)', function() {
  4871. this.bv.resetForm();
  4872. this.$meid.val('A00000049259B');
  4873. this.bv.validate();
  4874. expect(this.bv.isValid()).toBeFalsy();
  4875. });
  4876. it('Invalid MEID (18 dec, bad check digit)', function() {
  4877. this.bv.resetForm();
  4878. this.$meid.val('2936087365007037101');
  4879. this.bv.validate();
  4880. expect(this.bv.isValid()).toBeFalsy();
  4881. });
  4882. it('Invalid MEID (17 dec)', function() {
  4883. this.bv.resetForm();
  4884. this.$meid.val('29360873650070371');
  4885. this.bv.validate();
  4886. expect(this.bv.isValid()).toBeFalsy();
  4887. });
  4888. });
  4889. describe('phone', function() {
  4890. beforeEach(function() {
  4891. $([
  4892. '<form class="form-horizontal" id="phoneForm">',
  4893. '<div class="form-group">',
  4894. '<select class="form-control" name="country">',
  4895. '<option value="BR">Brazil</option>',
  4896. '<option value="CN">China</option>',
  4897. '<option value="DK">Denmark</option>',
  4898. '<option value="ES">Spain</option>',
  4899. '<option value="FR">France</option>',
  4900. '<option value="GB">United Kingdom</option>',
  4901. '<option value="MA">Morocco</option>',
  4902. '<option value="PK">Pakistan</option>',
  4903. '<option value="RO">Romania</option>',
  4904. '<option value="RU">Russia</option>',
  4905. '<option value="TH">Thailand</option>',
  4906. '<option value="US">USA</option>',
  4907. '<option value="VE">Venezuela</option>',
  4908. '</select>',
  4909. '</div>',
  4910. '<div class="form-group">',
  4911. '<input type="text" name="phone" data-bv-phone />',
  4912. '</div>',
  4913. '</form>',
  4914. ].join('\n')).appendTo('body');
  4915. $('#phoneForm').bootstrapValidator();
  4916. /**
  4917. * @type {BootstrapValidator}
  4918. */
  4919. this.bv = $('#phoneForm').data('bootstrapValidator');
  4920. this.$country = this.bv.getFieldElements('country');
  4921. this.$phone = this.bv.getFieldElements('phone');
  4922. });
  4923. afterEach(function() {
  4924. $('#phoneForm').bootstrapValidator('destroy').remove();
  4925. });
  4926. it('dynamic country', function() {
  4927. this.$phone.attr('data-bv-phone-country', 'country');
  4928. this.bv.destroy();
  4929. this.bv = $('#phoneForm').bootstrapValidator().data('bootstrapValidator');
  4930. this.$country.val('BR');
  4931. this.$phone.val('16920894635');
  4932. this.bv.validate();
  4933. expect(this.bv.isValid()).toBeTruthy();
  4934. this.bv.resetForm();
  4935. this.$country.val('FR');
  4936. this.$phone.val('0644444444');
  4937. this.bv.validate();
  4938. expect(this.bv.isValid()).toBeTruthy();
  4939. this.bv.resetForm();
  4940. this.$country.val('GB');
  4941. this.$phone.val('012345678900');
  4942. this.bv.validate();
  4943. expect(this.bv.isValid()).toBeFalsy();
  4944. });
  4945. it('Brazil phone number', function() {
  4946. this.bv.updateOption('phone', 'phone', 'country', 'BR');
  4947. // Valid samples
  4948. var validSamples = [
  4949. '0800.000.00.00', '0800-000-00-00', '0800 000 00 00', '0800-00-00-00', '0800.00.00.00', '0800 00 00 00',
  4950. '0800-000-0000', '0800 000 0000', '0800.000.0000', '08000000000',
  4951. '1692089-4635', '16920894635', '16992089-4635', '16 99202-4635', '(16)99202-4635', '(16)92089-4635',
  4952. '(16) 92089-4635', '(15) 4343-4343', '+55 15 3702-7523', '(+55) 15 3702-7523', '(+55)1537027523',
  4953. '(+55)(15)3702-7523', '(+55) 15 3702-7523', '(+55) 15 99202-7523', '99202-4635', '(16) 9208-4635'
  4954. ];
  4955. for (var i in validSamples) {
  4956. this.bv.resetForm();
  4957. this.$phone.val(validSamples[i]);
  4958. this.bv.validate();
  4959. expect(this.bv.isValid()).toBeTruthy();
  4960. }
  4961. });
  4962. it('China phone number', function() {
  4963. this.bv.updateOption('phone', 'phone', 'country', 'CN');
  4964. // Valid samples
  4965. var validSamples = [
  4966. '18911111111', '189 1111 1111', '189-1111-1111', '0086-18911111111', '+86-18911111111',
  4967. '86-18911111111', '0086 18911111111', '+86 18911111111', '86 18911111111', '0086 189-1111-1111',
  4968. '+86 189-1111-1111', '86 189-1111-1111', '02011111111', '020-11111111', '020 11111111',
  4969. '020 1111 1111', '020-1111-1111', '0086 020 82803159', '0086-020-82803159', '0086-020-82803159',
  4970. '+86 20 61302222-8866', '+86 20 6130-2222-8866', '+86 10 59081185'
  4971. ];
  4972. for (var i in validSamples) {
  4973. this.bv.resetForm();
  4974. this.$phone.val(validSamples[i]);
  4975. this.bv.validate();
  4976. expect(this.bv.isValid()).toBeTruthy();
  4977. }
  4978. });
  4979. it('France phone number', function() {
  4980. this.bv.updateOption('phone', 'phone', 'country', 'FR');
  4981. // Valid samples
  4982. var validSamples = [
  4983. // National formats
  4984. '0644444444', '06 44 44 44 44', '06-44-44-44-44', '06.44.44.44.44',
  4985. // International formats
  4986. '+33644444444', '+336.44.44.44.44', '+33 6.44.44.44.44', '0033644444444', '00336.44.44.44.44',
  4987. '0033 6.44.44.44.44',
  4988. // Some times
  4989. '+33(0)644444444', '+33 (0) 644444444'
  4990. ];
  4991. for (var i in validSamples) {
  4992. this.bv.resetForm();
  4993. this.$phone.val(validSamples[i]);
  4994. this.bv.validate();
  4995. expect(this.bv.isValid()).toBeTruthy();
  4996. }
  4997. // Invalid samples
  4998. var invalidSamples = [
  4999. // The separator between pairs of digits is not the same
  5000. '06 44.44-44.44', '06 44 44-44.44', '06 44 44-4444', '06 44 44-4444',
  5001. // Too many digits
  5002. '06444444444444',
  5003. // Missing leading 0
  5004. '6644444444',
  5005. // Too much non-numeric characters
  5006. '06 44.44-44.44', '+33 (0) 644444444',
  5007. // Bad parenthesis
  5008. '(0)644444444',
  5009. // Bad separator after the international prefix
  5010. '+33-(0)-644444444', '+33 (0)-644444444', '+33-(0) 644444444',
  5011. // Trailing separator
  5012. '06.44.44.44.44.'
  5013. ];
  5014. for (i in invalidSamples) {
  5015. this.bv.resetForm();
  5016. this.$phone.val(invalidSamples[i]);
  5017. this.bv.validate();
  5018. expect(this.bv.isValid()).toEqual(false);
  5019. }
  5020. });
  5021. it('United Kingdom phone number', function() {
  5022. this.bv.updateOption('phone', 'phone', 'country', 'GB');
  5023. // Valid samples
  5024. var validSamples = [
  5025. // National formats
  5026. '01611234567', '0161 123 4567', '(0161) 123 4567', '0161-123-4567',
  5027. // International formats
  5028. '+44 161 123 4567', '+441611234567', '+44(0)161234567', '00 44 161 1234567', '(011) 44 161 234567', '0161-158-5587',
  5029. // Extensions
  5030. '0161 123 4567 ext. 123', '01611234567x123', '+44161234567x123', '+44 (0) 161 1234567 ext 123'
  5031. ];
  5032. for (var i in validSamples) {
  5033. this.bv.resetForm();
  5034. this.$phone.val(validSamples[i]);
  5035. this.bv.validate();
  5036. expect(this.bv.isValid()).toBeTruthy();
  5037. }
  5038. // Invalid samples
  5039. var invalidSamples = [
  5040. '012345678900', // Too many digits
  5041. '1611234567', // Missing trunk
  5042. '012345678', // Not enough digits
  5043. '123 4567', // Missing area code
  5044. '061 123 4567' // Invalid area code
  5045. ];
  5046. for (i in invalidSamples) {
  5047. this.bv.resetForm();
  5048. this.$phone.val(invalidSamples[i]);
  5049. this.bv.validate();
  5050. expect(this.bv.isValid()).toEqual(false);
  5051. }
  5052. });
  5053. it('Morocco phone number', function() {
  5054. this.bv.updateOption('phone', 'phone', 'country', 'MA');
  5055. // Valid samples
  5056. var validSamples = [
  5057. // National formats
  5058. '0644444444', '0610245896', '0630548564', '06 44 44 44 44', '06-44-44-44-44', '06.44.44.44.44', '06 44.44-44.44',
  5059. '0528254856', '0535484541', '05 28 44 44 44', '05-28-44.44.44', '05.28.44.44.44', '05 28.44-44.44',
  5060. // International formats
  5061. '+212644444444', '+2126.44.44.44.44', '+212 6.44.44.44.44', '00212644444444', '002126.44.44.44.44', '00212 6.44.44.44.44',
  5062. // Some times
  5063. '+212(0)644444444', '+212 (0) 644444444'
  5064. ];
  5065. for (var i in validSamples) {
  5066. this.bv.resetForm();
  5067. this.$phone.val(validSamples[i]);
  5068. this.bv.validate();
  5069. expect(this.bv.isValid()).toBeTruthy();
  5070. }
  5071. // Invalid samples
  5072. var invalidSamples = [
  5073. '0625468961', '0512548632', '0542564896', // Not a valid phone numbers
  5074. '06444444444444', // Too many digits
  5075. '6644444444', // Missing leading 0
  5076. '06 44.44-44.44', '+212 (0) 644444444', // Too much non-numeric characters
  5077. '(0)644444444' // Bad parenthesis
  5078. ];
  5079. for (i in invalidSamples) {
  5080. this.bv.resetForm();
  5081. this.$phone.val(invalidSamples[i]);
  5082. this.bv.validate();
  5083. expect(this.bv.isValid()).toEqual(false);
  5084. }
  5085. });
  5086. it('Pakistan phone number', function() {
  5087. this.bv.updateOption('phone', 'phone', 'country', 'PK');
  5088. // Valid samples
  5089. var validSamples = ['03336527366'];
  5090. for (var i in validSamples) {
  5091. this.bv.resetForm();
  5092. this.$phone.val(validSamples[i]);
  5093. this.bv.validate();
  5094. expect(this.bv.isValid()).toBeTruthy();
  5095. }
  5096. });
  5097. it('Romania phone number', function() {
  5098. this.bv.updateOption('phone', 'phone', 'country', 'RO');
  5099. // Valid samples
  5100. var validSamples = [
  5101. '+40213-564-864', '+40213.564.864', '+40213 564 864', '0213-564-864',
  5102. '0213564864', '0313564864',
  5103. '0720512346', '0730512346', '0740512346', '0750512346', '+40750512346', '+40750.512.346',
  5104. '0760512346', '0770512346', '0780512346'
  5105. ];
  5106. for (var i in validSamples) {
  5107. this.bv.resetForm();
  5108. this.$phone.val(validSamples[i]);
  5109. this.bv.validate();
  5110. expect(this.bv.isValid()).toBeTruthy();
  5111. }
  5112. // Invalid samples
  5113. var invalidSamples = [
  5114. '0213/564/864', // Invalid separator
  5115. '0413564864', // Invalid land line number (The valid one should be +402, +403 or inside country 02 - 03)
  5116. '0790512346' // Invalid mobile phone number (The valid one is 072xxxxxxx - 078xxxxxxx)
  5117. ];
  5118. for (i in invalidSamples) {
  5119. this.bv.resetForm();
  5120. this.$phone.val(invalidSamples[i]);
  5121. this.bv.validate();
  5122. expect(this.bv.isValid()).toEqual(false);
  5123. }
  5124. });
  5125. it('Russia phone number', function() {
  5126. this.bv.updateOption('phone', 'phone', 'country', 'RU');
  5127. // Valid samples
  5128. var validSamples = ['+7(911)976-91-04'];
  5129. for (var i in validSamples) {
  5130. this.bv.resetForm();
  5131. this.$phone.val(validSamples[i]);
  5132. this.bv.validate();
  5133. expect(this.bv.isValid()).toBeTruthy();
  5134. }
  5135. });
  5136. });
  5137. describe('rgbColor', function() {
  5138. beforeEach(function() {
  5139. var html = [
  5140. '<div class="container">',
  5141. '<form class="form-horizontal" id="rgbColorForm">',
  5142. '<div class="form-group">',
  5143. '<input type="text" name="rgb" data-bv-rgbcolor />',
  5144. '</div>',
  5145. '</form>',
  5146. '</div>'
  5147. ].join('\n');
  5148. $(html).appendTo('body');
  5149. $('#rgbColorForm').bootstrapValidator();
  5150. this.bv = $('#rgbColorForm').data('bootstrapValidator');
  5151. this.$rgbColor = this.bv.getFieldElements('rgb');
  5152. });
  5153. afterEach(function() {
  5154. $('#rgbColorForm').bootstrapValidator('destroy').parent().remove();
  5155. });
  5156. it('accept rgb()', function() {
  5157. this.$rgbColor.val('rgb(255,255,255)');
  5158. this.bv.validate();
  5159. expect(this.bv.isValidField('rgb')).toBeTruthy();
  5160. });
  5161. it('accept spaces around numeric values', function() {
  5162. this.$rgbColor.val('rgb( 255 , 255 , 255 )');
  5163. this.bv.validate();
  5164. expect(this.bv.isValidField('rgb')).toBeTruthy();
  5165. });
  5166. it('accept multiple spaces around numeric values', function() {
  5167. this.$rgbColor.val('rgb( 255, 255, 255 )');
  5168. this.bv.validate();
  5169. expect(this.bv.isValidField('rgb')).toBeTruthy();
  5170. });
  5171. it('accept interger values', function() {
  5172. this.$rgbColor.val('rgb(255,255,255)');
  5173. this.bv.validate();
  5174. expect(this.bv.isValidField('rgb')).toBeTruthy();
  5175. });
  5176. it('accept percent values', function() {
  5177. this.$rgbColor.val('rgb(100%,100%,100%)');
  5178. this.bv.validate();
  5179. expect(this.bv.isValidField('rgb')).toBeTruthy();
  5180. });
  5181. it('reject mixed intergers and percentile input', function() {
  5182. this.$rgbColor.val('rgb(255,255,100%)');
  5183. this.bv.validate();
  5184. expect(this.bv.isValidField('rgb')).toEqual(false);
  5185. });
  5186. it('reject negative integers', function() {
  5187. this.$rgbColor.val('rgb(-10,255,255)');
  5188. this.bv.validate();
  5189. expect(this.bv.isValidField('rgb')).toEqual(false);
  5190. });
  5191. it('reject negative percentages', function() {
  5192. this.$rgbColor.val('rgb(-10%,100%,100%)');
  5193. this.bv.validate();
  5194. expect(this.bv.isValidField('rgb')).toEqual(false);
  5195. });
  5196. it('require rgb()', function() {
  5197. this.$rgbColor.val('255,255,255');
  5198. this.bv.validate();
  5199. expect(this.bv.isValidField('rgb')).toEqual(false);
  5200. });
  5201. it('reject intergers above 255', function() {
  5202. this.$rgbColor.val('rgb(255,255,256)');
  5203. this.bv.validate();
  5204. expect(this.bv.isValidField('rgb')).toEqual(false);
  5205. });
  5206. it('reject percentages above 100%', function() {
  5207. this.$rgbColor.val('rgb(100%,100%,101%)');
  5208. this.bv.validate();
  5209. expect(this.bv.isValidField('rgb')).toEqual(false);
  5210. });
  5211. it('reject space between rgb and (', function() {
  5212. this.$rgbColor.val('rgb (255,255,255)');
  5213. this.bv.validate();
  5214. expect(this.bv.isValidField('rgb')).toEqual(false);
  5215. });
  5216. it('reject leading space', function() {
  5217. this.$rgbColor.val(' rgb(255,255,255)');
  5218. this.bv.validate();
  5219. expect(this.bv.isValidField('rgb')).toEqual(false);
  5220. });
  5221. it('reject trailing space', function() {
  5222. this.$rgbColor.val('rgb(255,255,255) ');
  5223. this.bv.validate();
  5224. expect(this.bv.isValidField('rgb')).toEqual(false);
  5225. });
  5226. });
  5227. describe('rgbaColor', function() {
  5228. beforeEach(function() {
  5229. var html = [
  5230. '<div class="container">',
  5231. '<form class="form-horizontal" id="rgbaColorForm">',
  5232. '<div class="form-group">',
  5233. '<input type="text" name="rgba" data-bv-rgbacolor />',
  5234. '</div>',
  5235. '</form>',
  5236. '</div>'
  5237. ].join('\n');
  5238. $(html).appendTo('body');
  5239. $('#rgbaColorForm').bootstrapValidator();
  5240. this.bv = $('#rgbaColorForm').data('bootstrapValidator');
  5241. this.$rgbaColor = this.bv.getFieldElements('rgba');
  5242. });
  5243. afterEach(function() {
  5244. $('#rgbaColorForm').bootstrapValidator('destroy').parent().remove();
  5245. });
  5246. it('accept rgba()', function() {
  5247. this.$rgbaColor.val('rgba(255,255,255,1)');
  5248. this.bv.validate();
  5249. expect(this.bv.isValidField('rgba')).toBeTruthy();
  5250. });
  5251. it('accept spaces around numeric values', function() {
  5252. this.$rgbaColor.val('rgba( 255 , 255 , 255 , 1 )');
  5253. this.bv.validate();
  5254. expect(this.bv.isValidField('rgba')).toBeTruthy();
  5255. });
  5256. it('accept multiple spaces around numeric values', function() {
  5257. this.$rgbaColor.val('rgba( 255 , 255 , 255 , 1 )');
  5258. this.bv.validate();
  5259. expect(this.bv.isValidField('rgba')).toBeTruthy();
  5260. });
  5261. it('accept interger values', function() {
  5262. this.$rgbaColor.val('rgba(255,255,255,1)');
  5263. this.bv.validate();
  5264. expect(this.bv.isValidField('rgba')).toBeTruthy();
  5265. });
  5266. it('accept percent values', function() {
  5267. this.$rgbaColor.val('rgba(100%,100%,100%,1)');
  5268. this.bv.validate();
  5269. expect(this.bv.isValidField('rgba')).toBeTruthy();
  5270. });
  5271. it('accept integer alpha channel value of 0', function() {
  5272. this.$rgbaColor.val('rgba(255,255,255,0)');
  5273. this.bv.validate();
  5274. expect(this.bv.isValidField('rgba')).toBeTruthy();
  5275. });
  5276. it('accept integer alpha channel value of 1', function() {
  5277. this.$rgbaColor.val('rgba(255,255,255,1)');
  5278. this.bv.validate();
  5279. expect(this.bv.isValidField('rgba')).toBeTruthy();
  5280. });
  5281. it('accept floating alpha channel with leading 0', function() {
  5282. this.$rgbaColor.val('rgba(255,255,255,0.5)');
  5283. this.bv.validate();
  5284. expect(this.bv.isValidField('rgba')).toBeTruthy();
  5285. });
  5286. it('accept floating alpha channel without leading 0', function() {
  5287. this.$rgbaColor.val('rgba(255,255,255,.5)');
  5288. this.bv.validate();
  5289. expect(this.bv.isValidField('rgba')).toBeTruthy();
  5290. });
  5291. it('accept floating alpha channel with more than 1 decimal place', function() {
  5292. this.$rgbaColor.val('rgba(255,255,255,.524141)');
  5293. this.bv.validate();
  5294. expect(this.bv.isValidField('rgba')).toBeTruthy();
  5295. });
  5296. it('accept integer alpha channel value of 0 with percentage rgb values', function() {
  5297. this.$rgbaColor.val('rgba(100%,100%,100%,0)');
  5298. this.bv.validate();
  5299. expect(this.bv.isValidField('rgba')).toBeTruthy();
  5300. });
  5301. it('accept integer alpha channel value of 1 with percentage rgb values', function() {
  5302. this.$rgbaColor.val('rgba(100%,100%,100%,1)');
  5303. this.bv.validate();
  5304. expect(this.bv.isValidField('rgba')).toBeTruthy();
  5305. });
  5306. it('accept floating alpha channel with leading 0 with percentage rgb values', function() {
  5307. this.$rgbaColor.val('rgba(100%,100%,100%,0.5)');
  5308. this.bv.validate();
  5309. expect(this.bv.isValidField('rgba')).toBeTruthy();
  5310. });
  5311. it('accept floating alpha channel without leading 0 with percentage rgb values', function() {
  5312. this.$rgbaColor.val('rgba(100%,100%,100%,.5)');
  5313. this.bv.validate();
  5314. expect(this.bv.isValidField('rgba')).toBeTruthy();
  5315. });
  5316. it('accept floating alpha channel with more than 1 decimal place with percentage rgb values', function() {
  5317. this.$rgbaColor.val('rgba(100%,100%,100%,.524141)');
  5318. this.bv.validate();
  5319. expect(this.bv.isValidField('rgba')).toBeTruthy();
  5320. });
  5321. it('reject percentage value for alpha channel', function() {
  5322. this.$rgbaColor.val('rgba(100%,100%,100%,50%)');
  5323. this.bv.validate();
  5324. expect(this.bv.isValidField('rgba')).toEqual(false);
  5325. });
  5326. it('reject integers larger than 1 for alpha channel', function() {
  5327. this.$rgbaColor.val('rgba(255,255,255,2)');
  5328. this.bv.validate();
  5329. expect(this.bv.isValidField('rgba')).toEqual(false);
  5330. });
  5331. it('reject negative integers for alpha channel', function() {
  5332. this.$rgbaColor.val('rgba(255,255,255,-1)');
  5333. this.bv.validate();
  5334. expect(this.bv.isValidField('rgba')).toEqual(false);
  5335. });
  5336. it('reject floats larger than 1 for alpha channel', function() {
  5337. this.$rgbaColor.val('rgba(255,255,255,1.000000000001)');
  5338. this.bv.validate();
  5339. expect(this.bv.isValidField('rgba')).toEqual(false);
  5340. });
  5341. it('reject negative floats for alpha channel', function() {
  5342. this.$rgbaColor.val('rgba(255,255,255,-0.5)');
  5343. this.bv.validate();
  5344. expect(this.bv.isValidField('rgba')).toEqual(false);
  5345. });
  5346. it('reject more floats larger than 1 for alpha channel', function() {
  5347. this.$rgbaColor.val('rgba(255,255,255,2.3)');
  5348. this.bv.validate();
  5349. expect(this.bv.isValidField('rgba')).toEqual(false);
  5350. });
  5351. it('reject mixed intergers and percentile input', function() {
  5352. this.$rgbaColor.val('rgba(255,255,100%,1)');
  5353. this.bv.validate();
  5354. expect(this.bv.isValidField('rgba')).toEqual(false);
  5355. });
  5356. it('reject negative integers', function() {
  5357. this.$rgbaColor.val('rgba(-10,255,255,1)');
  5358. this.bv.validate();
  5359. expect(this.bv.isValidField('rgba')).toEqual(false);
  5360. });
  5361. it('reject negative percentages', function() {
  5362. this.$rgbaColor.val('rgba(-10%,100%,100%,1)');
  5363. this.bv.validate();
  5364. expect(this.bv.isValidField('rgba')).toEqual(false);
  5365. });
  5366. it('require rgba()', function() {
  5367. this.$rgbaColor.val('255,255,255,1');
  5368. this.bv.validate();
  5369. expect(this.bv.isValidField('rgba')).toEqual(false);
  5370. });
  5371. it('reject intergers above 255', function() {
  5372. this.$rgbaColor.val('rgba(255,255,256),1');
  5373. this.bv.validate();
  5374. expect(this.bv.isValidField('rgba')).toEqual(false);
  5375. });
  5376. it('reject percentages above 100%', function() {
  5377. this.$rgbaColor.val('rgba(100%,100%,101%,1)');
  5378. this.bv.validate();
  5379. expect(this.bv.isValidField('rgba')).toEqual(false);
  5380. });
  5381. it('reject space between rgba and (', function() {
  5382. this.$rgbaColor.val('rgba (255,255,255,1)');
  5383. this.bv.validate();
  5384. expect(this.bv.isValidField('rgba')).toEqual(false);
  5385. });
  5386. it('reject leading space', function() {
  5387. this.$rgbaColor.val(' rgba(255,255,255,1)');
  5388. this.bv.validate();
  5389. expect(this.bv.isValidField('rgba')).toEqual(false);
  5390. });
  5391. it('reject trailing space', function() {
  5392. this.$rgbaColor.val('rgba(255,255,255,1) ');
  5393. this.bv.validate();
  5394. expect(this.bv.isValidField('rgba')).toEqual(false);
  5395. });
  5396. });
  5397. describe('uri', function() {
  5398. beforeEach(function() {
  5399. $([
  5400. '<form class="form-horizontal" id="uriForm">',
  5401. '<div id="msg"></div>',
  5402. '<div class="form-group">',
  5403. '<input type="text" name="uri" data-bv-uri />',
  5404. '</div>',
  5405. '</form>'
  5406. ].join('\n')).appendTo('body');
  5407. $('#uriForm').bootstrapValidator();
  5408. this.bv = $('#uriForm').data('bootstrapValidator');
  5409. this.$uri = this.bv.getFieldElements('uri');
  5410. });
  5411. afterEach(function() {
  5412. $('#uriForm').bootstrapValidator('destroy').remove();
  5413. });
  5414. var validGlobalURIs = [
  5415. 'http://foo.com/blah_blah',
  5416. 'http://foo.com/blah_blah',
  5417. 'http://foo.com/blah_blah/',
  5418. 'http://foo.com/blah_blah_(wikipedia)',
  5419. 'http://foo.com/blah_blah_(wikipedia)_(again)',
  5420. 'http://www.example.com/wpstyle/?p=364',
  5421. 'https://www.example.com/foo/?bar=baz&inga=42&quux',
  5422. 'http://✪df.ws/123',
  5423. 'http://userid:password@example.com:8080',
  5424. 'http://userid:password@example.com:8080/',
  5425. 'http://userid@example.com',
  5426. 'http://userid@example.com/',
  5427. 'http://userid@example.com:8080',
  5428. 'http://userid@example.com:8080/',
  5429. 'http://userid:password@example.com',
  5430. 'http://userid:password@example.com/',
  5431. 'http://142.42.1.1/',
  5432. 'http://142.42.1.1:8080/',
  5433. 'http://➡.ws/䨹',
  5434. 'http://⌘.ws',
  5435. 'http://⌘.ws/',
  5436. 'http://foo.com/blah_(wikipedia)#cite-1',
  5437. 'http://foo.com/blah_(wikipedia)_blah#cite-1',
  5438. 'http://foo.com/unicode_(✪)_in_parens',
  5439. 'http://foo.com/(something)?after=parens',
  5440. 'http://☺.damowmow.com/',
  5441. 'http://code.google.com/events/#&product=browser',
  5442. 'http://j.mp',
  5443. 'ftp://foo.bar/baz',
  5444. 'http://foo.bar/?q=Test%20URL-encoded%20stuff',
  5445. 'http://مثال.إختبار',
  5446. 'http://例子.测试',
  5447. 'http://उदाहरण.परीक्षा',
  5448. "http://-.~_!$&'()*+,;=:%40:80%2f::::::@example.com",
  5449. 'http://1337.net',
  5450. 'http://a.b-c.de',
  5451. 'http://223.255.255.254'
  5452. ];
  5453. var invalidGlobalURIs = [
  5454. 'http://',
  5455. 'http://.',
  5456. 'http://..',
  5457. 'http://../',
  5458. 'http://?',
  5459. 'http://??',
  5460. 'http://??/',
  5461. 'http://#',
  5462. 'http://##',
  5463. 'http://##/',
  5464. 'http://foo.bar?q=Spaces should be encoded',
  5465. '//',
  5466. '//a',
  5467. '///a',
  5468. '///',
  5469. 'http:///a',
  5470. 'foo.com',
  5471. 'rdar://1234',
  5472. 'h://test',
  5473. 'http:// shouldfail.com',
  5474. ':// should fail',
  5475. 'http://foo.bar/foo(bar)baz quux',
  5476. 'ftps://foo.bar/',
  5477. 'http://-error-.invalid/',
  5478. 'http://a.b--c.de/',
  5479. 'http://-a.b.co',
  5480. 'http://a.b-.co',
  5481. 'http://.www.foo.bar/',
  5482. 'http://www.foo.bar./',
  5483. 'http://.www.foo.bar./'
  5484. ];
  5485. var localURIs = [
  5486. 'http://intranetsite',
  5487. 'http://intranetsite/test',
  5488. 'http://intranetsite:80',
  5489. 'http://intranetsite:80/test',
  5490. 'http://user:pass@intranetsite',
  5491. 'http://user:pass@intranetsite/test',
  5492. 'http://user:pass@intranetsite:80',
  5493. 'http://user:pass@intranetsite:80/test',
  5494. 'http://10.1.1.0',
  5495. 'http://10.1.1.255',
  5496. 'http://10.1.1.1',
  5497. 'http://10.1.1.254',
  5498. 'http://127.0.0.1',
  5499. 'http://192.168.0.1',
  5500. 'http://0.0.0.0',
  5501. 'http://224.1.1.1',
  5502. 'http://1.1.1.1.1',
  5503. 'http://123.123.123',
  5504. 'http://3628126748'
  5505. ];
  5506. it('Valid URIs (allowLocal=false)', function() {
  5507. var me = this;
  5508. $.each(validGlobalURIs, function(index, uri) {
  5509. me.bv.resetForm();
  5510. me.$uri.val(uri);
  5511. me.bv.validate();
  5512. expect(me.bv.isValid()).toBeTruthy();
  5513. });
  5514. });
  5515. it('Invalid URIs (allowLocal=false)', function() {
  5516. var me = this;
  5517. $.each(invalidGlobalURIs.concat(localURIs), function(index, uri) {
  5518. me.bv.resetForm();
  5519. me.$uri.val(uri);
  5520. me.bv.validate();
  5521. expect(me.bv.isValid()).toEqual(false);
  5522. });
  5523. });
  5524. it('Valid URIs (allowLocal=true)', function() {
  5525. var me = this;
  5526. me.bv.updateOption('uri', 'uri', 'allowLocal', true);
  5527. $.each(validGlobalURIs.concat(localURIs), function(index, uri) {
  5528. me.bv.resetForm();
  5529. me.$uri.val(uri);
  5530. me.bv.validate();
  5531. expect(me.bv.isValid()).toBeTruthy();
  5532. });
  5533. });
  5534. it('Invalid URIs (allowLocal=true)', function() {
  5535. var me = this;
  5536. me.bv.updateOption('uri', 'uri', 'allowLocal', true);
  5537. $.each(invalidGlobalURIs, function(index, uri) {
  5538. me.bv.resetForm();
  5539. me.$uri.val(uri);
  5540. me.bv.validate();
  5541. expect(me.bv.isValid()).toEqual(false);
  5542. });
  5543. });
  5544. });
  5545. describe('vat', function() {
  5546. beforeEach(function() {
  5547. $([
  5548. '<form class="form-horizontal" id="vatForm">',
  5549. '<div class="form-group">',
  5550. '<select class="form-control" name="country">',
  5551. '<option value="AT">Austria</option>',
  5552. '<option value="BE">Belgium</option>',
  5553. '<option value="BG">Bulgaria</option>',
  5554. '<option value="HR">Croatia</option>',
  5555. '<option value="CY">Cyprus</option>',
  5556. '<option value="CZ">Czech Republic</option>',
  5557. '<option value="DK">Denmark</option>',
  5558. '<option value="EE">Estonia</option>',
  5559. '<option value="FI">Finland</option>',
  5560. '<option value="FR">France</option>',
  5561. '<option value="DE">Germany</option>',
  5562. '<option value="GR">Greece</option>',
  5563. '<option value="HU">Hungary</option>',
  5564. '<option value="IE">Ireland</option>',
  5565. '<option value="IS">Iceland</option>',
  5566. '<option value="IT">Italy</option>',
  5567. '<option value="LV">Latvia</option>',
  5568. '<option value="LT">Lithuania</option>',
  5569. '<option value="LU">Luxembourg</option>',
  5570. '<option value="MT">Malta</option>',
  5571. '<option value="NL">Netherlands</option>',
  5572. '<option value="NO">Norway</option>',
  5573. '<option value="PL">Poland</option>',
  5574. '<option value="PT">Portugal</option>',
  5575. '<option value="RO">Romania</option>',
  5576. '<option value="RU">Russia</option>',
  5577. '<option value="RS">Serbia</option>',
  5578. '<option value="SK">Slovakia</option>',
  5579. '<option value="SI">Slovenia</option>',
  5580. '<option value="ES">Spain</option>',
  5581. '<option value="SE">Sweden</option>',
  5582. '<option value="CH">Switzerland</option>',
  5583. '<option value="GB">United Kingdom</option>',
  5584. '<option value="VE">Venezuela</option>',
  5585. '<option value="ZA">South Africa</option>',
  5586. '</select>',
  5587. '</div>',
  5588. '<div class="form-group">',
  5589. '<input type="text" name="vat" data-bv-vat />',
  5590. '</div>',
  5591. '</form>',
  5592. ].join('\n')).appendTo('body');
  5593. $('#vatForm').bootstrapValidator();
  5594. /**
  5595. * @type {BootstrapValidator}
  5596. */
  5597. this.bv = $('#vatForm').data('bootstrapValidator');
  5598. this.$country = this.bv.getFieldElements('country');
  5599. this.$vat = this.bv.getFieldElements('vat');
  5600. });
  5601. afterEach(function() {
  5602. $('#vatForm').bootstrapValidator('destroy').remove();
  5603. });
  5604. it('dynamic country', function() {
  5605. this.$vat.attr('data-bv-vat-country', 'country');
  5606. this.bv.destroy();
  5607. this.bv = $('#vatForm').bootstrapValidator().data('bootstrapValidator');
  5608. this.$country.val('AT');
  5609. this.$vat.val('ATU13585627');
  5610. this.bv.validate();
  5611. expect(this.bv.isValid()).toBeTruthy();
  5612. this.bv.resetForm();
  5613. this.$country.val('BG');
  5614. this.$vat.val('BE0428759497');
  5615. this.bv.validate();
  5616. expect(this.bv.isValid()).toEqual(false);
  5617. this.bv.resetForm();
  5618. this.$country.val('BE');
  5619. this.$vat.val('BE431150351');
  5620. this.bv.validate();
  5621. expect(this.bv.isValid()).toEqual(false);
  5622. });
  5623. it('Austrian VAT number', function() {
  5624. this.bv.updateOption('vat', 'vat', 'country', 'AT');
  5625. // Valid samples
  5626. var validSamples = ['ATU13585627', 'U13585627'];
  5627. for (var i in validSamples) {
  5628. this.bv.resetForm();
  5629. this.$vat.val(validSamples[i]);
  5630. this.bv.validate();
  5631. expect(this.bv.isValid()).toBeTruthy();
  5632. }
  5633. // Invalid samples
  5634. var invalidSamples = ['ATU13585626', 'U13585626'];
  5635. for (i in invalidSamples) {
  5636. this.bv.resetForm();
  5637. this.$vat.val(invalidSamples[i]);
  5638. this.bv.validate();
  5639. expect(this.bv.isValid()).toEqual(false);
  5640. }
  5641. });
  5642. it('Belgian VAT number', function() {
  5643. this.bv.updateOption('vat', 'vat', 'country', 'BE');
  5644. // Valid samples
  5645. var validSamples = ['BE0428759497', '0428759497'];
  5646. for (var i in validSamples) {
  5647. this.bv.resetForm();
  5648. this.$vat.val(validSamples[i]);
  5649. this.bv.validate();
  5650. expect(this.bv.isValid()).toBeTruthy();
  5651. }
  5652. // Invalid samples
  5653. var invalidSamples = ['BE431150351', '431150351'];
  5654. for (i in invalidSamples) {
  5655. this.bv.resetForm();
  5656. this.$vat.val(invalidSamples[i]);
  5657. this.bv.validate();
  5658. expect(this.bv.isValid()).toEqual(false);
  5659. }
  5660. });
  5661. it('Bulgarian VAT number', function() {
  5662. this.bv.updateOption('vat', 'vat', 'country', 'BG');
  5663. // Valid samples
  5664. var validSamples = ['BG175074752', 'BG7523169263', 'BG8032056031', 'BG7542011030', 'BG7111042925', '175074752', '7523169263', '8032056031'];
  5665. for (var i in validSamples) {
  5666. this.bv.resetForm();
  5667. this.$vat.val(validSamples[i]);
  5668. this.bv.validate();
  5669. expect(this.bv.isValid()).toBeTruthy();
  5670. }
  5671. // Invalid samples
  5672. var invalidSamples = ['BG175074753', 'BG7552A10004', 'BG7111042922', '175074753', '7552A10004'];
  5673. for (i in invalidSamples) {
  5674. this.bv.resetForm();
  5675. this.$vat.val(invalidSamples[i]);
  5676. this.bv.validate();
  5677. expect(this.bv.isValid()).toEqual(false);
  5678. }
  5679. });
  5680. it('Cypriot VAT number', function() {
  5681. this.bv.updateOption('vat', 'vat', 'country', 'CY');
  5682. // Valid samples
  5683. var validSamples = ['CY10259033P', '10259033P'];
  5684. for (var i in validSamples) {
  5685. this.bv.resetForm();
  5686. this.$vat.val(validSamples[i]);
  5687. this.bv.validate();
  5688. expect(this.bv.isValid()).toBeTruthy();
  5689. }
  5690. // Invalid samples
  5691. var invalidSamples = ['CY10259033Z', '10259033Z'];
  5692. for (i in invalidSamples) {
  5693. this.bv.resetForm();
  5694. this.$vat.val(invalidSamples[i]);
  5695. this.bv.validate();
  5696. expect(this.bv.isValid()).toEqual(false);
  5697. }
  5698. });
  5699. it('Czech Republic VAT number', function() {
  5700. this.bv.updateOption('vat', 'vat', 'country', 'CZ');
  5701. // Valid samples
  5702. var validSamples = ['CZ25123891', 'CZ7103192745', 'CZ991231123', 'CZ640903926', '25123891', '7103192745'];
  5703. for (var i in validSamples) {
  5704. this.bv.resetForm();
  5705. this.$vat.val(validSamples[i]);
  5706. this.bv.validate();
  5707. expect(this.bv.isValid()).toBeTruthy();
  5708. }
  5709. // Invalid samples
  5710. var invalidSamples = ['CZ25123890', 'CZ1103492745', 'CZ590312123', '25123890', '1103492745'];
  5711. for (i in invalidSamples) {
  5712. this.bv.resetForm();
  5713. this.$vat.val(invalidSamples[i]);
  5714. this.bv.validate();
  5715. expect(this.bv.isValid()).toEqual(false);
  5716. }
  5717. });
  5718. it('German VAT number', function() {
  5719. this.bv.updateOption('vat', 'vat', 'country', 'DE');
  5720. // Valid samples
  5721. var validSamples = ['DE136695976', '136695976'];
  5722. for (var i in validSamples) {
  5723. this.bv.resetForm();
  5724. this.$vat.val(validSamples[i]);
  5725. this.bv.validate();
  5726. expect(this.bv.isValid()).toBeTruthy();
  5727. }
  5728. // Invalid samples
  5729. var invalidSamples = ['DE136695978', '136695978'];
  5730. for (i in invalidSamples) {
  5731. this.bv.resetForm();
  5732. this.$vat.val(invalidSamples[i]);
  5733. this.bv.validate();
  5734. expect(this.bv.isValid()).toEqual(false);
  5735. }
  5736. });
  5737. it('Danish VAT number', function() {
  5738. this.bv.updateOption('vat', 'vat', 'country', 'DK');
  5739. // Valid samples
  5740. var validSamples = ['DK13585628', '13585628'];
  5741. for (var i in validSamples) {
  5742. this.bv.resetForm();
  5743. this.$vat.val(validSamples[i]);
  5744. this.bv.validate();
  5745. expect(this.bv.isValid()).toBeTruthy();
  5746. }
  5747. // Invalid samples
  5748. var invalidSamples = ['DK13585627', '13585627'];
  5749. for (i in invalidSamples) {
  5750. this.bv.resetForm();
  5751. this.$vat.val(invalidSamples[i]);
  5752. this.bv.validate();
  5753. expect(this.bv.isValid()).toEqual(false);
  5754. }
  5755. });
  5756. it('Estonian VAT number', function() {
  5757. this.bv.updateOption('vat', 'vat', 'country', 'EE');
  5758. // Valid samples
  5759. var validSamples = ['EE100931558', 'EE100594102', '100931558', '100594102'];
  5760. for (var i in validSamples) {
  5761. this.bv.resetForm();
  5762. this.$vat.val(validSamples[i]);
  5763. this.bv.validate();
  5764. expect(this.bv.isValid()).toBeTruthy();
  5765. }
  5766. // Invalid samples
  5767. var invalidSamples = ['EE100594103', '100594103'];
  5768. for (i in invalidSamples) {
  5769. this.bv.resetForm();
  5770. this.$vat.val(invalidSamples[i]);
  5771. this.bv.validate();
  5772. expect(this.bv.isValid()).toEqual(false);
  5773. }
  5774. });
  5775. it('Spanish VAT number (NIF)', function() {
  5776. this.bv.updateOption('vat', 'vat', 'country', 'ES');
  5777. // Valid samples
  5778. var validSamples = ['ES54362315K', 'ESX2482300W', 'ESX5253868R', 'ESM1234567L', 'ESJ99216582', 'ESB58378431', 'ESB64717838', '54362315K', 'X2482300W', 'X5253868R', 'M1234567L', 'J99216582'];
  5779. for (var i in validSamples) {
  5780. this.bv.resetForm();
  5781. this.$vat.val(validSamples[i]);
  5782. this.bv.validate();
  5783. expect(this.bv.isValid()).toBeTruthy();
  5784. }
  5785. // Invalid samples
  5786. var invalidSamples = ['ES54362315Z', 'ESX2482300A', 'ESJ99216583', '54362315Z', 'X2482300A'];
  5787. for (i in invalidSamples) {
  5788. this.bv.resetForm();
  5789. this.$vat.val(invalidSamples[i]);
  5790. this.bv.validate();
  5791. expect(this.bv.isValid()).toEqual(false);
  5792. }
  5793. });
  5794. it('Finnish VAT number', function() {
  5795. this.bv.updateOption('vat', 'vat', 'country', 'FI');
  5796. // Valid samples
  5797. var validSamples = ['FI20774740', '20774740'];
  5798. for (var i in validSamples) {
  5799. this.bv.resetForm();
  5800. this.$vat.val(validSamples[i]);
  5801. this.bv.validate();
  5802. expect(this.bv.isValid()).toBeTruthy();
  5803. }
  5804. // Invalid samples
  5805. var invalidSamples = ['FI20774741', '20774741'];
  5806. for (i in invalidSamples) {
  5807. this.bv.resetForm();
  5808. this.$vat.val(invalidSamples[i]);
  5809. this.bv.validate();
  5810. expect(this.bv.isValid()).toEqual(false);
  5811. }
  5812. });
  5813. it('French VAT number (TVA)', function() {
  5814. this.bv.updateOption('vat', 'vat', 'country', 'FR');
  5815. // Valid samples
  5816. var validSamples = ['FR40303265045', 'FR23334175221', 'FRK7399859412', 'FR4Z123456782', '40303265045', '23334175221', 'K7399859412'];
  5817. for (var i in validSamples) {
  5818. this.bv.resetForm();
  5819. this.$vat.val(validSamples[i]);
  5820. this.bv.validate();
  5821. expect(this.bv.isValid()).toBeTruthy();
  5822. }
  5823. // Invalid samples
  5824. var invalidSamples = ['FR84323140391', '84323140391'];
  5825. for (i in invalidSamples) {
  5826. this.bv.resetForm();
  5827. this.$vat.val(invalidSamples[i]);
  5828. this.bv.validate();
  5829. expect(this.bv.isValid()).toEqual(false);
  5830. }
  5831. });
  5832. it('United Kingdom VAT number', function() {
  5833. this.bv.updateOption('vat', 'vat', 'country', 'GB');
  5834. // Valid samples
  5835. var validSamples = ['GB980780684', '980780684'];
  5836. for (var i in validSamples) {
  5837. this.bv.resetForm();
  5838. this.$vat.val(validSamples[i]);
  5839. this.bv.validate();
  5840. expect(this.bv.isValid()).toBeTruthy();
  5841. }
  5842. // Invalid samples
  5843. var invalidSamples = ['GB802311781', '802311781'];
  5844. for (i in invalidSamples) {
  5845. this.bv.resetForm();
  5846. this.$vat.val(invalidSamples[i]);
  5847. this.bv.validate();
  5848. expect(this.bv.isValid()).toEqual(false);
  5849. }
  5850. });
  5851. it('Greek VAT number', function() {
  5852. this.bv.updateOption('vat', 'vat', 'country', 'GR');
  5853. // Valid samples
  5854. var validSamples = ['GR023456780', 'EL094259216', '023456780', '094259216'];
  5855. for (var i in validSamples) {
  5856. this.bv.resetForm();
  5857. this.$vat.val(validSamples[i]);
  5858. this.bv.validate();
  5859. expect(this.bv.isValid()).toBeTruthy();
  5860. }
  5861. // Invalid samples
  5862. var invalidSamples = ['GR123456781', '123456781'];
  5863. for (i in invalidSamples) {
  5864. this.bv.resetForm();
  5865. this.$vat.val(invalidSamples[i]);
  5866. this.bv.validate();
  5867. expect(this.bv.isValid()).toEqual(false);
  5868. }
  5869. });
  5870. it('Hungarian VAT number', function() {
  5871. this.bv.updateOption('vat', 'vat', 'country', 'HU');
  5872. // Valid samples
  5873. var validSamples = ['HU12892312', '12892312'];
  5874. for (var i in validSamples) {
  5875. this.bv.resetForm();
  5876. this.$vat.val(validSamples[i]);
  5877. this.bv.validate();
  5878. expect(this.bv.isValid()).toBeTruthy();
  5879. }
  5880. // Invalid samples
  5881. var invalidSamples = ['HU12892313', '12892313'];
  5882. for (i in invalidSamples) {
  5883. this.bv.resetForm();
  5884. this.$vat.val(invalidSamples[i]);
  5885. this.bv.validate();
  5886. expect(this.bv.isValid()).toEqual(false);
  5887. }
  5888. });
  5889. it('Croatian VAT number', function() {
  5890. this.bv.updateOption('vat', 'vat', 'country', 'HR');
  5891. // Valid samples
  5892. var validSamples = ['HR33392005961', '33392005961'];
  5893. for (var i in validSamples) {
  5894. this.bv.resetForm();
  5895. this.$vat.val(validSamples[i]);
  5896. this.bv.validate();
  5897. expect(this.bv.isValid()).toBeTruthy();
  5898. }
  5899. // Invalid samples
  5900. var invalidSamples = ['HR33392005962', '33392005962'];
  5901. for (i in invalidSamples) {
  5902. this.bv.resetForm();
  5903. this.$vat.val(invalidSamples[i]);
  5904. this.bv.validate();
  5905. expect(this.bv.isValid()).toEqual(false);
  5906. }
  5907. });
  5908. it('Irish VAT number', function() {
  5909. this.bv.updateOption('vat', 'vat', 'country', 'IE');
  5910. // Valid samples
  5911. var validSamples = ['IE6433435F', 'IE6433435OA', 'IE8D79739I', '6433435F', '6433435OA', '8D79739I'];
  5912. for (var i in validSamples) {
  5913. this.bv.resetForm();
  5914. this.$vat.val(validSamples[i]);
  5915. this.bv.validate();
  5916. expect(this.bv.isValid()).toBeTruthy();
  5917. }
  5918. // Invalid samples
  5919. var invalidSamples = ['IE8D79738J', '8D79738J'];
  5920. for (i in invalidSamples) {
  5921. this.bv.resetForm();
  5922. this.$vat.val(invalidSamples[i]);
  5923. this.bv.validate();
  5924. expect(this.bv.isValid()).toEqual(false);
  5925. }
  5926. });
  5927. it('Iceland VAT (VSK) number', function() {
  5928. this.bv.updateOption('vat', 'vat', 'country', 'IS');
  5929. // Valid samples
  5930. var validSamples = ['IS11111', 'IS111111', '11111', '111111'];
  5931. for (var i in validSamples) {
  5932. this.bv.resetForm();
  5933. this.$vat.val(validSamples[i]);
  5934. this.bv.validate();
  5935. expect(this.bv.isValid()).toBeTruthy();
  5936. }
  5937. // Invalid samples
  5938. var invalidSamples = ['IS1234567', 'IS123456ABC', '1234567', '123456ABC'];
  5939. for (i in invalidSamples) {
  5940. this.bv.resetForm();
  5941. this.$vat.val(invalidSamples[i]);
  5942. this.bv.validate();
  5943. expect(this.bv.isValid()).toEqual(false);
  5944. }
  5945. });
  5946. it('Italian VAT number', function() {
  5947. this.bv.updateOption('vat', 'vat', 'country', 'IT');
  5948. // Valid samples
  5949. var validSamples = ['IT00743110157', '00743110157'];
  5950. for (var i in validSamples) {
  5951. this.bv.resetForm();
  5952. this.$vat.val(validSamples[i]);
  5953. this.bv.validate();
  5954. expect(this.bv.isValid()).toBeTruthy();
  5955. }
  5956. // Invalid samples
  5957. var invalidSamples = ['IT00743110158', '00743110158'];
  5958. for (i in invalidSamples) {
  5959. this.bv.resetForm();
  5960. this.$vat.val(invalidSamples[i]);
  5961. this.bv.validate();
  5962. expect(this.bv.isValid()).toEqual(false);
  5963. }
  5964. });
  5965. it('Lithuanian VAT number', function() {
  5966. this.bv.updateOption('vat', 'vat', 'country', 'LT');
  5967. // Valid samples
  5968. var validSamples = ['LT119511515', 'LT100001919017', 'LT100004801610', '119511515', '100001919017', '100004801610'];
  5969. for (var i in validSamples) {
  5970. this.bv.resetForm();
  5971. this.$vat.val(validSamples[i]);
  5972. this.bv.validate();
  5973. expect(this.bv.isValid()).toBeTruthy();
  5974. }
  5975. // Invalid samples
  5976. var invalidSamples = ['LT100001919018', '100001919018'];
  5977. for (i in invalidSamples) {
  5978. this.bv.resetForm();
  5979. this.$vat.val(invalidSamples[i]);
  5980. this.bv.validate();
  5981. expect(this.bv.isValid()).toEqual(false);
  5982. }
  5983. });
  5984. it('Luxembourg VAT number', function() {
  5985. this.bv.updateOption('vat', 'vat', 'country', 'LU');
  5986. // Valid samples
  5987. var validSamples = ['LU15027442', '15027442'];
  5988. for (var i in validSamples) {
  5989. this.bv.resetForm();
  5990. this.$vat.val(validSamples[i]);
  5991. this.bv.validate();
  5992. expect(this.bv.isValid()).toBeTruthy();
  5993. }
  5994. // Invalid samples
  5995. var invalidSamples = ['LU15027443', '15027443'];
  5996. for (i in invalidSamples) {
  5997. this.bv.resetForm();
  5998. this.$vat.val(invalidSamples[i]);
  5999. this.bv.validate();
  6000. expect(this.bv.isValid()).toEqual(false);
  6001. }
  6002. });
  6003. it('Latvian VAT number', function() {
  6004. this.bv.updateOption('vat', 'vat', 'country', 'LV');
  6005. // Valid samples
  6006. var validSamples = ['LV40003521600', 'LV16117519997', '40003521600', '16117519997'];
  6007. for (var i in validSamples) {
  6008. this.bv.resetForm();
  6009. this.$vat.val(validSamples[i]);
  6010. this.bv.validate();
  6011. expect(this.bv.isValid()).toBeTruthy();
  6012. }
  6013. // Invalid samples
  6014. var invalidSamples = ['LV40003521601', 'LV16137519997', '40003521601', '16137519997'];
  6015. for (i in invalidSamples) {
  6016. this.bv.resetForm();
  6017. this.$vat.val(invalidSamples[i]);
  6018. this.bv.validate();
  6019. expect(this.bv.isValid()).toEqual(false);
  6020. }
  6021. });
  6022. it('Maltese VAT number', function() {
  6023. this.bv.updateOption('vat', 'vat', 'country', 'MT');
  6024. // Valid samples
  6025. var validSamples = ['MT11679112', '11679112'];
  6026. for (var i in validSamples) {
  6027. this.bv.resetForm();
  6028. this.$vat.val(validSamples[i]);
  6029. this.bv.validate();
  6030. expect(this.bv.isValid()).toBeTruthy();
  6031. }
  6032. // Invalid samples
  6033. var invalidSamples = ['MT11679113', '11679113'];
  6034. for (i in invalidSamples) {
  6035. this.bv.resetForm();
  6036. this.$vat.val(invalidSamples[i]);
  6037. this.bv.validate();
  6038. expect(this.bv.isValid()).toEqual(false);
  6039. }
  6040. });
  6041. it('Dutch VAT number', function() {
  6042. this.bv.updateOption('vat', 'vat', 'country', 'NL');
  6043. // Valid samples
  6044. var validSamples = ['NL004495445B01', '004495445B01'];
  6045. for (var i in validSamples) {
  6046. this.bv.resetForm();
  6047. this.$vat.val(validSamples[i]);
  6048. this.bv.validate();
  6049. expect(this.bv.isValid()).toBeTruthy();
  6050. }
  6051. // Invalid samples
  6052. var invalidSamples = ['NL123456789B90', '123456789B90'];
  6053. for (i in invalidSamples) {
  6054. this.bv.resetForm();
  6055. this.$vat.val(invalidSamples[i]);
  6056. this.bv.validate();
  6057. expect(this.bv.isValid()).toEqual(false);
  6058. }
  6059. });
  6060. it('Polish VAT number', function() {
  6061. this.bv.updateOption('vat', 'vat', 'country', 'PL');
  6062. // Valid samples
  6063. var validSamples = ['PL8567346215', '8567346215'];
  6064. for (var i in validSamples) {
  6065. this.bv.resetForm();
  6066. this.$vat.val(validSamples[i]);
  6067. this.bv.validate();
  6068. expect(this.bv.isValid()).toBeTruthy();
  6069. }
  6070. // Invalid samples
  6071. var invalidSamples = ['PL8567346216', '8567346216'];
  6072. for (i in invalidSamples) {
  6073. this.bv.resetForm();
  6074. this.$vat.val(invalidSamples[i]);
  6075. this.bv.validate();
  6076. expect(this.bv.isValid()).toEqual(false);
  6077. }
  6078. });
  6079. it('Portuguese VAT number', function() {
  6080. this.bv.updateOption('vat', 'vat', 'country', 'PT');
  6081. // Valid samples
  6082. var validSamples = ['PT501964843', '501964843'];
  6083. for (var i in validSamples) {
  6084. this.bv.resetForm();
  6085. this.$vat.val(validSamples[i]);
  6086. this.bv.validate();
  6087. expect(this.bv.isValid()).toBeTruthy();
  6088. }
  6089. // Invalid samples
  6090. var invalidSamples = ['PT501964842', '501964842'];
  6091. for (i in invalidSamples) {
  6092. this.bv.resetForm();
  6093. this.$vat.val(invalidSamples[i]);
  6094. this.bv.validate();
  6095. expect(this.bv.isValid()).toEqual(false);
  6096. }
  6097. });
  6098. it('Romanian VAT number', function() {
  6099. this.bv.updateOption('vat', 'vat', 'country', 'RO');
  6100. // Valid samples
  6101. var validSamples = ['RO18547290', '18547290'];
  6102. for (var i in validSamples) {
  6103. this.bv.resetForm();
  6104. this.$vat.val(validSamples[i]);
  6105. this.bv.validate();
  6106. expect(this.bv.isValid()).toBeTruthy();
  6107. }
  6108. // Invalid samples
  6109. var invalidSamples = ['RO18547291', '18547291'];
  6110. for (i in invalidSamples) {
  6111. this.bv.resetForm();
  6112. this.$vat.val(invalidSamples[i]);
  6113. this.bv.validate();
  6114. expect(this.bv.isValid()).toEqual(false);
  6115. }
  6116. });
  6117. it('Russian VAT number', function() {
  6118. this.bv.updateOption('vat', 'vat', 'country', 'RU');
  6119. // Valid samples
  6120. var validSamples = ['RU7805145876', 'RU781300557475', '7805145876', '781300557475'];
  6121. for (var i in validSamples) {
  6122. this.bv.resetForm();
  6123. this.$vat.val(validSamples[i]);
  6124. this.bv.validate();
  6125. expect(this.bv.isValid()).toBeTruthy();
  6126. }
  6127. // Invalid samples
  6128. var invalidSamples = ['RU7805145877', 'RU781300557474', '7805145877', '781300557474'];
  6129. for (i in invalidSamples) {
  6130. this.bv.resetForm();
  6131. this.$vat.val(invalidSamples[i]);
  6132. this.bv.validate();
  6133. expect(this.bv.isValid()).toEqual(false);
  6134. }
  6135. });
  6136. it('Swedish VAT number', function() {
  6137. this.bv.updateOption('vat', 'vat', 'country', 'SE');
  6138. // Valid samples
  6139. var validSamples = ['SE123456789701', '123456789701'];
  6140. for (var i in validSamples) {
  6141. this.bv.resetForm();
  6142. this.$vat.val(validSamples[i]);
  6143. this.bv.validate();
  6144. expect(this.bv.isValid()).toBeTruthy();
  6145. }
  6146. // Invalid samples
  6147. var invalidSamples = ['SE123456789101', '123456789101'];
  6148. for (i in invalidSamples) {
  6149. this.bv.resetForm();
  6150. this.$vat.val(invalidSamples[i]);
  6151. this.bv.validate();
  6152. expect(this.bv.isValid()).toEqual(false);
  6153. }
  6154. });
  6155. it('Slovenian VAT number', function() {
  6156. this.bv.updateOption('vat', 'vat', 'country', 'SI');
  6157. // Valid samples
  6158. var validSamples = ['SI50223054', '50223054'];
  6159. for (var i in validSamples) {
  6160. this.bv.resetForm();
  6161. this.$vat.val(validSamples[i]);
  6162. this.bv.validate();
  6163. expect(this.bv.isValid()).toBeTruthy();
  6164. }
  6165. // Invalid samples
  6166. var invalidSamples = ['SI50223055', '50223055'];
  6167. for (i in invalidSamples) {
  6168. this.bv.resetForm();
  6169. this.$vat.val(invalidSamples[i]);
  6170. this.bv.validate();
  6171. expect(this.bv.isValid()).toEqual(false);
  6172. }
  6173. });
  6174. it('Slovak VAT number', function() {
  6175. this.bv.updateOption('vat', 'vat', 'country', 'SK');
  6176. // Valid samples
  6177. var validSamples = ['SK2022749619', '2022749619'];
  6178. for (var i in validSamples) {
  6179. this.bv.resetForm();
  6180. this.$vat.val(validSamples[i]);
  6181. this.bv.validate();
  6182. expect(this.bv.isValid()).toBeTruthy();
  6183. }
  6184. // Invalid samples
  6185. var invalidSamples = ['SK2022749618', '2022749618'];
  6186. for (i in invalidSamples) {
  6187. this.bv.resetForm();
  6188. this.$vat.val(invalidSamples[i]);
  6189. this.bv.validate();
  6190. expect(this.bv.isValid()).toEqual(false);
  6191. }
  6192. });
  6193. it('South African VAT number', function() {
  6194. this.bv.updateOption('vat', 'vat', 'country', 'ZA');
  6195. // Valid samples
  6196. var validSamples = ['ZA4012345678', '4012345678'];
  6197. for (var i in validSamples) {
  6198. this.bv.resetForm();
  6199. this.$vat.val(validSamples[i]);
  6200. this.bv.validate();
  6201. expect(this.bv.isValid()).toBeTruthy();
  6202. }
  6203. // Invalid samples
  6204. var invalidSamples = ['ZA40123456789', 'ZA0123456789', '40123456789', '0123456789'];
  6205. for (i in invalidSamples) {
  6206. this.bv.resetForm();
  6207. this.$vat.val(invalidSamples[i]);
  6208. this.bv.validate();
  6209. expect(this.bv.isValid()).toEqual(false);
  6210. }
  6211. });
  6212. it('Venezuelan VAT number (RIF)', function() {
  6213. this.bv.updateOption('vat', 'vat', 'country', 'VE');
  6214. // Valid samples
  6215. var validSamples = ['VEJ309272292', 'VEV242818101', 'VEJ000126518', 'VEJ000458324', 'J309272292', 'V242818101', 'J000126518', 'J000458324'];
  6216. for (var i in validSamples) {
  6217. this.bv.resetForm();
  6218. this.$vat.val(validSamples[i]);
  6219. this.bv.validate();
  6220. expect(this.bv.isValid()).toBeTruthy();
  6221. }
  6222. // Invalid samples
  6223. var invalidSamples = ['VEJ309272293', 'VEV242818100', 'J000126519', 'J000458323'];
  6224. for (i in invalidSamples) {
  6225. this.bv.resetForm();
  6226. this.$vat.val(invalidSamples[i]);
  6227. this.bv.validate();
  6228. expect(this.bv.isValid()).toEqual(false);
  6229. }
  6230. });
  6231. });
  6232. function getCountryCode(value, validator, $field) {
  6233. $('#msg').html('getCountryCode() called');
  6234. return validator.getFieldElements('country').val();
  6235. };
  6236. TestSuite = $.extend({}, TestSuite, {
  6237. ZipCode: {
  6238. getCountryCode: function(value, validator, $field) {
  6239. $('#msg').html('TestSuite.ZipCode.getCountryCode() called');
  6240. return validator.getFieldElements('country').val();
  6241. }
  6242. }
  6243. });
  6244. describe('zipCode', function() {
  6245. beforeEach(function() {
  6246. $([
  6247. '<form class="form-horizontal" id="zipCodeForm">',
  6248. '<div id="msg"></div>',
  6249. '<div class="form-group">',
  6250. '<label class="col-md-3 control-label">Country:</label>',
  6251. '<div class="col-md-2">',
  6252. '<select class="form-control" name="country">',
  6253. '<option value="">Select a country</option>',
  6254. '<option value="US">United States</option>',
  6255. '<option value="CA">Canada</option>',
  6256. '<option value="DK">Denmark</option>',
  6257. '<option value="IT">Italy</option>',
  6258. '<option value="NL">Netherlands</option>',
  6259. '<option value="SE">Sweden</option>',
  6260. '<option value="GB">United Kingdom</option>',
  6261. '</select>',
  6262. '</div>',
  6263. '</div>',
  6264. '<div class="form-group">',
  6265. '<label class="col-md-3 control-label">Zipcode</label>',
  6266. '<div class="col-md-2">',
  6267. '<input type="text" class="form-control" name="zc" data-bv-zipcode data-bv-zipcode-country="US" />',
  6268. '</div>',
  6269. '</div>',
  6270. '</form>'
  6271. ].join('\n')).appendTo('body');
  6272. $('#zipCodeForm').bootstrapValidator();
  6273. /**
  6274. * @type {BootstrapValidator}
  6275. */
  6276. this.bv = $('#zipCodeForm').data('bootstrapValidator');
  6277. this.$country = this.bv.getFieldElements('country');
  6278. this.$zipCode = this.bv.getFieldElements('zc');
  6279. });
  6280. afterEach(function() {
  6281. $('#zipCodeForm').bootstrapValidator('destroy').remove();
  6282. });
  6283. it('country code US', function() {
  6284. this.$zipCode.val('12345');
  6285. this.bv.validate();
  6286. expect(this.bv.isValid()).toBeTruthy();
  6287. this.bv.resetForm();
  6288. this.$zipCode.val('123');
  6289. this.bv.validate();
  6290. expect(this.bv.isValid()).toEqual(false);
  6291. });
  6292. it('country code updateOption()', function() {
  6293. // Check IT postal code
  6294. this.bv.updateOption('zc', 'zipCode', 'country', 'IT');
  6295. this.$zipCode.val('1234');
  6296. this.bv.validate();
  6297. expect(this.bv.isValid()).toEqual(false);
  6298. this.bv.resetForm();
  6299. this.$zipCode.val('IT-12345');
  6300. this.bv.validate();
  6301. expect(this.bv.isValid()).toBeTruthy();
  6302. // Check United Kingdom postal code
  6303. this.bv.updateOption('zc', 'zipCode', 'country', 'GB');
  6304. var validUkSamples = ['EC1A 1BB', 'W1A 1HQ', 'M1 1AA', 'B33 8TH', 'CR2 6XH', 'DN55 1PT', 'AI-2640', 'ASCN 1ZZ', 'GIR 0AA'];
  6305. for (var i in validUkSamples) {
  6306. this.bv.resetForm();
  6307. this.$zipCode.val(validUkSamples[i]);
  6308. this.bv.validate();
  6309. expect(this.bv.isValid()).toBeTruthy();
  6310. }
  6311. });
  6312. it('country code other field declarative', function() {
  6313. this.$zipCode.attr('data-bv-zipcode-country', 'country');
  6314. // Need to destroy the plugin instance ...
  6315. $('#zipCodeForm').bootstrapValidator('destroy');
  6316. // ... and re-create it
  6317. this.bv = $('#zipCodeForm').bootstrapValidator().data('bootstrapValidator');
  6318. this.$country.val('IT');
  6319. this.bv.resetForm();
  6320. this.$zipCode.val('1234');
  6321. this.bv.validate();
  6322. expect(this.bv.isValid()).toEqual(false);
  6323. this.bv.resetForm();
  6324. this.$zipCode.val('I-12345');
  6325. this.bv.validate();
  6326. expect(this.bv.isValid()).toBeTruthy();
  6327. });
  6328. it('country code callback declarative function', function() {
  6329. this.$zipCode.attr('data-bv-zipcode-country', 'getCountryCode');
  6330. $('#zipCodeForm').bootstrapValidator('destroy');
  6331. this.bv = $('#zipCodeForm').bootstrapValidator().data('bootstrapValidator');
  6332. this.$country.val('NL');
  6333. this.$zipCode.val('0123');
  6334. this.bv.validate();
  6335. expect($('#msg').html()).toEqual('getCountryCode() called');
  6336. expect(this.bv.isValid()).toEqual(false);
  6337. });
  6338. it('country code callback declarative function()', function() {
  6339. this.$zipCode.attr('data-bv-zipcode-country', 'getCountryCode()');
  6340. $('#zipCodeForm').bootstrapValidator('destroy');
  6341. this.bv = $('#zipCodeForm').bootstrapValidator().data('bootstrapValidator');
  6342. this.$country.val('NL');
  6343. this.$zipCode.val('1234 ab');
  6344. this.bv.validate();
  6345. expect($('#msg').html()).toEqual('getCountryCode() called');
  6346. expect(this.bv.isValid()).toBeTruthy();
  6347. });
  6348. it('country code callback declarative A.B.C', function() {
  6349. this.$zipCode.attr('data-bv-zipcode-country', 'TestSuite.ZipCode.getCountryCode');
  6350. $('#zipCodeForm').bootstrapValidator('destroy');
  6351. this.bv = $('#zipCodeForm').bootstrapValidator().data('bootstrapValidator');
  6352. this.$country.val('DK');
  6353. this.$zipCode.val('DK 123');
  6354. this.bv.validate();
  6355. expect($('#msg').html()).toEqual('TestSuite.ZipCode.getCountryCode() called');
  6356. expect(this.bv.isValid()).toEqual(false);
  6357. });
  6358. it('country code callback declarative A.B.C()', function() {
  6359. this.$zipCode.attr('data-bv-zipcode-country', 'TestSuite.ZipCode.getCountryCode()');
  6360. $('#zipCodeForm').bootstrapValidator('destroy');
  6361. this.bv = $('#zipCodeForm').bootstrapValidator().data('bootstrapValidator');
  6362. this.$country.val('DK');
  6363. this.$zipCode.val('DK-1234');
  6364. this.bv.validate();
  6365. expect($('#msg').html()).toEqual('TestSuite.ZipCode.getCountryCode() called');
  6366. expect(this.bv.isValid()).toBeTruthy();
  6367. });
  6368. it('country code callback programmatically', function() {
  6369. this.$zipCode.removeAttr('data-bv-zipcode-country');
  6370. $('#zipCodeForm').bootstrapValidator('destroy');
  6371. this.bv = $('#zipCodeForm')
  6372. .bootstrapValidator({
  6373. fields: {
  6374. zc: {
  6375. validators: {
  6376. zipCode: {
  6377. country: function(value, validator, $field) {
  6378. return getCountryCode(value, validator, $field);
  6379. }
  6380. }
  6381. }
  6382. }
  6383. }
  6384. })
  6385. .data('bootstrapValidator');
  6386. this.$country.val('SE');
  6387. this.bv.resetForm();
  6388. this.$zipCode.val('S-567 8');
  6389. this.bv.validate();
  6390. expect($('#msg').html()).toEqual('getCountryCode() called');
  6391. expect(this.bv.isValid()).toEqual(false);
  6392. this.bv.resetForm();
  6393. this.$zipCode.val('S-12345');
  6394. this.bv.validate();
  6395. expect($('#msg').html()).toEqual('getCountryCode() called');
  6396. expect(this.bv.isValid()).toBeTruthy();
  6397. });
  6398. it('not supported country code', function() {
  6399. this.$zipCode.attr('data-bv-zipcode-country', 'NOT_SUPPORTED');
  6400. $('#zipCodeForm').bootstrapValidator('destroy');
  6401. this.bv = $('#zipCodeForm').bootstrapValidator().data('bootstrapValidator');
  6402. this.bv.resetForm();
  6403. this.$zipCode.val('1234');
  6404. this.bv.validate();
  6405. expect(this.bv.isValid()).toEqual(false);
  6406. expect(this.bv.getMessages(this.$zipCode, 'zipCode')[0]).toEqual($.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.zipCode.countryNotSupported, 'NOT_SUPPORTED'));
  6407. });
  6408. });