bootstrapValidator.js 255 KB

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