bootstrapValidator.js 241 KB

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