spec.js 297 KB

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