spec.js 285 KB

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