spec.js 241 KB

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