spec.js 243 KB

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