spec.js 205 KB

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