spec.js 295 KB

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