spec.js 289 KB

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