spec.js 267 KB

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