spec.js 253 KB

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