bootstrapValidator.js 273 KB

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