bootstrapValidator.js 245 KB

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