spec.js 275 KB

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