spec.js 280 KB

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