bootstrapValidator.js 273 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171
  1. /*!
  2. * BootstrapValidator (http://bootstrapvalidator.com)
  3. * The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3
  4. *
  5. * @version v0.5.2-dev, built on 2014-09-06 1:12:02 PM
  6. * @author https://twitter.com/nghuuphuoc
  7. * @copyright (c) 2013 - 2014 Nguyen Huu Phuoc
  8. * @license MIT
  9. */
  10. (function($) {
  11. var BootstrapValidator = function(form, options) {
  12. this.$form = $(form);
  13. this.options = $.extend({}, $.fn.bootstrapValidator.DEFAULT_OPTIONS, options);
  14. this.$invalidFields = $([]); // Array of invalid fields
  15. this.$submitButton = null; // The submit button which is clicked to submit form
  16. this.$hiddenButton = null;
  17. // Validating status
  18. this.STATUS_NOT_VALIDATED = 'NOT_VALIDATED';
  19. this.STATUS_VALIDATING = 'VALIDATING';
  20. this.STATUS_INVALID = 'INVALID';
  21. this.STATUS_VALID = 'VALID';
  22. // Determine the event that is fired when user change the field value
  23. // Most modern browsers supports input event except IE 7, 8.
  24. // IE 9 supports input event but the event is still not fired if I press the backspace key.
  25. // Get IE version
  26. // https://gist.github.com/padolsey/527683/#comment-7595
  27. var ieVersion = (function() {
  28. var v = 3, div = document.createElement('div'), a = div.all || [];
  29. while (div.innerHTML = '<!--[if gt IE '+(++v)+']><br><![endif]-->', a[0]) {}
  30. return v > 4 ? v : !v;
  31. }());
  32. var el = document.createElement('div');
  33. this._changeEvent = (ieVersion === 9 || !('oninput' in el)) ? 'keyup' : 'input';
  34. // The flag to indicate that the form is ready to submit when a remote/callback validator returns
  35. this._submitIfValid = null;
  36. // Field elements
  37. this._cacheFields = {};
  38. this._init();
  39. };
  40. BootstrapValidator.prototype = {
  41. constructor: BootstrapValidator,
  42. /**
  43. * Init form
  44. */
  45. _init: function() {
  46. var that = this,
  47. options = {
  48. container: this.$form.attr('data-bv-container'),
  49. events: {
  50. formInit: this.$form.attr('data-bv-events-form-init'),
  51. formError: this.$form.attr('data-bv-events-form-error'),
  52. formSuccess: this.$form.attr('data-bv-events-form-success'),
  53. fieldAdded: this.$form.attr('data-bv-events-field-added'),
  54. fieldRemoved: this.$form.attr('data-bv-events-field-removed'),
  55. fieldInit: this.$form.attr('data-bv-events-field-init'),
  56. fieldError: this.$form.attr('data-bv-events-field-error'),
  57. fieldSuccess: this.$form.attr('data-bv-events-field-success'),
  58. fieldStatus: this.$form.attr('data-bv-events-field-status'),
  59. validatorError: this.$form.attr('data-bv-events-validator-error'),
  60. validatorSuccess: this.$form.attr('data-bv-events-validator-success')
  61. },
  62. excluded: this.$form.attr('data-bv-excluded'),
  63. feedbackIcons: {
  64. valid: this.$form.attr('data-bv-feedbackicons-valid'),
  65. invalid: this.$form.attr('data-bv-feedbackicons-invalid'),
  66. validating: this.$form.attr('data-bv-feedbackicons-validating')
  67. },
  68. group: this.$form.attr('data-bv-group'),
  69. live: this.$form.attr('data-bv-live'),
  70. message: this.$form.attr('data-bv-message'),
  71. onError: this.$form.attr('data-bv-onerror'),
  72. onSuccess: this.$form.attr('data-bv-onsuccess'),
  73. submitButtons: this.$form.attr('data-bv-submitbuttons'),
  74. threshold: this.$form.attr('data-bv-threshold'),
  75. trigger: this.$form.attr('data-bv-trigger'),
  76. verbose: this.$form.attr('data-bv-verbose'),
  77. fields: {}
  78. };
  79. this.$form
  80. // Disable client side validation in HTML 5
  81. .attr('novalidate', 'novalidate')
  82. .addClass(this.options.elementClass)
  83. // Disable the default submission first
  84. .on('submit.bv', function(e) {
  85. e.preventDefault();
  86. that.validate();
  87. })
  88. .on('click.bv', this.options.submitButtons, function() {
  89. that.$submitButton = $(this);
  90. // The user just click the submit button
  91. that._submitIfValid = true;
  92. })
  93. // Find all fields which have either "name" or "data-bv-field" attribute
  94. .find('[name], [data-bv-field]')
  95. .each(function() {
  96. var $field = $(this),
  97. field = $field.attr('name') || $field.attr('data-bv-field'),
  98. opts = that._parseOptions($field);
  99. if (opts) {
  100. $field.attr('data-bv-field', field);
  101. options.fields[field] = $.extend({}, opts, options.fields[field]);
  102. }
  103. });
  104. this.options = $.extend(true, this.options, options);
  105. // When pressing Enter on any field in the form, the first submit button will do its job.
  106. // The form then will be submitted.
  107. // I create a first hidden submit button
  108. this.$hiddenButton = $('<button/>')
  109. .attr('type', 'submit')
  110. .prependTo(this.$form)
  111. .addClass('bv-hidden-submit')
  112. .css({ display: 'none', width: 0, height: 0 });
  113. this.$form
  114. .on('click.bv', '[type="submit"]', function(e) {
  115. // Don't perform validation when clicking on the submit button/input
  116. // which aren't defined by the 'submitButtons' option
  117. var $button = $(e.target).eq(0);
  118. if (that.options.submitButtons && !$button.is(that.options.submitButtons) && !$button.is(that.$hiddenButton)) {
  119. that.$form.off('submit.bv').submit();
  120. }
  121. });
  122. for (var field in this.options.fields) {
  123. this._initField(field);
  124. }
  125. this.$form.trigger($.Event(this.options.events.formInit), {
  126. bv: this,
  127. options: this.options
  128. });
  129. // Prepare the events
  130. if (this.options.onSuccess) {
  131. this.$form.on(this.options.events.formSuccess, function(e) {
  132. $.fn.bootstrapValidator.helpers.call(that.options.onSuccess, [e]);
  133. });
  134. }
  135. if (this.options.onError) {
  136. this.$form.on(this.options.events.formError, function(e) {
  137. $.fn.bootstrapValidator.helpers.call(that.options.onError, [e]);
  138. });
  139. }
  140. },
  141. /**
  142. * Parse the validator options from HTML attributes
  143. *
  144. * @param {jQuery} $field The field element
  145. * @returns {Object}
  146. */
  147. _parseOptions: function($field) {
  148. var field = $field.attr('name') || $field.attr('data-bv-field'),
  149. validators = {},
  150. validator,
  151. v, // Validator name
  152. enabled,
  153. optionName,
  154. optionValue,
  155. html5AttrName,
  156. html5AttrMap;
  157. for (v in $.fn.bootstrapValidator.validators) {
  158. validator = $.fn.bootstrapValidator.validators[v];
  159. enabled = $field.attr('data-bv-' + v.toLowerCase()) + '';
  160. html5AttrMap = ('function' === typeof validator.enableByHtml5) ? validator.enableByHtml5($field) : null;
  161. if ((html5AttrMap && enabled !== 'false')
  162. || (html5AttrMap !== true && ('' === enabled || 'true' === enabled)))
  163. {
  164. // Try to parse the options via attributes
  165. validator.html5Attributes = $.extend({}, { message: 'message', onerror: 'onError', onsuccess: 'onSuccess' }, validator.html5Attributes);
  166. validators[v] = $.extend({}, html5AttrMap === true ? {} : html5AttrMap, validators[v]);
  167. for (html5AttrName in validator.html5Attributes) {
  168. optionName = validator.html5Attributes[html5AttrName];
  169. optionValue = $field.attr('data-bv-' + v.toLowerCase() + '-' + html5AttrName);
  170. if (optionValue) {
  171. if ('true' === optionValue) {
  172. optionValue = true;
  173. } else if ('false' === optionValue) {
  174. optionValue = false;
  175. }
  176. validators[v][optionName] = optionValue;
  177. }
  178. }
  179. }
  180. }
  181. var opts = {
  182. container: $field.attr('data-bv-container'),
  183. excluded: $field.attr('data-bv-excluded'),
  184. feedbackIcons: $field.attr('data-bv-feedbackicons'),
  185. group: $field.attr('data-bv-group'),
  186. message: $field.attr('data-bv-message'),
  187. onError: $field.attr('data-bv-onerror'),
  188. onStatus: $field.attr('data-bv-onstatus'),
  189. onSuccess: $field.attr('data-bv-onsuccess'),
  190. selector: $field.attr('data-bv-selector'),
  191. threshold: $field.attr('data-bv-threshold'),
  192. trigger: $field.attr('data-bv-trigger'),
  193. verbose: $field.attr('data-bv-verbose'),
  194. validators: validators
  195. },
  196. emptyOptions = $.isEmptyObject(opts), // Check if the field options are set using HTML attributes
  197. emptyValidators = $.isEmptyObject(validators); // Check if the field validators are set using HTML attributes
  198. if (!emptyValidators || (!emptyOptions && this.options.fields && this.options.fields[field])) {
  199. opts.validators = validators;
  200. return opts;
  201. } else {
  202. return null;
  203. }
  204. },
  205. /**
  206. * Init field
  207. *
  208. * @param {String|jQuery} field The field name or field element
  209. */
  210. _initField: function(field) {
  211. var fields = $([]);
  212. switch (typeof field) {
  213. case 'object':
  214. fields = field;
  215. field = field.attr('data-bv-field');
  216. break;
  217. case 'string':
  218. fields = this.getFieldElements(field);
  219. fields.attr('data-bv-field', field);
  220. break;
  221. default:
  222. break;
  223. }
  224. // We don't need to validate non-existing fields
  225. if (fields.length === 0) {
  226. return;
  227. }
  228. if (this.options.fields[field] === null || this.options.fields[field].validators === null) {
  229. return;
  230. }
  231. var validatorName;
  232. for (validatorName in this.options.fields[field].validators) {
  233. if (!$.fn.bootstrapValidator.validators[validatorName]) {
  234. delete this.options.fields[field].validators[validatorName];
  235. }
  236. }
  237. if (this.options.fields[field].enabled === null) {
  238. this.options.fields[field].enabled = true;
  239. }
  240. var that = this,
  241. total = fields.length,
  242. type = fields.attr('type'),
  243. updateAll = (total === 1) || ('radio' === type) || ('checkbox' === type),
  244. event = ('radio' === type || 'checkbox' === type || 'file' === type || 'SELECT' === fields.eq(0).get(0).tagName) ? 'change' : this._changeEvent,
  245. trigger = (this.options.fields[field].trigger || this.options.trigger || event).split(' '),
  246. events = $.map(trigger, function(item) {
  247. return item + '.update.bv';
  248. }).join(' ');
  249. for (var i = 0; i < total; i++) {
  250. var $field = fields.eq(i),
  251. group = this.options.fields[field].group || this.options.group,
  252. $parent = $field.parents(group),
  253. // Allow user to indicate where the error messages are shown
  254. container = ('function' === typeof (this.options.fields[field].container || this.options.container)) ? (this.options.fields[field].container || this.options.container).call(this, $field, this) : (this.options.fields[field].container || this.options.container),
  255. $message = (container && container !== 'tooltip' && container !== 'popover') ? $(container) : this._getMessageContainer($field, group);
  256. if (container && container !== 'tooltip' && container !== 'popover') {
  257. $message.addClass('has-error');
  258. }
  259. // Remove all error messages and feedback icons
  260. $message.find('.help-block[data-bv-validator][data-bv-for="' + field + '"]').remove();
  261. $parent.find('i[data-bv-icon-for="' + field + '"]').remove();
  262. // Whenever the user change the field value, mark it as not validated yet
  263. $field.off(events).on(events, function() {
  264. that.updateStatus($(this), that.STATUS_NOT_VALIDATED);
  265. });
  266. // Create help block elements for showing the error messages
  267. $field.data('bv.messages', $message);
  268. for (validatorName in this.options.fields[field].validators) {
  269. $field.data('bv.result.' + validatorName, this.STATUS_NOT_VALIDATED);
  270. if (!updateAll || i === total - 1) {
  271. $('<small/>')
  272. .css('display', 'none')
  273. .addClass('help-block')
  274. .attr('data-bv-validator', validatorName)
  275. .attr('data-bv-for', field)
  276. .attr('data-bv-result', this.STATUS_NOT_VALIDATED)
  277. .html(this._getMessage(field, validatorName))
  278. .appendTo($message);
  279. }
  280. // Init the validator
  281. if ('function' === typeof $.fn.bootstrapValidator.validators[validatorName].init) {
  282. $.fn.bootstrapValidator.validators[validatorName].init(this, $field, this.options.fields[field].validators[validatorName]);
  283. }
  284. // Prepare the validator events
  285. if (this.options.fields[field].validators[validatorName].onSuccess) {
  286. $field.on(this.options.events.validatorSuccess, function(e, data) {
  287. $.fn.bootstrapValidator.helpers.call(that.options.fields[field].validators[validatorName].onSuccess, [e, data]);
  288. });
  289. }
  290. if (this.options.fields[field].validators[validatorName].onError) {
  291. $field.on(this.options.events.validatorError, function(e, data) {
  292. $.fn.bootstrapValidator.helpers.call(that.options.fields[field].validators[validatorName].onError, [e, data]);
  293. });
  294. }
  295. }
  296. // Prepare the feedback icons
  297. // Available from Bootstrap 3.1 (http://getbootstrap.com/css/#forms-control-validation)
  298. if (this.options.fields[field].feedbackIcons !== false && this.options.fields[field].feedbackIcons !== 'false'
  299. && this.options.feedbackIcons
  300. && this.options.feedbackIcons.validating && this.options.feedbackIcons.invalid && this.options.feedbackIcons.valid
  301. && (!updateAll || i === total - 1))
  302. {
  303. $parent.removeClass('has-success').removeClass('has-error').addClass('has-feedback');
  304. var $icon = $('<i/>')
  305. .css('display', 'none')
  306. .addClass('form-control-feedback')
  307. .attr('data-bv-icon-for', field)
  308. .insertAfter($field);
  309. // Place it after the container of checkbox/radio
  310. // so when clicking the icon, it doesn't effect to the checkbox/radio element
  311. if ('checkbox' === type || 'radio' === type) {
  312. var $fieldParent = $field.parent();
  313. if ($fieldParent.hasClass(type)) {
  314. $icon.insertAfter($fieldParent);
  315. } else if ($fieldParent.parent().hasClass(type)) {
  316. $icon.insertAfter($fieldParent.parent());
  317. }
  318. }
  319. // The feedback icon does not render correctly if there is no label
  320. // https://github.com/twbs/bootstrap/issues/12873
  321. if ($parent.find('label').length === 0) {
  322. $icon.addClass('bv-no-label');
  323. }
  324. // Fix feedback icons in input-group
  325. if ($parent.find('.input-group').length !== 0) {
  326. $icon.addClass('bv-icon-input-group')
  327. .insertAfter($parent.find('.input-group').eq(0));
  328. }
  329. }
  330. }
  331. // Prepare the events
  332. if (this.options.fields[field].onSuccess) {
  333. fields.on(this.options.events.fieldSuccess, function(e, data) {
  334. $.fn.bootstrapValidator.helpers.call(that.options.fields[field].onSuccess, [e, data]);
  335. });
  336. }
  337. if (this.options.fields[field].onError) {
  338. fields.on(this.options.events.fieldError, function(e, data) {
  339. $.fn.bootstrapValidator.helpers.call(that.options.fields[field].onError, [e, data]);
  340. });
  341. }
  342. if (this.options.fields[field].onStatus) {
  343. fields.on(this.options.events.fieldStatus, function(e, data) {
  344. $.fn.bootstrapValidator.helpers.call(that.options.fields[field].onStatus, [e, data]);
  345. });
  346. }
  347. // Set live mode
  348. events = $.map(trigger, function(item) {
  349. return item + '.live.bv';
  350. }).join(' ');
  351. switch (this.options.live) {
  352. case 'submitted':
  353. break;
  354. case 'disabled':
  355. fields.off(events);
  356. break;
  357. case 'enabled':
  358. /* falls through */
  359. default:
  360. fields.off(events).on(events, function() {
  361. if (that._exceedThreshold($(this))) {
  362. that.validateField($(this));
  363. }
  364. });
  365. break;
  366. }
  367. fields.trigger($.Event(this.options.events.fieldInit), {
  368. bv: this,
  369. field: field,
  370. element: fields
  371. });
  372. },
  373. /**
  374. * Get the error message for given field and validator
  375. *
  376. * @param {String} field The field name
  377. * @param {String} validatorName The validator name
  378. * @returns {String}
  379. */
  380. _getMessage: function(field, validatorName) {
  381. if (!this.options.fields[field] || !$.fn.bootstrapValidator.validators[validatorName]
  382. || !this.options.fields[field].validators || !this.options.fields[field].validators[validatorName])
  383. {
  384. return '';
  385. }
  386. var options = this.options.fields[field].validators[validatorName];
  387. switch (true) {
  388. case (!!options.message):
  389. return options.message;
  390. case (!!this.options.fields[field].message):
  391. return this.options.fields[field].message;
  392. case (!!$.fn.bootstrapValidator.i18n[validatorName]):
  393. return $.fn.bootstrapValidator.i18n[validatorName]['default'];
  394. default:
  395. return this.options.message;
  396. }
  397. },
  398. /**
  399. * Get the element to place the error messages
  400. *
  401. * @param {jQuery} $field The field element
  402. * @param {String} group
  403. * @returns {jQuery}
  404. */
  405. _getMessageContainer: function($field, group) {
  406. var $parent = $field.parent();
  407. if ($parent.is(group)) {
  408. return $parent;
  409. }
  410. var cssClasses = $parent.attr('class');
  411. if (!cssClasses) {
  412. return this._getMessageContainer($parent, group);
  413. }
  414. cssClasses = cssClasses.split(' ');
  415. var n = cssClasses.length;
  416. for (var i = 0; i < n; i++) {
  417. if (/^col-(xs|sm|md|lg)-\d+$/.test(cssClasses[i]) || /^col-(xs|sm|md|lg)-offset-\d+$/.test(cssClasses[i])) {
  418. return $parent;
  419. }
  420. }
  421. return this._getMessageContainer($parent, group);
  422. },
  423. /**
  424. * Called when all validations are completed
  425. */
  426. _submit: function() {
  427. var isValid = this.isValid(),
  428. eventType = isValid ? this.options.events.formSuccess : this.options.events.formError,
  429. e = $.Event(eventType);
  430. this.$form.trigger(e);
  431. // Call default handler
  432. // Check if whether the submit button is clicked
  433. if (this.$submitButton) {
  434. isValid ? this._onSuccess(e) : this._onError(e);
  435. }
  436. },
  437. /**
  438. * Check if the field is excluded.
  439. * Returning true means that the field will not be validated
  440. *
  441. * @param {jQuery} $field The field element
  442. * @returns {Boolean}
  443. */
  444. _isExcluded: function($field) {
  445. var excludedAttr = $field.attr('data-bv-excluded'),
  446. // I still need to check the 'name' attribute while initializing the field
  447. field = $field.attr('data-bv-field') || $field.attr('name');
  448. switch (true) {
  449. case (!!field && this.options.fields && this.options.fields[field] && (this.options.fields[field].excluded === 'true' || this.options.fields[field].excluded === true)):
  450. case (excludedAttr === 'true'):
  451. case (excludedAttr === ''):
  452. return true;
  453. case (!!field && this.options.fields && this.options.fields[field] && (this.options.fields[field].excluded === 'false' || this.options.fields[field].excluded === false)):
  454. case (excludedAttr === 'false'):
  455. return false;
  456. default:
  457. if (this.options.excluded) {
  458. // Convert to array first
  459. if ('string' === typeof this.options.excluded) {
  460. this.options.excluded = $.map(this.options.excluded.split(','), function(item) {
  461. // Trim the spaces
  462. return $.trim(item);
  463. });
  464. }
  465. var length = this.options.excluded.length;
  466. for (var i = 0; i < length; i++) {
  467. if (('string' === typeof this.options.excluded[i] && $field.is(this.options.excluded[i]))
  468. || ('function' === typeof this.options.excluded[i] && this.options.excluded[i].call(this, $field, this) === true))
  469. {
  470. return true;
  471. }
  472. }
  473. }
  474. return false;
  475. }
  476. },
  477. /**
  478. * Check if the number of characters of field value exceed the threshold or not
  479. *
  480. * @param {jQuery} $field The field element
  481. * @returns {Boolean}
  482. */
  483. _exceedThreshold: function($field) {
  484. var field = $field.attr('data-bv-field'),
  485. threshold = this.options.fields[field].threshold || this.options.threshold;
  486. if (!threshold) {
  487. return true;
  488. }
  489. var cannotType = $.inArray($field.attr('type'), ['button', 'checkbox', 'file', 'hidden', 'image', 'radio', 'reset', 'submit']) !== -1;
  490. return (cannotType || $field.val().length >= threshold);
  491. },
  492. // ---
  493. // Events
  494. // ---
  495. /**
  496. * The default handler of error.form.bv event.
  497. * It will be called when there is a invalid field
  498. *
  499. * @param {jQuery.Event} e The jQuery event object
  500. */
  501. _onError: function(e) {
  502. if (e.isDefaultPrevented()) {
  503. return;
  504. }
  505. if ('submitted' === this.options.live) {
  506. // Enable live mode
  507. this.options.live = 'enabled';
  508. var that = this;
  509. for (var field in this.options.fields) {
  510. (function(f) {
  511. var fields = that.getFieldElements(f);
  512. if (fields.length) {
  513. var type = $(fields[0]).attr('type'),
  514. event = ('radio' === type || 'checkbox' === type || 'file' === type || 'SELECT' === $(fields[0]).get(0).tagName) ? 'change' : that._changeEvent,
  515. trigger = that.options.fields[field].trigger || that.options.trigger || event,
  516. events = $.map(trigger.split(' '), function(item) {
  517. return item + '.live.bv';
  518. }).join(' ');
  519. fields.off(events).on(events, function() {
  520. if (that._exceedThreshold($(this))) {
  521. that.validateField($(this));
  522. }
  523. });
  524. }
  525. })(field);
  526. }
  527. }
  528. var $invalidField = this.$invalidFields.eq(0);
  529. if ($invalidField) {
  530. // Activate the tab containing the invalid field if exists
  531. var $tabPane = $invalidField.parents('.tab-pane'), tabId;
  532. if ($tabPane && (tabId = $tabPane.attr('id'))) {
  533. $('a[href="#' + tabId + '"][data-toggle="tab"]').tab('show');
  534. }
  535. // Focus to the first invalid field
  536. $invalidField.focus();
  537. }
  538. },
  539. /**
  540. * The default handler of success.form.bv event.
  541. * It will be called when all the fields are valid
  542. *
  543. * @param {jQuery.Event} e The jQuery event object
  544. */
  545. _onSuccess: function(e) {
  546. if (e.isDefaultPrevented()) {
  547. return;
  548. }
  549. // Submit the form
  550. this.disableSubmitButtons(true).defaultSubmit();
  551. },
  552. /**
  553. * Called after validating a field element
  554. *
  555. * @param {jQuery} $field The field element
  556. * @param {String} [validatorName] The validator name
  557. */
  558. _onFieldValidated: function($field, validatorName) {
  559. var field = $field.attr('data-bv-field'),
  560. validators = this.options.fields[field].validators,
  561. counter = {},
  562. numValidators = 0,
  563. data = {
  564. bv: this,
  565. field: field,
  566. element: $field,
  567. validator: validatorName
  568. };
  569. // Trigger an event after given validator completes
  570. if (validatorName) {
  571. switch ($field.data('bv.result.' + validatorName)) {
  572. case this.STATUS_INVALID:
  573. $field.trigger($.Event(this.options.events.validatorError), data);
  574. break;
  575. case this.STATUS_VALID:
  576. $field.trigger($.Event(this.options.events.validatorSuccess), data);
  577. break;
  578. default:
  579. break;
  580. }
  581. }
  582. counter[this.STATUS_NOT_VALIDATED] = 0;
  583. counter[this.STATUS_VALIDATING] = 0;
  584. counter[this.STATUS_INVALID] = 0;
  585. counter[this.STATUS_VALID] = 0;
  586. for (var v in validators) {
  587. if (validators[v].enabled === false) {
  588. continue;
  589. }
  590. numValidators++;
  591. var result = $field.data('bv.result.' + v);
  592. if (result) {
  593. counter[result]++;
  594. }
  595. }
  596. if (counter[this.STATUS_VALID] === numValidators) {
  597. // Remove from the list of invalid fields
  598. this.$invalidFields = this.$invalidFields.not($field);
  599. $field.trigger($.Event(this.options.events.fieldSuccess), data);
  600. }
  601. // If all validators are completed and there is at least one validator which doesn't pass
  602. else if (counter[this.STATUS_NOT_VALIDATED] === 0 && counter[this.STATUS_VALIDATING] === 0 && counter[this.STATUS_INVALID] > 0) {
  603. // Add to the list of invalid fields
  604. this.$invalidFields = this.$invalidFields.add($field);
  605. $field.trigger($.Event(this.options.events.fieldError), data);
  606. }
  607. },
  608. // ---
  609. // Public methods
  610. // ---
  611. /**
  612. * Retrieve the field elements by given name
  613. *
  614. * @param {String} field The field name
  615. * @returns {null|jQuery[]}
  616. */
  617. getFieldElements: function(field) {
  618. if (!this._cacheFields[field]) {
  619. this._cacheFields[field] = (this.options.fields[field] && this.options.fields[field].selector)
  620. ? $(this.options.fields[field].selector)
  621. : this.$form.find('[name="' + field + '"]');
  622. }
  623. return this._cacheFields[field];
  624. },
  625. /**
  626. * Disable/enable submit buttons
  627. *
  628. * @param {Boolean} disabled Can be true or false
  629. * @returns {BootstrapValidator}
  630. */
  631. disableSubmitButtons: function(disabled) {
  632. if (!disabled) {
  633. this.$form.find(this.options.submitButtons).removeAttr('disabled');
  634. } else if (this.options.live !== 'disabled') {
  635. // Don't disable if the live validating mode is disabled
  636. this.$form.find(this.options.submitButtons).attr('disabled', 'disabled');
  637. }
  638. return this;
  639. },
  640. /**
  641. * Validate the form
  642. *
  643. * @returns {BootstrapValidator}
  644. */
  645. validate: function() {
  646. if (!this.options.fields) {
  647. return this;
  648. }
  649. this.disableSubmitButtons(true);
  650. for (var field in this.options.fields) {
  651. this.validateField(field);
  652. }
  653. this._submit();
  654. return this;
  655. },
  656. /**
  657. * Validate given field
  658. *
  659. * @param {String|jQuery} field The field name or field element
  660. * @returns {BootstrapValidator}
  661. */
  662. validateField: function(field) {
  663. var fields = $([]);
  664. switch (typeof field) {
  665. case 'object':
  666. fields = field;
  667. field = field.attr('data-bv-field');
  668. break;
  669. case 'string':
  670. fields = this.getFieldElements(field);
  671. break;
  672. default:
  673. break;
  674. }
  675. if (fields.length === 0 || (this.options.fields[field] && this.options.fields[field].enabled === false)) {
  676. return this;
  677. }
  678. var that = this,
  679. type = fields.attr('type'),
  680. total = ('radio' === type || 'checkbox' === type) ? 1 : fields.length,
  681. updateAll = ('radio' === type || 'checkbox' === type),
  682. validators = this.options.fields[field].validators,
  683. verbose = this.options.fields[field].verbose === 'true' || this.options.fields[field].verbose === true || this.options.verbose === 'true' || this.options.verbose === true,
  684. validatorName,
  685. validateResult;
  686. for (var i = 0; i < total; i++) {
  687. var $field = fields.eq(i);
  688. if (this._isExcluded($field)) {
  689. continue;
  690. }
  691. var stop = false;
  692. for (validatorName in validators) {
  693. if ($field.data('bv.dfs.' + validatorName)) {
  694. $field.data('bv.dfs.' + validatorName).reject();
  695. }
  696. if (stop) {
  697. break;
  698. }
  699. // Don't validate field if it is already done
  700. var result = $field.data('bv.result.' + validatorName);
  701. if (result === this.STATUS_VALID || result === this.STATUS_INVALID) {
  702. this._onFieldValidated($field, validatorName);
  703. continue;
  704. } else if (validators[validatorName].enabled === false) {
  705. this.updateStatus(updateAll ? field : $field, this.STATUS_VALID, validatorName);
  706. continue;
  707. }
  708. $field.data('bv.result.' + validatorName, this.STATUS_VALIDATING);
  709. validateResult = $.fn.bootstrapValidator.validators[validatorName].validate(this, $field, validators[validatorName]);
  710. // validateResult can be a $.Deferred object ...
  711. if ('object' === typeof validateResult && validateResult.resolve) {
  712. this.updateStatus(updateAll ? field : $field, this.STATUS_VALIDATING, validatorName);
  713. $field.data('bv.dfs.' + validatorName, validateResult);
  714. validateResult.done(function($f, v, isValid, message) {
  715. // v is validator name
  716. $f.removeData('bv.dfs.' + v);
  717. if (message) {
  718. that.updateMessage($f, v, message);
  719. }
  720. that.updateStatus(updateAll ? $f.attr('data-bv-field') : $f, isValid ? that.STATUS_VALID : that.STATUS_INVALID, v);
  721. if (isValid && that._submitIfValid === true) {
  722. // If a remote validator returns true and the form is ready to submit, then do it
  723. that._submit();
  724. } else if (!isValid && !verbose) {
  725. stop = true;
  726. }
  727. });
  728. }
  729. // ... or object { valid: true/false, message: 'dynamic message' }
  730. else if ('object' === typeof validateResult && validateResult.valid !== undefined && validateResult.message !== undefined) {
  731. this.updateMessage(updateAll ? field : $field, validatorName, validateResult.message);
  732. this.updateStatus(updateAll ? field : $field, validateResult.valid ? this.STATUS_VALID : this.STATUS_INVALID, validatorName);
  733. if (!validateResult.valid && !verbose) {
  734. break;
  735. }
  736. }
  737. // ... or a boolean value
  738. else if ('boolean' === typeof validateResult) {
  739. this.updateStatus(updateAll ? field : $field, validateResult ? this.STATUS_VALID : this.STATUS_INVALID, validatorName);
  740. if (!validateResult && !verbose) {
  741. break;
  742. }
  743. }
  744. }
  745. }
  746. return this;
  747. },
  748. /**
  749. * Update the error message
  750. *
  751. * @param {String|jQuery} field The field name or field element
  752. * @param {String} validator The validator name
  753. * @param {String} message The message
  754. * @returns {BootstrapValidator}
  755. */
  756. updateMessage: function(field, validator, message) {
  757. var $fields = $([]);
  758. switch (typeof field) {
  759. case 'object':
  760. $fields = field;
  761. field = field.attr('data-bv-field');
  762. break;
  763. case 'string':
  764. $fields = this.getFieldElements(field);
  765. break;
  766. default:
  767. break;
  768. }
  769. $fields.each(function() {
  770. $(this).data('bv.messages').find('.help-block[data-bv-validator="' + validator + '"][data-bv-for="' + field + '"]').html(message);
  771. });
  772. },
  773. /**
  774. * Update all validating results of field
  775. *
  776. * @param {String|jQuery} field The field name or field element
  777. * @param {String} status The status. Can be 'NOT_VALIDATED', 'VALIDATING', 'INVALID' or 'VALID'
  778. * @param {String} [validatorName] The validator name. If null, the method updates validity result for all validators
  779. * @returns {BootstrapValidator}
  780. */
  781. updateStatus: function(field, status, validatorName) {
  782. var fields = $([]);
  783. switch (typeof field) {
  784. case 'object':
  785. fields = field;
  786. field = field.attr('data-bv-field');
  787. break;
  788. case 'string':
  789. fields = this.getFieldElements(field);
  790. break;
  791. default:
  792. break;
  793. }
  794. if (status === this.STATUS_NOT_VALIDATED) {
  795. // Reset the flag
  796. this._submitIfValid = false;
  797. }
  798. var that = this,
  799. type = fields.attr('type'),
  800. group = this.options.fields[field].group || this.options.group,
  801. total = ('radio' === type || 'checkbox' === type) ? 1 : fields.length;
  802. for (var i = 0; i < total; i++) {
  803. var $field = fields.eq(i);
  804. if (this._isExcluded($field)) {
  805. continue;
  806. }
  807. var $parent = $field.parents(group),
  808. $message = $field.data('bv.messages'),
  809. $allErrors = $message.find('.help-block[data-bv-validator][data-bv-for="' + field + '"]'),
  810. $errors = validatorName ? $allErrors.filter('[data-bv-validator="' + validatorName + '"]') : $allErrors,
  811. $icon = $parent.find('.form-control-feedback[data-bv-icon-for="' + field + '"]'),
  812. container = ('function' === typeof (this.options.fields[field].container || this.options.container)) ? (this.options.fields[field].container || this.options.container).call(this, $field, this) : (this.options.fields[field].container || this.options.container),
  813. isValidField = null;
  814. // Update status
  815. if (validatorName) {
  816. $field.data('bv.result.' + validatorName, status);
  817. } else {
  818. for (var v in this.options.fields[field].validators) {
  819. $field.data('bv.result.' + v, status);
  820. }
  821. }
  822. // Show/hide error elements and feedback icons
  823. $errors.attr('data-bv-result', status);
  824. // Determine the tab containing the element
  825. var $tabPane = $field.parents('.tab-pane'),
  826. tabId, $tab;
  827. if ($tabPane && (tabId = $tabPane.attr('id'))) {
  828. $tab = $('a[href="#' + tabId + '"][data-toggle="tab"]').parent();
  829. }
  830. switch (status) {
  831. case this.STATUS_VALIDATING:
  832. isValidField = null;
  833. this.disableSubmitButtons(true);
  834. $parent.removeClass('has-success').removeClass('has-error');
  835. if ($icon) {
  836. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.invalid).addClass(this.options.feedbackIcons.validating).show();
  837. }
  838. if ($tab) {
  839. $tab.removeClass('bv-tab-success').removeClass('bv-tab-error');
  840. }
  841. break;
  842. case this.STATUS_INVALID:
  843. isValidField = false;
  844. this.disableSubmitButtons(true);
  845. $parent.removeClass('has-success').addClass('has-error');
  846. if ($icon) {
  847. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.validating).addClass(this.options.feedbackIcons.invalid).show();
  848. }
  849. if ($tab) {
  850. $tab.removeClass('bv-tab-success').addClass('bv-tab-error');
  851. }
  852. break;
  853. case this.STATUS_VALID:
  854. // If the field is valid (passes all validators)
  855. isValidField = ($allErrors.filter('[data-bv-result="' + this.STATUS_NOT_VALIDATED +'"]').length === 0)
  856. ? ($allErrors.filter('[data-bv-result="' + this.STATUS_VALID +'"]').length === $allErrors.length) // All validators are completed
  857. : null; // There are some validators that have not done
  858. if (isValidField !== null) {
  859. this.disableSubmitButtons(this.$submitButton ? !this.isValid() : !isValidField);
  860. if ($icon) {
  861. $icon
  862. .removeClass(this.options.feedbackIcons.invalid).removeClass(this.options.feedbackIcons.validating).removeClass(this.options.feedbackIcons.valid)
  863. .addClass(isValidField ? this.options.feedbackIcons.valid : this.options.feedbackIcons.invalid)
  864. .show();
  865. }
  866. }
  867. $parent.removeClass('has-error has-success').addClass(this.isValidContainer($parent) ? 'has-success' : 'has-error');
  868. if ($tab) {
  869. $tab.removeClass('bv-tab-success').removeClass('bv-tab-error').addClass(this.isValidContainer($tabPane) ? 'bv-tab-success' : 'bv-tab-error');
  870. }
  871. break;
  872. case this.STATUS_NOT_VALIDATED:
  873. /* falls through */
  874. default:
  875. isValidField = null;
  876. this.disableSubmitButtons(false);
  877. $parent.removeClass('has-success').removeClass('has-error');
  878. if ($icon) {
  879. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.invalid).removeClass(this.options.feedbackIcons.validating).hide();
  880. }
  881. if ($tab) {
  882. $tab.removeClass('bv-tab-success').removeClass('bv-tab-error');
  883. }
  884. break;
  885. }
  886. switch (true) {
  887. // Only show the first error message if it is placed inside a tooltip ...
  888. case ($icon && 'tooltip' === container):
  889. (isValidField === false)
  890. ? $icon.css('cursor', 'pointer').tooltip('destroy').tooltip({
  891. container: 'body',
  892. html: true,
  893. placement: 'top',
  894. title: $allErrors.filter('[data-bv-result="' + that.STATUS_INVALID + '"]').eq(0).html()
  895. })
  896. : $icon.css('cursor', '').tooltip('destroy');
  897. break;
  898. // ... or popover
  899. case ($icon && 'popover' === container):
  900. (isValidField === false)
  901. ? $icon.css('cursor', 'pointer').popover('destroy').popover({
  902. container: 'body',
  903. content: $allErrors.filter('[data-bv-result="' + that.STATUS_INVALID + '"]').eq(0).html(),
  904. html: true,
  905. placement: 'top',
  906. trigger: 'hover click'
  907. })
  908. : $icon.css('cursor', '').popover('destroy');
  909. break;
  910. default:
  911. (status === this.STATUS_INVALID) ? $errors.show() : $errors.hide();
  912. break;
  913. }
  914. // Trigger an event
  915. $field.trigger($.Event(this.options.events.fieldStatus), {
  916. bv: this,
  917. field: field,
  918. element: $field,
  919. status: status
  920. });
  921. this._onFieldValidated($field, validatorName);
  922. }
  923. return this;
  924. },
  925. /**
  926. * Check the form validity
  927. *
  928. * @returns {Boolean}
  929. */
  930. isValid: function() {
  931. for (var field in this.options.fields) {
  932. if (!this.isValidField(field)) {
  933. return false;
  934. }
  935. }
  936. return true;
  937. },
  938. /**
  939. * Check if the field is valid or not
  940. *
  941. * @param {String|jQuery} field The field name or field element
  942. * @returns {Boolean}
  943. */
  944. isValidField: function(field) {
  945. var fields = $([]);
  946. switch (typeof field) {
  947. case 'object':
  948. fields = field;
  949. field = field.attr('data-bv-field');
  950. break;
  951. case 'string':
  952. fields = this.getFieldElements(field);
  953. break;
  954. default:
  955. break;
  956. }
  957. if (fields.length === 0 || this.options.fields[field] === null || this.options.fields[field].enabled === false) {
  958. return true;
  959. }
  960. var type = fields.attr('type'),
  961. total = ('radio' === type || 'checkbox' === type) ? 1 : fields.length,
  962. $field, validatorName, status;
  963. for (var i = 0; i < total; i++) {
  964. $field = fields.eq(i);
  965. if (this._isExcluded($field)) {
  966. continue;
  967. }
  968. for (validatorName in this.options.fields[field].validators) {
  969. if (this.options.fields[field].validators[validatorName].enabled === false) {
  970. continue;
  971. }
  972. status = $field.data('bv.result.' + validatorName);
  973. if (status !== this.STATUS_VALID) {
  974. return false;
  975. }
  976. }
  977. }
  978. return true;
  979. },
  980. /**
  981. * Check if all fields inside a given container are valid.
  982. * It's useful when working with a wizard-like such as tab, collapse
  983. *
  984. * @param {String|jQuery} container The container selector or element
  985. * @returns {Boolean}
  986. */
  987. isValidContainer: function(container) {
  988. var that = this,
  989. map = {},
  990. $container = ('string' === typeof container) ? $(container) : container;
  991. if ($container.length === 0) {
  992. return true;
  993. }
  994. $container.find('[data-bv-field]').each(function() {
  995. var $field = $(this),
  996. field = $field.attr('data-bv-field');
  997. if (!that._isExcluded($field) && !map[field]) {
  998. map[field] = $field;
  999. }
  1000. });
  1001. for (var field in map) {
  1002. var $f = map[field];
  1003. if ($f.data('bv.messages')
  1004. .find('.help-block[data-bv-validator][data-bv-for="' + field + '"]')
  1005. .filter('[data-bv-result="' + this.STATUS_INVALID +'"]')
  1006. .length > 0)
  1007. {
  1008. return false;
  1009. }
  1010. }
  1011. return true;
  1012. },
  1013. /**
  1014. * Submit the form using default submission.
  1015. * It also does not perform any validations when submitting the form
  1016. */
  1017. defaultSubmit: function() {
  1018. if (this.$submitButton) {
  1019. // Create hidden input to send the submit buttons
  1020. $('<input/>')
  1021. .attr('type', 'hidden')
  1022. .attr('data-bv-submit-hidden', '')
  1023. .attr('name', this.$submitButton.attr('name'))
  1024. .val(this.$submitButton.val())
  1025. .appendTo(this.$form);
  1026. }
  1027. // Submit form
  1028. this.$form.off('submit.bv').submit();
  1029. },
  1030. // ---
  1031. // Useful APIs which aren't used internally
  1032. // ---
  1033. /**
  1034. * Get the list of invalid fields
  1035. *
  1036. * @returns {jQuery[]}
  1037. */
  1038. getInvalidFields: function() {
  1039. return this.$invalidFields;
  1040. },
  1041. /**
  1042. * Returns the clicked submit button
  1043. *
  1044. * @returns {jQuery}
  1045. */
  1046. getSubmitButton: function() {
  1047. return this.$submitButton;
  1048. },
  1049. /**
  1050. * Get the error messages
  1051. *
  1052. * @param {String|jQuery} [field] The field name or field element
  1053. * If the field is not defined, the method returns all error messages of all fields
  1054. * @param {String} [validator] The name of validator
  1055. * If the validator is not defined, the method returns error messages of all validators
  1056. * @returns {String[]}
  1057. */
  1058. getMessages: function(field, validator) {
  1059. var that = this,
  1060. messages = [],
  1061. $fields = $([]);
  1062. switch (true) {
  1063. case (field && 'object' === typeof field):
  1064. $fields = field;
  1065. break;
  1066. case (field && 'string' === typeof field):
  1067. var f = this.getFieldElements(field);
  1068. if (f.length > 0) {
  1069. var type = f.attr('type');
  1070. $fields = ('radio' === type || 'checkbox' === type) ? f.eq(0) : f;
  1071. }
  1072. break;
  1073. default:
  1074. $fields = this.$invalidFields;
  1075. break;
  1076. }
  1077. var filter = validator ? '[data-bv-validator="' + validator + '"]' : '';
  1078. $fields.each(function() {
  1079. messages = messages.concat(
  1080. $(this)
  1081. .data('bv.messages')
  1082. .find('.help-block[data-bv-for="' + $(this).attr('data-bv-field') + '"][data-bv-result="' + that.STATUS_INVALID + '"]' + filter)
  1083. .map(function() {
  1084. var v = $(this).attr('data-bv-validator'),
  1085. f = $(this).attr('data-bv-for');
  1086. return (that.options.fields[f].validators[v].enabled === false) ? '' : $(this).html();
  1087. })
  1088. .get()
  1089. );
  1090. });
  1091. return messages;
  1092. },
  1093. /**
  1094. * Get the field options
  1095. *
  1096. * @param {String|jQuery} [field] The field name or field element. If it is not set, the method returns the form options
  1097. * @param {String} [validator] The name of validator. It null, the method returns form options
  1098. * @param {String} [option] The option name
  1099. * @return {String|Object}
  1100. */
  1101. getOptions: function(field, validator, option) {
  1102. if (!field) {
  1103. return this.options;
  1104. }
  1105. if ('object' === typeof field) {
  1106. field = field.attr('data-bv-field');
  1107. }
  1108. if (!this.options.fields[field]) {
  1109. return null;
  1110. }
  1111. var options = this.options.fields[field];
  1112. if (!validator) {
  1113. return options;
  1114. }
  1115. if (!options.validators || !options.validators[validator]) {
  1116. return null;
  1117. }
  1118. return option ? options.validators[validator][option] : options.validators[validator];
  1119. },
  1120. /**
  1121. * Update the option of a specific validator
  1122. *
  1123. * @param {String|jQuery} field The field name or field element
  1124. * @param {String} validator The validator name
  1125. * @param {String} option The option name
  1126. * @param {String} value The value to set
  1127. * @returns {BootstrapValidator}
  1128. */
  1129. updateOption: function(field, validator, option, value) {
  1130. if ('object' === typeof field) {
  1131. field = field.attr('data-bv-field');
  1132. }
  1133. if (this.options.fields[field] && this.options.fields[field].validators[validator]) {
  1134. this.options.fields[field].validators[validator][option] = value;
  1135. this.updateStatus(field, this.STATUS_NOT_VALIDATED, validator);
  1136. }
  1137. return this;
  1138. },
  1139. /**
  1140. * Add a new field
  1141. *
  1142. * @param {String|jQuery} field The field name or field element
  1143. * @param {Object} [options] The validator rules
  1144. * @returns {BootstrapValidator}
  1145. */
  1146. addField: function(field, options) {
  1147. var fields = $([]);
  1148. switch (typeof field) {
  1149. case 'object':
  1150. fields = field;
  1151. field = field.attr('data-bv-field') || field.attr('name');
  1152. break;
  1153. case 'string':
  1154. delete this._cacheFields[field];
  1155. fields = this.getFieldElements(field);
  1156. break;
  1157. default:
  1158. break;
  1159. }
  1160. fields.attr('data-bv-field', field);
  1161. var type = fields.attr('type'),
  1162. total = ('radio' === type || 'checkbox' === type) ? 1 : fields.length;
  1163. for (var i = 0; i < total; i++) {
  1164. var $field = fields.eq(i);
  1165. // Try to parse the options from HTML attributes
  1166. var opts = this._parseOptions($field);
  1167. opts = (opts === null) ? options : $.extend(true, options, opts);
  1168. this.options.fields[field] = $.extend(true, this.options.fields[field], opts);
  1169. // Update the cache
  1170. this._cacheFields[field] = this._cacheFields[field] ? this._cacheFields[field].add($field) : $field;
  1171. // Init the element
  1172. this._initField(('checkbox' === type || 'radio' === type) ? field : $field);
  1173. }
  1174. this.disableSubmitButtons(false);
  1175. // Trigger an event
  1176. this.$form.trigger($.Event(this.options.events.fieldAdded), {
  1177. field: field,
  1178. element: fields,
  1179. options: this.options.fields[field]
  1180. });
  1181. return this;
  1182. },
  1183. /**
  1184. * Remove a given field
  1185. *
  1186. * @param {String|jQuery} field The field name or field element
  1187. * @returns {BootstrapValidator}
  1188. */
  1189. removeField: function(field) {
  1190. var fields = $([]);
  1191. switch (typeof field) {
  1192. case 'object':
  1193. fields = field;
  1194. field = field.attr('data-bv-field') || field.attr('name');
  1195. fields.attr('data-bv-field', field);
  1196. break;
  1197. case 'string':
  1198. fields = this.getFieldElements(field);
  1199. break;
  1200. default:
  1201. break;
  1202. }
  1203. if (fields.length === 0) {
  1204. return this;
  1205. }
  1206. var type = fields.attr('type'),
  1207. total = ('radio' === type || 'checkbox' === type) ? 1 : fields.length;
  1208. for (var i = 0; i < total; i++) {
  1209. var $field = fields.eq(i);
  1210. // Remove from the list of invalid fields
  1211. this.$invalidFields = this.$invalidFields.not($field);
  1212. // Update the cache
  1213. this._cacheFields[field] = this._cacheFields[field].not($field);
  1214. }
  1215. if (!this._cacheFields[field] || this._cacheFields[field].length === 0) {
  1216. delete this.options.fields[field];
  1217. }
  1218. if ('checkbox' === type || 'radio' === type) {
  1219. this._initField(field);
  1220. }
  1221. this.disableSubmitButtons(false);
  1222. // Trigger an event
  1223. this.$form.trigger($.Event(this.options.events.fieldRemoved), {
  1224. field: field,
  1225. element: fields
  1226. });
  1227. return this;
  1228. },
  1229. /**
  1230. * Reset given field
  1231. *
  1232. * @param {String|jQuery} field The field name or field element
  1233. * @param {Boolean} [resetValue] If true, the method resets field value to empty or remove checked/selected attribute (for radio/checkbox)
  1234. * @returns {BootstrapValidator}
  1235. */
  1236. resetField: function(field, resetValue) {
  1237. var $fields = $([]);
  1238. switch (typeof field) {
  1239. case 'object':
  1240. $fields = field;
  1241. field = field.attr('data-bv-field');
  1242. break;
  1243. case 'string':
  1244. $fields = this.getFieldElements(field);
  1245. break;
  1246. default:
  1247. break;
  1248. }
  1249. var total = $fields.length;
  1250. if (this.options.fields[field]) {
  1251. for (var i = 0; i < total; i++) {
  1252. for (var validator in this.options.fields[field].validators) {
  1253. $fields.eq(i).removeData('bv.dfs.' + validator);
  1254. }
  1255. }
  1256. }
  1257. // Mark field as not validated yet
  1258. this.updateStatus(field, this.STATUS_NOT_VALIDATED);
  1259. if (resetValue) {
  1260. var type = $fields.attr('type');
  1261. ('radio' === type || 'checkbox' === type) ? $fields.removeAttr('checked').removeAttr('selected') : $fields.val('');
  1262. }
  1263. return this;
  1264. },
  1265. /**
  1266. * Reset the form
  1267. *
  1268. * @param {Boolean} [resetValue] If true, the method resets field value to empty or remove checked/selected attribute (for radio/checkbox)
  1269. * @returns {BootstrapValidator}
  1270. */
  1271. resetForm: function(resetValue) {
  1272. for (var field in this.options.fields) {
  1273. this.resetField(field, resetValue);
  1274. }
  1275. this.$invalidFields = $([]);
  1276. this.$submitButton = null;
  1277. // Enable submit buttons
  1278. this.disableSubmitButtons(false);
  1279. return this;
  1280. },
  1281. /**
  1282. * Revalidate given field
  1283. * It's used when you need to revalidate the field which its value is updated by other plugin
  1284. *
  1285. * @param {String|jQuery} field The field name of field element
  1286. * @returns {BootstrapValidator}
  1287. */
  1288. revalidateField: function(field) {
  1289. this.updateStatus(field, this.STATUS_NOT_VALIDATED)
  1290. .validateField(field);
  1291. return this;
  1292. },
  1293. /**
  1294. * Enable/Disable all validators to given field
  1295. *
  1296. * @param {String} field The field name
  1297. * @param {Boolean} enabled Enable/Disable field validators
  1298. * @param {String} [validatorName] The validator name. If null, all validators will be enabled/disabled
  1299. * @returns {BootstrapValidator}
  1300. */
  1301. enableFieldValidators: function(field, enabled, validatorName) {
  1302. var validators = this.options.fields[field].validators;
  1303. // Enable/disable particular validator
  1304. if (validatorName
  1305. && validators
  1306. && validators[validatorName] && validators[validatorName].enabled !== enabled)
  1307. {
  1308. this.options.fields[field].validators[validatorName].enabled = enabled;
  1309. this.updateStatus(field, this.STATUS_NOT_VALIDATED, validatorName);
  1310. }
  1311. // Enable/disable all validators
  1312. else if (!validatorName && this.options.fields[field].enabled !== enabled) {
  1313. this.options.fields[field].enabled = enabled;
  1314. for (var v in validators) {
  1315. this.enableFieldValidators(field, enabled, v);
  1316. }
  1317. }
  1318. return this;
  1319. },
  1320. /**
  1321. * Some validators have option which its value is dynamic.
  1322. * For example, the zipCode validator has the country option which might be changed dynamically by a select element.
  1323. *
  1324. * @param {jQuery|String} field The field name or element
  1325. * @param {String|Function} option The option which can be determined by:
  1326. * - a string
  1327. * - name of field which defines the value
  1328. * - name of function which returns the value
  1329. * - a function returns the value
  1330. *
  1331. * The callback function has the format of
  1332. * callback: function(value, validator, $field) {
  1333. * // value is the value of field
  1334. * // validator is the BootstrapValidator instance
  1335. * // $field is the field element
  1336. * }
  1337. *
  1338. * @returns {String}
  1339. */
  1340. getDynamicOption: function(field, option) {
  1341. var $field = ('string' === typeof field) ? this.getFieldElements(field) : field,
  1342. value = $field.val();
  1343. // Option can be determined by
  1344. // ... a function
  1345. if ('function' === typeof option) {
  1346. return $.fn.bootstrapValidator.helpers.call(option, [value, this, $field]);
  1347. }
  1348. // ... value of other field
  1349. else if ('string' === typeof option) {
  1350. var $f = this.getFieldElements(option);
  1351. if ($f.length) {
  1352. return $f.val();
  1353. }
  1354. // ... return value of callback
  1355. else {
  1356. return $.fn.bootstrapValidator.helpers.call(option, [value, this, $field]) || option;
  1357. }
  1358. }
  1359. return null;
  1360. },
  1361. /**
  1362. * Destroy the plugin
  1363. * It will remove all error messages, feedback icons and turn off the events
  1364. */
  1365. destroy: function() {
  1366. var field, fields, $field, validator, $icon, group;
  1367. for (field in this.options.fields) {
  1368. fields = this.getFieldElements(field);
  1369. group = this.options.fields[field].group || this.options.group;
  1370. for (var i = 0; i < fields.length; i++) {
  1371. $field = fields.eq(i);
  1372. $field
  1373. // Remove all error messages
  1374. .data('bv.messages')
  1375. .find('.help-block[data-bv-validator][data-bv-for="' + field + '"]').remove().end()
  1376. .end()
  1377. .removeData('bv.messages')
  1378. // Remove feedback classes
  1379. .parents(group)
  1380. .removeClass('has-feedback has-error has-success')
  1381. .end()
  1382. // Turn off events
  1383. .off('.bv')
  1384. .removeAttr('data-bv-field');
  1385. // Remove feedback icons, tooltip/popover container
  1386. $icon = $field.parents(group).find('i[data-bv-icon-for="' + field + '"]');
  1387. if ($icon) {
  1388. var container = ('function' === typeof (this.options.fields[field].container || this.options.container)) ? (this.options.fields[field].container || this.options.container).call(this, $field, this) : (this.options.fields[field].container || this.options.container);
  1389. switch (container) {
  1390. case 'tooltip':
  1391. $icon.tooltip('destroy').remove();
  1392. break;
  1393. case 'popover':
  1394. $icon.popover('destroy').remove();
  1395. break;
  1396. default:
  1397. $icon.remove();
  1398. break;
  1399. }
  1400. }
  1401. for (validator in this.options.fields[field].validators) {
  1402. if ($field.data('bv.dfs.' + validator)) {
  1403. $field.data('bv.dfs.' + validator).reject();
  1404. }
  1405. $field.removeData('bv.result.' + validator).removeData('bv.dfs.' + validator);
  1406. // Destroy the validator
  1407. if ('function' === typeof $.fn.bootstrapValidator.validators[validator].destroy) {
  1408. $.fn.bootstrapValidator.validators[validator].destroy(this, $field, this.options.fields[field].validators[validator]);
  1409. }
  1410. }
  1411. }
  1412. }
  1413. this.disableSubmitButtons(false); // Enable submit buttons
  1414. this.$hiddenButton.remove(); // Remove the hidden button
  1415. this.$form
  1416. .removeClass(this.options.elementClass)
  1417. .off('.bv')
  1418. .removeData('bootstrapValidator')
  1419. // Remove generated hidden elements
  1420. .find('[data-bv-submit-hidden]').remove().end()
  1421. .find('[type="submit"]').off('click.bv');
  1422. }
  1423. };
  1424. // Plugin definition
  1425. $.fn.bootstrapValidator = function(option) {
  1426. var params = arguments;
  1427. return this.each(function() {
  1428. var $this = $(this),
  1429. data = $this.data('bootstrapValidator'),
  1430. options = 'object' === typeof option && option;
  1431. if (!data) {
  1432. data = new BootstrapValidator(this, options);
  1433. $this.data('bootstrapValidator', data);
  1434. }
  1435. // Allow to call plugin method
  1436. if ('string' === typeof option) {
  1437. data[option].apply(data, Array.prototype.slice.call(params, 1));
  1438. }
  1439. });
  1440. };
  1441. // The default options
  1442. $.fn.bootstrapValidator.DEFAULT_OPTIONS = {
  1443. // The form CSS class
  1444. elementClass: 'bv-form',
  1445. // Default invalid message
  1446. message: 'This value is not valid',
  1447. // The CSS selector for indicating the element consists the field
  1448. // By default, each field is placed inside the <div class="form-group"></div>
  1449. // You should adjust this option if your form group consists of many fields which not all of them need to be validated
  1450. group: '.form-group',
  1451. //The error messages container. It can be:
  1452. // - 'tooltip' if you want to use Bootstrap tooltip to show error messages
  1453. // - 'popover' if you want to use Bootstrap popover to show error messages
  1454. // - a CSS selector indicating the container
  1455. // In the first two cases, since the tooltip/popover should be small enough, the plugin only shows only one error message
  1456. // You also can define the message container for particular field
  1457. container: null,
  1458. // The field will not be live validated if its length is less than this number of characters
  1459. threshold: null,
  1460. // Indicate fields which won't be validated
  1461. // By default, the plugin will not validate the following kind of fields:
  1462. // - disabled
  1463. // - hidden
  1464. // - invisible
  1465. //
  1466. // The setting consists of jQuery filters. Accept 3 formats:
  1467. // - A string. Use a comma to separate filter
  1468. // - An array. Each element is a filter
  1469. // - An array. Each element can be a callback function
  1470. // function($field, validator) {
  1471. // $field is jQuery object representing the field element
  1472. // validator is the BootstrapValidator instance
  1473. // return true or false;
  1474. // }
  1475. //
  1476. // The 3 following settings are equivalent:
  1477. //
  1478. // 1) ':disabled, :hidden, :not(:visible)'
  1479. // 2) [':disabled', ':hidden', ':not(:visible)']
  1480. // 3) [':disabled', ':hidden', function($field) {
  1481. // return !$field.is(':visible');
  1482. // }]
  1483. excluded: [':disabled', ':hidden', ':not(:visible)'],
  1484. // Shows ok/error/loading icons based on the field validity.
  1485. // This feature requires Bootstrap v3.1.0 or later (http://getbootstrap.com/css/#forms-control-validation).
  1486. // Since Bootstrap doesn't provide any methods to know its version, this option cannot be on/off automatically.
  1487. // In other word, to use this feature you have to upgrade your Bootstrap to v3.1.0 or later.
  1488. //
  1489. // Examples:
  1490. // - Use Glyphicons icons:
  1491. // feedbackIcons: {
  1492. // valid: 'glyphicon glyphicon-ok',
  1493. // invalid: 'glyphicon glyphicon-remove',
  1494. // validating: 'glyphicon glyphicon-refresh'
  1495. // }
  1496. // - Use FontAwesome icons:
  1497. // feedbackIcons: {
  1498. // valid: 'fa fa-check',
  1499. // invalid: 'fa fa-times',
  1500. // validating: 'fa fa-refresh'
  1501. // }
  1502. feedbackIcons: {
  1503. valid: null,
  1504. invalid: null,
  1505. validating: null
  1506. },
  1507. // The submit buttons selector
  1508. // These buttons will be disabled to prevent the valid form from multiple submissions
  1509. submitButtons: '[type="submit"]',
  1510. // Live validating option
  1511. // Can be one of 3 values:
  1512. // - enabled: The plugin validates fields as soon as they are changed
  1513. // - disabled: Disable the live validating. The error messages are only shown after the form is submitted
  1514. // - submitted: The live validating is enabled after the form is submitted
  1515. live: 'enabled',
  1516. // Map the field name with validator rules
  1517. fields: null,
  1518. // Use custom event name to avoid window.onerror being invoked by jQuery
  1519. // See https://github.com/nghuuphuoc/bootstrapvalidator/issues/630
  1520. events: {
  1521. formInit: 'init.form.bv',
  1522. formError: 'error.form.bv',
  1523. formSuccess: 'success.form.bv',
  1524. fieldAdded: 'added.field.bv',
  1525. fieldRemoved: 'removed.field.bv',
  1526. fieldInit: 'init.field.bv',
  1527. fieldError: 'error.field.bv',
  1528. fieldSuccess: 'success.field.bv',
  1529. fieldStatus: 'status.field.bv',
  1530. validatorError: 'error.validator.bv',
  1531. validatorSuccess: 'success.validator.bv'
  1532. },
  1533. // Whether to be verbose when validating a field or not.
  1534. // Possible values:
  1535. // - true: when a field has multiple validators, all of them will be checked, and respectively - if errors occur in
  1536. // multiple validators, all of them will be displayed to the user
  1537. // - false: when a field has multiple validators, validation for this field will be terminated upon the first encountered error.
  1538. // Thus, only the very first error message related to this field will be displayed to the user
  1539. verbose: true
  1540. };
  1541. // Available validators
  1542. $.fn.bootstrapValidator.validators = {};
  1543. // i18n
  1544. $.fn.bootstrapValidator.i18n = {};
  1545. $.fn.bootstrapValidator.Constructor = BootstrapValidator;
  1546. // Helper methods, which can be used in validator class
  1547. $.fn.bootstrapValidator.helpers = {
  1548. /**
  1549. * Execute a callback function
  1550. *
  1551. * @param {String|Function} functionName Can be
  1552. * - name of global function
  1553. * - name of namespace function (such as A.B.C)
  1554. * - a function
  1555. * @param {Array} args The callback arguments
  1556. */
  1557. call: function(functionName, args) {
  1558. if ('function' === typeof functionName) {
  1559. return functionName.apply(this, args);
  1560. } else if ('string' === typeof functionName) {
  1561. if ('()' === functionName.substring(functionName.length - 2)) {
  1562. functionName = functionName.substring(0, functionName.length - 2);
  1563. }
  1564. var ns = functionName.split('.'),
  1565. func = ns.pop(),
  1566. context = window;
  1567. for (var i = 0; i < ns.length; i++) {
  1568. context = context[ns[i]];
  1569. }
  1570. return (typeof context[func] === 'undefined') ? null : context[func].apply(this, args);
  1571. }
  1572. },
  1573. /**
  1574. * Format a string
  1575. * It's used to format the error message
  1576. * format('The field must between %s and %s', [10, 20]) = 'The field must between 10 and 20'
  1577. *
  1578. * @param {String} message
  1579. * @param {Array} parameters
  1580. * @returns {String}
  1581. */
  1582. format: function(message, parameters) {
  1583. if (!$.isArray(parameters)) {
  1584. parameters = [parameters];
  1585. }
  1586. for (var i in parameters) {
  1587. message = message.replace('%s', parameters[i]);
  1588. }
  1589. return message;
  1590. },
  1591. /**
  1592. * Validate a date
  1593. *
  1594. * @param {Number} year The full year in 4 digits
  1595. * @param {Number} month The month number
  1596. * @param {Number} day The day number
  1597. * @param {Boolean} [notInFuture] If true, the date must not be in the future
  1598. * @returns {Boolean}
  1599. */
  1600. date: function(year, month, day, notInFuture) {
  1601. if (isNaN(year) || isNaN(month) || isNaN(day)) {
  1602. return false;
  1603. }
  1604. if (day.length > 2 || month.length > 2 || year.length > 4) {
  1605. return false;
  1606. }
  1607. day = parseInt(day, 10);
  1608. month = parseInt(month, 10);
  1609. year = parseInt(year, 10);
  1610. if (year < 1000 || year > 9999 || month <= 0 || month > 12) {
  1611. return false;
  1612. }
  1613. var numDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  1614. // Update the number of days in Feb of leap year
  1615. if (year % 400 === 0 || (year % 100 !== 0 && year % 4 === 0)) {
  1616. numDays[1] = 29;
  1617. }
  1618. // Check the day
  1619. if (day <= 0 || day > numDays[month - 1]) {
  1620. return false;
  1621. }
  1622. if (notInFuture === true) {
  1623. var currentDate = new Date(),
  1624. currentYear = currentDate.getFullYear(),
  1625. currentMonth = currentDate.getMonth(),
  1626. currentDay = currentDate.getDate();
  1627. return (year < currentYear
  1628. || (year === currentYear && month - 1 < currentMonth)
  1629. || (year === currentYear && month - 1 === currentMonth && day < currentDay));
  1630. }
  1631. return true;
  1632. },
  1633. /**
  1634. * Implement Luhn validation algorithm
  1635. * Credit to https://gist.github.com/ShirtlessKirk/2134376
  1636. *
  1637. * @see http://en.wikipedia.org/wiki/Luhn
  1638. * @param {String} value
  1639. * @returns {Boolean}
  1640. */
  1641. luhn: function(value) {
  1642. var length = value.length,
  1643. mul = 0,
  1644. prodArr = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]],
  1645. sum = 0;
  1646. while (length--) {
  1647. sum += prodArr[mul][parseInt(value.charAt(length), 10)];
  1648. mul ^= 1;
  1649. }
  1650. return (sum % 10 === 0 && sum > 0);
  1651. },
  1652. /**
  1653. * Implement modulus 11, 10 (ISO 7064) algorithm
  1654. *
  1655. * @param {String} value
  1656. * @returns {Boolean}
  1657. */
  1658. mod11And10: function(value) {
  1659. var check = 5,
  1660. length = value.length;
  1661. for (var i = 0; i < length; i++) {
  1662. check = (((check || 10) * 2) % 11 + parseInt(value.charAt(i), 10)) % 10;
  1663. }
  1664. return (check === 1);
  1665. },
  1666. /**
  1667. * Implements Mod 37, 36 (ISO 7064) algorithm
  1668. * Usages:
  1669. * mod37And36('A12425GABC1234002M')
  1670. * mod37And36('002006673085', '0123456789')
  1671. *
  1672. * @param {String} value
  1673. * @param {String} [alphabet]
  1674. * @returns {Boolean}
  1675. */
  1676. mod37And36: function(value, alphabet) {
  1677. alphabet = alphabet || '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  1678. var modulus = alphabet.length,
  1679. length = value.length,
  1680. check = Math.floor(modulus / 2);
  1681. for (var i = 0; i < length; i++) {
  1682. check = (((check || modulus) * 2) % (modulus + 1) + alphabet.indexOf(value.charAt(i))) % modulus;
  1683. }
  1684. return (check === 1);
  1685. }
  1686. };
  1687. }(window.jQuery));
  1688. ;(function($) {
  1689. $.fn.bootstrapValidator.i18n.base64 = $.extend($.fn.bootstrapValidator.i18n.base64 || {}, {
  1690. 'default': 'Please enter a valid base 64 encoded'
  1691. });
  1692. $.fn.bootstrapValidator.validators.base64 = {
  1693. /**
  1694. * Return true if the input value is a base 64 encoded string.
  1695. *
  1696. * @param {BootstrapValidator} validator The validator plugin instance
  1697. * @param {jQuery} $field Field element
  1698. * @param {Object} options Can consist of the following keys:
  1699. * - message: The invalid message
  1700. * @returns {Boolean}
  1701. */
  1702. validate: function(validator, $field, options) {
  1703. var value = $field.val();
  1704. if (value === '') {
  1705. return true;
  1706. }
  1707. return /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$/.test(value);
  1708. }
  1709. };
  1710. }(window.jQuery));
  1711. ;(function($) {
  1712. $.fn.bootstrapValidator.i18n.between = $.extend($.fn.bootstrapValidator.i18n.between || {}, {
  1713. 'default': 'Please enter a value between %s and %s',
  1714. notInclusive: 'Please enter a value between %s and %s strictly'
  1715. });
  1716. $.fn.bootstrapValidator.validators.between = {
  1717. html5Attributes: {
  1718. message: 'message',
  1719. min: 'min',
  1720. max: 'max',
  1721. inclusive: 'inclusive'
  1722. },
  1723. enableByHtml5: function($field) {
  1724. if ('range' === $field.attr('type')) {
  1725. return {
  1726. min: $field.attr('min'),
  1727. max: $field.attr('max')
  1728. };
  1729. }
  1730. return false;
  1731. },
  1732. /**
  1733. * Return true if the input value is between (strictly or not) two given numbers
  1734. *
  1735. * @param {BootstrapValidator} validator The validator plugin instance
  1736. * @param {jQuery} $field Field element
  1737. * @param {Object} options Can consist of the following keys:
  1738. * - min
  1739. * - max
  1740. *
  1741. * The min, max keys define the number which the field value compares to. min, max can be
  1742. * - A number
  1743. * - Name of field which its value defines the number
  1744. * - Name of callback function that returns the number
  1745. * - A callback function that returns the number
  1746. *
  1747. * - inclusive [optional]: Can be true or false. Default is true
  1748. * - message: The invalid message
  1749. * @returns {Boolean|Object}
  1750. */
  1751. validate: function(validator, $field, options) {
  1752. var value = $field.val();
  1753. if (value === '') {
  1754. return true;
  1755. }
  1756. if (!$.isNumeric(value)) {
  1757. return false;
  1758. }
  1759. var min = $.isNumeric(options.min) ? options.min : validator.getDynamicOption($field, options.min),
  1760. max = $.isNumeric(options.max) ? options.max : validator.getDynamicOption($field, options.max);
  1761. value = parseFloat(value);
  1762. return (options.inclusive === true || options.inclusive === undefined)
  1763. ? {
  1764. valid: value >= min && value <= max,
  1765. message: $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.between['default'], [min, max])
  1766. }
  1767. : {
  1768. valid: value > min && value < max,
  1769. message: $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.between.notInclusive, [min, max])
  1770. };
  1771. }
  1772. };
  1773. }(window.jQuery));
  1774. ;(function($) {
  1775. $.fn.bootstrapValidator.validators.blank = {
  1776. /**
  1777. * Placeholder validator that can be used to display a custom validation message
  1778. * returned from the server
  1779. * Example:
  1780. *
  1781. * (1) a "blank" validator is applied to an input field.
  1782. * (2) data is entered via the UI that is unable to be validated client-side.
  1783. * (3) server returns a 400 with JSON data that contains the field that failed
  1784. * validation and an associated message.
  1785. * (4) ajax 400 call handler does the following:
  1786. *
  1787. * bv.updateMessage(field, 'blank', errorMessage);
  1788. * bv.updateStatus(field, 'INVALID');
  1789. *
  1790. * @see https://github.com/nghuuphuoc/bootstrapvalidator/issues/542
  1791. * @see https://github.com/nghuuphuoc/bootstrapvalidator/pull/666
  1792. * @param {BootstrapValidator} validator The validator plugin instance
  1793. * @param {jQuery} $field Field element
  1794. * @param {Object} options Can consist of the following keys:
  1795. * - message: The invalid message
  1796. * @returns {Boolean}
  1797. */
  1798. validate: function(validator, $field, options) {
  1799. return true;
  1800. }
  1801. };
  1802. }(window.jQuery));
  1803. ;(function($) {
  1804. $.fn.bootstrapValidator.i18n.callback = $.extend($.fn.bootstrapValidator.i18n.callback || {}, {
  1805. 'default': 'Please enter a valid value'
  1806. });
  1807. $.fn.bootstrapValidator.validators.callback = {
  1808. html5Attributes: {
  1809. message: 'message',
  1810. callback: 'callback'
  1811. },
  1812. /**
  1813. * Return result from the callback method
  1814. *
  1815. * @param {BootstrapValidator} validator The validator plugin instance
  1816. * @param {jQuery} $field Field element
  1817. * @param {Object} options Can consist of the following keys:
  1818. * - callback: The callback method that passes 2 parameters:
  1819. * callback: function(fieldValue, validator, $field) {
  1820. * // fieldValue is the value of field
  1821. * // validator is instance of BootstrapValidator
  1822. * // $field is the field element
  1823. * }
  1824. * - message: The invalid message
  1825. * @returns {Boolean|Deferred}
  1826. */
  1827. validate: function(validator, $field, options) {
  1828. var value = $field.val();
  1829. if (options.callback) {
  1830. var dfd = new $.Deferred(),
  1831. response = $.fn.bootstrapValidator.helpers.call(options.callback, [value, validator, $field]);
  1832. dfd.resolve($field, 'callback', 'boolean' === typeof response ? response : response.valid, 'object' === typeof response && response.message ? response.message : null);
  1833. return dfd;
  1834. }
  1835. return true;
  1836. }
  1837. };
  1838. }(window.jQuery));
  1839. ;(function($) {
  1840. $.fn.bootstrapValidator.i18n.choice = $.extend($.fn.bootstrapValidator.i18n.choice || {}, {
  1841. 'default': 'Please enter a valid value',
  1842. less: 'Please choose %s options at minimum',
  1843. more: 'Please choose %s options at maximum',
  1844. between: 'Please choose %s - %s options'
  1845. });
  1846. $.fn.bootstrapValidator.validators.choice = {
  1847. html5Attributes: {
  1848. message: 'message',
  1849. min: 'min',
  1850. max: 'max'
  1851. },
  1852. /**
  1853. * Check if the number of checked boxes are less or more than a given number
  1854. *
  1855. * @param {BootstrapValidator} validator The validator plugin instance
  1856. * @param {jQuery} $field Field element
  1857. * @param {Object} options Consists of following keys:
  1858. * - min
  1859. * - max
  1860. *
  1861. * At least one of two keys is required
  1862. * The min, max keys define the number which the field value compares to. min, max can be
  1863. * - A number
  1864. * - Name of field which its value defines the number
  1865. * - Name of callback function that returns the number
  1866. * - A callback function that returns the number
  1867. *
  1868. * - message: The invalid message
  1869. * @returns {Object}
  1870. */
  1871. validate: function(validator, $field, options) {
  1872. var numChoices = $field.is('select')
  1873. ? validator.getFieldElements($field.attr('data-bv-field')).find('option').filter(':selected').length
  1874. : validator.getFieldElements($field.attr('data-bv-field')).filter(':checked').length,
  1875. min = options.min ? ($.isNumeric(options.min) ? options.min : validator.getDynamicOption($field, options.min)) : null,
  1876. max = options.max ? ($.isNumeric(options.max) ? options.max : validator.getDynamicOption($field, options.max)) : null,
  1877. isValid = true,
  1878. message = options.message || $.fn.bootstrapValidator.i18n.choice['default'];
  1879. if ((min && numChoices < parseInt(min, 10)) || (max && numChoices > parseInt(max, 10))) {
  1880. isValid = false;
  1881. }
  1882. switch (true) {
  1883. case (!!min && !!max):
  1884. message = $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.choice.between, [parseInt(min, 10), parseInt(max, 10)]);
  1885. break;
  1886. case (!!min):
  1887. message = $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.choice.less, parseInt(min, 10));
  1888. break;
  1889. case (!!max):
  1890. message = $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.choice.more, parseInt(max, 10));
  1891. break;
  1892. default:
  1893. break;
  1894. }
  1895. return { valid: isValid, message: message };
  1896. }
  1897. };
  1898. }(window.jQuery));
  1899. ;(function($) {
  1900. $.fn.bootstrapValidator.i18n.creditCard = $.extend($.fn.bootstrapValidator.i18n.creditCard || {}, {
  1901. 'default': 'Please enter a valid credit card number'
  1902. });
  1903. $.fn.bootstrapValidator.validators.creditCard = {
  1904. /**
  1905. * Return true if the input value is valid credit card number
  1906. * Based on https://gist.github.com/DiegoSalazar/4075533
  1907. *
  1908. * @param {BootstrapValidator} validator The validator plugin instance
  1909. * @param {jQuery} $field Field element
  1910. * @param {Object} [options] Can consist of the following key:
  1911. * - message: The invalid message
  1912. * @returns {Boolean}
  1913. */
  1914. validate: function(validator, $field, options) {
  1915. var value = $field.val();
  1916. if (value === '') {
  1917. return true;
  1918. }
  1919. // Accept only digits, dashes or spaces
  1920. if (/[^0-9-\s]+/.test(value)) {
  1921. return false;
  1922. }
  1923. value = value.replace(/\D/g, '');
  1924. if (!$.fn.bootstrapValidator.helpers.luhn(value)) {
  1925. return false;
  1926. }
  1927. // Validate the card number based on prefix (IIN ranges) and length
  1928. var cards = {
  1929. AMERICAN_EXPRESS: {
  1930. length: [15],
  1931. prefix: ['34', '37']
  1932. },
  1933. DINERS_CLUB: {
  1934. length: [14],
  1935. prefix: ['300', '301', '302', '303', '304', '305', '36']
  1936. },
  1937. DINERS_CLUB_US: {
  1938. length: [16],
  1939. prefix: ['54', '55']
  1940. },
  1941. DISCOVER: {
  1942. length: [16],
  1943. prefix: ['6011', '622126', '622127', '622128', '622129', '62213',
  1944. '62214', '62215', '62216', '62217', '62218', '62219',
  1945. '6222', '6223', '6224', '6225', '6226', '6227', '6228',
  1946. '62290', '62291', '622920', '622921', '622922', '622923',
  1947. '622924', '622925', '644', '645', '646', '647', '648',
  1948. '649', '65']
  1949. },
  1950. JCB: {
  1951. length: [16],
  1952. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  1953. },
  1954. LASER: {
  1955. length: [16, 17, 18, 19],
  1956. prefix: ['6304', '6706', '6771', '6709']
  1957. },
  1958. MAESTRO: {
  1959. length: [12, 13, 14, 15, 16, 17, 18, 19],
  1960. prefix: ['5018', '5020', '5038', '6304', '6759', '6761', '6762', '6763', '6764', '6765', '6766']
  1961. },
  1962. MASTERCARD: {
  1963. length: [16],
  1964. prefix: ['51', '52', '53', '54', '55']
  1965. },
  1966. SOLO: {
  1967. length: [16, 18, 19],
  1968. prefix: ['6334', '6767']
  1969. },
  1970. UNIONPAY: {
  1971. length: [16, 17, 18, 19],
  1972. prefix: ['622126', '622127', '622128', '622129', '62213', '62214',
  1973. '62215', '62216', '62217', '62218', '62219', '6222', '6223',
  1974. '6224', '6225', '6226', '6227', '6228', '62290', '62291',
  1975. '622920', '622921', '622922', '622923', '622924', '622925']
  1976. },
  1977. VISA: {
  1978. length: [16],
  1979. prefix: ['4']
  1980. }
  1981. };
  1982. var type, i;
  1983. for (type in cards) {
  1984. for (i in cards[type].prefix) {
  1985. if (value.substr(0, cards[type].prefix[i].length) === cards[type].prefix[i] // Check the prefix
  1986. && $.inArray(value.length, cards[type].length) !== -1) // and length
  1987. {
  1988. return true;
  1989. }
  1990. }
  1991. }
  1992. return false;
  1993. }
  1994. };
  1995. }(window.jQuery));
  1996. ;(function($) {
  1997. $.fn.bootstrapValidator.i18n.cusip = $.extend($.fn.bootstrapValidator.i18n.cusip || {}, {
  1998. 'default': 'Please enter a valid CUSIP number'
  1999. });
  2000. $.fn.bootstrapValidator.validators.cusip = {
  2001. /**
  2002. * Validate a CUSIP
  2003. * Examples:
  2004. * - Valid: 037833100, 931142103, 14149YAR8, 126650BG6
  2005. * - Invalid: 31430F200, 022615AC2
  2006. *
  2007. * @see http://en.wikipedia.org/wiki/CUSIP
  2008. * @param {BootstrapValidator} validator The validator plugin instance
  2009. * @param {jQuery} $field Field element
  2010. * @param {Object} [options] Can consist of the following keys:
  2011. * - message: The invalid message
  2012. * @returns {Boolean}
  2013. */
  2014. validate: function(validator, $field, options) {
  2015. var value = $field.val();
  2016. if (value === '') {
  2017. return true;
  2018. }
  2019. value = value.toUpperCase();
  2020. if (!/^[0-9A-Z]{9}$/.test(value)) {
  2021. return false;
  2022. }
  2023. var converted = $.map(value.split(''), function(item) {
  2024. var code = item.charCodeAt(0);
  2025. return (code >= 'A'.charCodeAt(0) && code <= 'Z'.charCodeAt(0))
  2026. // Replace A, B, C, ..., Z with 10, 11, ..., 35
  2027. ? (code - 'A'.charCodeAt(0) + 10)
  2028. : item;
  2029. }),
  2030. length = converted.length,
  2031. sum = 0;
  2032. for (var i = 0; i < length - 1; i++) {
  2033. var num = parseInt(converted[i], 10);
  2034. if (i % 2 !== 0) {
  2035. num *= 2;
  2036. }
  2037. if (num > 9) {
  2038. num -= 9;
  2039. }
  2040. sum += num;
  2041. }
  2042. sum = (10 - (sum % 10)) % 10;
  2043. return sum === converted[length - 1];
  2044. }
  2045. };
  2046. }(window.jQuery));
  2047. ;(function($) {
  2048. $.fn.bootstrapValidator.i18n.cvv = $.extend($.fn.bootstrapValidator.i18n.cvv || {}, {
  2049. 'default': 'Please enter a valid CVV number'
  2050. });
  2051. $.fn.bootstrapValidator.validators.cvv = {
  2052. html5Attributes: {
  2053. message: 'message',
  2054. ccfield: 'creditCardField'
  2055. },
  2056. /**
  2057. * Return true if the input value is a valid CVV number.
  2058. *
  2059. * @param {BootstrapValidator} validator The validator plugin instance
  2060. * @param {jQuery} $field Field element
  2061. * @param {Object} options Can consist of the following keys:
  2062. * - creditCardField: The credit card number field. It can be null
  2063. * - message: The invalid message
  2064. * @returns {Boolean}
  2065. */
  2066. validate: function(validator, $field, options) {
  2067. var value = $field.val();
  2068. if (value === '') {
  2069. return true;
  2070. }
  2071. if (!/^[0-9]{3,4}$/.test(value)) {
  2072. return false;
  2073. }
  2074. if (!options.creditCardField) {
  2075. return true;
  2076. }
  2077. // Get the credit card number
  2078. var creditCard = validator.getFieldElements(options.creditCardField).val();
  2079. if (creditCard === '') {
  2080. return true;
  2081. }
  2082. creditCard = creditCard.replace(/\D/g, '');
  2083. // Supported credit card types
  2084. var cards = {
  2085. AMERICAN_EXPRESS: {
  2086. length: [15],
  2087. prefix: ['34', '37']
  2088. },
  2089. DINERS_CLUB: {
  2090. length: [14],
  2091. prefix: ['300', '301', '302', '303', '304', '305', '36']
  2092. },
  2093. DINERS_CLUB_US: {
  2094. length: [16],
  2095. prefix: ['54', '55']
  2096. },
  2097. DISCOVER: {
  2098. length: [16],
  2099. prefix: ['6011', '622126', '622127', '622128', '622129', '62213',
  2100. '62214', '62215', '62216', '62217', '62218', '62219',
  2101. '6222', '6223', '6224', '6225', '6226', '6227', '6228',
  2102. '62290', '62291', '622920', '622921', '622922', '622923',
  2103. '622924', '622925', '644', '645', '646', '647', '648',
  2104. '649', '65']
  2105. },
  2106. JCB: {
  2107. length: [16],
  2108. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  2109. },
  2110. LASER: {
  2111. length: [16, 17, 18, 19],
  2112. prefix: ['6304', '6706', '6771', '6709']
  2113. },
  2114. MAESTRO: {
  2115. length: [12, 13, 14, 15, 16, 17, 18, 19],
  2116. prefix: ['5018', '5020', '5038', '6304', '6759', '6761', '6762', '6763', '6764', '6765', '6766']
  2117. },
  2118. MASTERCARD: {
  2119. length: [16],
  2120. prefix: ['51', '52', '53', '54', '55']
  2121. },
  2122. SOLO: {
  2123. length: [16, 18, 19],
  2124. prefix: ['6334', '6767']
  2125. },
  2126. UNIONPAY: {
  2127. length: [16, 17, 18, 19],
  2128. prefix: ['622126', '622127', '622128', '622129', '62213', '62214',
  2129. '62215', '62216', '62217', '62218', '62219', '6222', '6223',
  2130. '6224', '6225', '6226', '6227', '6228', '62290', '62291',
  2131. '622920', '622921', '622922', '622923', '622924', '622925']
  2132. },
  2133. VISA: {
  2134. length: [16],
  2135. prefix: ['4']
  2136. }
  2137. };
  2138. var type, i, creditCardType = null;
  2139. for (type in cards) {
  2140. for (i in cards[type].prefix) {
  2141. if (creditCard.substr(0, cards[type].prefix[i].length) === cards[type].prefix[i] // Check the prefix
  2142. && $.inArray(creditCard.length, cards[type].length) !== -1) // and length
  2143. {
  2144. creditCardType = type;
  2145. break;
  2146. }
  2147. }
  2148. }
  2149. return (creditCardType === null)
  2150. ? false
  2151. : (('AMERICAN_EXPRESS' === creditCardType) ? (value.length === 4) : (value.length === 3));
  2152. }
  2153. };
  2154. }(window.jQuery));
  2155. ;(function($) {
  2156. $.fn.bootstrapValidator.i18n.date = $.extend($.fn.bootstrapValidator.i18n.date || {}, {
  2157. 'default': 'Please enter a valid date'
  2158. });
  2159. $.fn.bootstrapValidator.validators.date = {
  2160. html5Attributes: {
  2161. message: 'message',
  2162. format: 'format',
  2163. separator: 'separator'
  2164. },
  2165. /**
  2166. * Return true if the input value is valid date
  2167. *
  2168. * @param {BootstrapValidator} validator The validator plugin instance
  2169. * @param {jQuery} $field Field element
  2170. * @param {Object} options Can consist of the following keys:
  2171. * - message: The invalid message
  2172. * - separator: Use to separate the date, month, and year.
  2173. * By default, it is /
  2174. * - format: The date format. Default is MM/DD/YYYY
  2175. * The format can be:
  2176. *
  2177. * i) date: Consist of DD, MM, YYYY parts which are separated by the separator option
  2178. * ii) date and time:
  2179. * The time can consist of h, m, s parts which are separated by :
  2180. * ii) date, time and A (indicating AM or PM)
  2181. * @returns {Boolean}
  2182. */
  2183. validate: function(validator, $field, options) {
  2184. var value = $field.val();
  2185. if (value === '') {
  2186. return true;
  2187. }
  2188. options.format = options.format || 'MM/DD/YYYY';
  2189. // #683: Force the format to YYYY-MM-DD as the default browser behaviour when using type="date" attribute
  2190. if ($field.attr('type') === 'date') {
  2191. options.format = 'YYYY-MM-DD';
  2192. }
  2193. var formats = options.format.split(' '),
  2194. dateFormat = formats[0],
  2195. timeFormat = (formats.length > 1) ? formats[1] : null,
  2196. amOrPm = (formats.length > 2) ? formats[2] : null,
  2197. sections = value.split(' '),
  2198. date = sections[0],
  2199. time = (sections.length > 1) ? sections[1] : null;
  2200. if (formats.length !== sections.length) {
  2201. return false;
  2202. }
  2203. // Determine the separator
  2204. var separator = options.separator;
  2205. if (!separator) {
  2206. separator = (date.indexOf('/') !== -1) ? '/' : ((date.indexOf('-') !== -1) ? '-' : null);
  2207. }
  2208. if (separator === null || date.indexOf(separator) === -1) {
  2209. return false;
  2210. }
  2211. // Determine the date
  2212. date = date.split(separator);
  2213. dateFormat = dateFormat.split(separator);
  2214. if (date.length !== dateFormat.length) {
  2215. return false;
  2216. }
  2217. var year = date[$.inArray('YYYY', dateFormat)],
  2218. month = date[$.inArray('MM', dateFormat)],
  2219. day = date[$.inArray('DD', dateFormat)];
  2220. if (!year || !month || !day || year.length !== 4) {
  2221. return false;
  2222. }
  2223. // Determine the time
  2224. var minutes = null, hours = null, seconds = null;
  2225. if (timeFormat) {
  2226. timeFormat = timeFormat.split(':');
  2227. time = time.split(':');
  2228. if (timeFormat.length !== time.length) {
  2229. return false;
  2230. }
  2231. hours = time.length > 0 ? time[0] : null;
  2232. minutes = time.length > 1 ? time[1] : null;
  2233. seconds = time.length > 2 ? time[2] : null;
  2234. // Validate seconds
  2235. if (seconds) {
  2236. if (isNaN(seconds) || seconds.length > 2) {
  2237. return false;
  2238. }
  2239. seconds = parseInt(seconds, 10);
  2240. if (seconds < 0 || seconds > 60) {
  2241. return false;
  2242. }
  2243. }
  2244. // Validate hours
  2245. if (hours) {
  2246. if (isNaN(hours) || hours.length > 2) {
  2247. return false;
  2248. }
  2249. hours = parseInt(hours, 10);
  2250. if (hours < 0 || hours >= 24 || (amOrPm && hours > 12)) {
  2251. return false;
  2252. }
  2253. }
  2254. // Validate minutes
  2255. if (minutes) {
  2256. if (isNaN(minutes) || minutes.length > 2) {
  2257. return false;
  2258. }
  2259. minutes = parseInt(minutes, 10);
  2260. if (minutes < 0 || minutes > 59) {
  2261. return false;
  2262. }
  2263. }
  2264. }
  2265. // Validate day, month, and year
  2266. return $.fn.bootstrapValidator.helpers.date(year, month, day);
  2267. }
  2268. };
  2269. }(window.jQuery));
  2270. ;(function($) {
  2271. $.fn.bootstrapValidator.i18n.different = $.extend($.fn.bootstrapValidator.i18n.different || {}, {
  2272. 'default': 'Please enter a different value'
  2273. });
  2274. $.fn.bootstrapValidator.validators.different = {
  2275. html5Attributes: {
  2276. message: 'message',
  2277. field: 'field'
  2278. },
  2279. /**
  2280. * Return true if the input value is different with given field's value
  2281. *
  2282. * @param {BootstrapValidator} validator The validator plugin instance
  2283. * @param {jQuery} $field Field element
  2284. * @param {Object} options Consists of the following key:
  2285. * - field: The name of field that will be used to compare with current one
  2286. * - message: The invalid message
  2287. * @returns {Boolean}
  2288. */
  2289. validate: function(validator, $field, options) {
  2290. var value = $field.val();
  2291. if (value === '') {
  2292. return true;
  2293. }
  2294. var fields = options.field.split(','),
  2295. isValid = true;
  2296. for (var i = 0; i < fields.length; i++) {
  2297. var compareWith = validator.getFieldElements(fields[i]);
  2298. if (compareWith == null || compareWith.length === 0) {
  2299. continue;
  2300. }
  2301. var compareValue = compareWith.val();
  2302. if (value === compareValue) {
  2303. isValid = false;
  2304. } else if (compareValue !== '') {
  2305. validator.updateStatus(compareWith, validator.STATUS_VALID, 'different');
  2306. }
  2307. }
  2308. return isValid;
  2309. }
  2310. };
  2311. }(window.jQuery));
  2312. ;(function($) {
  2313. $.fn.bootstrapValidator.i18n.digits = $.extend($.fn.bootstrapValidator.i18n.digits || {}, {
  2314. 'default': 'Please enter only digits'
  2315. });
  2316. $.fn.bootstrapValidator.validators.digits = {
  2317. /**
  2318. * Return true if the input value contains digits only
  2319. *
  2320. * @param {BootstrapValidator} validator Validate plugin instance
  2321. * @param {jQuery} $field Field element
  2322. * @param {Object} [options]
  2323. * @returns {Boolean}
  2324. */
  2325. validate: function(validator, $field, options) {
  2326. var value = $field.val();
  2327. if (value === '') {
  2328. return true;
  2329. }
  2330. return /^\d+$/.test(value);
  2331. }
  2332. };
  2333. }(window.jQuery));
  2334. ;(function($) {
  2335. $.fn.bootstrapValidator.i18n.ean = $.extend($.fn.bootstrapValidator.i18n.ean || {}, {
  2336. 'default': 'Please enter a valid EAN number'
  2337. });
  2338. $.fn.bootstrapValidator.validators.ean = {
  2339. /**
  2340. * Validate EAN (International Article Number)
  2341. * Examples:
  2342. * - Valid: 73513537, 9780471117094, 4006381333931
  2343. * - Invalid: 73513536
  2344. *
  2345. * @see http://en.wikipedia.org/wiki/European_Article_Number
  2346. * @param {BootstrapValidator} validator The validator plugin instance
  2347. * @param {jQuery} $field Field element
  2348. * @param {Object} options Can consist of the following keys:
  2349. * - message: The invalid message
  2350. * @returns {Boolean}
  2351. */
  2352. validate: function(validator, $field, options) {
  2353. var value = $field.val();
  2354. if (value === '') {
  2355. return true;
  2356. }
  2357. if (!/^(\d{8}|\d{12}|\d{13})$/.test(value)) {
  2358. return false;
  2359. }
  2360. var length = value.length,
  2361. sum = 0,
  2362. weight = (length === 8) ? [3, 1] : [1, 3];
  2363. for (var i = 0; i < length - 1; i++) {
  2364. sum += parseInt(value.charAt(i), 10) * weight[i % 2];
  2365. }
  2366. sum = (10 - sum % 10) % 10;
  2367. return (sum + '' === value.charAt(length - 1));
  2368. }
  2369. };
  2370. }(window.jQuery));
  2371. ;(function($) {
  2372. $.fn.bootstrapValidator.i18n.emailAddress = $.extend($.fn.bootstrapValidator.i18n.emailAddress || {}, {
  2373. 'default': 'Please enter a valid email address'
  2374. });
  2375. $.fn.bootstrapValidator.validators.emailAddress = {
  2376. html5Attributes: {
  2377. message: 'message',
  2378. multiple: 'multiple',
  2379. separator: 'separator'
  2380. },
  2381. enableByHtml5: function($field) {
  2382. return ('email' === $field.attr('type'));
  2383. },
  2384. /**
  2385. * Return true if and only if the input value is a valid email address
  2386. *
  2387. * @param {BootstrapValidator} validator Validate plugin instance
  2388. * @param {jQuery} $field Field element
  2389. * @param {Object} [options]
  2390. * - multiple: Allow multiple email addresses, separated by a comma or semicolon; default is false.
  2391. * - separator: Regex for character or characters expected as separator between addresses; default is comma /[,;]/, i.e. comma or semicolon.
  2392. * @returns {Boolean}
  2393. */
  2394. validate: function(validator, $field, options) {
  2395. var value = $field.val();
  2396. if (value === '') {
  2397. return true;
  2398. }
  2399. // Email address regular expression
  2400. // http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
  2401. var emailRegExp = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,
  2402. allowMultiple = options.multiple === true || options.multiple === 'true';
  2403. if (allowMultiple) {
  2404. var separator = options.separator || /[,;]/,
  2405. addresses = this._splitEmailAddresses(value, separator);
  2406. for (var i = 0; i < addresses.length; i++) {
  2407. if (!emailRegExp.test(addresses[i])) {
  2408. return false;
  2409. }
  2410. }
  2411. return true;
  2412. } else {
  2413. return emailRegExp.test(value);
  2414. }
  2415. },
  2416. _splitEmailAddresses: function(emailAddresses, separator) {
  2417. var quotedFragments = emailAddresses.split(/"/),
  2418. quotedFragmentCount = quotedFragments.length,
  2419. emailAddressArray = [],
  2420. nextEmailAddress = '';
  2421. for (var i = 0; i < quotedFragmentCount; i++) {
  2422. if (i % 2 === 0) {
  2423. var splitEmailAddressFragments = quotedFragments[i].split(separator),
  2424. splitEmailAddressFragmentCount = splitEmailAddressFragments.length;
  2425. if (splitEmailAddressFragmentCount === 1) {
  2426. nextEmailAddress += splitEmailAddressFragments[0];
  2427. } else {
  2428. emailAddressArray.push(nextEmailAddress + splitEmailAddressFragments[0]);
  2429. for (var j = 1; j < splitEmailAddressFragmentCount - 1; j++) {
  2430. emailAddressArray.push(splitEmailAddressFragments[j]);
  2431. }
  2432. nextEmailAddress = splitEmailAddressFragments[splitEmailAddressFragmentCount - 1];
  2433. }
  2434. } else {
  2435. nextEmailAddress += '"' + quotedFragments[i];
  2436. if (i < quotedFragmentCount - 1) {
  2437. nextEmailAddress += '"';
  2438. }
  2439. }
  2440. }
  2441. emailAddressArray.push(nextEmailAddress);
  2442. return emailAddressArray;
  2443. }
  2444. };
  2445. }(window.jQuery));
  2446. ;(function($) {
  2447. $.fn.bootstrapValidator.i18n.file = $.extend($.fn.bootstrapValidator.i18n.file || {}, {
  2448. 'default': 'Please choose a valid file'
  2449. });
  2450. $.fn.bootstrapValidator.validators.file = {
  2451. html5Attributes: {
  2452. extension: 'extension',
  2453. maxsize: 'maxSize',
  2454. message: 'message',
  2455. type: 'type'
  2456. },
  2457. /**
  2458. * Validate upload file. Use HTML 5 API if the browser supports
  2459. *
  2460. * @param {BootstrapValidator} validator The validator plugin instance
  2461. * @param {jQuery} $field Field element
  2462. * @param {Object} options Can consist of the following keys:
  2463. * - extension: The allowed extensions, separated by a comma
  2464. * - maxSize: The maximum size in bytes
  2465. * - message: The invalid message
  2466. * - type: The allowed MIME type, separated by a comma
  2467. * @returns {Boolean}
  2468. */
  2469. validate: function(validator, $field, options) {
  2470. var value = $field.val();
  2471. if (value === '') {
  2472. return true;
  2473. }
  2474. var ext,
  2475. extensions = options.extension ? options.extension.toLowerCase().split(',') : null,
  2476. types = options.type ? options.type.toLowerCase().split(',') : null,
  2477. html5 = (window.File && window.FileList && window.FileReader);
  2478. if (html5) {
  2479. // Get FileList instance
  2480. var files = $field.get(0).files,
  2481. total = files.length;
  2482. for (var i = 0; i < total; i++) {
  2483. // Check file size
  2484. if (options.maxSize && files[i].size > parseInt(options.maxSize, 10)) {
  2485. return false;
  2486. }
  2487. // Check file extension
  2488. ext = files[i].name.substr(files[i].name.lastIndexOf('.') + 1);
  2489. if (extensions && $.inArray(ext.toLowerCase(), extensions) === -1) {
  2490. return false;
  2491. }
  2492. // Check file type
  2493. if (files[i].type && types && $.inArray(files[i].type.toLowerCase(), types) === -1) {
  2494. return false;
  2495. }
  2496. }
  2497. } else {
  2498. // Check file extension
  2499. ext = value.substr(value.lastIndexOf('.') + 1);
  2500. if (extensions && $.inArray(ext.toLowerCase(), extensions) === -1) {
  2501. return false;
  2502. }
  2503. }
  2504. return true;
  2505. }
  2506. };
  2507. }(window.jQuery));
  2508. ;(function($) {
  2509. $.fn.bootstrapValidator.i18n.greaterThan = $.extend($.fn.bootstrapValidator.i18n.greaterThan || {}, {
  2510. 'default': 'Please enter a value greater than or equal to %s',
  2511. notInclusive: 'Please enter a value greater than %s'
  2512. });
  2513. $.fn.bootstrapValidator.validators.greaterThan = {
  2514. html5Attributes: {
  2515. message: 'message',
  2516. value: 'value',
  2517. inclusive: 'inclusive'
  2518. },
  2519. enableByHtml5: function($field) {
  2520. var type = $field.attr('type'),
  2521. min = $field.attr('min');
  2522. if (min && type !== 'date') {
  2523. return {
  2524. value: min
  2525. };
  2526. }
  2527. return false;
  2528. },
  2529. /**
  2530. * Return true if the input value is greater than or equals to given number
  2531. *
  2532. * @param {BootstrapValidator} validator Validate plugin instance
  2533. * @param {jQuery} $field Field element
  2534. * @param {Object} options Can consist of the following keys:
  2535. * - value: Define the number to compare with. It can be
  2536. * - A number
  2537. * - Name of field which its value defines the number
  2538. * - Name of callback function that returns the number
  2539. * - A callback function that returns the number
  2540. *
  2541. * - inclusive [optional]: Can be true or false. Default is true
  2542. * - message: The invalid message
  2543. * @returns {Boolean|Object}
  2544. */
  2545. validate: function(validator, $field, options) {
  2546. var value = $field.val();
  2547. if (value === '') {
  2548. return true;
  2549. }
  2550. if (!$.isNumeric(value)) {
  2551. return false;
  2552. }
  2553. var compareTo = $.isNumeric(options.value) ? options.value : validator.getDynamicOption($field, options.value);
  2554. value = parseFloat(value);
  2555. return (options.inclusive === true || options.inclusive === undefined)
  2556. ? {
  2557. valid: value >= compareTo,
  2558. message: $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.greaterThan['default'], compareTo)
  2559. }
  2560. : {
  2561. valid: value > compareTo,
  2562. message: $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.greaterThan.notInclusive, compareTo)
  2563. };
  2564. }
  2565. };
  2566. }(window.jQuery));
  2567. ;(function($) {
  2568. $.fn.bootstrapValidator.i18n.grid = $.extend($.fn.bootstrapValidator.i18n.grid || {}, {
  2569. 'default': 'Please enter a valid GRId number'
  2570. });
  2571. $.fn.bootstrapValidator.validators.grid = {
  2572. /**
  2573. * Validate GRId (Global Release Identifier)
  2574. * Examples:
  2575. * - Valid: A12425GABC1234002M, A1-2425G-ABC1234002-M, A1 2425G ABC1234002 M, Grid:A1-2425G-ABC1234002-M
  2576. * - Invalid: A1-2425G-ABC1234002-Q
  2577. *
  2578. * @see http://en.wikipedia.org/wiki/Global_Release_Identifier
  2579. * @param {BootstrapValidator} validator The validator plugin instance
  2580. * @param {jQuery} $field Field element
  2581. * @param {Object} options Can consist of the following keys:
  2582. * - message: The invalid message
  2583. * @returns {Boolean}
  2584. */
  2585. validate: function(validator, $field, options) {
  2586. var value = $field.val();
  2587. if (value === '') {
  2588. return true;
  2589. }
  2590. value = value.toUpperCase();
  2591. if (!/^[GRID:]*([0-9A-Z]{2})[-\s]*([0-9A-Z]{5})[-\s]*([0-9A-Z]{10})[-\s]*([0-9A-Z]{1})$/g.test(value)) {
  2592. return false;
  2593. }
  2594. value = value.replace(/\s/g, '').replace(/-/g, '');
  2595. if ('GRID:' === value.substr(0, 5)) {
  2596. value = value.substr(5);
  2597. }
  2598. return $.fn.bootstrapValidator.helpers.mod37And36(value);
  2599. }
  2600. };
  2601. }(window.jQuery));
  2602. ;(function($) {
  2603. $.fn.bootstrapValidator.i18n.hex = $.extend($.fn.bootstrapValidator.i18n.hex || {}, {
  2604. 'default': 'Please enter a valid hexadecimal number'
  2605. });
  2606. $.fn.bootstrapValidator.validators.hex = {
  2607. /**
  2608. * Return true if and only if the input value is a valid hexadecimal number
  2609. *
  2610. * @param {BootstrapValidator} validator The validator plugin instance
  2611. * @param {jQuery} $field Field element
  2612. * @param {Object} options Consist of key:
  2613. * - message: The invalid message
  2614. * @returns {Boolean}
  2615. */
  2616. validate: function(validator, $field, options) {
  2617. var value = $field.val();
  2618. if (value === '') {
  2619. return true;
  2620. }
  2621. return /^[0-9a-fA-F]+$/.test(value);
  2622. }
  2623. };
  2624. }(window.jQuery));
  2625. ;(function($) {
  2626. $.fn.bootstrapValidator.i18n.hexColor = $.extend($.fn.bootstrapValidator.i18n.hexColor || {}, {
  2627. 'default': 'Please enter a valid hex color'
  2628. });
  2629. $.fn.bootstrapValidator.validators.hexColor = {
  2630. enableByHtml5: function($field) {
  2631. return ('color' === $field.attr('type'));
  2632. },
  2633. /**
  2634. * Return true if the input value is a valid hex color
  2635. *
  2636. * @param {BootstrapValidator} validator The validator plugin instance
  2637. * @param {jQuery} $field Field element
  2638. * @param {Object} options Can consist of the following keys:
  2639. * - message: The invalid message
  2640. * @returns {Boolean}
  2641. */
  2642. validate: function(validator, $field, options) {
  2643. var value = $field.val();
  2644. if (value === '') {
  2645. return true;
  2646. }
  2647. return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(value);
  2648. }
  2649. };
  2650. }(window.jQuery));
  2651. ;(function($) {
  2652. $.fn.bootstrapValidator.i18n.iban = $.extend($.fn.bootstrapValidator.i18n.iban || {}, {
  2653. 'default': 'Please enter a valid IBAN number',
  2654. countryNotSupported: 'The country code %s is not supported',
  2655. country: 'Please enter a valid IBAN number in %s',
  2656. countries: {
  2657. AD: 'Andorra',
  2658. AE: 'United Arab Emirates',
  2659. AL: 'Albania',
  2660. AO: 'Angola',
  2661. AT: 'Austria',
  2662. AZ: 'Azerbaijan',
  2663. BA: 'Bosnia and Herzegovina',
  2664. BE: 'Belgium',
  2665. BF: 'Burkina Faso',
  2666. BG: 'Bulgaria',
  2667. BH: 'Bahrain',
  2668. BI: 'Burundi',
  2669. BJ: 'Benin',
  2670. BR: 'Brazil',
  2671. CH: 'Switzerland',
  2672. CI: 'Ivory Coast',
  2673. CM: 'Cameroon',
  2674. CR: 'Costa Rica',
  2675. CV: 'Cape Verde',
  2676. CY: 'Cyprus',
  2677. CZ: 'Czech',
  2678. DE: 'Germany',
  2679. DK: 'Denmark',
  2680. DO: 'Dominica',
  2681. DZ: 'Algeria',
  2682. EE: 'Estonia',
  2683. ES: 'Spain',
  2684. FI: 'Finland',
  2685. FO: 'Faroe Islands',
  2686. FR: 'France',
  2687. GB: 'United Kingdom',
  2688. GE: 'Georgia',
  2689. GI: 'Gibraltar',
  2690. GL: 'Greenland',
  2691. GR: 'Greece',
  2692. GT: 'Guatemala',
  2693. HR: 'Croatia',
  2694. HU: 'Hungary',
  2695. IE: 'Ireland',
  2696. IL: 'Israel',
  2697. IR: 'Iran',
  2698. IS: 'Iceland',
  2699. IT: 'Italy',
  2700. JO: 'Jordan',
  2701. KW: 'Kuwait',
  2702. KZ: 'Kazakhstan',
  2703. LB: 'Lebanon',
  2704. LI: 'Liechtenstein',
  2705. LT: 'Lithuania',
  2706. LU: 'Luxembourg',
  2707. LV: 'Latvia',
  2708. MC: 'Monaco',
  2709. MD: 'Moldova',
  2710. ME: 'Montenegro',
  2711. MG: 'Madagascar',
  2712. MK: 'Macedonia',
  2713. ML: 'Mali',
  2714. MR: 'Mauritania',
  2715. MT: 'Malta',
  2716. MU: 'Mauritius',
  2717. MZ: 'Mozambique',
  2718. NL: 'Netherlands',
  2719. NO: 'Norway',
  2720. PK: 'Pakistan',
  2721. PL: 'Poland',
  2722. PS: 'Palestine',
  2723. PT: 'Portugal',
  2724. QA: 'Qatar',
  2725. RO: 'Romania',
  2726. RS: 'Serbia',
  2727. SA: 'Saudi Arabia',
  2728. SE: 'Sweden',
  2729. SI: 'Slovenia',
  2730. SK: 'Slovakia',
  2731. SM: 'San Marino',
  2732. SN: 'Senegal',
  2733. TN: 'Tunisia',
  2734. TR: 'Turkey',
  2735. VG: 'Virgin Islands, British'
  2736. }
  2737. });
  2738. $.fn.bootstrapValidator.validators.iban = {
  2739. html5Attributes: {
  2740. message: 'message',
  2741. country: 'country'
  2742. },
  2743. // http://www.swift.com/dsp/resources/documents/IBAN_Registry.pdf
  2744. // http://en.wikipedia.org/wiki/International_Bank_Account_Number#IBAN_formats_by_country
  2745. REGEX: {
  2746. AD: 'AD[0-9]{2}[0-9]{4}[0-9]{4}[A-Z0-9]{12}', // Andorra
  2747. AE: 'AE[0-9]{2}[0-9]{3}[0-9]{16}', // United Arab Emirates
  2748. AL: 'AL[0-9]{2}[0-9]{8}[A-Z0-9]{16}', // Albania
  2749. AO: 'AO[0-9]{2}[0-9]{21}', // Angola
  2750. AT: 'AT[0-9]{2}[0-9]{5}[0-9]{11}', // Austria
  2751. AZ: 'AZ[0-9]{2}[A-Z]{4}[A-Z0-9]{20}', // Azerbaijan
  2752. BA: 'BA[0-9]{2}[0-9]{3}[0-9]{3}[0-9]{8}[0-9]{2}', // Bosnia and Herzegovina
  2753. BE: 'BE[0-9]{2}[0-9]{3}[0-9]{7}[0-9]{2}', // Belgium
  2754. BF: 'BF[0-9]{2}[0-9]{23}', // Burkina Faso
  2755. BG: 'BG[0-9]{2}[A-Z]{4}[0-9]{4}[0-9]{2}[A-Z0-9]{8}', // Bulgaria
  2756. BH: 'BH[0-9]{2}[A-Z]{4}[A-Z0-9]{14}', // Bahrain
  2757. BI: 'BI[0-9]{2}[0-9]{12}', // Burundi
  2758. BJ: 'BJ[0-9]{2}[A-Z]{1}[0-9]{23}', // Benin
  2759. BR: 'BR[0-9]{2}[0-9]{8}[0-9]{5}[0-9]{10}[A-Z][A-Z0-9]', // Brazil
  2760. CH: 'CH[0-9]{2}[0-9]{5}[A-Z0-9]{12}', // Switzerland
  2761. CI: 'CI[0-9]{2}[A-Z]{1}[0-9]{23}', // Ivory Coast
  2762. CM: 'CM[0-9]{2}[0-9]{23}', // Cameroon
  2763. CR: 'CR[0-9]{2}[0-9]{3}[0-9]{14}', // Costa Rica
  2764. CV: 'CV[0-9]{2}[0-9]{21}', // Cape Verde
  2765. CY: 'CY[0-9]{2}[0-9]{3}[0-9]{5}[A-Z0-9]{16}', // Cyprus
  2766. CZ: 'CZ[0-9]{2}[0-9]{20}', // Czech Republic
  2767. DE: 'DE[0-9]{2}[0-9]{8}[0-9]{10}', // Germany
  2768. DK: 'DK[0-9]{2}[0-9]{14}', // Denmark
  2769. DO: 'DO[0-9]{2}[A-Z0-9]{4}[0-9]{20}', // Dominican Republic
  2770. DZ: 'DZ[0-9]{2}[0-9]{20}', // Algeria
  2771. EE: 'EE[0-9]{2}[0-9]{2}[0-9]{2}[0-9]{11}[0-9]{1}', // Estonia
  2772. ES: 'ES[0-9]{2}[0-9]{4}[0-9]{4}[0-9]{1}[0-9]{1}[0-9]{10}', // Spain
  2773. FI: 'FI[0-9]{2}[0-9]{6}[0-9]{7}[0-9]{1}', // Finland
  2774. FO: 'FO[0-9]{2}[0-9]{4}[0-9]{9}[0-9]{1}', // Faroe Islands
  2775. FR: 'FR[0-9]{2}[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}', // France
  2776. GB: 'GB[0-9]{2}[A-Z]{4}[0-9]{6}[0-9]{8}', // United Kingdom
  2777. GE: 'GE[0-9]{2}[A-Z]{2}[0-9]{16}', // Georgia
  2778. GI: 'GI[0-9]{2}[A-Z]{4}[A-Z0-9]{15}', // Gibraltar
  2779. GL: 'GL[0-9]{2}[0-9]{4}[0-9]{9}[0-9]{1}', // Greenland
  2780. GR: 'GR[0-9]{2}[0-9]{3}[0-9]{4}[A-Z0-9]{16}', // Greece
  2781. GT: 'GT[0-9]{2}[A-Z0-9]{4}[A-Z0-9]{20}', // Guatemala
  2782. HR: 'HR[0-9]{2}[0-9]{7}[0-9]{10}', // Croatia
  2783. HU: 'HU[0-9]{2}[0-9]{3}[0-9]{4}[0-9]{1}[0-9]{15}[0-9]{1}', // Hungary
  2784. IE: 'IE[0-9]{2}[A-Z]{4}[0-9]{6}[0-9]{8}', // Ireland
  2785. IL: 'IL[0-9]{2}[0-9]{3}[0-9]{3}[0-9]{13}', // Israel
  2786. IR: 'IR[0-9]{2}[0-9]{22}', // Iran
  2787. IS: 'IS[0-9]{2}[0-9]{4}[0-9]{2}[0-9]{6}[0-9]{10}', // Iceland
  2788. IT: 'IT[0-9]{2}[A-Z]{1}[0-9]{5}[0-9]{5}[A-Z0-9]{12}', // Italy
  2789. JO: 'JO[0-9]{2}[A-Z]{4}[0-9]{4}[0]{8}[A-Z0-9]{10}', // Jordan
  2790. KW: 'KW[0-9]{2}[A-Z]{4}[0-9]{22}', // Kuwait
  2791. KZ: 'KZ[0-9]{2}[0-9]{3}[A-Z0-9]{13}', // Kazakhstan
  2792. LB: 'LB[0-9]{2}[0-9]{4}[A-Z0-9]{20}', // Lebanon
  2793. LI: 'LI[0-9]{2}[0-9]{5}[A-Z0-9]{12}', // Liechtenstein
  2794. LT: 'LT[0-9]{2}[0-9]{5}[0-9]{11}', // Lithuania
  2795. LU: 'LU[0-9]{2}[0-9]{3}[A-Z0-9]{13}', // Luxembourg
  2796. LV: 'LV[0-9]{2}[A-Z]{4}[A-Z0-9]{13}', // Latvia
  2797. MC: 'MC[0-9]{2}[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}', // Monaco
  2798. MD: 'MD[0-9]{2}[A-Z0-9]{20}', // Moldova
  2799. ME: 'ME[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}', // Montenegro
  2800. MG: 'MG[0-9]{2}[0-9]{23}', // Madagascar
  2801. MK: 'MK[0-9]{2}[0-9]{3}[A-Z0-9]{10}[0-9]{2}', // Macedonia
  2802. ML: 'ML[0-9]{2}[A-Z]{1}[0-9]{23}', // Mali
  2803. MR: 'MR13[0-9]{5}[0-9]{5}[0-9]{11}[0-9]{2}', // Mauritania
  2804. MT: 'MT[0-9]{2}[A-Z]{4}[0-9]{5}[A-Z0-9]{18}', // Malta
  2805. MU: 'MU[0-9]{2}[A-Z]{4}[0-9]{2}[0-9]{2}[0-9]{12}[0-9]{3}[A-Z]{3}', // Mauritius
  2806. MZ: 'MZ[0-9]{2}[0-9]{21}', // Mozambique
  2807. NL: 'NL[0-9]{2}[A-Z]{4}[0-9]{10}', // Netherlands
  2808. NO: 'NO[0-9]{2}[0-9]{4}[0-9]{6}[0-9]{1}', // Norway
  2809. PK: 'PK[0-9]{2}[A-Z]{4}[A-Z0-9]{16}', // Pakistan
  2810. PL: 'PL[0-9]{2}[0-9]{8}[0-9]{16}', // Poland
  2811. PS: 'PS[0-9]{2}[A-Z]{4}[A-Z0-9]{21}', // Palestinian
  2812. PT: 'PT[0-9]{2}[0-9]{4}[0-9]{4}[0-9]{11}[0-9]{2}', // Portugal
  2813. QA: 'QA[0-9]{2}[A-Z]{4}[A-Z0-9]{21}', // Qatar
  2814. RO: 'RO[0-9]{2}[A-Z]{4}[A-Z0-9]{16}', // Romania
  2815. RS: 'RS[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}', // Serbia
  2816. SA: 'SA[0-9]{2}[0-9]{2}[A-Z0-9]{18}', // Saudi Arabia
  2817. SE: 'SE[0-9]{2}[0-9]{3}[0-9]{16}[0-9]{1}', // Sweden
  2818. SI: 'SI[0-9]{2}[0-9]{5}[0-9]{8}[0-9]{2}', // Slovenia
  2819. SK: 'SK[0-9]{2}[0-9]{4}[0-9]{6}[0-9]{10}', // Slovakia
  2820. SM: 'SM[0-9]{2}[A-Z]{1}[0-9]{5}[0-9]{5}[A-Z0-9]{12}', // San Marino
  2821. SN: 'SN[0-9]{2}[A-Z]{1}[0-9]{23}', // Senegal
  2822. TN: 'TN59[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}', // Tunisia
  2823. TR: 'TR[0-9]{2}[0-9]{5}[A-Z0-9]{1}[A-Z0-9]{16}', // Turkey
  2824. VG: 'VG[0-9]{2}[A-Z]{4}[0-9]{16}' // Virgin Islands, British
  2825. },
  2826. /**
  2827. * Validate an International Bank Account Number (IBAN)
  2828. * To test it, take the sample IBAN from
  2829. * http://www.nordea.com/Our+services/International+products+and+services/Cash+Management/IBAN+countries/908462.html
  2830. *
  2831. * @param {BootstrapValidator} validator The validator plugin instance
  2832. * @param {jQuery} $field Field element
  2833. * @param {Object} options Can consist of the following keys:
  2834. * - message: The invalid message
  2835. * - country: The ISO 3166-1 country code. It can be
  2836. * - A country code
  2837. * - Name of field which its value defines the country code
  2838. * - Name of callback function that returns the country code
  2839. * - A callback function that returns the country code
  2840. * @returns {Boolean|Object}
  2841. */
  2842. validate: function(validator, $field, options) {
  2843. var value = $field.val();
  2844. if (value === '') {
  2845. return true;
  2846. }
  2847. value = value.replace(/[^a-zA-Z0-9]/g, '').toUpperCase();
  2848. var country = options.country;
  2849. if (!country) {
  2850. country = value.substr(0, 2);
  2851. } else if (typeof country !== 'string' || !this.REGEX[country]) {
  2852. // Determine the country code
  2853. country = validator.getDynamicOption($field, country);
  2854. }
  2855. if (!this.REGEX[country]) {
  2856. return {
  2857. valid: false,
  2858. message: $.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.iban.countryNotSupported, country)
  2859. };
  2860. }
  2861. if (!(new RegExp('^' + this.REGEX[country] + '$')).test(value)) {
  2862. return {
  2863. valid: false,
  2864. message: $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.iban.country, $.fn.bootstrapValidator.i18n.iban.countries[country])
  2865. };
  2866. }
  2867. value = value.substr(4) + value.substr(0, 4);
  2868. value = $.map(value.split(''), function(n) {
  2869. var code = n.charCodeAt(0);
  2870. return (code >= 'A'.charCodeAt(0) && code <= 'Z'.charCodeAt(0))
  2871. // Replace A, B, C, ..., Z with 10, 11, ..., 35
  2872. ? (code - 'A'.charCodeAt(0) + 10)
  2873. : n;
  2874. });
  2875. value = value.join('');
  2876. var temp = parseInt(value.substr(0, 1), 10),
  2877. length = value.length;
  2878. for (var i = 1; i < length; ++i) {
  2879. temp = (temp * 10 + parseInt(value.substr(i, 1), 10)) % 97;
  2880. }
  2881. return {
  2882. valid: (temp === 1),
  2883. message: $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.iban.country, $.fn.bootstrapValidator.i18n.iban.countries[country])
  2884. };
  2885. }
  2886. };
  2887. }(window.jQuery));
  2888. ;(function($) {
  2889. $.fn.bootstrapValidator.i18n.id = $.extend($.fn.bootstrapValidator.i18n.id || {}, {
  2890. 'default': 'Please enter a valid identification number',
  2891. countryNotSupported: 'The country code %s is not supported',
  2892. country: 'Please enter a valid identification number in %s',
  2893. countries: {
  2894. BA: 'Bosnia and Herzegovina',
  2895. BG: 'Bulgaria',
  2896. BR: 'Brazil',
  2897. CH: 'Switzerland',
  2898. CL: 'Chile',
  2899. CZ: 'Czech',
  2900. DK: 'Denmark',
  2901. EE: 'Estonia',
  2902. ES: 'Spain',
  2903. FI: 'Finland',
  2904. HR: 'Croatia',
  2905. IE: 'Ireland',
  2906. IS: 'Iceland',
  2907. LT: 'Lithuania',
  2908. LV: 'Latvia',
  2909. ME: 'Montenegro',
  2910. MK: 'Macedonia',
  2911. NL: 'Netherlands',
  2912. RO: 'Romania',
  2913. RS: 'Serbia',
  2914. SE: 'Sweden',
  2915. SI: 'Slovenia',
  2916. SK: 'Slovakia',
  2917. SM: 'San Marino',
  2918. TH: 'Thailand',
  2919. ZA: 'South Africa'
  2920. }
  2921. });
  2922. $.fn.bootstrapValidator.validators.id = {
  2923. html5Attributes: {
  2924. message: 'message',
  2925. country: 'country'
  2926. },
  2927. // Supported country codes
  2928. COUNTRY_CODES: [
  2929. 'BA', 'BG', 'BR', 'CH', 'CL', 'CZ', 'DK', 'EE', 'ES', 'FI', 'HR', 'IE', 'IS', 'LT', 'LV', 'ME', 'MK', 'NL',
  2930. 'RO', 'RS', 'SE', 'SI', 'SK', 'SM', 'TH', 'ZA'
  2931. ],
  2932. /**
  2933. * Validate identification number in different countries
  2934. *
  2935. * @see http://en.wikipedia.org/wiki/National_identification_number
  2936. * @param {BootstrapValidator} validator The validator plugin instance
  2937. * @param {jQuery} $field Field element
  2938. * @param {Object} options Consist of key:
  2939. * - message: The invalid message
  2940. * - country: The ISO 3166-1 country code. It can be
  2941. * - One of country code defined in COUNTRY_CODES
  2942. * - Name of field which its value defines the country code
  2943. * - Name of callback function that returns the country code
  2944. * - A callback function that returns the country code
  2945. * @returns {Boolean|Object}
  2946. */
  2947. validate: function(validator, $field, options) {
  2948. var value = $field.val();
  2949. if (value === '') {
  2950. return true;
  2951. }
  2952. var country = options.country;
  2953. if (!country) {
  2954. country = value.substr(0, 2);
  2955. } else if (typeof country !== 'string' || $.inArray(country.toUpperCase(), this.COUNTRY_CODES) === -1) {
  2956. // Determine the country code
  2957. country = validator.getDynamicOption($field, country);
  2958. }
  2959. if ($.inArray(country, this.COUNTRY_CODES) === -1) {
  2960. return { valid: false, message: $.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.id.countryNotSupported, country) };
  2961. }
  2962. var method = ['_', country.toLowerCase()].join('');
  2963. return this[method](value)
  2964. ? true
  2965. : {
  2966. valid: false,
  2967. message: $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.id.country, $.fn.bootstrapValidator.i18n.id.countries[country.toUpperCase()])
  2968. };
  2969. },
  2970. /**
  2971. * Validate Unique Master Citizen Number which uses in
  2972. * - Bosnia and Herzegovina (country code: BA)
  2973. * - Macedonia (MK)
  2974. * - Montenegro (ME)
  2975. * - Serbia (RS)
  2976. * - Slovenia (SI)
  2977. *
  2978. * @see http://en.wikipedia.org/wiki/Unique_Master_Citizen_Number
  2979. * @param {String} value The ID
  2980. * @param {String} countryCode The ISO country code, can be BA, MK, ME, RS, SI
  2981. * @returns {Boolean}
  2982. */
  2983. _validateJMBG: function(value, countryCode) {
  2984. if (!/^\d{13}$/.test(value)) {
  2985. return false;
  2986. }
  2987. var day = parseInt(value.substr(0, 2), 10),
  2988. month = parseInt(value.substr(2, 2), 10),
  2989. year = parseInt(value.substr(4, 3), 10),
  2990. rr = parseInt(value.substr(7, 2), 10),
  2991. k = parseInt(value.substr(12, 1), 10);
  2992. // Validate date of birth
  2993. // FIXME: Validate the year of birth
  2994. if (day > 31 || month > 12) {
  2995. return false;
  2996. }
  2997. // Validate checksum
  2998. var sum = 0;
  2999. for (var i = 0; i < 6; i++) {
  3000. sum += (7 - i) * (parseInt(value.charAt(i), 10) + parseInt(value.charAt(i + 6), 10));
  3001. }
  3002. sum = 11 - sum % 11;
  3003. if (sum === 10 || sum === 11) {
  3004. sum = 0;
  3005. }
  3006. if (sum !== k) {
  3007. return false;
  3008. }
  3009. // Validate political region
  3010. // rr is the political region of birth, which can be in ranges:
  3011. // 10-19: Bosnia and Herzegovina
  3012. // 20-29: Montenegro
  3013. // 30-39: Croatia (not used anymore)
  3014. // 41-49: Macedonia
  3015. // 50-59: Slovenia (only 50 is used)
  3016. // 70-79: Central Serbia
  3017. // 80-89: Serbian province of Vojvodina
  3018. // 90-99: Kosovo
  3019. switch (countryCode.toUpperCase()) {
  3020. case 'BA':
  3021. return (10 <= rr && rr <= 19);
  3022. case 'MK':
  3023. return (41 <= rr && rr <= 49);
  3024. case 'ME':
  3025. return (20 <= rr && rr <= 29);
  3026. case 'RS':
  3027. return (70 <= rr && rr <= 99);
  3028. case 'SI':
  3029. return (50 <= rr && rr <= 59);
  3030. default:
  3031. return true;
  3032. }
  3033. },
  3034. _ba: function(value) {
  3035. return this._validateJMBG(value, 'BA');
  3036. },
  3037. _mk: function(value) {
  3038. return this._validateJMBG(value, 'MK');
  3039. },
  3040. _me: function(value) {
  3041. return this._validateJMBG(value, 'ME');
  3042. },
  3043. _rs: function(value) {
  3044. return this._validateJMBG(value, 'RS');
  3045. },
  3046. /**
  3047. * Examples: 0101006500006
  3048. */
  3049. _si: function(value) {
  3050. return this._validateJMBG(value, 'SI');
  3051. },
  3052. /**
  3053. * Validate Bulgarian national identification number (EGN)
  3054. * Examples:
  3055. * - Valid: 7523169263, 8032056031, 803205 603 1, 8001010008, 7501020018, 7552010005, 7542011030
  3056. * - Invalid: 8019010008
  3057. *
  3058. * @see http://en.wikipedia.org/wiki/Uniform_civil_number
  3059. * @param {String} value The ID
  3060. * @returns {Boolean}
  3061. */
  3062. _bg: function(value) {
  3063. if (!/^\d{10}$/.test(value) && !/^\d{6}\s\d{3}\s\d{1}$/.test(value)) {
  3064. return false;
  3065. }
  3066. value = value.replace(/\s/g, '');
  3067. // Check the birth date
  3068. var year = parseInt(value.substr(0, 2), 10) + 1900,
  3069. month = parseInt(value.substr(2, 2), 10),
  3070. day = parseInt(value.substr(4, 2), 10);
  3071. if (month > 40) {
  3072. year += 100;
  3073. month -= 40;
  3074. } else if (month > 20) {
  3075. year -= 100;
  3076. month -= 20;
  3077. }
  3078. if (!$.fn.bootstrapValidator.helpers.date(year, month, day)) {
  3079. return false;
  3080. }
  3081. var sum = 0,
  3082. weight = [2, 4, 8, 5, 10, 9, 7, 3, 6];
  3083. for (var i = 0; i < 9; i++) {
  3084. sum += parseInt(value.charAt(i), 10) * weight[i];
  3085. }
  3086. sum = (sum % 11) % 10;
  3087. return (sum + '' === value.substr(9, 1));
  3088. },
  3089. /**
  3090. * Validate Brazilian national identification number (CPF)
  3091. * Examples:
  3092. * - Valid: 39053344705, 390.533.447-05, 111.444.777-35
  3093. * - Invalid: 231.002.999-00
  3094. *
  3095. * @see http://en.wikipedia.org/wiki/Cadastro_de_Pessoas_F%C3%ADsicas
  3096. * @param {String} value The ID
  3097. * @returns {Boolean}
  3098. */
  3099. _br: function(value) {
  3100. if (/^1{11}|2{11}|3{11}|4{11}|5{11}|6{11}|7{11}|8{11}|9{11}|0{11}$/.test(value)) {
  3101. return false;
  3102. }
  3103. if (!/^\d{11}$/.test(value) && !/^\d{3}\.\d{3}\.\d{3}-\d{2}$/.test(value)) {
  3104. return false;
  3105. }
  3106. value = value.replace(/\./g, '').replace(/-/g, '');
  3107. var d1 = 0;
  3108. for (var i = 0; i < 9; i++) {
  3109. d1 += (10 - i) * parseInt(value.charAt(i), 10);
  3110. }
  3111. d1 = 11 - d1 % 11;
  3112. if (d1 === 10 || d1 === 11) {
  3113. d1 = 0;
  3114. }
  3115. if (d1 + '' !== value.charAt(9)) {
  3116. return false;
  3117. }
  3118. var d2 = 0;
  3119. for (i = 0; i < 10; i++) {
  3120. d2 += (11 - i) * parseInt(value.charAt(i), 10);
  3121. }
  3122. d2 = 11 - d2 % 11;
  3123. if (d2 === 10 || d2 === 11) {
  3124. d2 = 0;
  3125. }
  3126. return (d2 + '' === value.charAt(10));
  3127. },
  3128. /**
  3129. * Validate Swiss Social Security Number (AHV-Nr/No AVS)
  3130. * Examples:
  3131. * - Valid: 756.1234.5678.95, 7561234567895
  3132. *
  3133. * @see http://en.wikipedia.org/wiki/National_identification_number#Switzerland
  3134. * @see http://www.bsv.admin.ch/themen/ahv/00011/02185/index.html?lang=de
  3135. * @param {String} value The ID
  3136. * @returns {Boolean}
  3137. */
  3138. _ch: function(value) {
  3139. if (!/^756[\.]{0,1}[0-9]{4}[\.]{0,1}[0-9]{4}[\.]{0,1}[0-9]{2}$/.test(value)) {
  3140. return false;
  3141. }
  3142. value = value.replace(/\D/g, '').substr(3);
  3143. var length = value.length,
  3144. sum = 0,
  3145. weight = (length === 8) ? [3, 1] : [1, 3];
  3146. for (var i = 0; i < length - 1; i++) {
  3147. sum += parseInt(value.charAt(i), 10) * weight[i % 2];
  3148. }
  3149. sum = 10 - sum % 10;
  3150. return (sum + '' === value.charAt(length - 1));
  3151. },
  3152. /**
  3153. * Validate Chilean national identification number (RUN/RUT)
  3154. * Examples:
  3155. * - Valid: 76086428-5, 22060449-7, 12531909-2
  3156. *
  3157. * @see http://en.wikipedia.org/wiki/National_identification_number#Chile
  3158. * @see https://palena.sii.cl/cvc/dte/ee_empresas_emisoras.html for samples
  3159. * @param {String} value The ID
  3160. * @returns {Boolean}
  3161. */
  3162. _cl: function(value) {
  3163. if (!/^\d{7,8}[-]{0,1}[0-9K]$/i.test(value)) {
  3164. return false;
  3165. }
  3166. value = value.replace(/\-/g, '');
  3167. while (value.length < 9) {
  3168. value = '0' + value;
  3169. }
  3170. var sum = 0,
  3171. weight = [3, 2, 7, 6, 5, 4, 3, 2];
  3172. for (var i = 0; i < 8; i++) {
  3173. sum += parseInt(value.charAt(i), 10) * weight[i];
  3174. }
  3175. sum = 11 - sum % 11;
  3176. if (sum === 11) {
  3177. sum = 0;
  3178. } else if (sum === 10) {
  3179. sum = 'K';
  3180. }
  3181. return sum + '' === value.charAt(8).toUpperCase();
  3182. },
  3183. /**
  3184. * Validate Czech national identification number (RC)
  3185. * Examples:
  3186. * - Valid: 7103192745, 991231123
  3187. * - Invalid: 1103492745, 590312123
  3188. *
  3189. * @param {String} value The ID
  3190. * @returns {Boolean}
  3191. */
  3192. _cz: function(value) {
  3193. if (!/^\d{9,10}$/.test(value)) {
  3194. return false;
  3195. }
  3196. var year = 1900 + parseInt(value.substr(0, 2), 10),
  3197. month = parseInt(value.substr(2, 2), 10) % 50 % 20,
  3198. day = parseInt(value.substr(4, 2), 10);
  3199. if (value.length === 9) {
  3200. if (year >= 1980) {
  3201. year -= 100;
  3202. }
  3203. if (year > 1953) {
  3204. return false;
  3205. }
  3206. } else if (year < 1954) {
  3207. year += 100;
  3208. }
  3209. if (!$.fn.bootstrapValidator.helpers.date(year, month, day)) {
  3210. return false;
  3211. }
  3212. // Check that the birth date is not in the future
  3213. if (value.length === 10) {
  3214. var check = parseInt(value.substr(0, 9), 10) % 11;
  3215. if (year < 1985) {
  3216. check = check % 10;
  3217. }
  3218. return (check + '' === value.substr(9, 1));
  3219. }
  3220. return true;
  3221. },
  3222. /**
  3223. * Validate Danish Personal Identification number (CPR)
  3224. * Examples:
  3225. * - Valid: 2110625629, 211062-5629
  3226. * - Invalid: 511062-5629
  3227. *
  3228. * @see https://en.wikipedia.org/wiki/Personal_identification_number_(Denmark)
  3229. * @param {String} value The ID
  3230. * @returns {Boolean}
  3231. */
  3232. _dk: function(value) {
  3233. if (!/^[0-9]{6}[-]{0,1}[0-9]{4}$/.test(value)) {
  3234. return false;
  3235. }
  3236. value = value.replace(/-/g, '');
  3237. var day = parseInt(value.substr(0, 2), 10),
  3238. month = parseInt(value.substr(2, 2), 10),
  3239. year = parseInt(value.substr(4, 2), 10);
  3240. switch (true) {
  3241. case ('5678'.indexOf(value.charAt(6)) !== -1 && year >= 58):
  3242. year += 1800;
  3243. break;
  3244. case ('0123'.indexOf(value.charAt(6)) !== -1):
  3245. case ('49'.indexOf(value.charAt(6)) !== -1 && year >= 37):
  3246. year += 1900;
  3247. break;
  3248. default:
  3249. year += 2000;
  3250. break;
  3251. }
  3252. return $.fn.bootstrapValidator.helpers.date(year, month, day);
  3253. },
  3254. /**
  3255. * Validate Estonian Personal Identification Code (isikukood)
  3256. * Examples:
  3257. * - Valid: 37605030299
  3258. *
  3259. * @see http://et.wikipedia.org/wiki/Isikukood
  3260. * @param {String} value The ID
  3261. * @returns {Boolean}
  3262. */
  3263. _ee: function(value) {
  3264. // Use the same format as Lithuanian Personal Code
  3265. return this._lt(value);
  3266. },
  3267. /**
  3268. * Validate Spanish personal identity code (DNI)
  3269. * Support i) DNI (for Spanish citizens) and ii) NIE (for foreign people)
  3270. *
  3271. * Examples:
  3272. * - Valid: i) 54362315K, 54362315-K; ii) X2482300W, X-2482300W, X-2482300-W
  3273. * - Invalid: i) 54362315Z; ii) X-2482300A
  3274. *
  3275. * @see https://en.wikipedia.org/wiki/National_identification_number#Spain
  3276. * @param {String} value The ID
  3277. * @returns {Boolean}
  3278. */
  3279. _es: function(value) {
  3280. if (!/^[0-9A-Z]{8}[-]{0,1}[0-9A-Z]$/.test(value) // DNI
  3281. && !/^[XYZ][-]{0,1}[0-9]{7}[-]{0,1}[0-9A-Z]$/.test(value)) { // NIE
  3282. return false;
  3283. }
  3284. value = value.replace(/-/g, '');
  3285. var index = 'XYZ'.indexOf(value.charAt(0));
  3286. if (index !== -1) {
  3287. // It is NIE number
  3288. value = index + value.substr(1) + '';
  3289. }
  3290. var check = parseInt(value.substr(0, 8), 10);
  3291. check = 'TRWAGMYFPDXBNJZSQVHLCKE'[check % 23];
  3292. return (check === value.substr(8, 1));
  3293. },
  3294. /**
  3295. * Validate Finnish Personal Identity Code (HETU)
  3296. * Examples:
  3297. * - Valid: 311280-888Y, 131052-308T
  3298. * - Invalid: 131052-308U, 310252-308Y
  3299. *
  3300. * @param {String} value The ID
  3301. * @returns {Boolean}
  3302. */
  3303. _fi: function(value) {
  3304. if (!/^[0-9]{6}[-+A][0-9]{3}[0-9ABCDEFHJKLMNPRSTUVWXY]$/.test(value)) {
  3305. return false;
  3306. }
  3307. var day = parseInt(value.substr(0, 2), 10),
  3308. month = parseInt(value.substr(2, 2), 10),
  3309. year = parseInt(value.substr(4, 2), 10),
  3310. centuries = {
  3311. '+': 1800,
  3312. '-': 1900,
  3313. 'A': 2000
  3314. };
  3315. year = centuries[value.charAt(6)] + year;
  3316. if (!$.fn.bootstrapValidator.helpers.date(year, month, day)) {
  3317. return false;
  3318. }
  3319. var individual = parseInt(value.substr(7, 3), 10);
  3320. if (individual < 2) {
  3321. return false;
  3322. }
  3323. var n = value.substr(0, 6) + value.substr(7, 3) + '';
  3324. n = parseInt(n, 10);
  3325. return '0123456789ABCDEFHJKLMNPRSTUVWXY'.charAt(n % 31) === value.charAt(10);
  3326. },
  3327. /**
  3328. * Validate Croatian personal identification number (OIB)
  3329. * Examples:
  3330. * - Valid: 33392005961
  3331. * - Invalid: 33392005962
  3332. *
  3333. * @param {String} value The ID
  3334. * @returns {Boolean}
  3335. */
  3336. _hr: function(value) {
  3337. if (!/^[0-9]{11}$/.test(value)) {
  3338. return false;
  3339. }
  3340. return $.fn.bootstrapValidator.helpers.mod11And10(value);
  3341. },
  3342. /**
  3343. * Validate Irish Personal Public Service Number (PPS)
  3344. * Examples:
  3345. * - Valid: 6433435F, 6433435FT, 6433435FW, 6433435OA, 6433435IH, 1234567TW, 1234567FA
  3346. * - Invalid: 6433435E, 6433435VH
  3347. *
  3348. * @see https://en.wikipedia.org/wiki/Personal_Public_Service_Number
  3349. * @param {String} value The ID
  3350. * @returns {Boolean}
  3351. */
  3352. _ie: function(value) {
  3353. if (!/^\d{7}[A-W][AHWTX]?$/.test(value)) {
  3354. return false;
  3355. }
  3356. var getCheckDigit = function(value) {
  3357. while (value.length < 7) {
  3358. value = '0' + value;
  3359. }
  3360. var alphabet = 'WABCDEFGHIJKLMNOPQRSTUV',
  3361. sum = 0;
  3362. for (var i = 0; i < 7; i++) {
  3363. sum += parseInt(value.charAt(i), 10) * (8 - i);
  3364. }
  3365. sum += 9 * alphabet.indexOf(value.substr(7));
  3366. return alphabet[sum % 23];
  3367. };
  3368. // 2013 format
  3369. if (value.length === 9 && ('A' === value.charAt(8) || 'H' === value.charAt(8))) {
  3370. return value.charAt(7) === getCheckDigit(value.substr(0, 7) + value.substr(8) + '');
  3371. }
  3372. // The old format
  3373. else {
  3374. return value.charAt(7) === getCheckDigit(value.substr(0, 7));
  3375. }
  3376. },
  3377. /**
  3378. * Validate Iceland national identification number (Kennitala)
  3379. * Examples:
  3380. * - Valid: 120174-3399, 1201743399, 0902862349
  3381. *
  3382. * @see http://en.wikipedia.org/wiki/Kennitala
  3383. * @param {String} value The ID
  3384. * @returns {Boolean}
  3385. */
  3386. _is: function(value) {
  3387. if (!/^[0-9]{6}[-]{0,1}[0-9]{4}$/.test(value)) {
  3388. return false;
  3389. }
  3390. value = value.replace(/-/g, '');
  3391. var day = parseInt(value.substr(0, 2), 10),
  3392. month = parseInt(value.substr(2, 2), 10),
  3393. year = parseInt(value.substr(4, 2), 10),
  3394. century = parseInt(value.charAt(9), 10);
  3395. year = (century === 9) ? (1900 + year) : ((20 + century) * 100 + year);
  3396. if (!$.fn.bootstrapValidator.helpers.date(year, month, day, true)) {
  3397. return false;
  3398. }
  3399. // Validate the check digit
  3400. var sum = 0,
  3401. weight = [3, 2, 7, 6, 5, 4, 3, 2];
  3402. for (var i = 0; i < 8; i++) {
  3403. sum += parseInt(value.charAt(i), 10) * weight[i];
  3404. }
  3405. sum = 11 - sum % 11;
  3406. return (sum + '' === value.charAt(8));
  3407. },
  3408. /**
  3409. * Validate Lithuanian Personal Code (Asmens kodas)
  3410. * Examples:
  3411. * - Valid: 38703181745
  3412. * - Invalid: 38703181746, 78703181745, 38703421745
  3413. *
  3414. * @see http://en.wikipedia.org/wiki/National_identification_number#Lithuania
  3415. * @see http://www.adomas.org/midi2007/pcode.html
  3416. * @param {String} value The ID
  3417. * @returns {Boolean}
  3418. */
  3419. _lt: function(value) {
  3420. if (!/^[0-9]{11}$/.test(value)) {
  3421. return false;
  3422. }
  3423. var gender = parseInt(value.charAt(0), 10),
  3424. year = parseInt(value.substr(1, 2), 10),
  3425. month = parseInt(value.substr(3, 2), 10),
  3426. day = parseInt(value.substr(5, 2), 10),
  3427. century = (gender % 2 === 0) ? (17 + gender / 2) : (17 + (gender + 1) / 2);
  3428. year = century * 100 + year;
  3429. if (!$.fn.bootstrapValidator.helpers.date(year, month, day, true)) {
  3430. return false;
  3431. }
  3432. // Validate the check digit
  3433. var sum = 0,
  3434. weight = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1];
  3435. for (var i = 0; i < 10; i++) {
  3436. sum += parseInt(value.charAt(i), 10) * weight[i];
  3437. }
  3438. sum = sum % 11;
  3439. if (sum !== 10) {
  3440. return sum + '' === value.charAt(10);
  3441. }
  3442. // Re-calculate the check digit
  3443. sum = 0;
  3444. weight = [3, 4, 5, 6, 7, 8, 9, 1, 2, 3];
  3445. for (i = 0; i < 10; i++) {
  3446. sum += parseInt(value.charAt(i), 10) * weight[i];
  3447. }
  3448. sum = sum % 11;
  3449. if (sum === 10) {
  3450. sum = 0;
  3451. }
  3452. return (sum + '' === value.charAt(10));
  3453. },
  3454. /**
  3455. * Validate Latvian Personal Code (Personas kods)
  3456. * Examples:
  3457. * - Valid: 161175-19997, 16117519997
  3458. * - Invalid: 161375-19997
  3459. *
  3460. * @see http://laacz.lv/2006/11/25/pk-parbaudes-algoritms/
  3461. * @param {String} value The ID
  3462. * @returns {Boolean}
  3463. */
  3464. _lv: function(value) {
  3465. if (!/^[0-9]{6}[-]{0,1}[0-9]{5}$/.test(value)) {
  3466. return false;
  3467. }
  3468. value = value.replace(/\D/g, '');
  3469. // Check birth date
  3470. var day = parseInt(value.substr(0, 2), 10),
  3471. month = parseInt(value.substr(2, 2), 10),
  3472. year = parseInt(value.substr(4, 2), 10);
  3473. year = year + 1800 + parseInt(value.charAt(6), 10) * 100;
  3474. if (!$.fn.bootstrapValidator.helpers.date(year, month, day, true)) {
  3475. return false;
  3476. }
  3477. // Check personal code
  3478. var sum = 0,
  3479. weight = [10, 5, 8, 4, 2, 1, 6, 3, 7, 9];
  3480. for (var i = 0; i < 10; i++) {
  3481. sum += parseInt(value.charAt(i), 10) * weight[i];
  3482. }
  3483. sum = (sum + 1) % 11 % 10;
  3484. return (sum + '' === value.charAt(10));
  3485. },
  3486. /**
  3487. * Validate Dutch national identification number (BSN)
  3488. * Examples:
  3489. * - Valid: 111222333, 941331490, 9413.31.490
  3490. * - Invalid: 111252333
  3491. *
  3492. * @see https://nl.wikipedia.org/wiki/Burgerservicenummer
  3493. * @param {String} value The ID
  3494. * @returns {Boolean}
  3495. */
  3496. _nl: function(value) {
  3497. while (value.length < 9) {
  3498. value = '0' + value;
  3499. }
  3500. if (!/^[0-9]{4}[.]{0,1}[0-9]{2}[.]{0,1}[0-9]{3}$/.test(value)) {
  3501. return false;
  3502. }
  3503. value = value.replace(/\./g, '');
  3504. if (parseInt(value, 10) === 0) {
  3505. return false;
  3506. }
  3507. var sum = 0,
  3508. length = value.length;
  3509. for (var i = 0; i < length - 1; i++) {
  3510. sum += (9 - i) * parseInt(value.charAt(i), 10);
  3511. }
  3512. sum = sum % 11;
  3513. if (sum === 10) {
  3514. sum = 0;
  3515. }
  3516. return (sum + '' === value.charAt(length - 1));
  3517. },
  3518. /**
  3519. * Validate Romanian numerical personal code (CNP)
  3520. * Examples:
  3521. * - Valid: 1630615123457, 1800101221144
  3522. * - Invalid: 8800101221144, 1632215123457, 1630615123458
  3523. *
  3524. * @see http://en.wikipedia.org/wiki/National_identification_number#Romania
  3525. * @param {String} value The ID
  3526. * @returns {Boolean}
  3527. */
  3528. _ro: function(value) {
  3529. if (!/^[0-9]{13}$/.test(value)) {
  3530. return false;
  3531. }
  3532. var gender = parseInt(value.charAt(0), 10);
  3533. if (gender === 0 || gender === 7 || gender === 8) {
  3534. return false;
  3535. }
  3536. // Determine the date of birth
  3537. var year = parseInt(value.substr(1, 2), 10),
  3538. month = parseInt(value.substr(3, 2), 10),
  3539. day = parseInt(value.substr(5, 2), 10),
  3540. // The year of date is determined base on the gender
  3541. centuries = {
  3542. '1': 1900, // Male born between 1900 and 1999
  3543. '2': 1900, // Female born between 1900 and 1999
  3544. '3': 1800, // Male born between 1800 and 1899
  3545. '4': 1800, // Female born between 1800 and 1899
  3546. '5': 2000, // Male born after 2000
  3547. '6': 2000 // Female born after 2000
  3548. };
  3549. if (day > 31 && month > 12) {
  3550. return false;
  3551. }
  3552. if (gender !== 9) {
  3553. year = centuries[gender + ''] + year;
  3554. if (!$.fn.bootstrapValidator.helpers.date(year, month, day)) {
  3555. return false;
  3556. }
  3557. }
  3558. // Validate the check digit
  3559. var sum = 0,
  3560. weight = [2, 7, 9, 1, 4, 6, 3, 5, 8, 2, 7, 9],
  3561. length = value.length;
  3562. for (var i = 0; i < length - 1; i++) {
  3563. sum += parseInt(value.charAt(i), 10) * weight[i];
  3564. }
  3565. sum = sum % 11;
  3566. if (sum === 10) {
  3567. sum = 1;
  3568. }
  3569. return (sum + '' === value.charAt(length - 1));
  3570. },
  3571. /**
  3572. * Validate Swedish personal identity number (personnummer)
  3573. * Examples:
  3574. * - Valid: 8112289874, 811228-9874, 811228+9874
  3575. * - Invalid: 811228-9873
  3576. *
  3577. * @see http://en.wikipedia.org/wiki/Personal_identity_number_(Sweden)
  3578. * @param {String} value The ID
  3579. * @returns {Boolean}
  3580. */
  3581. _se: function(value) {
  3582. if (!/^[0-9]{10}$/.test(value) && !/^[0-9]{6}[-|+][0-9]{4}$/.test(value)) {
  3583. return false;
  3584. }
  3585. value = value.replace(/[^0-9]/g, '');
  3586. var year = parseInt(value.substr(0, 2), 10) + 1900,
  3587. month = parseInt(value.substr(2, 2), 10),
  3588. day = parseInt(value.substr(4, 2), 10);
  3589. if (!$.fn.bootstrapValidator.helpers.date(year, month, day)) {
  3590. return false;
  3591. }
  3592. // Validate the last check digit
  3593. return $.fn.bootstrapValidator.helpers.luhn(value);
  3594. },
  3595. /**
  3596. * Validate Slovak national identifier number (RC)
  3597. * Examples:
  3598. * - Valid: 7103192745, 991231123
  3599. * - Invalid: 7103192746, 1103492745
  3600. *
  3601. * @param {String} value The ID
  3602. * @returns {Boolean}
  3603. */
  3604. _sk: function(value) {
  3605. // Slovakia uses the same format as Czech Republic
  3606. return this._cz(value);
  3607. },
  3608. /**
  3609. * Validate San Marino citizen number
  3610. *
  3611. * @see http://en.wikipedia.org/wiki/National_identification_number#San_Marino
  3612. * @param {String} value The ID
  3613. * @returns {Boolean}
  3614. */
  3615. _sm: function(value) {
  3616. return /^\d{5}$/.test(value);
  3617. },
  3618. /**
  3619. * Validate Thailand citizen number
  3620. * Examples:
  3621. * - Valid: 7145620509547, 3688699975685, 2368719339716
  3622. * - Invalid: 1100800092310
  3623. *
  3624. * @see http://en.wikipedia.org/wiki/National_identification_number#Thailand
  3625. * @param {String} value The ID
  3626. * @returns {Boolean}
  3627. */
  3628. _th: function(value) {
  3629. if (value.length !== 13) {
  3630. return false;
  3631. }
  3632. var sum = 0;
  3633. for (var i = 0; i < 12; i++) {
  3634. sum += parseInt(value.charAt(i), 10) * (13 - i);
  3635. }
  3636. return (11 - sum % 11) % 10 === parseInt(value.charAt(12), 10);
  3637. },
  3638. /**
  3639. * Validate South African ID
  3640. * Example:
  3641. * - Valid: 8001015009087
  3642. * - Invalid: 8001015009287, 8001015009086
  3643. *
  3644. * @see http://en.wikipedia.org/wiki/National_identification_number#South_Africa
  3645. * @param {String} value The ID
  3646. * @returns {Boolean}
  3647. */
  3648. _za: function(value) {
  3649. if (!/^[0-9]{10}[0|1][8|9][0-9]$/.test(value)) {
  3650. return false;
  3651. }
  3652. var year = parseInt(value.substr(0, 2), 10),
  3653. currentYear = new Date().getFullYear() % 100,
  3654. month = parseInt(value.substr(2, 2), 10),
  3655. day = parseInt(value.substr(4, 2), 10);
  3656. year = (year >= currentYear) ? (year + 1900) : (year + 2000);
  3657. if (!$.fn.bootstrapValidator.helpers.date(year, month, day)) {
  3658. return false;
  3659. }
  3660. // Validate the last check digit
  3661. return $.fn.bootstrapValidator.helpers.luhn(value);
  3662. }
  3663. };
  3664. }(window.jQuery));
  3665. ;(function($) {
  3666. $.fn.bootstrapValidator.i18n.identical = $.extend($.fn.bootstrapValidator.i18n.identical || {}, {
  3667. 'default': 'Please enter the same value'
  3668. });
  3669. $.fn.bootstrapValidator.validators.identical = {
  3670. html5Attributes: {
  3671. message: 'message',
  3672. field: 'field'
  3673. },
  3674. /**
  3675. * Check if input value equals to value of particular one
  3676. *
  3677. * @param {BootstrapValidator} validator The validator plugin instance
  3678. * @param {jQuery} $field Field element
  3679. * @param {Object} options Consists of the following key:
  3680. * - field: The name of field that will be used to compare with current one
  3681. * @returns {Boolean}
  3682. */
  3683. validate: function(validator, $field, options) {
  3684. var value = $field.val();
  3685. if (value === '') {
  3686. return true;
  3687. }
  3688. var compareWith = validator.getFieldElements(options.field);
  3689. if (compareWith === null || compareWith.length === 0) {
  3690. return true;
  3691. }
  3692. if (value === compareWith.val()) {
  3693. validator.updateStatus(options.field, validator.STATUS_VALID, 'identical');
  3694. return true;
  3695. } else {
  3696. return false;
  3697. }
  3698. }
  3699. };
  3700. }(window.jQuery));
  3701. ;(function($) {
  3702. $.fn.bootstrapValidator.i18n.imei = $.extend($.fn.bootstrapValidator.i18n.imei || {}, {
  3703. 'default': 'Please enter a valid IMEI number'
  3704. });
  3705. $.fn.bootstrapValidator.validators.imei = {
  3706. /**
  3707. * Validate IMEI (International Mobile Station Equipment Identity)
  3708. * Examples:
  3709. * - Valid: 35-209900-176148-1, 35-209900-176148-23, 3568680000414120, 490154203237518
  3710. * - Invalid: 490154203237517
  3711. *
  3712. * @see http://en.wikipedia.org/wiki/International_Mobile_Station_Equipment_Identity
  3713. * @param {BootstrapValidator} validator The validator plugin instance
  3714. * @param {jQuery} $field Field element
  3715. * @param {Object} options Can consist of the following keys:
  3716. * - message: The invalid message
  3717. * @returns {Boolean}
  3718. */
  3719. validate: function(validator, $field, options) {
  3720. var value = $field.val();
  3721. if (value === '') {
  3722. return true;
  3723. }
  3724. switch (true) {
  3725. case /^\d{15}$/.test(value):
  3726. case /^\d{2}-\d{6}-\d{6}-\d{1}$/.test(value):
  3727. case /^\d{2}\s\d{6}\s\d{6}\s\d{1}$/.test(value):
  3728. value = value.replace(/[^0-9]/g, '');
  3729. return $.fn.bootstrapValidator.helpers.luhn(value);
  3730. case /^\d{14}$/.test(value):
  3731. case /^\d{16}$/.test(value):
  3732. case /^\d{2}-\d{6}-\d{6}(|-\d{2})$/.test(value):
  3733. case /^\d{2}\s\d{6}\s\d{6}(|\s\d{2})$/.test(value):
  3734. return true;
  3735. default:
  3736. return false;
  3737. }
  3738. }
  3739. };
  3740. }(window.jQuery));
  3741. ;(function($) {
  3742. $.fn.bootstrapValidator.i18n.imo = $.extend($.fn.bootstrapValidator.i18n.imo || {}, {
  3743. 'default': 'Please enter a valid IMO number'
  3744. });
  3745. $.fn.bootstrapValidator.validators.imo = {
  3746. /**
  3747. * Validate IMO (International Maritime Organization)
  3748. * Examples:
  3749. * - Valid: IMO 8814275, IMO 9176187
  3750. * - Invalid: IMO 8814274
  3751. *
  3752. * @see http://en.wikipedia.org/wiki/IMO_Number
  3753. * @param {BootstrapValidator} validator The validator plugin instance
  3754. * @param {jQuery} $field Field element
  3755. * @param {Object} options Can consist of the following keys:
  3756. * - message: The invalid message
  3757. * @returns {Boolean}
  3758. */
  3759. validate: function(validator, $field, options) {
  3760. var value = $field.val();
  3761. if (value === '') {
  3762. return true;
  3763. }
  3764. if (!/^IMO \d{7}$/i.test(value)) {
  3765. return false;
  3766. }
  3767. // Grab just the digits
  3768. var sum = 0,
  3769. digits = value.replace(/^.*(\d{7})$/, '$1');
  3770. // Go over each char, multiplying by the inverse of it's position
  3771. // IMO 9176187
  3772. // (9 * 7) + (1 * 6) + (7 * 5) + (6 * 4) + (1 * 3) + (8 * 2) = 147
  3773. // Take the last digit of that, that's the check digit (7)
  3774. for (var i = 6; i >= 1; i--) {
  3775. sum += (digits.slice((6 - i), -i) * (i + 1));
  3776. }
  3777. return sum % 10 === parseInt(digits.charAt(6), 10);
  3778. }
  3779. };
  3780. }(window.jQuery));
  3781. ;(function($) {
  3782. $.fn.bootstrapValidator.i18n.integer = $.extend($.fn.bootstrapValidator.i18n.integer || {}, {
  3783. 'default': 'Please enter a valid number'
  3784. });
  3785. $.fn.bootstrapValidator.validators.integer = {
  3786. enableByHtml5: function($field) {
  3787. return ('number' === $field.attr('type')) && ($field.attr('step') === undefined || $field.attr('step') % 1 === 0);
  3788. },
  3789. /**
  3790. * Return true if the input value is an integer
  3791. *
  3792. * @param {BootstrapValidator} validator The validator plugin instance
  3793. * @param {jQuery} $field Field element
  3794. * @param {Object} options Can consist of the following key:
  3795. * - message: The invalid message
  3796. * @returns {Boolean}
  3797. */
  3798. validate: function(validator, $field, options) {
  3799. if (this.enableByHtml5($field) && $field.get(0).validity && $field.get(0).validity.badInput === true) {
  3800. return false;
  3801. }
  3802. var value = $field.val();
  3803. if (value === '') {
  3804. return true;
  3805. }
  3806. return /^(?:-?(?:0|[1-9][0-9]*))$/.test(value);
  3807. }
  3808. };
  3809. }(window.jQuery));
  3810. ;(function($) {
  3811. $.fn.bootstrapValidator.i18n.ip = $.extend($.fn.bootstrapValidator.i18n.ip || {}, {
  3812. 'default': 'Please enter a valid IP address',
  3813. ipv4: 'Please enter a valid IPv4 address',
  3814. ipv6: 'Please enter a valid IPv6 address'
  3815. });
  3816. $.fn.bootstrapValidator.validators.ip = {
  3817. html5Attributes: {
  3818. message: 'message',
  3819. ipv4: 'ipv4',
  3820. ipv6: 'ipv6'
  3821. },
  3822. /**
  3823. * Return true if the input value is a IP address.
  3824. *
  3825. * @param {BootstrapValidator} validator The validator plugin instance
  3826. * @param {jQuery} $field Field element
  3827. * @param {Object} options Can consist of the following keys:
  3828. * - ipv4: Enable IPv4 validator, default to true
  3829. * - ipv6: Enable IPv6 validator, default to true
  3830. * - message: The invalid message
  3831. * @returns {Boolean|Object}
  3832. */
  3833. validate: function(validator, $field, options) {
  3834. var value = $field.val();
  3835. if (value === '') {
  3836. return true;
  3837. }
  3838. options = $.extend({}, { ipv4: true, ipv6: true }, options);
  3839. var ipv4Regex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/,
  3840. ipv6Regex = /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/,
  3841. valid = false,
  3842. message;
  3843. switch (true) {
  3844. case (options.ipv4 && !options.ipv6):
  3845. valid = ipv4Regex.test(value);
  3846. message = options.message || $.fn.bootstrapValidator.i18n.ip.ipv4;
  3847. break;
  3848. case (!options.ipv4 && options.ipv6):
  3849. valid = ipv6Regex.test(value);
  3850. message = options.message || $.fn.bootstrapValidator.i18n.ip.ipv6;
  3851. break;
  3852. case (options.ipv4 && options.ipv6):
  3853. /* falls through */
  3854. default:
  3855. valid = ipv4Regex.test(value) && ipv6Regex.test(value);
  3856. message = options.message || $.fn.bootstrapValidator.i18n.ip['default'];
  3857. break;
  3858. }
  3859. return {
  3860. valid: valid,
  3861. message: message
  3862. };
  3863. }
  3864. };
  3865. }(window.jQuery));;(function($) {
  3866. $.fn.bootstrapValidator.i18n.isbn = $.extend($.fn.bootstrapValidator.i18n.isbn || {}, {
  3867. 'default': 'Please enter a valid ISBN number'
  3868. });
  3869. $.fn.bootstrapValidator.validators.isbn = {
  3870. /**
  3871. * Return true if the input value is a valid ISBN 10 or ISBN 13 number
  3872. * Examples:
  3873. * - Valid:
  3874. * ISBN 10: 99921-58-10-7, 9971-5-0210-0, 960-425-059-0, 80-902734-1-6, 85-359-0277-5, 1-84356-028-3, 0-684-84328-5, 0-8044-2957-X, 0-85131-041-9, 0-943396-04-2, 0-9752298-0-X
  3875. * ISBN 13: 978-0-306-40615-7
  3876. * - Invalid:
  3877. * ISBN 10: 99921-58-10-6
  3878. * ISBN 13: 978-0-306-40615-6
  3879. *
  3880. * @see http://en.wikipedia.org/wiki/International_Standard_Book_Number
  3881. * @param {BootstrapValidator} validator The validator plugin instance
  3882. * @param {jQuery} $field Field element
  3883. * @param {Object} [options] Can consist of the following keys:
  3884. * - message: The invalid message
  3885. * @returns {Boolean}
  3886. */
  3887. validate: function(validator, $field, options) {
  3888. var value = $field.val();
  3889. if (value === '') {
  3890. return true;
  3891. }
  3892. // http://en.wikipedia.org/wiki/International_Standard_Book_Number#Overview
  3893. // Groups are separated by a hyphen or a space
  3894. var type;
  3895. switch (true) {
  3896. case /^\d{9}[\dX]$/.test(value):
  3897. case (value.length === 13 && /^(\d+)-(\d+)-(\d+)-([\dX])$/.test(value)):
  3898. case (value.length === 13 && /^(\d+)\s(\d+)\s(\d+)\s([\dX])$/.test(value)):
  3899. type = 'ISBN10';
  3900. break;
  3901. case /^(978|979)\d{9}[\dX]$/.test(value):
  3902. case (value.length === 17 && /^(978|979)-(\d+)-(\d+)-(\d+)-([\dX])$/.test(value)):
  3903. case (value.length === 17 && /^(978|979)\s(\d+)\s(\d+)\s(\d+)\s([\dX])$/.test(value)):
  3904. type = 'ISBN13';
  3905. break;
  3906. default:
  3907. return false;
  3908. }
  3909. // Replace all special characters except digits and X
  3910. value = value.replace(/[^0-9X]/gi, '');
  3911. var chars = value.split(''),
  3912. length = chars.length,
  3913. sum = 0,
  3914. i,
  3915. checksum;
  3916. switch (type) {
  3917. case 'ISBN10':
  3918. sum = 0;
  3919. for (i = 0; i < length - 1; i++) {
  3920. sum += parseInt(chars[i], 10) * (10 - i);
  3921. }
  3922. checksum = 11 - (sum % 11);
  3923. if (checksum === 11) {
  3924. checksum = 0;
  3925. } else if (checksum === 10) {
  3926. checksum = 'X';
  3927. }
  3928. return (checksum + '' === chars[length - 1]);
  3929. case 'ISBN13':
  3930. sum = 0;
  3931. for (i = 0; i < length - 1; i++) {
  3932. sum += ((i % 2 === 0) ? parseInt(chars[i], 10) : (parseInt(chars[i], 10) * 3));
  3933. }
  3934. checksum = 10 - (sum % 10);
  3935. if (checksum === 10) {
  3936. checksum = '0';
  3937. }
  3938. return (checksum + '' === chars[length - 1]);
  3939. default:
  3940. return false;
  3941. }
  3942. }
  3943. };
  3944. }(window.jQuery));
  3945. ;(function($) {
  3946. $.fn.bootstrapValidator.i18n.isin = $.extend($.fn.bootstrapValidator.i18n.isin || {}, {
  3947. 'default': 'Please enter a valid ISIN number'
  3948. });
  3949. $.fn.bootstrapValidator.validators.isin = {
  3950. // Available country codes
  3951. // See http://isin.net/country-codes/
  3952. COUNTRY_CODES: 'AF|AX|AL|DZ|AS|AD|AO|AI|AQ|AG|AR|AM|AW|AU|AT|AZ|BS|BH|BD|BB|BY|BE|BZ|BJ|BM|BT|BO|BQ|BA|BW|BV|BR|IO|BN|BG|BF|BI|KH|CM|CA|CV|KY|CF|TD|CL|CN|CX|CC|CO|KM|CG|CD|CK|CR|CI|HR|CU|CW|CY|CZ|DK|DJ|DM|DO|EC|EG|SV|GQ|ER|EE|ET|FK|FO|FJ|FI|FR|GF|PF|TF|GA|GM|GE|DE|GH|GI|GR|GL|GD|GP|GU|GT|GG|GN|GW|GY|HT|HM|VA|HN|HK|HU|IS|IN|ID|IR|IQ|IE|IM|IL|IT|JM|JP|JE|JO|KZ|KE|KI|KP|KR|KW|KG|LA|LV|LB|LS|LR|LY|LI|LT|LU|MO|MK|MG|MW|MY|MV|ML|MT|MH|MQ|MR|MU|YT|MX|FM|MD|MC|MN|ME|MS|MA|MZ|MM|NA|NR|NP|NL|NC|NZ|NI|NE|NG|NU|NF|MP|NO|OM|PK|PW|PS|PA|PG|PY|PE|PH|PN|PL|PT|PR|QA|RE|RO|RU|RW|BL|SH|KN|LC|MF|PM|VC|WS|SM|ST|SA|SN|RS|SC|SL|SG|SX|SK|SI|SB|SO|ZA|GS|SS|ES|LK|SD|SR|SJ|SZ|SE|CH|SY|TW|TJ|TZ|TH|TL|TG|TK|TO|TT|TN|TR|TM|TC|TV|UG|UA|AE|GB|US|UM|UY|UZ|VU|VE|VN|VG|VI|WF|EH|YE|ZM|ZW',
  3953. /**
  3954. * Validate an ISIN (International Securities Identification Number)
  3955. * Examples:
  3956. * - Valid: US0378331005, AU0000XVGZA3, GB0002634946
  3957. * - Invalid: US0378331004, AA0000XVGZA3
  3958. *
  3959. * @see http://en.wikipedia.org/wiki/International_Securities_Identifying_Number
  3960. * @param {BootstrapValidator} validator The validator plugin instance
  3961. * @param {jQuery} $field Field element
  3962. * @param {Object} options Can consist of the following keys:
  3963. * - message: The invalid message
  3964. * @returns {Boolean}
  3965. */
  3966. validate: function(validator, $field, options) {
  3967. var value = $field.val();
  3968. if (value === '') {
  3969. return true;
  3970. }
  3971. value = value.toUpperCase();
  3972. var regex = new RegExp('^(' + this.COUNTRY_CODES + ')[0-9A-Z]{10}$');
  3973. if (!regex.test(value)) {
  3974. return false;
  3975. }
  3976. var converted = '',
  3977. length = value.length;
  3978. // Convert letters to number
  3979. for (var i = 0; i < length - 1; i++) {
  3980. var c = value.charCodeAt(i);
  3981. converted += ((c > 57) ? (c - 55).toString() : value.charAt(i));
  3982. }
  3983. var digits = '',
  3984. n = converted.length,
  3985. group = (n % 2 !== 0) ? 0 : 1;
  3986. for (i = 0; i < n; i++) {
  3987. digits += (parseInt(converted[i], 10) * ((i % 2) === group ? 2 : 1) + '');
  3988. }
  3989. var sum = 0;
  3990. for (i = 0; i < digits.length; i++) {
  3991. sum += parseInt(digits.charAt(i), 10);
  3992. }
  3993. sum = (10 - (sum % 10)) % 10;
  3994. return sum + '' === value.charAt(length - 1);
  3995. }
  3996. };
  3997. }(window.jQuery));
  3998. ;(function($) {
  3999. $.fn.bootstrapValidator.i18n.ismn = $.extend($.fn.bootstrapValidator.i18n.ismn || {}, {
  4000. 'default': 'Please enter a valid ISMN number'
  4001. });
  4002. $.fn.bootstrapValidator.validators.ismn = {
  4003. /**
  4004. * Validate ISMN (International Standard Music Number)
  4005. * Examples:
  4006. * - Valid: M230671187, 979-0-0601-1561-5, 979 0 3452 4680 5, 9790060115615
  4007. * - Invalid: 9790060115614
  4008. *
  4009. * @see http://en.wikipedia.org/wiki/International_Standard_Music_Number
  4010. * @param {BootstrapValidator} validator The validator plugin instance
  4011. * @param {jQuery} $field Field element
  4012. * @param {Object} options Can consist of the following keys:
  4013. * - message: The invalid message
  4014. * @returns {Boolean}
  4015. */
  4016. validate: function(validator, $field, options) {
  4017. var value = $field.val();
  4018. if (value === '') {
  4019. return true;
  4020. }
  4021. // Groups are separated by a hyphen or a space
  4022. var type;
  4023. switch (true) {
  4024. case /^M\d{9}$/.test(value):
  4025. case /^M-\d{4}-\d{4}-\d{1}$/.test(value):
  4026. case /^M\s\d{4}\s\d{4}\s\d{1}$/.test(value):
  4027. type = 'ISMN10';
  4028. break;
  4029. case /^9790\d{9}$/.test(value):
  4030. case /^979-0-\d{4}-\d{4}-\d{1}$/.test(value):
  4031. case /^979\s0\s\d{4}\s\d{4}\s\d{1}$/.test(value):
  4032. type = 'ISMN13';
  4033. break;
  4034. default:
  4035. return false;
  4036. }
  4037. if ('ISMN10' === type) {
  4038. value = '9790' + value.substr(1);
  4039. }
  4040. // Replace all special characters except digits
  4041. value = value.replace(/[^0-9]/gi, '');
  4042. var length = value.length,
  4043. sum = 0,
  4044. weight = [1, 3];
  4045. for (var i = 0; i < length - 1; i++) {
  4046. sum += parseInt(value.charAt(i), 10) * weight[i % 2];
  4047. }
  4048. sum = 10 - sum % 10;
  4049. return (sum + '' === value.charAt(length - 1));
  4050. }
  4051. };
  4052. }(window.jQuery));
  4053. ;(function($) {
  4054. $.fn.bootstrapValidator.i18n.issn = $.extend($.fn.bootstrapValidator.i18n.issn || {}, {
  4055. 'default': 'Please enter a valid ISSN number'
  4056. });
  4057. $.fn.bootstrapValidator.validators.issn = {
  4058. /**
  4059. * Validate ISSN (International Standard Serial Number)
  4060. * Examples:
  4061. * - Valid: 0378-5955, 0024-9319, 0032-1478
  4062. * - Invalid: 0032-147X
  4063. *
  4064. * @see http://en.wikipedia.org/wiki/International_Standard_Serial_Number
  4065. * @param {BootstrapValidator} validator The validator plugin instance
  4066. * @param {jQuery} $field Field element
  4067. * @param {Object} options Can consist of the following keys:
  4068. * - message: The invalid message
  4069. * @returns {Boolean}
  4070. */
  4071. validate: function(validator, $field, options) {
  4072. var value = $field.val();
  4073. if (value === '') {
  4074. return true;
  4075. }
  4076. // Groups are separated by a hyphen or a space
  4077. if (!/^\d{4}\-\d{3}[\dX]$/.test(value)) {
  4078. return false;
  4079. }
  4080. // Replace all special characters except digits and X
  4081. value = value.replace(/[^0-9X]/gi, '');
  4082. var chars = value.split(''),
  4083. length = chars.length,
  4084. sum = 0;
  4085. if (chars[7] === 'X') {
  4086. chars[7] = 10;
  4087. }
  4088. for (var i = 0; i < length; i++) {
  4089. sum += parseInt(chars[i], 10) * (8 - i);
  4090. }
  4091. return (sum % 11 === 0);
  4092. }
  4093. };
  4094. }(window.jQuery));
  4095. ;(function($) {
  4096. $.fn.bootstrapValidator.i18n.lessThan = $.extend($.fn.bootstrapValidator.i18n.lessThan || {}, {
  4097. 'default': 'Please enter a value less than or equal to %s',
  4098. notInclusive: 'Please enter a value less than %s'
  4099. });
  4100. $.fn.bootstrapValidator.validators.lessThan = {
  4101. html5Attributes: {
  4102. message: 'message',
  4103. value: 'value',
  4104. inclusive: 'inclusive'
  4105. },
  4106. enableByHtml5: function($field) {
  4107. var type = $field.attr('type'),
  4108. max = $field.attr('max');
  4109. if (max && type !== 'date') {
  4110. return {
  4111. value: max
  4112. };
  4113. }
  4114. return false;
  4115. },
  4116. /**
  4117. * Return true if the input value is less than or equal to given number
  4118. *
  4119. * @param {BootstrapValidator} validator The validator plugin instance
  4120. * @param {jQuery} $field Field element
  4121. * @param {Object} options Can consist of the following keys:
  4122. * - value: The number used to compare to. It can be
  4123. * - A number
  4124. * - Name of field which its value defines the number
  4125. * - Name of callback function that returns the number
  4126. * - A callback function that returns the number
  4127. *
  4128. * - inclusive [optional]: Can be true or false. Default is true
  4129. * - message: The invalid message
  4130. * @returns {Boolean|Object}
  4131. */
  4132. validate: function(validator, $field, options) {
  4133. var value = $field.val();
  4134. if (value === '') {
  4135. return true;
  4136. }
  4137. if (!$.isNumeric(value)) {
  4138. return false;
  4139. }
  4140. var compareTo = $.isNumeric(options.value) ? options.value : validator.getDynamicOption($field, options.value);
  4141. value = parseFloat(value);
  4142. return (options.inclusive === true || options.inclusive === undefined)
  4143. ? {
  4144. valid: value <= compareTo,
  4145. message: $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.lessThan['default'], compareTo)
  4146. }
  4147. : {
  4148. valid: value < compareTo,
  4149. message: $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.lessThan.notInclusive, compareTo)
  4150. };
  4151. }
  4152. };
  4153. }(window.jQuery));
  4154. ;(function($) {
  4155. $.fn.bootstrapValidator.i18n.mac = $.extend($.fn.bootstrapValidator.i18n.mac || {}, {
  4156. 'default': 'Please enter a valid MAC address'
  4157. });
  4158. $.fn.bootstrapValidator.validators.mac = {
  4159. /**
  4160. * Return true if the input value is a MAC address.
  4161. *
  4162. * @param {BootstrapValidator} validator The validator plugin instance
  4163. * @param {jQuery} $field Field element
  4164. * @param {Object} options Can consist of the following keys:
  4165. * - message: The invalid message
  4166. * @returns {Boolean}
  4167. */
  4168. validate: function(validator, $field, options) {
  4169. var value = $field.val();
  4170. if (value === '') {
  4171. return true;
  4172. }
  4173. return /^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$/.test(value);
  4174. }
  4175. };
  4176. }(window.jQuery));
  4177. ;(function($) {
  4178. $.fn.bootstrapValidator.i18n.meid = $.extend($.fn.bootstrapValidator.i18n.meid || {}, {
  4179. 'default': 'Please enter a valid MEID number'
  4180. });
  4181. $.fn.bootstrapValidator.validators.meid = {
  4182. /**
  4183. * Validate MEID (Mobile Equipment Identifier)
  4184. * Examples:
  4185. * - Valid: 293608736500703710, 29360-87365-0070-3710, AF0123450ABCDE, AF-012345-0ABCDE
  4186. * - Invalid: 2936087365007037101
  4187. *
  4188. * @see http://en.wikipedia.org/wiki/Mobile_equipment_identifier
  4189. * @param {BootstrapValidator} validator The validator plugin instance
  4190. * @param {jQuery} $field Field element
  4191. * @param {Object} options Can consist of the following keys:
  4192. * - message: The invalid message
  4193. * @returns {Boolean}
  4194. */
  4195. validate: function(validator, $field, options) {
  4196. var value = $field.val();
  4197. if (value === '') {
  4198. return true;
  4199. }
  4200. switch (true) {
  4201. // 14 digit hex representation (no check digit)
  4202. case /^[0-9A-F]{15}$/i.test(value):
  4203. // 14 digit hex representation + dashes or spaces (no check digit)
  4204. case /^[0-9A-F]{2}[- ][0-9A-F]{6}[- ][0-9A-F]{6}[- ][0-9A-F]$/i.test(value):
  4205. // 18 digit decimal representation (no check digit)
  4206. case /^\d{19}$/.test(value):
  4207. // 18 digit decimal representation + dashes or spaces (no check digit)
  4208. case /^\d{5}[- ]\d{5}[- ]\d{4}[- ]\d{4}[- ]\d$/.test(value):
  4209. // Grab the check digit
  4210. var cd = value.charAt(value.length - 1);
  4211. // Strip any non-hex chars
  4212. value = value.replace(/[- ]/g, '');
  4213. // If it's all digits, luhn base 10 is used
  4214. if (value.match(/^\d*$/i)) {
  4215. return $.fn.bootstrapValidator.helpers.luhn(value);
  4216. }
  4217. // Strip the check digit
  4218. value = value.slice(0, -1);
  4219. // Get every other char, and double it
  4220. var cdCalc = '';
  4221. for (var i = 1; i <= 13; i += 2) {
  4222. cdCalc += (parseInt(value.charAt(i), 16) * 2).toString(16);
  4223. }
  4224. // Get the sum of each char in the string
  4225. var sum = 0;
  4226. for (i = 0; i < cdCalc.length; i++) {
  4227. sum += parseInt(cdCalc.charAt(i), 16);
  4228. }
  4229. // If the last digit of the calc is 0, the check digit is 0
  4230. return (sum % 10 === 0)
  4231. ? (cd === '0')
  4232. // Subtract it from the next highest 10s number (64 goes to 70) and subtract the sum
  4233. // Double it and turn it into a hex char
  4234. : (cd === ((Math.floor((sum + 10) / 10) * 10 - sum) * 2).toString(16));
  4235. // 14 digit hex representation (no check digit)
  4236. case /^[0-9A-F]{14}$/i.test(value):
  4237. // 14 digit hex representation + dashes or spaces (no check digit)
  4238. case /^[0-9A-F]{2}[- ][0-9A-F]{6}[- ][0-9A-F]{6}$/i.test(value):
  4239. // 18 digit decimal representation (no check digit)
  4240. case /^\d{18}$/.test(value):
  4241. // 18 digit decimal representation + dashes or spaces (no check digit)
  4242. case /^\d{5}[- ]\d{5}[- ]\d{4}[- ]\d{4}$/.test(value):
  4243. return true;
  4244. default:
  4245. return false;
  4246. }
  4247. }
  4248. };
  4249. }(window.jQuery));
  4250. ;(function($) {
  4251. $.fn.bootstrapValidator.i18n.notEmpty = $.extend($.fn.bootstrapValidator.i18n.notEmpty || {}, {
  4252. 'default': 'Please enter a value'
  4253. });
  4254. $.fn.bootstrapValidator.validators.notEmpty = {
  4255. enableByHtml5: function($field) {
  4256. var required = $field.attr('required') + '';
  4257. return ('required' === required || 'true' === required);
  4258. },
  4259. /**
  4260. * Check if input value is empty or not
  4261. *
  4262. * @param {BootstrapValidator} validator The validator plugin instance
  4263. * @param {jQuery} $field Field element
  4264. * @param {Object} options
  4265. * @returns {Boolean}
  4266. */
  4267. validate: function(validator, $field, options) {
  4268. var type = $field.attr('type');
  4269. if ('radio' === type || 'checkbox' === type) {
  4270. return validator
  4271. .getFieldElements($field.attr('data-bv-field'))
  4272. .filter(':checked')
  4273. .length > 0;
  4274. }
  4275. if ('number' === type && $field.get(0).validity && $field.get(0).validity.badInput === true) {
  4276. return true;
  4277. }
  4278. return $.trim($field.val()) !== '';
  4279. }
  4280. };
  4281. }(window.jQuery));
  4282. ;(function($) {
  4283. $.fn.bootstrapValidator.i18n.numeric = $.extend($.fn.bootstrapValidator.i18n.numeric || {}, {
  4284. 'default': 'Please enter a valid float number'
  4285. });
  4286. $.fn.bootstrapValidator.validators.numeric = {
  4287. html5Attributes: {
  4288. message: 'message',
  4289. separator: 'separator'
  4290. },
  4291. enableByHtml5: function($field) {
  4292. return ('number' === $field.attr('type')) && ($field.attr('step') !== undefined) && ($field.attr('step') % 1 !== 0);
  4293. },
  4294. /**
  4295. * Validate decimal number
  4296. *
  4297. * @param {BootstrapValidator} validator The validator plugin instance
  4298. * @param {jQuery} $field Field element
  4299. * @param {Object} options Consist of key:
  4300. * - message: The invalid message
  4301. * - separator: The decimal separator. Can be "." (default), ","
  4302. * @returns {Boolean}
  4303. */
  4304. validate: function(validator, $field, options) {
  4305. if (this.enableByHtml5($field) && $field.get(0).validity && $field.get(0).validity.badInput === true) {
  4306. return false;
  4307. }
  4308. var value = $field.val();
  4309. if (value === '') {
  4310. return true;
  4311. }
  4312. var separator = options.separator || '.';
  4313. if (separator !== '.') {
  4314. value = value.replace(separator, '.');
  4315. }
  4316. return !isNaN(parseFloat(value)) && isFinite(value);
  4317. }
  4318. };
  4319. }(window.jQuery));
  4320. ;(function($) {
  4321. $.fn.bootstrapValidator.i18n.phone = $.extend($.fn.bootstrapValidator.i18n.phone || {}, {
  4322. 'default': 'Please enter a valid phone number',
  4323. countryNotSupported: 'The country code %s is not supported',
  4324. country: 'Please enter a valid phone number in %s',
  4325. countries: {
  4326. BR: 'Brazil',
  4327. CN: 'China',
  4328. DK: 'Denmark',
  4329. ES: 'Spain',
  4330. FR: 'France',
  4331. GB: 'United Kingdom',
  4332. MA: 'Morocco',
  4333. PK: 'Pakistan',
  4334. RO: 'Romania',
  4335. TH: 'Thailand',
  4336. US: 'USA'
  4337. }
  4338. });
  4339. $.fn.bootstrapValidator.validators.phone = {
  4340. html5Attributes: {
  4341. message: 'message',
  4342. country: 'country'
  4343. },
  4344. // The supported countries
  4345. COUNTRY_CODES: ['BR', 'CN', 'DK', 'ES', 'FR', 'GB', 'MA', 'PK', 'RO', 'TH', 'US'],
  4346. /**
  4347. * Return true if the input value contains a valid phone number for the country
  4348. * selected in the options
  4349. *
  4350. * @param {BootstrapValidator} validator Validate plugin instance
  4351. * @param {jQuery} $field Field element
  4352. * @param {Object} options Consist of key:
  4353. * - message: The invalid message
  4354. * - country: The ISO-3166 country code. It can be
  4355. * - A country code
  4356. * - Name of field which its value defines the country code
  4357. * - Name of callback function that returns the country code
  4358. * - A callback function that returns the country code
  4359. *
  4360. * @returns {Boolean|Object}
  4361. */
  4362. validate: function(validator, $field, options) {
  4363. var value = $field.val();
  4364. if (value === '') {
  4365. return true;
  4366. }
  4367. var country = options.country;
  4368. if (typeof country !== 'string' || $.inArray(country, this.COUNTRY_CODES) === -1) {
  4369. // Try to determine the country
  4370. country = validator.getDynamicOption($field, country);
  4371. }
  4372. if (!country || $.inArray(country.toUpperCase(), this.COUNTRY_CODES) === -1) {
  4373. return {
  4374. valid: false,
  4375. message: $.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.phone.countryNotSupported, country)
  4376. };
  4377. }
  4378. var isValid = true;
  4379. switch (country.toUpperCase()) {
  4380. case 'BR':
  4381. // Test: http://regexr.com/399m1
  4382. value = $.trim(value);
  4383. isValid = (/^(([\d]{4}[-.\s]{1}[\d]{2,3}[-.\s]{1}[\d]{2}[-.\s]{1}[\d]{2})|([\d]{4}[-.\s]{1}[\d]{3}[-.\s]{1}[\d]{4})|((\(?\+?[0-9]{2}\)?\s?)?(\(?\d{2}\)?\s?)?\d{4,5}[-.\s]?\d{4}))$/).test(value);
  4384. break;
  4385. case 'CN':
  4386. // http://regexr.com/39dq4
  4387. value = $.trim(value);
  4388. isValid = (/^((00|\+)?(86(?:-| )))?((\d{11})|(\d{3}[- ]{1}\d{4}[- ]{1}\d{4})|((\d{2,4}[- ]){1}(\d{7,8}|(\d{3,4}[- ]{1}\d{4}))([- ]{1}\d{1,4})?))$/).test(value);
  4389. break;
  4390. case 'DK':
  4391. // Mathing DK phone numbers with country code in 1 of 3 formats and an
  4392. // 8 digit phone number not starting with a 0 or 1. Can have 1 space
  4393. // between each character except inside the country code.
  4394. // Test: http://regex101.com/r/sS8fO4/1
  4395. value = $.trim(value);
  4396. isValid = (/^(\+45|0045|\(45\))?\s?[2-9](\s?\d){7}$/).test(value);
  4397. break;
  4398. case 'ES':
  4399. // http://regex101.com/r/rB9mA9/1
  4400. value = $.trim(value);
  4401. isValid = (/^(?:(?:(?:\+|00)34\D?))?(?:9|6)(?:\d\D?){8}$/).test(value);
  4402. break;
  4403. case 'FR':
  4404. // http://regexr.com/39a2p
  4405. value = $.trim(value);
  4406. isValid = (/^(?:(?:(?:\+|00)33[ ]?(?:\(0\)[ ]?)?)|0){1}[1-9]{1}([ .-]?)(?:\d{2}\1?){3}\d{2}$/).test(value);
  4407. break;
  4408. case 'GB':
  4409. // http://aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_GB_Telephone_Numbers#Match_GB_telephone_number_in_any_format
  4410. // Test: http://regexr.com/38uhv
  4411. value = $.trim(value);
  4412. isValid = (/^\(?(?:(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?\(?(?:0\)?[\s-]?\(?)?|0)(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}|\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4}|\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3})|\d{5}\)?[\s-]?\d{4,5}|8(?:00[\s-]?11[\s-]?11|45[\s-]?46[\s-]?4\d))(?:(?:[\s-]?(?:x|ext\.?\s?|\#)\d+)?)$/).test(value);
  4413. break;
  4414. case 'MA':
  4415. // http://en.wikipedia.org/wiki/Telephone_numbers_in_Morocco
  4416. // Test: http://regexr.com/399n8
  4417. value = $.trim(value);
  4418. isValid = (/^(?:(?:(?:\+|00)212[\s]?(?:[\s]?\(0\)[\s]?)?)|0){1}(?:5[\s.-]?[2-3]|6[\s.-]?[13-9]){1}[0-9]{1}(?:[\s.-]?\d{2}){3}$/).test(value);
  4419. break;
  4420. case 'PK':
  4421. // http://regex101.com/r/yH8aV9/2
  4422. value = $.trim(value);
  4423. isValid = (/^0?3[0-9]{2}[0-9]{7}$/).test(value);
  4424. break;
  4425. case 'RO':
  4426. // All mobile network and land line
  4427. isValid = (/^(\+4|)?(07[0-8]{1}[0-9]{1}|02[0-9]{2}|03[0-9]{2}){1}?(\s|\.|\-)?([0-9]{3}(\s|\.|\-|)){2}$/g).test(value);
  4428. break;
  4429. case 'TH':
  4430. // http://regex101.com/r/vM5mZ4/2
  4431. isValid = (/^0\(?([6|8-9]{2})*-([0-9]{3})*-([0-9]{4})$/).test(value);
  4432. break;
  4433. case 'US':
  4434. /* falls through */
  4435. default:
  4436. // Make sure US phone numbers have 10 digits
  4437. // May start with 1, +1, or 1-; should discard
  4438. // Area code may be delimited with (), & sections may be delimited with . or -
  4439. // Test: http://regexr.com/38mqi
  4440. value = value.replace(/\D/g, '');
  4441. isValid = (/^(?:(1\-?)|(\+1 ?))?\(?(\d{3})[\)\-\.]?(\d{3})[\-\.]?(\d{4})$/).test(value) && (value.length === 10);
  4442. break;
  4443. }
  4444. return {
  4445. valid: isValid,
  4446. message: $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.phone.country, $.fn.bootstrapValidator.i18n.phone.countries[country])
  4447. };
  4448. }
  4449. };
  4450. }(window.jQuery));
  4451. ;(function($) {
  4452. $.fn.bootstrapValidator.i18n.regexp = $.extend($.fn.bootstrapValidator.i18n.regexp || {}, {
  4453. 'default': 'Please enter a value matching the pattern'
  4454. });
  4455. $.fn.bootstrapValidator.validators.regexp = {
  4456. html5Attributes: {
  4457. message: 'message',
  4458. regexp: 'regexp'
  4459. },
  4460. enableByHtml5: function($field) {
  4461. var pattern = $field.attr('pattern');
  4462. if (pattern) {
  4463. return {
  4464. regexp: pattern
  4465. };
  4466. }
  4467. return false;
  4468. },
  4469. /**
  4470. * Check if the element value matches given regular expression
  4471. *
  4472. * @param {BootstrapValidator} validator The validator plugin instance
  4473. * @param {jQuery} $field Field element
  4474. * @param {Object} options Consists of the following key:
  4475. * - regexp: The regular expression you need to check
  4476. * @returns {Boolean}
  4477. */
  4478. validate: function(validator, $field, options) {
  4479. var value = $field.val();
  4480. if (value === '') {
  4481. return true;
  4482. }
  4483. var regexp = ('string' === typeof options.regexp) ? new RegExp(options.regexp) : options.regexp;
  4484. return regexp.test(value);
  4485. }
  4486. };
  4487. }(window.jQuery));
  4488. ;(function($) {
  4489. $.fn.bootstrapValidator.i18n.remote = $.extend($.fn.bootstrapValidator.i18n.remote || {}, {
  4490. 'default': 'Please enter a valid value'
  4491. });
  4492. $.fn.bootstrapValidator.validators.remote = {
  4493. html5Attributes: {
  4494. message: 'message',
  4495. name: 'name',
  4496. type: 'type',
  4497. url: 'url',
  4498. delay: 'delay'
  4499. },
  4500. /**
  4501. * Destroy the timer when destroying the bootstrapValidator (using validator.destroy() method)
  4502. */
  4503. destroy: function(validator, $field, options) {
  4504. if ($field.data('bv.remote.timer')) {
  4505. clearTimeout($field.data('bv.remote.timer'));
  4506. $field.removeData('bv.remote.timer');
  4507. }
  4508. },
  4509. /**
  4510. * Request a remote server to check the input value
  4511. *
  4512. * @param {BootstrapValidator} validator Plugin instance
  4513. * @param {jQuery} $field Field element
  4514. * @param {Object} options Can consist of the following keys:
  4515. * - url {String|Function}
  4516. * - type {String} [optional] Can be GET or POST (default)
  4517. * - data {Object|Function} [optional]: By default, it will take the value
  4518. * {
  4519. * <fieldName>: <fieldValue>
  4520. * }
  4521. * - delay
  4522. * - name {String} [optional]: Override the field name for the request.
  4523. * - message: The invalid message
  4524. * - headers: Additional headers
  4525. * @returns {Boolean|Deferred}
  4526. */
  4527. validate: function(validator, $field, options) {
  4528. var value = $field.val();
  4529. if (value === '') {
  4530. return true;
  4531. }
  4532. var name = $field.attr('data-bv-field'),
  4533. data = options.data || {},
  4534. url = options.url,
  4535. type = options.type || 'GET',
  4536. headers = options.headers || {};
  4537. // Support dynamic data
  4538. if ('function' === typeof data) {
  4539. data = data.call(this, validator);
  4540. }
  4541. // Support dynamic url
  4542. if ('function' === typeof url) {
  4543. url = url.call(this, validator);
  4544. }
  4545. data[options.name || name] = value;
  4546. var dfd = new $.Deferred();
  4547. function runCallback() {
  4548. var xhr = $.ajax({
  4549. type: type,
  4550. headers: headers,
  4551. url: url,
  4552. dataType: 'json',
  4553. data: data
  4554. });
  4555. xhr.then(function(response) {
  4556. dfd.resolve($field, 'remote', response.valid === true || response.valid === 'true', response.message ? response.message : null);
  4557. });
  4558. dfd.fail(function() {
  4559. xhr.abort();
  4560. });
  4561. return dfd;
  4562. }
  4563. if (options.delay) {
  4564. // Since the form might have multiple fields with the same name
  4565. // I have to attach the timer to the field element
  4566. if ($field.data('bv.remote.timer')) {
  4567. clearTimeout($field.data('bv.remote.timer'));
  4568. }
  4569. $field.data('bv.remote.timer', setTimeout(runCallback, options.delay));
  4570. return dfd;
  4571. } else {
  4572. return runCallback();
  4573. }
  4574. }
  4575. };
  4576. }(window.jQuery));
  4577. ;(function($) {
  4578. $.fn.bootstrapValidator.i18n.rtn = $.extend($.fn.bootstrapValidator.i18n.rtn || {}, {
  4579. 'default': 'Please enter a valid RTN number'
  4580. });
  4581. $.fn.bootstrapValidator.validators.rtn = {
  4582. /**
  4583. * Validate a RTN (Routing transit number)
  4584. * Examples:
  4585. * - Valid: 021200025, 789456124
  4586. *
  4587. * @see http://en.wikipedia.org/wiki/Routing_transit_number
  4588. * @param {BootstrapValidator} validator The validator plugin instance
  4589. * @param {jQuery} $field Field element
  4590. * @param {Object} options Can consist of the following keys:
  4591. * - message: The invalid message
  4592. * @returns {Boolean}
  4593. */
  4594. validate: function(validator, $field, options) {
  4595. var value = $field.val();
  4596. if (value === '') {
  4597. return true;
  4598. }
  4599. if (!/^\d{9}$/.test(value)) {
  4600. return false;
  4601. }
  4602. var sum = 0;
  4603. for (var i = 0; i < value.length; i += 3) {
  4604. sum += parseInt(value.charAt(i), 10) * 3
  4605. + parseInt(value.charAt(i + 1), 10) * 7
  4606. + parseInt(value.charAt(i + 2), 10);
  4607. }
  4608. return (sum !== 0 && sum % 10 === 0);
  4609. }
  4610. };
  4611. }(window.jQuery));
  4612. ;(function($) {
  4613. $.fn.bootstrapValidator.i18n.sedol = $.extend($.fn.bootstrapValidator.i18n.sedol || {}, {
  4614. 'default': 'Please enter a valid SEDOL number'
  4615. });
  4616. $.fn.bootstrapValidator.validators.sedol = {
  4617. /**
  4618. * Validate a SEDOL (Stock Exchange Daily Official List)
  4619. * Examples:
  4620. * - Valid: 0263494, B0WNLY7
  4621. *
  4622. * @see http://en.wikipedia.org/wiki/SEDOL
  4623. * @param {BootstrapValidator} validator The validator plugin instance
  4624. * @param {jQuery} $field Field element
  4625. * @param {Object} options Can consist of the following keys:
  4626. * - message: The invalid message
  4627. * @returns {Boolean}
  4628. */
  4629. validate: function(validator, $field, options) {
  4630. var value = $field.val();
  4631. if (value === '') {
  4632. return true;
  4633. }
  4634. value = value.toUpperCase();
  4635. if (!/^[0-9A-Z]{7}$/.test(value)) {
  4636. return false;
  4637. }
  4638. var sum = 0,
  4639. weight = [1, 3, 1, 7, 3, 9, 1],
  4640. length = value.length;
  4641. for (var i = 0; i < length - 1; i++) {
  4642. sum += weight[i] * parseInt(value.charAt(i), 36);
  4643. }
  4644. sum = (10 - sum % 10) % 10;
  4645. return sum + '' === value.charAt(length - 1);
  4646. }
  4647. };
  4648. }(window.jQuery));
  4649. ;(function($) {
  4650. $.fn.bootstrapValidator.i18n.siren = $.extend($.fn.bootstrapValidator.i18n.siren || {}, {
  4651. 'default': 'Please enter a valid SIREN number'
  4652. });
  4653. $.fn.bootstrapValidator.validators.siren = {
  4654. /**
  4655. * Check if a string is a siren number
  4656. *
  4657. * @param {BootstrapValidator} validator The validator plugin instance
  4658. * @param {jQuery} $field Field element
  4659. * @param {Object} options Consist of key:
  4660. * - message: The invalid message
  4661. * @returns {Boolean}
  4662. */
  4663. validate: function(validator, $field, options) {
  4664. var value = $field.val();
  4665. if (value === '') {
  4666. return true;
  4667. }
  4668. if (!/^\d{9}$/.test(value)) {
  4669. return false;
  4670. }
  4671. return $.fn.bootstrapValidator.helpers.luhn(value);
  4672. }
  4673. };
  4674. }(window.jQuery));
  4675. ;(function($) {
  4676. $.fn.bootstrapValidator.i18n.siret = $.extend($.fn.bootstrapValidator.i18n.siret || {}, {
  4677. 'default': 'Please enter a valid SIRET number'
  4678. });
  4679. $.fn.bootstrapValidator.validators.siret = {
  4680. /**
  4681. * Check if a string is a siret number
  4682. *
  4683. * @param {BootstrapValidator} validator The validator plugin instance
  4684. * @param {jQuery} $field Field element
  4685. * @param {Object} options Consist of key:
  4686. * - message: The invalid message
  4687. * @returns {Boolean}
  4688. */
  4689. validate: function(validator, $field, options) {
  4690. var value = $field.val();
  4691. if (value === '') {
  4692. return true;
  4693. }
  4694. var sum = 0,
  4695. length = value.length,
  4696. tmp;
  4697. for (var i = 0; i < length; i++) {
  4698. tmp = parseInt(value.charAt(i), 10);
  4699. if ((i % 2) === 0) {
  4700. tmp = tmp * 2;
  4701. if (tmp > 9) {
  4702. tmp -= 9;
  4703. }
  4704. }
  4705. sum += tmp;
  4706. }
  4707. return (sum % 10 === 0);
  4708. }
  4709. };
  4710. }(window.jQuery));
  4711. ;(function($) {
  4712. $.fn.bootstrapValidator.i18n.step = $.extend($.fn.bootstrapValidator.i18n.step || {}, {
  4713. 'default': 'Please enter a valid step of %s'
  4714. });
  4715. $.fn.bootstrapValidator.validators.step = {
  4716. html5Attributes: {
  4717. message: 'message',
  4718. base: 'baseValue',
  4719. step: 'step'
  4720. },
  4721. /**
  4722. * Return true if the input value is valid step one
  4723. *
  4724. * @param {BootstrapValidator} validator The validator plugin instance
  4725. * @param {jQuery} $field Field element
  4726. * @param {Object} options Can consist of the following keys:
  4727. * - baseValue: The base value
  4728. * - step: The step
  4729. * - message: The invalid message
  4730. * @returns {Boolean|Object}
  4731. */
  4732. validate: function(validator, $field, options) {
  4733. var value = $field.val();
  4734. if (value === '') {
  4735. return true;
  4736. }
  4737. options = $.extend({}, { baseValue: 0, step: 1 }, options);
  4738. value = parseFloat(value);
  4739. if (!$.isNumeric(value)) {
  4740. return false;
  4741. }
  4742. var round = function(x, precision) {
  4743. var m = Math.pow(10, precision);
  4744. x = x * m;
  4745. var sign = (x > 0) | -(x < 0),
  4746. isHalf = (x % 1 === 0.5 * sign);
  4747. if (isHalf) {
  4748. return (Math.floor(x) + (sign > 0)) / m;
  4749. } else {
  4750. return Math.round(x) / m;
  4751. }
  4752. },
  4753. floatMod = function(x, y) {
  4754. if (y === 0.0) {
  4755. return 1.0;
  4756. }
  4757. var dotX = (x + '').split('.'),
  4758. dotY = (y + '').split('.'),
  4759. precision = ((dotX.length === 1) ? 0 : dotX[1].length) + ((dotY.length === 1) ? 0 : dotY[1].length);
  4760. return round(x - y * Math.floor(x / y), precision);
  4761. };
  4762. var mod = floatMod(value - options.baseValue, options.step);
  4763. return {
  4764. valid: mod === 0.0 || mod === options.step,
  4765. message: $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.step['default'], [options.step])
  4766. };
  4767. }
  4768. };
  4769. }(window.jQuery));
  4770. ;(function($) {
  4771. $.fn.bootstrapValidator.i18n.stringCase = $.extend($.fn.bootstrapValidator.i18n.stringCase || {}, {
  4772. 'default': 'Please enter only lowercase characters',
  4773. upper: 'Please enter only uppercase characters'
  4774. });
  4775. $.fn.bootstrapValidator.validators.stringCase = {
  4776. html5Attributes: {
  4777. message: 'message',
  4778. 'case': 'case'
  4779. },
  4780. /**
  4781. * Check if a string is a lower or upper case one
  4782. *
  4783. * @param {BootstrapValidator} validator The validator plugin instance
  4784. * @param {jQuery} $field Field element
  4785. * @param {Object} options Consist of key:
  4786. * - message: The invalid message
  4787. * - case: Can be 'lower' (default) or 'upper'
  4788. * @returns {Object}
  4789. */
  4790. validate: function(validator, $field, options) {
  4791. var value = $field.val();
  4792. if (value === '') {
  4793. return true;
  4794. }
  4795. var stringCase = (options['case'] || 'lower').toLowerCase();
  4796. return {
  4797. valid: ('upper' === stringCase) ? value === value.toUpperCase() : value === value.toLowerCase(),
  4798. message: options.message || (('upper' === stringCase) ? $.fn.bootstrapValidator.i18n.stringCase.upper : $.fn.bootstrapValidator.i18n.stringCase['default'])
  4799. };
  4800. }
  4801. };
  4802. }(window.jQuery));
  4803. ;(function($) {
  4804. $.fn.bootstrapValidator.i18n.stringLength = $.extend($.fn.bootstrapValidator.i18n.stringLength || {}, {
  4805. 'default': 'Please enter a value with valid length',
  4806. less: 'Please enter less than %s characters',
  4807. more: 'Please enter more than %s characters',
  4808. between: 'Please enter value between %s and %s characters long'
  4809. });
  4810. $.fn.bootstrapValidator.validators.stringLength = {
  4811. html5Attributes: {
  4812. message: 'message',
  4813. min: 'min',
  4814. max: 'max'
  4815. },
  4816. enableByHtml5: function($field) {
  4817. var maxLength = $field.attr('maxlength');
  4818. if (maxLength) {
  4819. return {
  4820. max: parseInt(maxLength, 10)
  4821. };
  4822. }
  4823. return false;
  4824. },
  4825. /**
  4826. * Check if the length of element value is less or more than given number
  4827. *
  4828. * @param {BootstrapValidator} validator The validator plugin instance
  4829. * @param {jQuery} $field Field element
  4830. * @param {Object} options Consists of following keys:
  4831. * - min
  4832. * - max
  4833. * At least one of two keys is required
  4834. * The min, max keys define the number which the field value compares to. min, max can be
  4835. * - A number
  4836. * - Name of field which its value defines the number
  4837. * - Name of callback function that returns the number
  4838. * - A callback function that returns the number
  4839. *
  4840. * - message: The invalid message
  4841. * @returns {Object}
  4842. */
  4843. validate: function(validator, $field, options) {
  4844. var value = $field.val();
  4845. if (value === '') {
  4846. return true;
  4847. }
  4848. var min = $.isNumeric(options.min) ? options.min : validator.getDynamicOption($field, options.min),
  4849. max = $.isNumeric(options.max) ? options.max : validator.getDynamicOption($field, options.max),
  4850. length = value.length,
  4851. isValid = true,
  4852. message = options.message || $.fn.bootstrapValidator.i18n.stringLength['default'];
  4853. if ((min && length < parseInt(min, 10)) || (max && length > parseInt(max, 10))) {
  4854. isValid = false;
  4855. }
  4856. switch (true) {
  4857. case (!!min && !!max):
  4858. message = $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.stringLength.between, [parseInt(min, 10), parseInt(max, 10)]);
  4859. break;
  4860. case (!!min):
  4861. message = $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.stringLength.more, parseInt(min, 10));
  4862. break;
  4863. case (!!max):
  4864. message = $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.stringLength.less, parseInt(max, 10));
  4865. break;
  4866. default:
  4867. break;
  4868. }
  4869. return { valid: isValid, message: message };
  4870. }
  4871. };
  4872. }(window.jQuery));
  4873. ;(function($) {
  4874. $.fn.bootstrapValidator.i18n.uri = $.extend($.fn.bootstrapValidator.i18n.uri || {}, {
  4875. 'default': 'Please enter a valid URI'
  4876. });
  4877. $.fn.bootstrapValidator.validators.uri = {
  4878. html5Attributes: {
  4879. message: 'message',
  4880. allowlocal: 'allowLocal',
  4881. protocol: 'protocol'
  4882. },
  4883. enableByHtml5: function($field) {
  4884. return ('url' === $field.attr('type'));
  4885. },
  4886. /**
  4887. * Return true if the input value is a valid URL
  4888. *
  4889. * @param {BootstrapValidator} validator The validator plugin instance
  4890. * @param {jQuery} $field Field element
  4891. * @param {Object} options
  4892. * - message: The error message
  4893. * - allowLocal: Allow the private and local network IP. Default to false
  4894. * - protocol: The protocols, separated by a comma. Default to "http, https, ftp"
  4895. * @returns {Boolean}
  4896. */
  4897. validate: function(validator, $field, options) {
  4898. var value = $field.val();
  4899. if (value === '') {
  4900. return true;
  4901. }
  4902. // Credit to https://gist.github.com/dperini/729294
  4903. //
  4904. // Regular Expression for URL validation
  4905. //
  4906. // Author: Diego Perini
  4907. // Updated: 2010/12/05
  4908. //
  4909. // the regular expression composed & commented
  4910. // could be easily tweaked for RFC compliance,
  4911. // it was expressly modified to fit & satisfy
  4912. // these test for an URL shortener:
  4913. //
  4914. // http://mathiasbynens.be/demo/url-regex
  4915. //
  4916. // Notes on possible differences from a standard/generic validation:
  4917. //
  4918. // - utf-8 char class take in consideration the full Unicode range
  4919. // - TLDs are mandatory unless `allowLocal` is true
  4920. // - protocols have been restricted to ftp, http and https only as requested
  4921. //
  4922. // Changes:
  4923. //
  4924. // - IP address dotted notation validation, range: 1.0.0.0 - 223.255.255.255
  4925. // first and last IP address of each class is considered invalid
  4926. // (since they are broadcast/network addresses)
  4927. //
  4928. // - Added exclusion of private, reserved and/or local networks ranges
  4929. // unless `allowLocal` is true
  4930. //
  4931. // - Added possibility of choosing a custom protocol
  4932. //
  4933. var allowLocal = options.allowLocal === true || options.allowLocal === 'true',
  4934. protocol = (options.protocol || 'http, https, ftp').split(',').join('|').replace(/\s/g, ''),
  4935. urlExp = new RegExp(
  4936. "^" +
  4937. // protocol identifier
  4938. "(?:(?:" + protocol + ")://)" +
  4939. // user:pass authentication
  4940. "(?:\\S+(?::\\S*)?@)?" +
  4941. "(?:" +
  4942. // IP address exclusion
  4943. // private & local networks
  4944. (allowLocal
  4945. ? ''
  4946. : ("(?!(?:10|127)(?:\\.\\d{1,3}){3})" +
  4947. "(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})" +
  4948. "(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})")) +
  4949. // IP address dotted notation octets
  4950. // excludes loopback network 0.0.0.0
  4951. // excludes reserved space >= 224.0.0.0
  4952. // excludes network & broadcast addresses
  4953. // (first & last IP address of each class)
  4954. "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
  4955. "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
  4956. "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
  4957. "|" +
  4958. // host name
  4959. "(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)" +
  4960. // domain name
  4961. "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*" +
  4962. // TLD identifier
  4963. "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" +
  4964. // Allow intranet sites (no TLD) if `allowLocal` is true
  4965. (allowLocal ? '?' : '') +
  4966. ")" +
  4967. // port number
  4968. "(?::\\d{2,5})?" +
  4969. // resource path
  4970. "(?:/[^\\s]*)?" +
  4971. "$", "i"
  4972. );
  4973. return urlExp.test(value);
  4974. }
  4975. };
  4976. }(window.jQuery));
  4977. ;(function($) {
  4978. $.fn.bootstrapValidator.i18n.uuid = $.extend($.fn.bootstrapValidator.i18n.uuid || {}, {
  4979. 'default': 'Please enter a valid UUID number',
  4980. version: 'Please enter a valid UUID version %s number'
  4981. });
  4982. $.fn.bootstrapValidator.validators.uuid = {
  4983. html5Attributes: {
  4984. message: 'message',
  4985. version: 'version'
  4986. },
  4987. /**
  4988. * Return true if and only if the input value is a valid UUID string
  4989. *
  4990. * @see http://en.wikipedia.org/wiki/Universally_unique_identifier
  4991. * @param {BootstrapValidator} validator The validator plugin instance
  4992. * @param {jQuery} $field Field element
  4993. * @param {Object} options Consist of key:
  4994. * - message: The invalid message
  4995. * - version: Can be 3, 4, 5, null
  4996. * @returns {Boolean|Object}
  4997. */
  4998. validate: function(validator, $field, options) {
  4999. var value = $field.val();
  5000. if (value === '') {
  5001. return true;
  5002. }
  5003. // See the format at http://en.wikipedia.org/wiki/Universally_unique_identifier#Variants_and_versions
  5004. var patterns = {
  5005. '3': /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
  5006. '4': /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
  5007. '5': /^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
  5008. all: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i
  5009. },
  5010. version = options.version ? (options.version + '') : 'all';
  5011. return {
  5012. valid: (null === patterns[version]) ? true : patterns[version].test(value),
  5013. message: options.version
  5014. ? $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.uuid.version, options.version)
  5015. : (options.message || $.fn.bootstrapValidator.i18n.uuid['default'])
  5016. };
  5017. }
  5018. };
  5019. }(window.jQuery));
  5020. ;(function($) {
  5021. $.fn.bootstrapValidator.i18n.vat = $.extend($.fn.bootstrapValidator.i18n.vat || {}, {
  5022. 'default': 'Please enter a valid VAT number',
  5023. countryNotSupported: 'The country code %s is not supported',
  5024. country: 'Please enter a valid VAT number in %s',
  5025. countries: {
  5026. AT: 'Austria',
  5027. BE: 'Belgium',
  5028. BG: 'Bulgaria',
  5029. BR: 'Brazil',
  5030. CH: 'Switzerland',
  5031. CY: 'Cyprus',
  5032. CZ: 'Czech',
  5033. DE: 'Germany',
  5034. DK: 'Denmark',
  5035. EE: 'Estonia',
  5036. ES: 'Spain',
  5037. FI: 'Finland',
  5038. FR: 'France',
  5039. GB: 'United Kingdom',
  5040. GR: 'Greek',
  5041. EL: 'Greek',
  5042. HU: 'Hungary',
  5043. HR: 'Croatia',
  5044. IE: 'Ireland',
  5045. IS: 'Iceland',
  5046. IT: 'Italy',
  5047. LT: 'Lithuania',
  5048. LU: 'Luxembourg',
  5049. LV: 'Latvia',
  5050. MT: 'Malta',
  5051. NL: 'Netherlands',
  5052. NO: 'Norway',
  5053. PL: 'Poland',
  5054. PT: 'Portugal',
  5055. RO: 'Romania',
  5056. RU: 'Russia',
  5057. RS: 'Serbia',
  5058. SE: 'Sweden',
  5059. SI: 'Slovenia',
  5060. SK: 'Slovakia',
  5061. VE: 'Venezuela',
  5062. ZA: 'South Africa'
  5063. }
  5064. });
  5065. $.fn.bootstrapValidator.validators.vat = {
  5066. html5Attributes: {
  5067. message: 'message',
  5068. country: 'country'
  5069. },
  5070. // Supported country codes
  5071. COUNTRY_CODES: [
  5072. 'AT', 'BE', 'BG', 'BR', 'CH', 'CY', 'CZ', 'DE', 'DK', 'EE', 'EL', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU',
  5073. 'IE', 'IS', 'IT', 'LT', 'LU', 'LV', 'MT', 'NL', 'NO', 'PL', 'PT', 'RO', 'RU', 'RS', 'SE', 'SK', 'SI', 'VE',
  5074. 'ZA'
  5075. ],
  5076. /**
  5077. * Validate an European VAT number
  5078. *
  5079. * @param {BootstrapValidator} validator The validator plugin instance
  5080. * @param {jQuery} $field Field element
  5081. * @param {Object} options Consist of key:
  5082. * - message: The invalid message
  5083. * - country: The ISO 3166-1 country code. It can be
  5084. * - One of country code defined in COUNTRY_CODES
  5085. * - Name of field which its value defines the country code
  5086. * - Name of callback function that returns the country code
  5087. * - A callback function that returns the country code
  5088. * @returns {Boolean|Object}
  5089. */
  5090. validate: function(validator, $field, options) {
  5091. var value = $field.val();
  5092. if (value === '') {
  5093. return true;
  5094. }
  5095. var country = options.country;
  5096. if (!country) {
  5097. country = value.substr(0, 2);
  5098. } else if (typeof country !== 'string' || $.inArray(country.toUpperCase(), this.COUNTRY_CODES) === -1) {
  5099. // Determine the country code
  5100. country = validator.getDynamicOption($field, country);
  5101. }
  5102. if ($.inArray(country, this.COUNTRY_CODES) === -1) {
  5103. return {
  5104. valid: false,
  5105. message: $.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.vat.countryNotSupported, country)
  5106. };
  5107. }
  5108. var method = ['_', country.toLowerCase()].join('');
  5109. return this[method](value)
  5110. ? true
  5111. : {
  5112. valid: false,
  5113. message: $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.vat.country, $.fn.bootstrapValidator.i18n.vat.countries[country.toUpperCase()])
  5114. };
  5115. },
  5116. // VAT validators
  5117. /**
  5118. * Validate Austrian VAT number
  5119. * Example:
  5120. * - Valid: ATU13585627
  5121. * - Invalid: ATU13585626
  5122. *
  5123. * @param {String} value VAT number
  5124. * @returns {Boolean}
  5125. */
  5126. _at: function(value) {
  5127. if (/^ATU[0-9]{8}$/.test(value)) {
  5128. value = value.substr(2);
  5129. }
  5130. if (!/^U[0-9]{8}$/.test(value)) {
  5131. return false;
  5132. }
  5133. value = value.substr(1);
  5134. var sum = 0,
  5135. weight = [1, 2, 1, 2, 1, 2, 1],
  5136. temp = 0;
  5137. for (var i = 0; i < 7; i++) {
  5138. temp = parseInt(value.charAt(i), 10) * weight[i];
  5139. if (temp > 9) {
  5140. temp = Math.floor(temp / 10) + temp % 10;
  5141. }
  5142. sum += temp;
  5143. }
  5144. sum = 10 - (sum + 4) % 10;
  5145. if (sum === 10) {
  5146. sum = 0;
  5147. }
  5148. return (sum + '' === value.substr(7, 1));
  5149. },
  5150. /**
  5151. * Validate Belgian VAT number
  5152. * Example:
  5153. * - Valid: BE0428759497
  5154. * - Invalid: BE431150351
  5155. *
  5156. * @param {String} value VAT number
  5157. * @returns {Boolean}
  5158. */
  5159. _be: function(value) {
  5160. if (/^BE[0]{0,1}[0-9]{9}$/.test(value)) {
  5161. value = value.substr(2);
  5162. }
  5163. if (!/^[0]{0,1}[0-9]{9}$/.test(value)) {
  5164. return false;
  5165. }
  5166. if (value.length === 9) {
  5167. value = '0' + value;
  5168. }
  5169. if (value.substr(1, 1) === '0') {
  5170. return false;
  5171. }
  5172. var sum = parseInt(value.substr(0, 8), 10) + parseInt(value.substr(8, 2), 10);
  5173. return (sum % 97 === 0);
  5174. },
  5175. /**
  5176. * Validate Bulgarian VAT number
  5177. * Example:
  5178. * - Valid: BG175074752,
  5179. * BG7523169263, BG8032056031,
  5180. * BG7542011030,
  5181. * BG7111042925
  5182. * - Invalid: BG175074753, BG7552A10004, BG7111042922
  5183. *
  5184. * @param {String} value VAT number
  5185. * @returns {Boolean}
  5186. */
  5187. _bg: function(value) {
  5188. if (/^BG[0-9]{9,10}$/.test(value)) {
  5189. value = value.substr(2);
  5190. }
  5191. if (!/^[0-9]{9,10}$/.test(value)) {
  5192. return false;
  5193. }
  5194. var sum = 0, i = 0;
  5195. // Legal entities
  5196. if (value.length === 9) {
  5197. for (i = 0; i < 8; i++) {
  5198. sum += parseInt(value.charAt(i), 10) * (i + 1);
  5199. }
  5200. sum = sum % 11;
  5201. if (sum === 10) {
  5202. sum = 0;
  5203. for (i = 0; i < 8; i++) {
  5204. sum += parseInt(value.charAt(i), 10) * (i + 3);
  5205. }
  5206. }
  5207. sum = sum % 10;
  5208. return (sum + '' === value.substr(8));
  5209. }
  5210. // Physical persons, foreigners and others
  5211. else if (value.length === 10) {
  5212. // Validate Bulgarian national identification numbers
  5213. var egn = function(value) {
  5214. // Check the birth date
  5215. var year = parseInt(value.substr(0, 2), 10) + 1900,
  5216. month = parseInt(value.substr(2, 2), 10),
  5217. day = parseInt(value.substr(4, 2), 10);
  5218. if (month > 40) {
  5219. year += 100;
  5220. month -= 40;
  5221. } else if (month > 20) {
  5222. year -= 100;
  5223. month -= 20;
  5224. }
  5225. if (!$.fn.bootstrapValidator.helpers.date(year, month, day)) {
  5226. return false;
  5227. }
  5228. var sum = 0,
  5229. weight = [2, 4, 8, 5, 10, 9, 7, 3, 6];
  5230. for (var i = 0; i < 9; i++) {
  5231. sum += parseInt(value.charAt(i), 10) * weight[i];
  5232. }
  5233. sum = (sum % 11) % 10;
  5234. return (sum + '' === value.substr(9, 1));
  5235. },
  5236. // Validate Bulgarian personal number of a foreigner
  5237. pnf = function(value) {
  5238. var sum = 0,
  5239. weight = [21, 19, 17, 13, 11, 9, 7, 3, 1];
  5240. for (var i = 0; i < 9; i++) {
  5241. sum += parseInt(value.charAt(i), 10) * weight[i];
  5242. }
  5243. sum = sum % 10;
  5244. return (sum + '' === value.substr(9, 1));
  5245. },
  5246. // Finally, consider it as a VAT number
  5247. vat = function(value) {
  5248. var sum = 0,
  5249. weight = [4, 3, 2, 7, 6, 5, 4, 3, 2];
  5250. for (var i = 0; i < 9; i++) {
  5251. sum += parseInt(value.charAt(i), 10) * weight[i];
  5252. }
  5253. sum = 11 - sum % 11;
  5254. if (sum === 10) {
  5255. return false;
  5256. }
  5257. if (sum === 11) {
  5258. sum = 0;
  5259. }
  5260. return (sum + '' === value.substr(9, 1));
  5261. };
  5262. return (egn(value) || pnf(value) || vat(value));
  5263. }
  5264. return false;
  5265. },
  5266. /**
  5267. * Validate Brazilian VAT number (CNPJ)
  5268. *
  5269. * @param {String} value VAT number
  5270. * @returns {Boolean}
  5271. */
  5272. _br: function(value) {
  5273. if (value === '') {
  5274. return true;
  5275. }
  5276. var cnpj = value.replace(/[^\d]+/g, '');
  5277. if (cnpj === '' || cnpj.length !== 14) {
  5278. return false;
  5279. }
  5280. // Remove invalids CNPJs
  5281. if (cnpj === '00000000000000' || cnpj === '11111111111111' || cnpj === '22222222222222' ||
  5282. cnpj === '33333333333333' || cnpj === '44444444444444' || cnpj === '55555555555555' ||
  5283. cnpj === '66666666666666' || cnpj === '77777777777777' || cnpj === '88888888888888' ||
  5284. cnpj === '99999999999999')
  5285. {
  5286. return false;
  5287. }
  5288. // Validate verification digits
  5289. var length = cnpj.length - 2,
  5290. numbers = cnpj.substring(0, length),
  5291. digits = cnpj.substring(length),
  5292. sum = 0,
  5293. pos = length - 7;
  5294. for (var i = length; i >= 1; i--) {
  5295. sum += parseInt(numbers.charAt(length - i), 10) * pos--;
  5296. if (pos < 2) {
  5297. pos = 9;
  5298. }
  5299. }
  5300. var result = sum % 11 < 2 ? 0 : 11 - sum % 11;
  5301. if (result !== parseInt(digits.charAt(0), 10)) {
  5302. return false;
  5303. }
  5304. length = length + 1;
  5305. numbers = cnpj.substring(0, length);
  5306. sum = 0;
  5307. pos = length - 7;
  5308. for (i = length; i >= 1; i--) {
  5309. sum += parseInt(numbers.charAt(length - i), 10) * pos--;
  5310. if (pos < 2) {
  5311. pos = 9;
  5312. }
  5313. }
  5314. result = sum % 11 < 2 ? 0 : 11 - sum % 11;
  5315. return (result === parseInt(digits.charAt(1), 10));
  5316. },
  5317. /**
  5318. * Validate Swiss VAT number
  5319. *
  5320. * @param {String} value VAT number
  5321. * @returns {Boolean}
  5322. */
  5323. _ch: function(value) {
  5324. if (/^CHE[0-9]{9}(MWST)?$/.test(value)) {
  5325. value = value.substr(2);
  5326. }
  5327. if (!/^E[0-9]{9}(MWST)?$/.test(value)) {
  5328. return false;
  5329. }
  5330. value = value.substr(1);
  5331. var sum = 0,
  5332. weight = [5, 4, 3, 2, 7, 6, 5, 4];
  5333. for (var i = 0; i < 8; i++) {
  5334. sum += parseInt(value.charAt(i), 10) * weight[i];
  5335. }
  5336. sum = 11 - sum % 11;
  5337. if (sum === 10) {
  5338. return false;
  5339. }
  5340. if (sum === 11) {
  5341. sum = 0;
  5342. }
  5343. return (sum + '' === value.substr(8, 1));
  5344. },
  5345. /**
  5346. * Validate Cypriot VAT number
  5347. * Examples:
  5348. * - Valid: CY10259033P
  5349. * - Invalid: CY10259033Z
  5350. *
  5351. * @param {String} value VAT number
  5352. * @returns {Boolean}
  5353. */
  5354. _cy: function(value) {
  5355. if (/^CY[0-5|9]{1}[0-9]{7}[A-Z]{1}$/.test(value)) {
  5356. value = value.substr(2);
  5357. }
  5358. if (!/^[0-5|9]{1}[0-9]{7}[A-Z]{1}$/.test(value)) {
  5359. return false;
  5360. }
  5361. // Do not allow to start with "12"
  5362. if (value.substr(0, 2) === '12') {
  5363. return false;
  5364. }
  5365. // Extract the next digit and multiply by the counter.
  5366. var sum = 0,
  5367. translation = {
  5368. '0': 1, '1': 0, '2': 5, '3': 7, '4': 9,
  5369. '5': 13, '6': 15, '7': 17, '8': 19, '9': 21
  5370. };
  5371. for (var i = 0; i < 8; i++) {
  5372. var temp = parseInt(value.charAt(i), 10);
  5373. if (i % 2 === 0) {
  5374. temp = translation[temp + ''];
  5375. }
  5376. sum += temp;
  5377. }
  5378. sum = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[sum % 26];
  5379. return (sum + '' === value.substr(8, 1));
  5380. },
  5381. /**
  5382. * Validate Czech Republic VAT number
  5383. * Can be:
  5384. * i) Legal entities (8 digit numbers)
  5385. * ii) Individuals with a RC (the 9 or 10 digit Czech birth number)
  5386. * iii) Individuals without a RC (9 digit numbers beginning with 6)
  5387. *
  5388. * Examples:
  5389. * - Valid: i) CZ25123891; ii) CZ7103192745, CZ991231123; iii) CZ640903926
  5390. * - Invalid: i) CZ25123890; ii) CZ1103492745, CZ590312123
  5391. *
  5392. * @param {String} value VAT number
  5393. * @returns {Boolean}
  5394. */
  5395. _cz: function(value) {
  5396. if (/^CZ[0-9]{8,10}$/.test(value)) {
  5397. value = value.substr(2);
  5398. }
  5399. if (!/^[0-9]{8,10}$/.test(value)) {
  5400. return false;
  5401. }
  5402. var sum = 0,
  5403. i = 0;
  5404. if (value.length === 8) {
  5405. // Do not allow to start with '9'
  5406. if (value.charAt(0) + '' === '9') {
  5407. return false;
  5408. }
  5409. sum = 0;
  5410. for (i = 0; i < 7; i++) {
  5411. sum += parseInt(value.charAt(i), 10) * (8 - i);
  5412. }
  5413. sum = 11 - sum % 11;
  5414. if (sum === 10) {
  5415. sum = 0;
  5416. }
  5417. if (sum === 11) {
  5418. sum = 1;
  5419. }
  5420. return (sum + '' === value.substr(7, 1));
  5421. } else if (value.length === 9 && (value.charAt(0) + '' === '6')) {
  5422. sum = 0;
  5423. // Skip the first (which is 6)
  5424. for (i = 0; i < 7; i++) {
  5425. sum += parseInt(value.charAt(i + 1), 10) * (8 - i);
  5426. }
  5427. sum = 11 - sum % 11;
  5428. if (sum === 10) {
  5429. sum = 0;
  5430. }
  5431. if (sum === 11) {
  5432. sum = 1;
  5433. }
  5434. sum = [8, 7, 6, 5, 4, 3, 2, 1, 0, 9, 10][sum - 1];
  5435. return (sum + '' === value.substr(8, 1));
  5436. } else if (value.length === 9 || value.length === 10) {
  5437. // Validate Czech birth number (Rodné číslo), which is also national identifier
  5438. var year = 1900 + parseInt(value.substr(0, 2), 10),
  5439. month = parseInt(value.substr(2, 2), 10) % 50 % 20,
  5440. day = parseInt(value.substr(4, 2), 10);
  5441. if (value.length === 9) {
  5442. if (year >= 1980) {
  5443. year -= 100;
  5444. }
  5445. if (year > 1953) {
  5446. return false;
  5447. }
  5448. } else if (year < 1954) {
  5449. year += 100;
  5450. }
  5451. if (!$.fn.bootstrapValidator.helpers.date(year, month, day)) {
  5452. return false;
  5453. }
  5454. // Check that the birth date is not in the future
  5455. if (value.length === 10) {
  5456. var check = parseInt(value.substr(0, 9), 10) % 11;
  5457. if (year < 1985) {
  5458. check = check % 10;
  5459. }
  5460. return (check + '' === value.substr(9, 1));
  5461. }
  5462. return true;
  5463. }
  5464. return false;
  5465. },
  5466. /**
  5467. * Validate German VAT number
  5468. * Examples:
  5469. * - Valid: DE136695976
  5470. * - Invalid: DE136695978
  5471. *
  5472. * @param {String} value VAT number
  5473. * @returns {Boolean}
  5474. */
  5475. _de: function(value) {
  5476. if (/^DE[0-9]{9}$/.test(value)) {
  5477. value = value.substr(2);
  5478. }
  5479. if (!/^[0-9]{9}$/.test(value)) {
  5480. return false;
  5481. }
  5482. return $.fn.bootstrapValidator.helpers.mod11And10(value);
  5483. },
  5484. /**
  5485. * Validate Danish VAT number
  5486. * Example:
  5487. * - Valid: DK13585628
  5488. * - Invalid: DK13585627
  5489. *
  5490. * @param {String} value VAT number
  5491. * @returns {Boolean}
  5492. */
  5493. _dk: function(value) {
  5494. if (/^DK[0-9]{8}$/.test(value)) {
  5495. value = value.substr(2);
  5496. }
  5497. if (!/^[0-9]{8}$/.test(value)) {
  5498. return false;
  5499. }
  5500. var sum = 0,
  5501. weight = [2, 7, 6, 5, 4, 3, 2, 1];
  5502. for (var i = 0; i < 8; i++) {
  5503. sum += parseInt(value.charAt(i), 10) * weight[i];
  5504. }
  5505. return (sum % 11 === 0);
  5506. },
  5507. /**
  5508. * Validate Estonian VAT number
  5509. * Examples:
  5510. * - Valid: EE100931558, EE100594102
  5511. * - Invalid: EE100594103
  5512. *
  5513. * @param {String} value VAT number
  5514. * @returns {Boolean}
  5515. */
  5516. _ee: function(value) {
  5517. if (/^EE[0-9]{9}$/.test(value)) {
  5518. value = value.substr(2);
  5519. }
  5520. if (!/^[0-9]{9}$/.test(value)) {
  5521. return false;
  5522. }
  5523. var sum = 0,
  5524. weight = [3, 7, 1, 3, 7, 1, 3, 7, 1];
  5525. for (var i = 0; i < 9; i++) {
  5526. sum += parseInt(value.charAt(i), 10) * weight[i];
  5527. }
  5528. return (sum % 10 === 0);
  5529. },
  5530. /**
  5531. * Validate Spanish VAT number (NIF - Número de Identificación Fiscal)
  5532. * Can be:
  5533. * i) DNI (Documento nacional de identidad), for Spaniards
  5534. * ii) NIE (Número de Identificación de Extranjeros), for foreigners
  5535. * iii) CIF (Certificado de Identificación Fiscal), for legal entities and others
  5536. *
  5537. * Examples:
  5538. * - Valid: i) ES54362315K; ii) ESX2482300W, ESX5253868R; iii) ESM1234567L, ESJ99216582, ESB58378431, ESB64717838
  5539. * - Invalid: i) ES54362315Z; ii) ESX2482300A; iii) ESJ99216583
  5540. *
  5541. * @param {String} value VAT number
  5542. * @returns {Boolean}
  5543. */
  5544. _es: function(value) {
  5545. if (/^ES[0-9A-Z][0-9]{7}[0-9A-Z]$/.test(value)) {
  5546. value = value.substr(2);
  5547. }
  5548. if (!/^[0-9A-Z][0-9]{7}[0-9A-Z]$/.test(value)) {
  5549. return false;
  5550. }
  5551. var dni = function(value) {
  5552. var check = parseInt(value.substr(0, 8), 10);
  5553. check = 'TRWAGMYFPDXBNJZSQVHLCKE'[check % 23];
  5554. return (check + '' === value.substr(8, 1));
  5555. },
  5556. nie = function(value) {
  5557. var check = ['XYZ'.indexOf(value.charAt(0)), value.substr(1)].join('');
  5558. check = parseInt(check, 10);
  5559. check = 'TRWAGMYFPDXBNJZSQVHLCKE'[check % 23];
  5560. return (check + '' === value.substr(8, 1));
  5561. },
  5562. cif = function(value) {
  5563. var first = value.charAt(0), check;
  5564. if ('KLM'.indexOf(first) !== -1) {
  5565. // K: Spanish younger than 14 year old
  5566. // L: Spanish living outside Spain without DNI
  5567. // M: Granted the tax to foreigners who have no NIE
  5568. check = parseInt(value.substr(1, 8), 10);
  5569. check = 'TRWAGMYFPDXBNJZSQVHLCKE'[check % 23];
  5570. return (check + '' === value.substr(8, 1));
  5571. } else if ('ABCDEFGHJNPQRSUVW'.indexOf(first) !== -1) {
  5572. var sum = 0,
  5573. weight = [2, 1, 2, 1, 2, 1, 2],
  5574. temp = 0;
  5575. for (var i = 0; i < 7; i++) {
  5576. temp = parseInt(value.charAt(i + 1), 10) * weight[i];
  5577. if (temp > 9) {
  5578. temp = Math.floor(temp / 10) + temp % 10;
  5579. }
  5580. sum += temp;
  5581. }
  5582. sum = 10 - sum % 10;
  5583. return (sum + '' === value.substr(8, 1) || 'JABCDEFGHI'[sum] === value.substr(8, 1));
  5584. }
  5585. return false;
  5586. };
  5587. var first = value.charAt(0);
  5588. if (/^[0-9]$/.test(first)) {
  5589. return dni(value);
  5590. } else if (/^[XYZ]$/.test(first)) {
  5591. return nie(value);
  5592. } else {
  5593. return cif(value);
  5594. }
  5595. },
  5596. /**
  5597. * Validate Finnish VAT number
  5598. * Examples:
  5599. * - Valid: FI20774740
  5600. * - Invalid: FI20774741
  5601. *
  5602. * @param {String} value VAT number
  5603. * @returns {Boolean}
  5604. */
  5605. _fi: function(value) {
  5606. if (/^FI[0-9]{8}$/.test(value)) {
  5607. value = value.substr(2);
  5608. }
  5609. if (!/^[0-9]{8}$/.test(value)) {
  5610. return false;
  5611. }
  5612. var sum = 0,
  5613. weight = [7, 9, 10, 5, 8, 4, 2, 1];
  5614. for (var i = 0; i < 8; i++) {
  5615. sum += parseInt(value.charAt(i), 10) * weight[i];
  5616. }
  5617. return (sum % 11 === 0);
  5618. },
  5619. /**
  5620. * Validate French VAT number (TVA - taxe sur la valeur ajoutée)
  5621. * It's constructed by a SIREN number, prefixed by two characters.
  5622. *
  5623. * Examples:
  5624. * - Valid: FR40303265045, FR23334175221, FRK7399859412, FR4Z123456782
  5625. * - Invalid: FR84323140391
  5626. *
  5627. * @param {String} value VAT number
  5628. * @returns {Boolean}
  5629. */
  5630. _fr: function(value) {
  5631. if (/^FR[0-9A-Z]{2}[0-9]{9}$/.test(value)) {
  5632. value = value.substr(2);
  5633. }
  5634. if (!/^[0-9A-Z]{2}[0-9]{9}$/.test(value)) {
  5635. return false;
  5636. }
  5637. if (!$.fn.bootstrapValidator.helpers.luhn(value.substr(2))) {
  5638. return false;
  5639. }
  5640. if (/^[0-9]{2}$/.test(value.substr(0, 2))) {
  5641. // First two characters are digits
  5642. return value.substr(0, 2) === (parseInt(value.substr(2) + '12', 10) % 97 + '');
  5643. } else {
  5644. // The first characters cann't be O and I
  5645. var alphabet = '0123456789ABCDEFGHJKLMNPQRSTUVWXYZ',
  5646. check;
  5647. // First one is digit
  5648. if (/^[0-9]{1}$/.test(value.charAt(0))) {
  5649. check = alphabet.indexOf(value.charAt(0)) * 24 + alphabet.indexOf(value.charAt(1)) - 10;
  5650. } else {
  5651. check = alphabet.indexOf(value.charAt(0)) * 34 + alphabet.indexOf(value.charAt(1)) - 100;
  5652. }
  5653. return ((parseInt(value.substr(2), 10) + 1 + Math.floor(check / 11)) % 11) === (check % 11);
  5654. }
  5655. },
  5656. /**
  5657. * Validate United Kingdom VAT number
  5658. * Example:
  5659. * - Valid: GB980780684
  5660. * - Invalid: GB802311781
  5661. *
  5662. * @param {String} value VAT number
  5663. * @returns {Boolean}
  5664. */
  5665. _gb: function(value) {
  5666. if (/^GB[0-9]{9}$/.test(value) /* Standard */
  5667. || /^GB[0-9]{12}$/.test(value) /* Branches */
  5668. || /^GBGD[0-9]{3}$/.test(value) /* Government department */
  5669. || /^GBHA[0-9]{3}$/.test(value) /* Health authority */
  5670. || /^GB(GD|HA)8888[0-9]{5}$/.test(value))
  5671. {
  5672. value = value.substr(2);
  5673. }
  5674. if (!/^[0-9]{9}$/.test(value)
  5675. && !/^[0-9]{12}$/.test(value)
  5676. && !/^GD[0-9]{3}$/.test(value)
  5677. && !/^HA[0-9]{3}$/.test(value)
  5678. && !/^(GD|HA)8888[0-9]{5}$/.test(value))
  5679. {
  5680. return false;
  5681. }
  5682. var length = value.length;
  5683. if (length === 5) {
  5684. var firstTwo = value.substr(0, 2),
  5685. lastThree = parseInt(value.substr(2), 10);
  5686. return ('GD' === firstTwo && lastThree < 500) || ('HA' === firstTwo && lastThree >= 500);
  5687. } else if (length === 11 && ('GD8888' === value.substr(0, 6) || 'HA8888' === value.substr(0, 6))) {
  5688. if (('GD' === value.substr(0, 2) && parseInt(value.substr(6, 3), 10) >= 500)
  5689. || ('HA' === value.substr(0, 2) && parseInt(value.substr(6, 3), 10) < 500))
  5690. {
  5691. return false;
  5692. }
  5693. return (parseInt(value.substr(6, 3), 10) % 97 === parseInt(value.substr(9, 2), 10));
  5694. } else if (length === 9 || length === 12) {
  5695. var sum = 0,
  5696. weight = [8, 7, 6, 5, 4, 3, 2, 10, 1];
  5697. for (var i = 0; i < 9; i++) {
  5698. sum += parseInt(value.charAt(i), 10) * weight[i];
  5699. }
  5700. sum = sum % 97;
  5701. if (parseInt(value.substr(0, 3), 10) >= 100) {
  5702. return (sum === 0 || sum === 42 || sum === 55);
  5703. } else {
  5704. return (sum === 0);
  5705. }
  5706. }
  5707. return true;
  5708. },
  5709. /**
  5710. * Validate Greek VAT number
  5711. * Examples:
  5712. * - Valid: GR023456780, EL094259216
  5713. * - Invalid: EL123456781
  5714. *
  5715. * @param {String} value VAT number
  5716. * @returns {Boolean}
  5717. */
  5718. _gr: function(value) {
  5719. if (/^(GR|EL)[0-9]{9}$/.test(value)) {
  5720. value = value.substr(2);
  5721. }
  5722. if (!/^[0-9]{9}$/.test(value)) {
  5723. return false;
  5724. }
  5725. if (value.length === 8) {
  5726. value = '0' + value;
  5727. }
  5728. var sum = 0,
  5729. weight = [256, 128, 64, 32, 16, 8, 4, 2];
  5730. for (var i = 0; i < 8; i++) {
  5731. sum += parseInt(value.charAt(i), 10) * weight[i];
  5732. }
  5733. sum = (sum % 11) % 10;
  5734. return (sum + '' === value.substr(8, 1));
  5735. },
  5736. // EL is traditionally prefix of Greek VAT numbers
  5737. _el: function(value) {
  5738. return this._gr(value);
  5739. },
  5740. /**
  5741. * Validate Hungarian VAT number
  5742. * Examples:
  5743. * - Valid: HU12892312
  5744. * - Invalid: HU12892313
  5745. *
  5746. * @param {String} value VAT number
  5747. * @returns {Boolean}
  5748. */
  5749. _hu: function(value) {
  5750. if (/^HU[0-9]{8}$/.test(value)) {
  5751. value = value.substr(2);
  5752. }
  5753. if (!/^[0-9]{8}$/.test(value)) {
  5754. return false;
  5755. }
  5756. var sum = 0,
  5757. weight = [9, 7, 3, 1, 9, 7, 3, 1];
  5758. for (var i = 0; i < 8; i++) {
  5759. sum += parseInt(value.charAt(i), 10) * weight[i];
  5760. }
  5761. return (sum % 10 === 0);
  5762. },
  5763. /**
  5764. * Validate Croatian VAT number
  5765. * Examples:
  5766. * - Valid: HR33392005961
  5767. * - Invalid: HR33392005962
  5768. *
  5769. * @param {String} value VAT number
  5770. * @returns {Boolean}
  5771. */
  5772. _hr: function(value) {
  5773. if (/^HR[0-9]{11}$/.test(value)) {
  5774. value = value.substr(2);
  5775. }
  5776. if (!/^[0-9]{11}$/.test(value)) {
  5777. return false;
  5778. }
  5779. return $.fn.bootstrapValidator.helpers.mod11And10(value);
  5780. },
  5781. /**
  5782. * Validate Irish VAT number
  5783. * Examples:
  5784. * - Valid: IE6433435F, IE6433435OA, IE8D79739I
  5785. * - Invalid: IE8D79738J
  5786. *
  5787. * @param {String} value VAT number
  5788. * @returns {Boolean}
  5789. */
  5790. _ie: function(value) {
  5791. if (/^IE[0-9]{1}[0-9A-Z\*\+]{1}[0-9]{5}[A-Z]{1,2}$/.test(value)) {
  5792. value = value.substr(2);
  5793. }
  5794. if (!/^[0-9]{1}[0-9A-Z\*\+]{1}[0-9]{5}[A-Z]{1,2}$/.test(value)) {
  5795. return false;
  5796. }
  5797. var getCheckDigit = function(value) {
  5798. while (value.length < 7) {
  5799. value = '0' + value;
  5800. }
  5801. var alphabet = 'WABCDEFGHIJKLMNOPQRSTUV',
  5802. sum = 0;
  5803. for (var i = 0; i < 7; i++) {
  5804. sum += parseInt(value.charAt(i), 10) * (8 - i);
  5805. }
  5806. sum += 9 * alphabet.indexOf(value.substr(7));
  5807. return alphabet[sum % 23];
  5808. };
  5809. // The first 7 characters are digits
  5810. if (/^[0-9]+$/.test(value.substr(0, 7))) {
  5811. // New system
  5812. return value.charAt(7) === getCheckDigit(value.substr(0, 7) + value.substr(8) + '');
  5813. } else if ('ABCDEFGHIJKLMNOPQRSTUVWXYZ+*'.indexOf(value.charAt(1)) !== -1) {
  5814. // Old system
  5815. return value.charAt(7) === getCheckDigit(value.substr(2, 5) + value.substr(0, 1) + '');
  5816. }
  5817. return true;
  5818. },
  5819. /**
  5820. * Validate Icelandic VAT (VSK) number
  5821. * Examples:
  5822. * - Valid: 12345, 123456
  5823. * - Invalid: 1234567
  5824. *
  5825. * @params {String} value VAT number
  5826. * @returns {Boolean}
  5827. */
  5828. _is: function(value) {
  5829. if (/^IS[0-9]{5,6}$/.test(value)) {
  5830. value = value.substr(2);
  5831. }
  5832. return /^[0-9]{5,6}$/.test(value);
  5833. },
  5834. /**
  5835. * Validate Italian VAT number, which consists of 11 digits.
  5836. * - First 7 digits are a company identifier
  5837. * - Next 3 are the province of residence
  5838. * - The last one is a check digit
  5839. *
  5840. * Examples:
  5841. * - Valid: IT00743110157
  5842. * - Invalid: IT00743110158
  5843. *
  5844. * @param {String} value VAT number
  5845. * @returns {Boolean}
  5846. */
  5847. _it: function(value) {
  5848. if (/^IT[0-9]{11}$/.test(value)) {
  5849. value = value.substr(2);
  5850. }
  5851. if (!/^[0-9]{11}$/.test(value)) {
  5852. return false;
  5853. }
  5854. if (parseInt(value.substr(0, 7), 10) === 0) {
  5855. return false;
  5856. }
  5857. var lastThree = parseInt(value.substr(7, 3), 10);
  5858. if ((lastThree < 1) || (lastThree > 201) && lastThree !== 999 && lastThree !== 888) {
  5859. return false;
  5860. }
  5861. return $.fn.bootstrapValidator.helpers.luhn(value);
  5862. },
  5863. /**
  5864. * Validate Lithuanian VAT number
  5865. * It can be:
  5866. * - 9 digits, for legal entities
  5867. * - 12 digits, for temporarily registered taxpayers
  5868. *
  5869. * Examples:
  5870. * - Valid: LT119511515, LT100001919017, LT100004801610
  5871. * - Invalid: LT100001919018
  5872. *
  5873. * @param {String} value VAT number
  5874. * @returns {Boolean}
  5875. */
  5876. _lt: function(value) {
  5877. if (/^LT([0-9]{7}1[0-9]{1}|[0-9]{10}1[0-9]{1})$/.test(value)) {
  5878. value = value.substr(2);
  5879. }
  5880. if (!/^([0-9]{7}1[0-9]{1}|[0-9]{10}1[0-9]{1})$/.test(value)) {
  5881. return false;
  5882. }
  5883. var length = value.length,
  5884. sum = 0,
  5885. i;
  5886. for (i = 0; i < length - 1; i++) {
  5887. sum += parseInt(value.charAt(i), 10) * (1 + i % 9);
  5888. }
  5889. var check = sum % 11;
  5890. if (check === 10) {
  5891. sum = 0;
  5892. for (i = 0; i < length - 1; i++) {
  5893. sum += parseInt(value.charAt(i), 10) * (1 + (i + 2) % 9);
  5894. }
  5895. }
  5896. check = check % 11 % 10;
  5897. return (check + '' === value.charAt(length - 1));
  5898. },
  5899. /**
  5900. * Validate Luxembourg VAT number
  5901. * Examples:
  5902. * - Valid: LU15027442
  5903. * - Invalid: LU15027443
  5904. *
  5905. * @param {String} value VAT number
  5906. * @returns {Boolean}
  5907. */
  5908. _lu: function(value) {
  5909. if (/^LU[0-9]{8}$/.test(value)) {
  5910. value = value.substr(2);
  5911. }
  5912. if (!/^[0-9]{8}$/.test(value)) {
  5913. return false;
  5914. }
  5915. return ((parseInt(value.substr(0, 6), 10) % 89) + '' === value.substr(6, 2));
  5916. },
  5917. /**
  5918. * Validate Latvian VAT number
  5919. * Examples:
  5920. * - Valid: LV40003521600, LV16117519997
  5921. * - Invalid: LV40003521601, LV16137519997
  5922. *
  5923. * @param {String} value VAT number
  5924. * @returns {Boolean}
  5925. */
  5926. _lv: function(value) {
  5927. if (/^LV[0-9]{11}$/.test(value)) {
  5928. value = value.substr(2);
  5929. }
  5930. if (!/^[0-9]{11}$/.test(value)) {
  5931. return false;
  5932. }
  5933. var first = parseInt(value.charAt(0), 10),
  5934. sum = 0,
  5935. weight = [],
  5936. i,
  5937. length = value.length;
  5938. if (first > 3) {
  5939. // Legal entity
  5940. sum = 0;
  5941. weight = [9, 1, 4, 8, 3, 10, 2, 5, 7, 6, 1];
  5942. for (i = 0; i < length; i++) {
  5943. sum += parseInt(value.charAt(i), 10) * weight[i];
  5944. }
  5945. sum = sum % 11;
  5946. return (sum === 3);
  5947. } else {
  5948. // Check birth date
  5949. var day = parseInt(value.substr(0, 2), 10),
  5950. month = parseInt(value.substr(2, 2), 10),
  5951. year = parseInt(value.substr(4, 2), 10);
  5952. year = year + 1800 + parseInt(value.charAt(6), 10) * 100;
  5953. if (!$.fn.bootstrapValidator.helpers.date(year, month, day)) {
  5954. return false;
  5955. }
  5956. // Check personal code
  5957. sum = 0;
  5958. weight = [10, 5, 8, 4, 2, 1, 6, 3, 7, 9];
  5959. for (i = 0; i < length - 1; i++) {
  5960. sum += parseInt(value.charAt(i), 10) * weight[i];
  5961. }
  5962. sum = (sum + 1) % 11 % 10;
  5963. return (sum + '' === value.charAt(length - 1));
  5964. }
  5965. },
  5966. /**
  5967. * Validate Maltese VAT number
  5968. * Examples:
  5969. * - Valid: MT11679112
  5970. * - Invalid: MT11679113
  5971. *
  5972. * @param {String} value VAT number
  5973. * @returns {Boolean}
  5974. */
  5975. _mt: function(value) {
  5976. if (/^MT[0-9]{8}$/.test(value)) {
  5977. value = value.substr(2);
  5978. }
  5979. if (!/^[0-9]{8}$/.test(value)) {
  5980. return false;
  5981. }
  5982. var sum = 0,
  5983. weight = [3, 4, 6, 7, 8, 9, 10, 1];
  5984. for (var i = 0; i < 8; i++) {
  5985. sum += parseInt(value.charAt(i), 10) * weight[i];
  5986. }
  5987. return (sum % 37 === 0);
  5988. },
  5989. /**
  5990. * Validate Dutch VAT number
  5991. * Examples:
  5992. * - Valid: NL004495445B01
  5993. * - Invalid: NL123456789B90
  5994. *
  5995. * @param {String} value VAT number
  5996. * @returns {Boolean}
  5997. */
  5998. _nl: function(value) {
  5999. if (/^NL[0-9]{9}B[0-9]{2}$/.test(value)) {
  6000. value = value.substr(2);
  6001. }
  6002. if (!/^[0-9]{9}B[0-9]{2}$/.test(value)) {
  6003. return false;
  6004. }
  6005. var sum = 0,
  6006. weight = [9, 8, 7, 6, 5, 4, 3, 2];
  6007. for (var i = 0; i < 8; i++) {
  6008. sum += parseInt(value.charAt(i), 10) * weight[i];
  6009. }
  6010. sum = sum % 11;
  6011. if (sum > 9) {
  6012. sum = 0;
  6013. }
  6014. return (sum + '' === value.substr(8, 1));
  6015. },
  6016. /**
  6017. * Validate Norwegian VAT number
  6018. *
  6019. * @see http://www.brreg.no/english/coordination/number.html
  6020. * @param {String} value VAT number
  6021. * @returns {Boolean}
  6022. */
  6023. _no: function(value) {
  6024. if (/^NO[0-9]{9}$/.test(value)) {
  6025. value = value.substr(2);
  6026. }
  6027. if (!/^[0-9]{9}$/.test(value)) {
  6028. return false;
  6029. }
  6030. var sum = 0,
  6031. weight = [3, 2, 7, 6, 5, 4, 3, 2];
  6032. for (var i = 0; i < 8; i++) {
  6033. sum += parseInt(value.charAt(i), 10) * weight[i];
  6034. }
  6035. sum = 11 - sum % 11;
  6036. if (sum === 11) {
  6037. sum = 0;
  6038. }
  6039. return (sum + '' === value.substr(8, 1));
  6040. },
  6041. /**
  6042. * Validate Polish VAT number
  6043. * Examples:
  6044. * - Valid: PL8567346215
  6045. * - Invalid: PL8567346216
  6046. *
  6047. * @param {String} value VAT number
  6048. * @returns {Boolean}
  6049. */
  6050. _pl: function(value) {
  6051. if (/^PL[0-9]{10}$/.test(value)) {
  6052. value = value.substr(2);
  6053. }
  6054. if (!/^[0-9]{10}$/.test(value)) {
  6055. return false;
  6056. }
  6057. var sum = 0,
  6058. weight = [6, 5, 7, 2, 3, 4, 5, 6, 7, -1];
  6059. for (var i = 0; i < 10; i++) {
  6060. sum += parseInt(value.charAt(i), 10) * weight[i];
  6061. }
  6062. return (sum % 11 === 0);
  6063. },
  6064. /**
  6065. * Validate Portuguese VAT number
  6066. * Examples:
  6067. * - Valid: PT501964843
  6068. * - Invalid: PT501964842
  6069. *
  6070. * @param {String} value VAT number
  6071. * @returns {Boolean}
  6072. */
  6073. _pt: function(value) {
  6074. if (/^PT[0-9]{9}$/.test(value)) {
  6075. value = value.substr(2);
  6076. }
  6077. if (!/^[0-9]{9}$/.test(value)) {
  6078. return false;
  6079. }
  6080. var sum = 0,
  6081. weight = [9, 8, 7, 6, 5, 4, 3, 2];
  6082. for (var i = 0; i < 8; i++) {
  6083. sum += parseInt(value.charAt(i), 10) * weight[i];
  6084. }
  6085. sum = 11 - sum % 11;
  6086. if (sum > 9) {
  6087. sum = 0;
  6088. }
  6089. return (sum + '' === value.substr(8, 1));
  6090. },
  6091. /**
  6092. * Validate Romanian VAT number
  6093. * Examples:
  6094. * - Valid: RO18547290
  6095. * - Invalid: RO18547291
  6096. *
  6097. * @param {String} value VAT number
  6098. * @returns {Boolean}
  6099. */
  6100. _ro: function(value) {
  6101. if (/^RO[1-9][0-9]{1,9}$/.test(value)) {
  6102. value = value.substr(2);
  6103. }
  6104. if (!/^[1-9][0-9]{1,9}$/.test(value)) {
  6105. return false;
  6106. }
  6107. var length = value.length,
  6108. weight = [7, 5, 3, 2, 1, 7, 5, 3, 2].slice(10 - length),
  6109. sum = 0;
  6110. for (var i = 0; i < length - 1; i++) {
  6111. sum += parseInt(value.charAt(i), 10) * weight[i];
  6112. }
  6113. sum = (10 * sum) % 11 % 10;
  6114. return (sum + '' === value.substr(length - 1, 1));
  6115. },
  6116. /**
  6117. * Validate Russian VAT number (Taxpayer Identification Number - INN)
  6118. *
  6119. * @param {String} value VAT number
  6120. * @returns {Boolean}
  6121. */
  6122. _ru: function(value) {
  6123. if (/^RU([0-9]{9}|[0-9]{12})$/.test(value)) {
  6124. value = value.substr(2);
  6125. }
  6126. if (!/^([0-9]{9}|[0-9]{12})$/.test(value)) {
  6127. return false;
  6128. }
  6129. var i = 0;
  6130. if (value.length === 10) {
  6131. var sum = 0,
  6132. weight = [2, 4, 10, 3, 5, 9, 4, 6, 8, 0];
  6133. for (i = 0; i < 10; i++) {
  6134. sum += parseInt(value.charAt(i), 10) * weight[i];
  6135. }
  6136. sum = sum % 11;
  6137. if (sum > 9) {
  6138. sum = sum % 10;
  6139. }
  6140. return (sum + '' === value.substr(9, 1));
  6141. } else if (value.length === 12) {
  6142. var sum1 = 0,
  6143. weight1 = [7, 2, 4, 10, 3, 5, 9, 4, 6, 8, 0],
  6144. sum2 = 0,
  6145. weight2 = [3, 7, 2, 4, 10, 3, 5, 9, 4, 6, 8, 0];
  6146. for (i = 0; i < 11; i++) {
  6147. sum1 += parseInt(value.charAt(i), 10) * weight1[i];
  6148. sum2 += parseInt(value.charAt(i), 10) * weight2[i];
  6149. }
  6150. sum1 = sum1 % 11;
  6151. if (sum1 > 9) {
  6152. sum1 = sum1 % 10;
  6153. }
  6154. sum2 = sum2 % 11;
  6155. if (sum2 > 9) {
  6156. sum2 = sum2 % 10;
  6157. }
  6158. return (sum1 + '' === value.substr(10, 1) && sum2 + '' === value.substr(11, 1));
  6159. }
  6160. return false;
  6161. },
  6162. /**
  6163. * Validate Serbian VAT number
  6164. *
  6165. * @param {String} value VAT number
  6166. * @returns {Boolean}
  6167. */
  6168. _rs: function(value) {
  6169. if (/^RS[0-9]{9}$/.test(value)) {
  6170. value = value.substr(2);
  6171. }
  6172. if (!/^[0-9]{9}$/.test(value)) {
  6173. return false;
  6174. }
  6175. var sum = 10,
  6176. temp = 0;
  6177. for (var i = 0; i < 8; i++) {
  6178. temp = (parseInt(value.charAt(i), 10) + sum) % 10;
  6179. if (temp === 0) {
  6180. temp = 10;
  6181. }
  6182. sum = (2 * temp) % 11;
  6183. }
  6184. return ((sum + parseInt(value.substr(8, 1), 10)) % 10 === 1);
  6185. },
  6186. /**
  6187. * Validate Swedish VAT number
  6188. * Examples:
  6189. * - Valid: SE123456789701
  6190. * - Invalid: SE123456789101
  6191. *
  6192. * @param {String} value VAT number
  6193. * @returns {Boolean}
  6194. */
  6195. _se: function(value) {
  6196. if (/^SE[0-9]{10}01$/.test(value)) {
  6197. value = value.substr(2);
  6198. }
  6199. if (!/^[0-9]{10}01$/.test(value)) {
  6200. return false;
  6201. }
  6202. value = value.substr(0, 10);
  6203. return $.fn.bootstrapValidator.helpers.luhn(value);
  6204. },
  6205. /**
  6206. * Validate Slovenian VAT number
  6207. * Examples:
  6208. * - Valid: SI50223054
  6209. * - Invalid: SI50223055
  6210. *
  6211. * @param {String} value VAT number
  6212. * @returns {Boolean}
  6213. */
  6214. _si: function(value) {
  6215. if (/^SI[0-9]{8}$/.test(value)) {
  6216. value = value.substr(2);
  6217. }
  6218. if (!/^[0-9]{8}$/.test(value)) {
  6219. return false;
  6220. }
  6221. var sum = 0,
  6222. weight = [8, 7, 6, 5, 4, 3, 2];
  6223. for (var i = 0; i < 7; i++) {
  6224. sum += parseInt(value.charAt(i), 10) * weight[i];
  6225. }
  6226. sum = 11 - sum % 11;
  6227. if (sum === 10) {
  6228. sum = 0;
  6229. }
  6230. return (sum + '' === value.substr(7, 1));
  6231. },
  6232. /**
  6233. * Validate Slovak VAT number
  6234. * Examples:
  6235. * - Valid: SK2022749619
  6236. * - Invalid: SK2022749618
  6237. *
  6238. * @param {String} value VAT number
  6239. * @returns {Boolean}
  6240. */
  6241. _sk: function(value) {
  6242. if (/^SK[1-9][0-9][(2-4)|(6-9)][0-9]{7}$/.test(value)) {
  6243. value = value.substr(2);
  6244. }
  6245. if (!/^[1-9][0-9][(2-4)|(6-9)][0-9]{7}$/.test(value)) {
  6246. return false;
  6247. }
  6248. return (parseInt(value, 10) % 11 === 0);
  6249. },
  6250. /**
  6251. * Validate Venezuelan VAT number (RIF)
  6252. * Examples:
  6253. * - Valid: VEJ309272292, VEV242818101, VEJ000126518, VEJ000458324, J309272292, V242818101, J000126518, J000458324
  6254. * - Invalid: VEJ309272293, VEV242818100, J000126519, J000458323
  6255. *
  6256. * @param {String} value VAT number
  6257. * @returns {Boolean}
  6258. */
  6259. _ve: function(value) {
  6260. if (/^VE[VEJPG][0-9]{9}$/.test(value)) {
  6261. value = value.substr(2);
  6262. }
  6263. if (!/^[VEJPG][0-9]{9}$/.test(value)) {
  6264. return false;
  6265. }
  6266. var types = {
  6267. 'V': 4,
  6268. 'E': 8,
  6269. 'J': 12,
  6270. 'P': 16,
  6271. 'G': 20
  6272. },
  6273. sum = types[value.charAt(0)],
  6274. weight = [3, 2, 7, 6, 5, 4, 3, 2];
  6275. for (var i = 0; i < 8; i++) {
  6276. sum += parseInt(value.charAt(i + 1), 10) * weight[i];
  6277. }
  6278. sum = 11 - sum % 11;
  6279. if (sum === 11 || sum === 10) {
  6280. sum = 0;
  6281. }
  6282. return (sum + '' === value.substr(9, 1));
  6283. },
  6284. /**
  6285. * Validate South African VAT number
  6286. * Examples:
  6287. * - Valid: 4012345678
  6288. * - Invalid: 40123456789, 3012345678
  6289. *
  6290. * @params {String} value VAT number
  6291. * @returns {Boolean}
  6292. */
  6293. _za: function(value) {
  6294. if (/^ZA4[0-9]{9}$/.test(value)) {
  6295. value = value.substr(2);
  6296. }
  6297. return /^4[0-9]{9}$/.test(value);
  6298. }
  6299. };
  6300. }(window.jQuery));
  6301. ;(function($) {
  6302. $.fn.bootstrapValidator.i18n.vin = $.extend($.fn.bootstrapValidator.i18n.vin || {}, {
  6303. 'default': 'Please enter a valid VIN number'
  6304. });
  6305. $.fn.bootstrapValidator.validators.vin = {
  6306. /**
  6307. * Validate an US VIN (Vehicle Identification Number)
  6308. *
  6309. * @param {BootstrapValidator} validator The validator plugin instance
  6310. * @param {jQuery} $field Field element
  6311. * @param {Object} options Consist of key:
  6312. * - message: The invalid message
  6313. * @returns {Boolean}
  6314. */
  6315. validate: function(validator, $field, options) {
  6316. var value = $field.val();
  6317. if (value === '') {
  6318. return true;
  6319. }
  6320. // Don't accept I, O, Q characters
  6321. if (!/^[a-hj-npr-z0-9]{8}[0-9xX][a-hj-npr-z0-9]{8}$/i.test(value)) {
  6322. return false;
  6323. }
  6324. value = value.toUpperCase();
  6325. var chars = {
  6326. A: 1, B: 2, C: 3, D: 4, E: 5, F: 6, G: 7, H: 8,
  6327. J: 1, K: 2, L: 3, M: 4, N: 5, P: 7, R: 9,
  6328. S: 2, T: 3, U: 4, V: 5, W: 6, X: 7, Y: 8, Z: 9,
  6329. '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '0': 0
  6330. },
  6331. weights = [8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2],
  6332. sum = 0,
  6333. length = value.length;
  6334. for (var i = 0; i < length; i++) {
  6335. sum += chars[value.charAt(i) + ''] * weights[i];
  6336. }
  6337. var reminder = sum % 11;
  6338. if (reminder === 10) {
  6339. reminder = 'X';
  6340. }
  6341. return (reminder + '') === value.charAt(8);
  6342. }
  6343. };
  6344. }(window.jQuery));
  6345. ;(function($) {
  6346. $.fn.bootstrapValidator.i18n.zipCode = $.extend($.fn.bootstrapValidator.i18n.zipCode || {}, {
  6347. 'default': 'Please enter a valid postal code',
  6348. countryNotSupported: 'The country code %s is not supported',
  6349. country: 'Please enter a valid postal code in %s',
  6350. countries: {
  6351. BR: 'Brazil',
  6352. CA: 'Canada',
  6353. DK: 'Denmark',
  6354. GB: 'United Kingdom',
  6355. IT: 'Italy',
  6356. MA: 'Morocco',
  6357. NL: 'Netherlands',
  6358. RO: 'Romania',
  6359. SE: 'Sweden',
  6360. SG: 'Singapore',
  6361. US: 'USA'
  6362. }
  6363. });
  6364. $.fn.bootstrapValidator.validators.zipCode = {
  6365. html5Attributes: {
  6366. message: 'message',
  6367. country: 'country'
  6368. },
  6369. COUNTRY_CODES: ['BR', 'CA', 'DK', 'GB', 'IT', 'MA', 'NL', 'RO', 'SE', 'SG', 'US'],
  6370. /**
  6371. * Return true if and only if the input value is a valid country zip code
  6372. *
  6373. * @param {BootstrapValidator} validator The validator plugin instance
  6374. * @param {jQuery} $field Field element
  6375. * @param {Object} options Consist of key:
  6376. * - message: The invalid message
  6377. * - country: The country
  6378. *
  6379. * The country can be defined by:
  6380. * - An ISO 3166 country code
  6381. * - Name of field which its value defines the country code
  6382. * - Name of callback function that returns the country code
  6383. * - A callback function that returns the country code
  6384. *
  6385. * callback: function(value, validator, $field) {
  6386. * // value is the value of field
  6387. * // validator is the BootstrapValidator instance
  6388. * // $field is jQuery element representing the field
  6389. * }
  6390. *
  6391. * @returns {Boolean|Object}
  6392. */
  6393. validate: function(validator, $field, options) {
  6394. var value = $field.val();
  6395. if (value === '' || !options.country) {
  6396. return true;
  6397. }
  6398. var country = options.country;
  6399. if (typeof country !== 'string' || $.inArray(country, this.COUNTRY_CODES) === -1) {
  6400. // Try to determine the country
  6401. country = validator.getDynamicOption($field, country);
  6402. }
  6403. if (!country || $.inArray(country.toUpperCase(), this.COUNTRY_CODES) === -1) {
  6404. return { valid: false, message: $.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.zipCode.countryNotSupported, country) };
  6405. }
  6406. var isValid = false;
  6407. country = country.toUpperCase();
  6408. switch (country) {
  6409. case 'BR':
  6410. isValid = /^(\d{2})([\.]?)(\d{3})([\-]?)(\d{3})$/.test(value);
  6411. break;
  6412. case 'CA':
  6413. isValid = /^(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|X|Y){1}[0-9]{1}(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|W|X|Y|Z){1}\s?[0-9]{1}(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|W|X|Y|Z){1}[0-9]{1}$/i.test(value);
  6414. break;
  6415. case 'DK':
  6416. isValid = /^(DK(-|\s)?)?\d{4}$/i.test(value);
  6417. break;
  6418. case 'GB':
  6419. isValid = this._gb(value);
  6420. break;
  6421. // http://en.wikipedia.org/wiki/List_of_postal_codes_in_Italy
  6422. case 'IT':
  6423. isValid = /^(I-|IT-)?\d{5}$/i.test(value);
  6424. break;
  6425. // http://en.wikipedia.org/wiki/List_of_postal_codes_in_Morocco
  6426. case 'MA':
  6427. isValid = /^[1-9][0-9]{4}$/i.test(value);
  6428. break;
  6429. // http://en.wikipedia.org/wiki/Postal_codes_in_the_Netherlands
  6430. case 'NL':
  6431. isValid = /^[1-9][0-9]{3} ?(?!sa|sd|ss)[a-z]{2}$/i.test(value);
  6432. break;
  6433. case 'RO':
  6434. isValid = /^(0[1-8]{1}|[1-9]{1}[0-5]{1})?[0-9]{4}$/i.test(value);
  6435. break;
  6436. case 'SE':
  6437. isValid = /^(S-)?\d{3}\s?\d{2}$/i.test(value);
  6438. break;
  6439. case 'SG':
  6440. isValid = /^([0][1-9]|[1-6][0-9]|[7]([0-3]|[5-9])|[8][0-2])(\d{4})$/i.test(value);
  6441. break;
  6442. case 'US':
  6443. /* falls through */
  6444. default:
  6445. isValid = /^\d{4,5}([\-]?\d{4})?$/.test(value);
  6446. break;
  6447. }
  6448. return {
  6449. valid: isValid,
  6450. message: $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.zipCode.country, $.fn.bootstrapValidator.i18n.zipCode.countries[country])
  6451. };
  6452. },
  6453. /**
  6454. * Validate United Kingdom postcode
  6455. * Examples:
  6456. * - Standard: EC1A 1BB, W1A 1HQ, M1 1AA, B33 8TH, CR2 6XH, DN55 1PT
  6457. * - Special cases:
  6458. * AI-2640, ASCN 1ZZ, GIR 0AA
  6459. *
  6460. * @see http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
  6461. * @param {String} value The postcode
  6462. * @returns {Boolean}
  6463. */
  6464. _gb: function(value) {
  6465. var firstChar = '[ABCDEFGHIJKLMNOPRSTUWYZ]', // Does not accept QVX
  6466. secondChar = '[ABCDEFGHKLMNOPQRSTUVWXY]', // Does not accept IJZ
  6467. thirdChar = '[ABCDEFGHJKPMNRSTUVWXY]',
  6468. fourthChar = '[ABEHMNPRVWXY]',
  6469. fifthChar = '[ABDEFGHJLNPQRSTUWXYZ]',
  6470. regexps = [
  6471. // AN NAA, ANN NAA, AAN NAA, AANN NAA format
  6472. new RegExp('^(' + firstChar + '{1}' + secondChar + '?[0-9]{1,2})(\\s*)([0-9]{1}' + fifthChar + '{2})$', 'i'),
  6473. // ANA NAA
  6474. new RegExp('^(' + firstChar + '{1}[0-9]{1}' + thirdChar + '{1})(\\s*)([0-9]{1}' + fifthChar + '{2})$', 'i'),
  6475. // AANA NAA
  6476. new RegExp('^(' + firstChar + '{1}' + secondChar + '{1}?[0-9]{1}' + fourthChar + '{1})(\\s*)([0-9]{1}' + fifthChar + '{2})$', 'i'),
  6477. new RegExp('^(BF1)(\\s*)([0-6]{1}[ABDEFGHJLNPQRST]{1}[ABDEFGHJLNPQRSTUWZYZ]{1})$', 'i'), // BFPO postcodes
  6478. /^(GIR)(\s*)(0AA)$/i, // Special postcode GIR 0AA
  6479. /^(BFPO)(\s*)([0-9]{1,4})$/i, // Standard BFPO numbers
  6480. /^(BFPO)(\s*)(c\/o\s*[0-9]{1,3})$/i, // c/o BFPO numbers
  6481. /^([A-Z]{4})(\s*)(1ZZ)$/i, // Overseas Territories
  6482. /^(AI-2640)$/i // Anguilla
  6483. ];
  6484. for (var i = 0; i < regexps.length; i++) {
  6485. if (regexps[i].test(value)) {
  6486. return true;
  6487. }
  6488. }
  6489. return false;
  6490. }
  6491. };
  6492. }(window.jQuery));