bootstrapValidator.js 255 KB

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