formValidation.js 374 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620
  1. /*!
  2. * FormValidation (http://formvalidation.io)
  3. * The best jQuery plugin to validate form fields. Support Bootstrap, Foundation, Pure, SemanticUI, UIKit and custom frameworks
  4. *
  5. * @version v0.8.1, built on 2016-07-29 1:10:54 AM
  6. * @author https://twitter.com/formvalidation
  7. * @copyright (c) 2013 - 2016 Nguyen Huu Phuoc
  8. * @license http://formvalidation.io/license/
  9. */
  10. // Register the namespace
  11. window.FormValidation = {
  12. AddOn: {}, // Add-ons
  13. Framework: {}, // Supported frameworks
  14. I18n: {}, // i18n
  15. Validator: {} // Available validators
  16. };
  17. if (typeof jQuery === 'undefined') {
  18. throw new Error('FormValidation requires jQuery');
  19. }
  20. (function($) {
  21. var version = $.fn.jquery.split(' ')[0].split('.');
  22. if ((+version[0] < 2 && +version[1] < 9) || (+version[0] === 1 && +version[1] === 9 && +version[2] < 1)) {
  23. throw new Error('FormValidation requires jQuery version 1.9.1 or higher');
  24. }
  25. }(jQuery));
  26. (function($) {
  27. // TODO: Remove backward compatibility
  28. /**
  29. * Constructor
  30. *
  31. * @param {jQuery|String} form The form element or selector
  32. * @param {Object} options The options
  33. * @param {String} [namespace] The optional namespace which is used for data-{namespace}-xxx attributes and internal data.
  34. * Currently, it's used to support backward version
  35. * @constructor
  36. */
  37. FormValidation.Base = function(form, options, namespace) {
  38. this.$form = $(form);
  39. this.options = $.extend({}, $.fn.formValidation.DEFAULT_OPTIONS, options);
  40. this._namespace = namespace || 'fv';
  41. this.$invalidFields = $([]); // Array of invalid fields
  42. this.$submitButton = null; // The submit button which is clicked to submit form
  43. this.$hiddenButton = null;
  44. // Validating status
  45. this.STATUS_NOT_VALIDATED = 'NOT_VALIDATED';
  46. this.STATUS_VALIDATING = 'VALIDATING';
  47. this.STATUS_INVALID = 'INVALID';
  48. this.STATUS_VALID = 'VALID';
  49. this.STATUS_IGNORED = 'IGNORED';
  50. // Default message
  51. this.DEFAULT_MESSAGE = $.fn.formValidation.DEFAULT_MESSAGE;
  52. // Determine the event that is fired when user change the field value
  53. // Most modern browsers supports input event except IE 7, 8.
  54. // IE 9 supports input event but the event is still not fired if I press the backspace key.
  55. // Get IE version
  56. // https://gist.github.com/padolsey/527683/#comment-7595
  57. this._ieVersion = (function() {
  58. var v = 3, div = document.createElement('div'), a = div.all || [];
  59. while (div.innerHTML = '<!--[if gt IE '+(++v)+']><br><![endif]-->', a[0]) {}
  60. return v > 4 ? v : document.documentMode;
  61. }());
  62. var el = document.createElement('div');
  63. this._changeEvent = (this._ieVersion === 9 || !('oninput' in el)) ? 'keyup' : 'input';
  64. // The flag to indicate that the form is ready to submit when a remote/callback validator returns
  65. this._submitIfValid = null;
  66. // Field elements
  67. this._cacheFields = {};
  68. this._init();
  69. };
  70. FormValidation.Base.prototype = {
  71. constructor: FormValidation.Base,
  72. /**
  73. * Check if the number of characters of field value exceed the threshold or not
  74. *
  75. * @param {jQuery} $field The field element
  76. * @returns {Boolean}
  77. */
  78. _exceedThreshold: function($field) {
  79. var ns = this._namespace,
  80. field = $field.attr('data-' + ns + '-field'),
  81. threshold = this.options.fields[field].threshold || this.options.threshold;
  82. if (!threshold) {
  83. return true;
  84. }
  85. var cannotType = $.inArray($field.attr('type'), ['button', 'checkbox', 'file', 'hidden', 'image', 'radio', 'reset', 'submit']) !== -1;
  86. return (cannotType || $field.val().length >= threshold);
  87. },
  88. /**
  89. * Init form
  90. */
  91. _init: function() {
  92. var that = this,
  93. ns = this._namespace,
  94. options = {
  95. addOns: {},
  96. autoFocus: this.$form.attr('data-' + ns + '-autofocus'),
  97. button: {
  98. selector: this.$form.attr('data-' + ns + '-button-selector') || this.$form.attr('data-' + ns + '-submitbuttons'), // Support backward
  99. disabled: this.$form.attr('data-' + ns + '-button-disabled')
  100. },
  101. control: {
  102. valid: this.$form.attr('data-' + ns + '-control-valid'),
  103. invalid: this.$form.attr('data-' + ns + '-control-invalid')
  104. },
  105. err: {
  106. clazz: this.$form.attr('data-' + ns + '-err-clazz'),
  107. container: this.$form.attr('data-' + ns + '-err-container') || this.$form.attr('data-' + ns + '-container'), // Support backward
  108. parent: this.$form.attr('data-' + ns + '-err-parent')
  109. },
  110. events: {
  111. formInit: this.$form.attr('data-' + ns + '-events-form-init'),
  112. formPreValidate: this.$form.attr('data-' + ns + '-events-form-prevalidate'),
  113. formError: this.$form.attr('data-' + ns + '-events-form-error'),
  114. formReset: this.$form.attr('data-' + ns + '-events-form-reset'),
  115. formSuccess: this.$form.attr('data-' + ns + '-events-form-success'),
  116. fieldAdded: this.$form.attr('data-' + ns + '-events-field-added'),
  117. fieldRemoved: this.$form.attr('data-' + ns + '-events-field-removed'),
  118. fieldInit: this.$form.attr('data-' + ns + '-events-field-init'),
  119. fieldError: this.$form.attr('data-' + ns + '-events-field-error'),
  120. fieldReset: this.$form.attr('data-' + ns + '-events-field-reset'),
  121. fieldSuccess: this.$form.attr('data-' + ns + '-events-field-success'),
  122. fieldStatus: this.$form.attr('data-' + ns + '-events-field-status'),
  123. localeChanged: this.$form.attr('data-' + ns + '-events-locale-changed'),
  124. validatorError: this.$form.attr('data-' + ns + '-events-validator-error'),
  125. validatorSuccess: this.$form.attr('data-' + ns + '-events-validator-success'),
  126. validatorIgnored: this.$form.attr('data-' + ns + '-events-validator-ignored')
  127. },
  128. excluded: this.$form.attr('data-' + ns + '-excluded'),
  129. icon: {
  130. valid: this.$form.attr('data-' + ns + '-icon-valid') || this.$form.attr('data-' + ns + '-feedbackicons-valid'), // Support backward
  131. invalid: this.$form.attr('data-' + ns + '-icon-invalid') || this.$form.attr('data-' + ns + '-feedbackicons-invalid'), // Support backward
  132. validating: this.$form.attr('data-' + ns + '-icon-validating') || this.$form.attr('data-' + ns + '-feedbackicons-validating'), // Support backward
  133. feedback: this.$form.attr('data-' + ns + '-icon-feedback')
  134. },
  135. live: this.$form.attr('data-' + ns + '-live'),
  136. locale: this.$form.attr('data-' + ns + '-locale'),
  137. message: this.$form.attr('data-' + ns + '-message'),
  138. onPreValidate: this.$form.attr('data-' + ns + '-onprevalidate'),
  139. onError: this.$form.attr('data-' + ns + '-onerror'),
  140. onReset: this.$form.attr('data-' + ns + '-onreset'),
  141. onSuccess: this.$form.attr('data-' + ns + '-onsuccess'),
  142. row: {
  143. selector: this.$form.attr('data-' + ns + '-row-selector') || this.$form.attr('data-' + ns + '-group'), // Support backward
  144. valid: this.$form.attr('data-' + ns + '-row-valid'),
  145. invalid: this.$form.attr('data-' + ns + '-row-invalid'),
  146. feedback: this.$form.attr('data-' + ns + '-row-feedback')
  147. },
  148. threshold: this.$form.attr('data-' + ns + '-threshold'),
  149. trigger: this.$form.attr('data-' + ns + '-trigger'),
  150. verbose: this.$form.attr('data-' + ns + '-verbose'),
  151. fields: {}
  152. };
  153. this.$form
  154. // Disable client side validation in HTML 5
  155. .attr('novalidate', 'novalidate')
  156. .addClass(this.options.elementClass)
  157. // Disable the default submission first
  158. .on('submit.' + ns, function(e) {
  159. e.preventDefault();
  160. that.validate();
  161. })
  162. .on('click.' + ns, this.options.button.selector, function() {
  163. that.$submitButton = $(this);
  164. // The user just click the submit button
  165. that._submitIfValid = true;
  166. });
  167. if (this.options.declarative === true || this.options.declarative === 'true') {
  168. // Find all fields which have either "name" or "data-{namespace}-field" attribute
  169. this.$form
  170. .find('[name], [data-' + ns + '-field]')
  171. .each(function() {
  172. var $field = $(this),
  173. field = $field.attr('name') || $field.attr('data-' + ns + '-field'),
  174. opts = that._parseOptions($field);
  175. if (opts) {
  176. $field.attr('data-' + ns + '-field', field);
  177. options.fields[field] = $.extend({}, opts, options.fields[field]);
  178. }
  179. });
  180. }
  181. this.options = $.extend(true, this.options, options);
  182. // Normalize the err.parent option
  183. if ('string' === typeof this.options.err.parent) {
  184. this.options.err.parent = new RegExp(this.options.err.parent);
  185. }
  186. // Support backward
  187. if (this.options.container) {
  188. this.options.err.container = this.options.container;
  189. delete this.options.container;
  190. }
  191. if (this.options.feedbackIcons) {
  192. this.options.icon = $.extend(true, this.options.icon, this.options.feedbackIcons);
  193. delete this.options.feedbackIcons;
  194. }
  195. if (this.options.group) {
  196. this.options.row.selector = this.options.group;
  197. delete this.options.group;
  198. }
  199. if (this.options.submitButtons) {
  200. this.options.button.selector = this.options.submitButtons;
  201. delete this.options.submitButtons;
  202. }
  203. // If the locale is not found, reset it to default one
  204. if (!FormValidation.I18n[this.options.locale]) {
  205. this.options.locale = $.fn.formValidation.DEFAULT_OPTIONS.locale;
  206. }
  207. // Parse the add-on options from HTML attributes
  208. if (this.options.declarative === true || this.options.declarative === 'true') {
  209. this.options = $.extend(true, this.options, { addOns: this._parseAddOnOptions() });
  210. }
  211. // When pressing Enter on any field in the form, the first submit button will do its job.
  212. // The form then will be submitted.
  213. // I create a first hidden submit button
  214. this.$hiddenButton = $('<button/>')
  215. .attr('type', 'submit')
  216. .prependTo(this.$form)
  217. .addClass('fv-hidden-submit')
  218. .css({ display: 'none', width: 0, height: 0 });
  219. this.$form
  220. .on('click.' + this._namespace, '[type="submit"]', function(e) {
  221. // #746: Check if the button click handler returns false
  222. if (!e.isDefaultPrevented()) {
  223. var $target = $(e.target),
  224. // The button might contain HTML tag
  225. $button = $target.is('[type="submit"]') ? $target.eq(0) : $target.parent('[type="submit"]').eq(0);
  226. // Don't perform validation when clicking on the submit button/input which
  227. // aren't defined by the 'button.selector' option
  228. if (that.options.button.selector && !$button.is(that.options.button.selector) && !$button.is(that.$hiddenButton)) {
  229. that.$form.off('submit.' + that._namespace).submit();
  230. // Fix the issue where 'formnovalidate' causes IE to send two postbacks to server
  231. return false;
  232. }
  233. }
  234. });
  235. for (var field in this.options.fields) {
  236. this._initField(field);
  237. }
  238. // Init the add-ons
  239. for (var addOn in this.options.addOns) {
  240. if ('function' === typeof FormValidation.AddOn[addOn].init) {
  241. FormValidation.AddOn[addOn].init(this, this.options.addOns[addOn]);
  242. }
  243. }
  244. this.$form.trigger($.Event(this.options.events.formInit), {
  245. bv: this, // Support backward
  246. fv: this,
  247. options: this.options
  248. });
  249. // Prepare the events
  250. if (this.options.onPreValidate) {
  251. this.$form.on(this.options.events.formPreValidate, function(e) {
  252. FormValidation.Helper.call(that.options.onPreValidate, [e]);
  253. });
  254. }
  255. if (this.options.onSuccess) {
  256. this.$form.on(this.options.events.formSuccess, function(e) {
  257. FormValidation.Helper.call(that.options.onSuccess, [e]);
  258. });
  259. }
  260. if (this.options.onError) {
  261. this.$form.on(this.options.events.formError, function(e) {
  262. FormValidation.Helper.call(that.options.onError, [e]);
  263. });
  264. }
  265. if (this.options.onReset) {
  266. this.$form.on(this.options.events.formReset, function(e) {
  267. FormValidation.Helper.call(that.options.onReset, [e]);
  268. });
  269. }
  270. },
  271. /**
  272. * Init field
  273. *
  274. * @param {String|jQuery} field The field name or field element
  275. */
  276. _initField: function(field) {
  277. var ns = this._namespace,
  278. fields = $([]);
  279. switch (typeof field) {
  280. case 'object':
  281. fields = field;
  282. field = field.attr('data-' + ns + '-field');
  283. break;
  284. case 'string':
  285. fields = this.getFieldElements(field);
  286. fields.attr('data-' + ns + '-field', field);
  287. break;
  288. default:
  289. break;
  290. }
  291. // We don't need to validate non-existing fields
  292. if (fields.length === 0) {
  293. return;
  294. }
  295. if (this.options.fields[field] === null || this.options.fields[field].validators === null) {
  296. return;
  297. }
  298. var validators = this.options.fields[field].validators,
  299. validatorName,
  300. alias;
  301. for (validatorName in validators) {
  302. alias = validators[validatorName].alias || validatorName;
  303. if (!FormValidation.Validator[alias]) {
  304. delete this.options.fields[field].validators[validatorName];
  305. }
  306. }
  307. if (this.options.fields[field].enabled === null) {
  308. this.options.fields[field].enabled = true;
  309. }
  310. var that = this,
  311. total = fields.length,
  312. type = fields.attr('type'),
  313. updateAll = (total === 1) || ('radio' === type) || ('checkbox' === type),
  314. trigger = this._getFieldTrigger(fields.eq(0)),
  315. clazz = this.options.err.clazz.split(' ').join('.'),
  316. events = $.map(trigger, function(item) {
  317. return item + '.update.' + ns;
  318. }).join(' ');
  319. for (var i = 0; i < total; i++) {
  320. var $field = fields.eq(i),
  321. row = this.options.fields[field].row || this.options.row.selector,
  322. $parent = $field.closest(row),
  323. // Allow user to indicate where the error messages are shown
  324. // Support backward
  325. container = ('function' === typeof (this.options.fields[field].container || this.options.fields[field].err || this.options.err.container))
  326. ? (this.options.fields[field].container || this.options.fields[field].err || this.options.err.container).call(this, $field, this)
  327. : (this.options.fields[field].container || this.options.fields[field].err || this.options.err.container),
  328. $message = (container && container !== 'tooltip' && container !== 'popover') ? $(container) : this._getMessageContainer($field, row);
  329. if (container && container !== 'tooltip' && container !== 'popover') {
  330. $message.addClass(this.options.err.clazz);
  331. }
  332. // Remove all error messages and feedback icons
  333. $message.find('.' + clazz + '[data-' + ns + '-validator][data-' + ns + '-for="' + field + '"]').remove();
  334. $parent.find('i[data-' + ns + '-icon-for="' + field + '"]').remove();
  335. // Whenever the user change the field value, mark it as not validated yet
  336. $field.off(events).on(events, function() {
  337. that.updateStatus($(this), that.STATUS_NOT_VALIDATED);
  338. });
  339. // Create help block elements for showing the error messages
  340. $field.data(ns + '.messages', $message);
  341. for (validatorName in validators) {
  342. $field.data(ns + '.result.' + validatorName, this.STATUS_NOT_VALIDATED);
  343. if (!updateAll || i === total - 1) {
  344. $('<small/>')
  345. .css('display', 'none')
  346. .addClass(this.options.err.clazz)
  347. .attr('data-' + ns + '-validator', validatorName)
  348. .attr('data-' + ns + '-for', field)
  349. .attr('data-' + ns + '-result', this.STATUS_NOT_VALIDATED)
  350. .html(this._getMessage(field, validatorName))
  351. .appendTo($message);
  352. }
  353. // Init the validator
  354. alias = validators[validatorName].alias || validatorName;
  355. if ('function' === typeof FormValidation.Validator[alias].init) {
  356. FormValidation.Validator[alias].init(this, $field, this.options.fields[field].validators[validatorName], validatorName);
  357. }
  358. }
  359. // Prepare the feedback icons
  360. if (this.options.fields[field].icon !== false && this.options.fields[field].icon !== 'false'
  361. && this.options.icon
  362. && this.options.icon.valid && this.options.icon.invalid && this.options.icon.validating
  363. && (!updateAll || i === total - 1))
  364. {
  365. // $parent.removeClass(this.options.row.valid).removeClass(this.options.row.invalid).addClass(this.options.row.feedback);
  366. // Keep error messages which are populated from back-end
  367. $parent.addClass(this.options.row.feedback);
  368. var $icon = $('<i/>')
  369. .css('display', 'none')
  370. .addClass(this.options.icon.feedback)
  371. .attr('data-' + ns + '-icon-for', field)
  372. .insertAfter($field);
  373. // Store the icon as a data of field element
  374. (!updateAll ? $field : fields).data(ns + '.icon', $icon);
  375. if ('tooltip' === container || 'popover' === container) {
  376. (!updateAll ? $field : fields)
  377. .on(this.options.events.fieldError, function() {
  378. $parent.addClass('fv-has-tooltip');
  379. })
  380. .on(this.options.events.fieldSuccess, function() {
  381. $parent.removeClass('fv-has-tooltip');
  382. });
  383. $field
  384. // Show tooltip/popover message when field gets focus
  385. .off('focus.container.' + ns)
  386. .on('focus.container.' + ns, function() {
  387. that._showTooltip($(this), container);
  388. })
  389. // and hide them when losing focus
  390. .off('blur.container.' + ns)
  391. .on('blur.container.' + ns, function() {
  392. that._hideTooltip($(this), container);
  393. });
  394. }
  395. if ('string' === typeof this.options.fields[field].icon && this.options.fields[field].icon !== 'true') {
  396. $icon.appendTo($(this.options.fields[field].icon));
  397. } else {
  398. this._fixIcon($field, $icon);
  399. }
  400. }
  401. }
  402. // Sort the validators by priority
  403. var sortedByPriority = [];
  404. for (validatorName in validators) {
  405. alias = validators[validatorName].alias || validatorName;
  406. // Determine the priority
  407. validators[validatorName].priority = parseInt(validators[validatorName].priority
  408. || FormValidation.Validator[alias].priority
  409. || 1, 10);
  410. sortedByPriority.push({
  411. validator: validatorName,
  412. priority: validators[validatorName].priority
  413. });
  414. }
  415. sortedByPriority = sortedByPriority.sort(function(a, b) {
  416. return a.priority - b.priority;
  417. });
  418. // Prepare the events
  419. fields
  420. .data(ns + '.validators', sortedByPriority)
  421. .on(this.options.events.fieldSuccess, function(e, data) {
  422. var onSuccess = that.getOptions(data.field, null, 'onSuccess');
  423. if (onSuccess) {
  424. FormValidation.Helper.call(onSuccess, [e, data]);
  425. }
  426. })
  427. .on(this.options.events.fieldError, function(e, data) {
  428. var onError = that.getOptions(data.field, null, 'onError');
  429. if (onError) {
  430. FormValidation.Helper.call(onError, [e, data]);
  431. }
  432. })
  433. .on(this.options.events.fieldReset, function(e, data) {
  434. var onReset = that.getOptions(data.field, null, 'onReset');
  435. if (onReset) {
  436. FormValidation.Helper.call(onReset, [e, data]);
  437. }
  438. })
  439. .on(this.options.events.fieldStatus, function(e, data) {
  440. var onStatus = that.getOptions(data.field, null, 'onStatus');
  441. if (onStatus) {
  442. FormValidation.Helper.call(onStatus, [e, data]);
  443. }
  444. })
  445. .on(this.options.events.validatorError, function(e, data) {
  446. var onError = that.getOptions(data.field, data.validator, 'onError');
  447. if (onError) {
  448. FormValidation.Helper.call(onError, [e, data]);
  449. }
  450. })
  451. .on(this.options.events.validatorIgnored, function(e, data) {
  452. var onIgnored = that.getOptions(data.field, data.validator, 'onIgnored');
  453. if (onIgnored) {
  454. FormValidation.Helper.call(onIgnored, [e, data]);
  455. }
  456. })
  457. .on(this.options.events.validatorSuccess, function(e, data) {
  458. var onSuccess = that.getOptions(data.field, data.validator, 'onSuccess');
  459. if (onSuccess) {
  460. FormValidation.Helper.call(onSuccess, [e, data]);
  461. }
  462. });
  463. // Set live mode
  464. this.onLiveChange(fields, 'live', function() {
  465. if (that._exceedThreshold($(this))) {
  466. that.validateField($(this));
  467. }
  468. });
  469. fields.trigger($.Event(this.options.events.fieldInit), {
  470. bv: this, // Support backward
  471. fv: this,
  472. field: field,
  473. element: fields
  474. });
  475. },
  476. /**
  477. * Check if the field is excluded.
  478. * Returning true means that the field will not be validated
  479. *
  480. * @param {jQuery} $field The field element
  481. * @returns {Boolean}
  482. */
  483. _isExcluded: function($field) {
  484. var ns = this._namespace,
  485. excludedAttr = $field.attr('data-' + ns + '-excluded'),
  486. // I still need to check the 'name' attribute while initializing the field
  487. field = $field.attr('data-' + ns + '-field') || $field.attr('name');
  488. switch (true) {
  489. case (!!field && this.options.fields && this.options.fields[field] && (this.options.fields[field].excluded === 'true' || this.options.fields[field].excluded === true)):
  490. case (excludedAttr === 'true'):
  491. case (excludedAttr === ''):
  492. return true;
  493. case (!!field && this.options.fields && this.options.fields[field] && (this.options.fields[field].excluded === 'false' || this.options.fields[field].excluded === false)):
  494. case (excludedAttr === 'false'):
  495. return false;
  496. case (!!field && this.options.fields && this.options.fields[field] && 'function' === typeof this.options.fields[field].excluded):
  497. return this.options.fields[field].excluded.call(this, $field, this);
  498. case (!!field && this.options.fields && this.options.fields[field] && 'string' === typeof this.options.fields[field].excluded):
  499. case (excludedAttr):
  500. return FormValidation.Helper.call(this.options.fields[field].excluded, [$field, this]);
  501. default:
  502. if (this.options.excluded) {
  503. // Convert to array first
  504. if ('string' === typeof this.options.excluded) {
  505. this.options.excluded = $.map(this.options.excluded.split(','), function(item) {
  506. // Trim the spaces
  507. return $.trim(item);
  508. });
  509. }
  510. var length = this.options.excluded.length;
  511. for (var i = 0; i < length; i++) {
  512. if (('string' === typeof this.options.excluded[i] && $field.is(this.options.excluded[i]))
  513. || ('function' === typeof this.options.excluded[i] && this.options.excluded[i].call(this, $field, this) === true))
  514. {
  515. return true;
  516. }
  517. }
  518. }
  519. return false;
  520. }
  521. },
  522. /**
  523. * Get a field changed trigger event
  524. *
  525. * @param {jQuery} $field The field element
  526. * @returns {String[]} The event names triggered on field change
  527. */
  528. _getFieldTrigger: function($field) {
  529. var ns = this._namespace,
  530. trigger = $field.data(ns + '.trigger');
  531. if (trigger) {
  532. return trigger;
  533. }
  534. // IE10/11 auto fires input event of elements using the placeholder attribute
  535. // https://connect.microsoft.com/IE/feedback/details/856700/
  536. var type = $field.attr('type'),
  537. name = $field.attr('data-' + ns + '-field'),
  538. event = ('radio' === type || 'checkbox' === type || 'file' === type || 'SELECT' === $field.get(0).tagName)
  539. ? 'change'
  540. : (this._ieVersion >= 10 && $field.attr('placeholder') ? 'keyup' : this._changeEvent);
  541. trigger = ((this.options.fields[name] ? this.options.fields[name].trigger : null) || this.options.trigger || event).split(' ');
  542. // Since the trigger data is used many times, I need to cache it to use later
  543. $field.data(ns + '.trigger', trigger);
  544. return trigger;
  545. },
  546. /**
  547. * Get the error message for given field and validator
  548. *
  549. * @param {String} field The field name
  550. * @param {String} validatorName The validator name
  551. * @returns {String}
  552. */
  553. _getMessage: function(field, validatorName) {
  554. if (!this.options.fields[field] || !this.options.fields[field].validators) {
  555. return '';
  556. }
  557. var validators = this.options.fields[field].validators,
  558. alias = (validators[validatorName] && validators[validatorName].alias) ? validators[validatorName].alias : validatorName;
  559. if (!FormValidation.Validator[alias]) {
  560. return '';
  561. }
  562. switch (true) {
  563. case !!validators[validatorName].message:
  564. return validators[validatorName].message;
  565. case !!this.options.fields[field].message:
  566. return this.options.fields[field].message;
  567. case !!this.options.message:
  568. return this.options.message;
  569. case (!!FormValidation.I18n[this.options.locale] && !!FormValidation.I18n[this.options.locale][alias] && !!FormValidation.I18n[this.options.locale][alias]['default']):
  570. return FormValidation.I18n[this.options.locale][alias]['default'];
  571. default:
  572. return this.DEFAULT_MESSAGE;
  573. }
  574. },
  575. /**
  576. * Get the element to place the error messages
  577. *
  578. * @param {jQuery} $field The field element
  579. * @param {String} row
  580. * @returns {jQuery}
  581. */
  582. _getMessageContainer: function($field, row) {
  583. if (!this.options.err.parent) {
  584. throw new Error('The err.parent option is not defined');
  585. }
  586. var $parent = $field.parent();
  587. if ($parent.is(row)) {
  588. return $parent;
  589. }
  590. var cssClasses = $parent.attr('class');
  591. if (!cssClasses) {
  592. return this._getMessageContainer($parent, row);
  593. }
  594. if (this.options.err.parent.test(cssClasses)) {
  595. return $parent;
  596. }
  597. return this._getMessageContainer($parent, row);
  598. },
  599. /**
  600. * Parse the add-on options from HTML attributes
  601. *
  602. * @returns {Object}
  603. */
  604. _parseAddOnOptions: function() {
  605. var ns = this._namespace,
  606. names = this.$form.attr('data-' + ns + '-addons'),
  607. addOns = this.options.addOns || {};
  608. if (names) {
  609. names = names.replace(/\s/g, '').split(',');
  610. for (var i = 0; i < names.length; i++) {
  611. if (!addOns[names[i]]) {
  612. addOns[names[i]] = {};
  613. }
  614. }
  615. }
  616. // Try to parse each add-on options
  617. var addOn, attrMap, attr, option;
  618. for (addOn in addOns) {
  619. if (!FormValidation.AddOn[addOn]) {
  620. // Add-on is not found
  621. delete addOns[addOn];
  622. continue;
  623. }
  624. attrMap = FormValidation.AddOn[addOn].html5Attributes;
  625. if (attrMap) {
  626. for (attr in attrMap) {
  627. option = this.$form.attr('data-' + ns + '-addons-' + addOn.toLowerCase() + '-' + attr.toLowerCase());
  628. if (option) {
  629. addOns[addOn][attrMap[attr]] = option;
  630. }
  631. }
  632. }
  633. }
  634. return addOns;
  635. },
  636. /**
  637. * Parse the validator options from HTML attributes
  638. *
  639. * @param {jQuery} $field The field element
  640. * @returns {Object}
  641. */
  642. _parseOptions: function($field) {
  643. var ns = this._namespace,
  644. field = $field.attr('name') || $field.attr('data-' + ns + '-field'),
  645. validators = {},
  646. aliasAttr = new RegExp('^data-' + ns + '-([a-z]+)-alias$'),
  647. validatorSet = $.extend({}, FormValidation.Validator),
  648. validator,
  649. v, // Validator name
  650. attrName,
  651. enabled,
  652. optionName,
  653. optionAttrName,
  654. optionValue,
  655. html5AttrName,
  656. html5AttrMap;
  657. // Determine whether the alias validator is used by checking the data-fv-validator-alias attribute
  658. $.each($field.get(0).attributes, function(i, attribute) {
  659. if (attribute.value && aliasAttr.test(attribute.name)) {
  660. v = attribute.name.split('-')[2];
  661. if (validatorSet[attribute.value]) {
  662. validatorSet[v] = validatorSet[attribute.value];
  663. validatorSet[v].alias = attribute.value;
  664. }
  665. }
  666. });
  667. for (v in validatorSet) {
  668. validator = validatorSet[v];
  669. attrName = 'data-' + ns + '-' + v.toLowerCase(),
  670. enabled = $field.attr(attrName) + '';
  671. html5AttrMap = ('function' === typeof validator.enableByHtml5) ? validator.enableByHtml5($field) : null;
  672. if ((html5AttrMap && enabled !== 'false')
  673. || (html5AttrMap !== true && ('' === enabled || 'true' === enabled || attrName === enabled.toLowerCase())))
  674. {
  675. // Try to parse the options via attributes
  676. validator.html5Attributes = $.extend({}, {
  677. message: 'message',
  678. onerror: 'onError',
  679. onreset: 'onReset',
  680. onsuccess: 'onSuccess',
  681. priority: 'priority',
  682. transformer: 'transformer'
  683. }, validator.html5Attributes);
  684. validators[v] = $.extend({}, html5AttrMap === true ? {} : html5AttrMap, validators[v]);
  685. if (validator.alias) {
  686. validators[v].alias = validator.alias;
  687. }
  688. for (html5AttrName in validator.html5Attributes) {
  689. optionName = validator.html5Attributes[html5AttrName];
  690. optionAttrName = 'data-' + ns + '-' + v.toLowerCase() + '-' + html5AttrName;
  691. optionValue = $field.attr(optionAttrName);
  692. if (optionValue) {
  693. if ('true' === optionValue || optionAttrName === optionValue.toLowerCase()) {
  694. optionValue = true;
  695. } else if ('false' === optionValue) {
  696. optionValue = false;
  697. }
  698. validators[v][optionName] = optionValue;
  699. }
  700. }
  701. }
  702. }
  703. var opts = {
  704. autoFocus: $field.attr('data-' + ns + '-autofocus'),
  705. err: $field.attr('data-' + ns + '-err-container') || $field.attr('data-' + ns + '-container'), // Support backward
  706. enabled: $field.attr('data-' + ns + '-enabled'),
  707. excluded: $field.attr('data-' + ns + '-excluded'),
  708. icon: $field.attr('data-' + ns + '-icon') || $field.attr('data-' + ns + '-feedbackicons') || (this.options.fields && this.options.fields[field] ? this.options.fields[field].feedbackIcons : null), // Support backward
  709. message: $field.attr('data-' + ns + '-message'),
  710. onError: $field.attr('data-' + ns + '-onerror'),
  711. onReset: $field.attr('data-' + ns + '-onreset'),
  712. onStatus: $field.attr('data-' + ns + '-onstatus'),
  713. onSuccess: $field.attr('data-' + ns + '-onsuccess'),
  714. row: $field.attr('data-' + ns + '-row') || $field.attr('data-' + ns + '-group') || (this.options.fields && this.options.fields[field] ? this.options.fields[field].group : null), // Support backward
  715. selector: $field.attr('data-' + ns + '-selector'),
  716. threshold: $field.attr('data-' + ns + '-threshold'),
  717. transformer: $field.attr('data-' + ns + '-transformer'),
  718. trigger: $field.attr('data-' + ns + '-trigger'),
  719. verbose: $field.attr('data-' + ns + '-verbose'),
  720. validators: validators
  721. },
  722. emptyOptions = $.isEmptyObject(opts), // Check if the field options are set using HTML attributes
  723. emptyValidators = $.isEmptyObject(validators); // Check if the field validators are set using HTML attributes
  724. if (!emptyValidators || (!emptyOptions && this.options.fields && this.options.fields[field])) {
  725. //opts.validators = validators;
  726. return opts;
  727. } else {
  728. return null;
  729. }
  730. },
  731. /**
  732. * Called when all validations are completed
  733. */
  734. _submit: function() {
  735. var isValid = this.isValid();
  736. if (isValid === null) {
  737. return;
  738. }
  739. var eventType = isValid ? this.options.events.formSuccess : this.options.events.formError,
  740. e = $.Event(eventType);
  741. this.$form.trigger(e);
  742. // Call default handler
  743. // Check if whether the submit button is clicked
  744. if (this.$submitButton) {
  745. isValid ? this._onSuccess(e) : this._onError(e);
  746. }
  747. },
  748. // ~~~~~~
  749. // Events
  750. // ~~~~~~
  751. /**
  752. * The default handler of error.form.fv event.
  753. * It will be called when there is a invalid field
  754. *
  755. * @param {jQuery.Event} e The jQuery event object
  756. */
  757. _onError: function(e) {
  758. if (e.isDefaultPrevented()) {
  759. return;
  760. }
  761. if ('submitted' === this.options.live) {
  762. // Enable live mode
  763. this.options.live = 'enabled';
  764. var that = this;
  765. for (var field in this.options.fields) {
  766. (function(f) {
  767. var fields = that.getFieldElements(f);
  768. if (fields.length) {
  769. that.onLiveChange(fields, 'live', function() {
  770. if (that._exceedThreshold($(this))) {
  771. that.validateField($(this));
  772. }
  773. });
  774. }
  775. })(field);
  776. }
  777. }
  778. // Determined the first invalid field which will be focused on automatically
  779. var ns = this._namespace;
  780. for (var i = 0; i < this.$invalidFields.length; i++) {
  781. var $field = this.$invalidFields.eq(i),
  782. autoFocus = this.isOptionEnabled($field.attr('data-' + ns + '-field'), 'autoFocus');
  783. if (autoFocus) {
  784. // Focus the field
  785. $field.focus();
  786. break;
  787. }
  788. }
  789. },
  790. /**
  791. * Called after validating a field element
  792. *
  793. * @param {jQuery} $field The field element
  794. * @param {String} [validatorName] The validator name
  795. */
  796. _onFieldValidated: function($field, validatorName) {
  797. var ns = this._namespace,
  798. field = $field.attr('data-' + ns + '-field'),
  799. validators = this.options.fields[field].validators,
  800. counter = {},
  801. numValidators = 0,
  802. data = {
  803. bv: this, // Support backward
  804. fv: this,
  805. field: field,
  806. element: $field,
  807. validator: validatorName,
  808. result: $field.data(ns + '.response.' + validatorName)
  809. };
  810. // Trigger an event after given validator completes
  811. if (validatorName) {
  812. switch ($field.data(ns + '.result.' + validatorName)) {
  813. case this.STATUS_INVALID:
  814. $field.trigger($.Event(this.options.events.validatorError), data);
  815. break;
  816. case this.STATUS_VALID:
  817. $field.trigger($.Event(this.options.events.validatorSuccess), data);
  818. break;
  819. case this.STATUS_IGNORED:
  820. $field.trigger($.Event(this.options.events.validatorIgnored), data);
  821. break;
  822. default:
  823. break;
  824. }
  825. }
  826. counter[this.STATUS_NOT_VALIDATED] = 0;
  827. counter[this.STATUS_VALIDATING] = 0;
  828. counter[this.STATUS_INVALID] = 0;
  829. counter[this.STATUS_VALID] = 0;
  830. counter[this.STATUS_IGNORED] = 0;
  831. for (var v in validators) {
  832. if (validators[v].enabled === false) {
  833. continue;
  834. }
  835. numValidators++;
  836. var result = $field.data(ns + '.result.' + v);
  837. if (result) {
  838. counter[result]++;
  839. }
  840. }
  841. // The sum of valid fields now also include ignored fields
  842. if (counter[this.STATUS_VALID] + counter[this.STATUS_IGNORED] === numValidators) {
  843. // Remove from the list of invalid fields
  844. this.$invalidFields = this.$invalidFields.not($field);
  845. $field.trigger($.Event(this.options.events.fieldSuccess), data);
  846. }
  847. // If all validators are completed and there is at least one validator which doesn't pass
  848. else if ((counter[this.STATUS_NOT_VALIDATED] === 0 || !this.isOptionEnabled(field, 'verbose')) && counter[this.STATUS_VALIDATING] === 0 && counter[this.STATUS_INVALID] > 0) {
  849. // Add to the list of invalid fields
  850. this.$invalidFields = this.$invalidFields.add($field);
  851. $field.trigger($.Event(this.options.events.fieldError), data);
  852. }
  853. },
  854. /**
  855. * The default handler of success.form.fv event.
  856. * It will be called when all the fields are valid
  857. *
  858. * @param {jQuery.Event} e The jQuery event object
  859. */
  860. _onSuccess: function(e) {
  861. if (e.isDefaultPrevented()) {
  862. return;
  863. }
  864. // Submit the form
  865. this.disableSubmitButtons(true).defaultSubmit();
  866. },
  867. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  868. // Abstract methods
  869. // Need to be implemented by sub-class that supports specific framework
  870. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  871. /**
  872. * Specific framework might need to adjust the icon position
  873. *
  874. * @param {jQuery} $field The field element
  875. * @param {jQuery} $icon The icon element
  876. */
  877. _fixIcon: function($field, $icon) {
  878. },
  879. /**
  880. * Create a tooltip or popover
  881. * It will be shown when focusing on the field
  882. *
  883. * @param {jQuery} $field The field element
  884. * @param {String} message The message
  885. * @param {String} type Can be 'tooltip' or 'popover'
  886. */
  887. _createTooltip: function($field, message, type) {
  888. },
  889. /**
  890. * Destroy the tooltip or popover
  891. *
  892. * @param {jQuery} $field The field element
  893. * @param {String} type Can be 'tooltip' or 'popover'
  894. */
  895. _destroyTooltip: function($field, type) {
  896. },
  897. /**
  898. * Hide a tooltip or popover
  899. *
  900. * @param {jQuery} $field The field element
  901. * @param {String} type Can be 'tooltip' or 'popover'
  902. */
  903. _hideTooltip: function($field, type) {
  904. },
  905. /**
  906. * Show a tooltip or popover
  907. *
  908. * @param {jQuery} $field The field element
  909. * @param {String} type Can be 'tooltip' or 'popover'
  910. */
  911. _showTooltip: function($field, type) {
  912. },
  913. // ~~~~~~~~~~~~~~
  914. // Public methods
  915. // ~~~~~~~~~~~~~~
  916. /**
  917. * Submit the form using default submission.
  918. * It also does not perform any validations when submitting the form
  919. */
  920. defaultSubmit: function() {
  921. var ns = this._namespace;
  922. if (this.$submitButton) {
  923. // Create hidden input to send the submit buttons
  924. $('<input/>')
  925. .attr({
  926. 'type': 'hidden',
  927. name: this.$submitButton.attr('name')
  928. })
  929. .attr('data-' + ns + '-submit-hidden', '')
  930. .val(this.$submitButton.val())
  931. .appendTo(this.$form);
  932. }
  933. // Submit form
  934. this.$form.off('submit.' + ns).submit();
  935. },
  936. /**
  937. * Disable/enable submit buttons
  938. *
  939. * @param {Boolean} disabled Can be true or false
  940. * @returns {FormValidation.Base}
  941. */
  942. disableSubmitButtons: function(disabled) {
  943. if (!disabled) {
  944. this.$form
  945. .find(this.options.button.selector)
  946. .removeAttr('disabled')
  947. .removeClass(this.options.button.disabled);
  948. } else if (this.options.live !== 'disabled') {
  949. // Don't disable if the live validating mode is disabled
  950. this.$form
  951. .find(this.options.button.selector)
  952. .attr('disabled', 'disabled')
  953. .addClass(this.options.button.disabled);
  954. }
  955. return this;
  956. },
  957. /**
  958. * Retrieve the field elements by given name
  959. *
  960. * @param {String} field The field name
  961. * @returns {null|jQuery[]}
  962. */
  963. getFieldElements: function(field) {
  964. if (!this._cacheFields[field]) {
  965. if (this.options.fields[field] && this.options.fields[field].selector) {
  966. // Look for the field inside the form first
  967. var f = this.$form.find(this.options.fields[field].selector);
  968. // If not found, search in entire document
  969. this._cacheFields[field] = f.length ? f : $(this.options.fields[field].selector);
  970. } else {
  971. this._cacheFields[field] = this.$form.find('[name="' + field + '"]');
  972. }
  973. }
  974. return this._cacheFields[field];
  975. },
  976. /**
  977. * Get the field value after applying transformer
  978. *
  979. * @param {String|jQuery} field The field name or field element
  980. * @param {String} validatorName The validator name
  981. * @returns {String}
  982. */
  983. getFieldValue: function(field, validatorName) {
  984. var $field, ns = this._namespace;
  985. if ('string' === typeof field) {
  986. $field = this.getFieldElements(field);
  987. if ($field.length === 0) {
  988. return null;
  989. }
  990. } else {
  991. $field = field;
  992. field = $field.attr('data-' + ns + '-field');
  993. }
  994. if (!field || !this.options.fields[field]) {
  995. return $field.val();
  996. }
  997. var transformer = (this.options.fields[field].validators && this.options.fields[field].validators[validatorName]
  998. ? this.options.fields[field].validators[validatorName].transformer : null)
  999. || this.options.fields[field].transformer;
  1000. return transformer ? FormValidation.Helper.call(transformer, [$field, validatorName, this]) : $field.val();
  1001. },
  1002. /**
  1003. * Get the namespace
  1004. *
  1005. * @returns {String}
  1006. */
  1007. getNamespace: function() {
  1008. return this._namespace;
  1009. },
  1010. /**
  1011. * Get the field options
  1012. *
  1013. * @param {String|jQuery} [field] The field name or field element. If it is not set, the method returns the form options
  1014. * @param {String} [validator] The name of validator. It null, the method returns form options
  1015. * @param {String} [option] The option name
  1016. * @return {String|Object}
  1017. */
  1018. getOptions: function(field, validator, option) {
  1019. var ns = this._namespace;
  1020. if (!field) {
  1021. return option ? this.options[option] : this.options;
  1022. }
  1023. if ('object' === typeof field) {
  1024. field = field.attr('data-' + ns + '-field');
  1025. }
  1026. if (!this.options.fields[field]) {
  1027. return null;
  1028. }
  1029. var options = this.options.fields[field];
  1030. if (!validator) {
  1031. return option ? options[option] : options;
  1032. }
  1033. if (!options.validators || !options.validators[validator]) {
  1034. return null;
  1035. }
  1036. return option ? options.validators[validator][option] : options.validators[validator];
  1037. },
  1038. /**
  1039. * Get the validating result of field
  1040. *
  1041. * @param {String|jQuery} field The field name or field element
  1042. * @param {String} validatorName The validator name
  1043. * @returns {String} The status. Can be 'NOT_VALIDATED', 'VALIDATING', 'INVALID', 'VALID' or 'IGNORED'
  1044. */
  1045. getStatus: function(field, validatorName) {
  1046. var ns = this._namespace;
  1047. switch (typeof field) {
  1048. case 'object':
  1049. return field.data(ns + '.result.' + validatorName);
  1050. case 'string':
  1051. /* falls through */
  1052. default:
  1053. return this.getFieldElements(field).eq(0).data(ns + '.result.' + validatorName);
  1054. }
  1055. },
  1056. /**
  1057. * Check whether or not a field option is enabled
  1058. *
  1059. * @param {String} field The field name
  1060. * @param {String} option The option name, "verbose", "autoFocus", for example
  1061. * @returns {Boolean}
  1062. */
  1063. isOptionEnabled: function(field, option) {
  1064. if (this.options.fields[field] && (this.options.fields[field][option] === 'true' || this.options.fields[field][option] === true)) {
  1065. return true;
  1066. }
  1067. if (this.options.fields[field] && (this.options.fields[field][option] === 'false' || this.options.fields[field][option] === false)) {
  1068. return false;
  1069. }
  1070. return this.options[option] === 'true' || this.options[option] === true;
  1071. },
  1072. /**
  1073. * Check the form validity
  1074. *
  1075. * @returns {Boolean|null} Returns one of three values
  1076. * - true, if all fields are valid
  1077. * - false, if there is one invalid field
  1078. * - null, if there is at least one field which is not validated yet or being validated
  1079. */
  1080. isValid: function() {
  1081. for (var field in this.options.fields) {
  1082. var isValidField = this.isValidField(field);
  1083. if (isValidField === null) {
  1084. return null;
  1085. }
  1086. if (isValidField === false) {
  1087. return false;
  1088. }
  1089. }
  1090. return true;
  1091. },
  1092. /**
  1093. * Check if all fields inside a given container are valid.
  1094. * It's useful when working with a wizard-like such as tab, collapse
  1095. *
  1096. * @param {String|jQuery} container The container selector or element
  1097. * @returns {Boolean|null} Returns one of three values
  1098. * - true, if all fields inside the container are valid
  1099. * - false, if there is one invalid field inside the container
  1100. * - null, if the container consists of at least one field which is not validated yet or being validated
  1101. */
  1102. isValidContainer: function(container) {
  1103. var that = this,
  1104. ns = this._namespace,
  1105. fields = [],
  1106. $container = ('string' === typeof container) ? $(container) : container;
  1107. if ($container.length === 0) {
  1108. return true;
  1109. }
  1110. $container.find('[data-' + ns + '-field]').each(function() {
  1111. var $field = $(this);
  1112. if (!that._isExcluded($field)) {
  1113. fields.push($field);
  1114. }
  1115. });
  1116. var total = fields.length,
  1117. clazz = this.options.err.clazz.split(' ').join('.');
  1118. for (var i = 0; i < total; i++) {
  1119. var $f = fields[i],
  1120. field = $f.attr('data-' + ns + '-field'),
  1121. $errors = $f.data(ns + '.messages')
  1122. .find('.' + clazz + '[data-' + ns + '-validator][data-' + ns + '-for="' + field + '"]');
  1123. if (this.options.fields && this.options.fields[field]
  1124. && (this.options.fields[field].enabled === 'false' || this.options.fields[field].enabled === false))
  1125. {
  1126. continue;
  1127. }
  1128. if ($errors.filter('[data-' + ns + '-result="' + this.STATUS_INVALID + '"]').length > 0) {
  1129. return false;
  1130. }
  1131. // If the field is not validated
  1132. if ($errors.filter('[data-' + ns + '-result="' + this.STATUS_NOT_VALIDATED + '"]').length > 0
  1133. || $errors.filter('[data-' + ns + '-result="' + this.STATUS_VALIDATING + '"]').length > 0)
  1134. {
  1135. return null;
  1136. }
  1137. }
  1138. return true;
  1139. },
  1140. /**
  1141. * Check if the field is valid or not
  1142. *
  1143. * @param {String|jQuery} field The field name or field element
  1144. * @returns {Boolean|null} Returns one of three values
  1145. * - true, if the field passes all validators
  1146. * - false, if the field doesn't pass any validator
  1147. * - null, if there is at least one validator which isn't validated yet or being validated
  1148. */
  1149. isValidField: function(field) {
  1150. var ns = this._namespace,
  1151. fields = $([]);
  1152. switch (typeof field) {
  1153. case 'object':
  1154. fields = field;
  1155. field = field.attr('data-' + ns + '-field');
  1156. break;
  1157. case 'string':
  1158. fields = this.getFieldElements(field);
  1159. break;
  1160. default:
  1161. break;
  1162. }
  1163. if (fields.length === 0 || !this.options.fields[field]
  1164. || this.options.fields[field].enabled === 'false' || this.options.fields[field].enabled === false)
  1165. {
  1166. return true;
  1167. }
  1168. var type = fields.attr('type'),
  1169. total = ('radio' === type || 'checkbox' === type) ? 1 : fields.length,
  1170. $field, validatorName, status;
  1171. for (var i = 0; i < total; i++) {
  1172. $field = fields.eq(i);
  1173. if (this._isExcluded($field)) {
  1174. continue;
  1175. }
  1176. for (validatorName in this.options.fields[field].validators) {
  1177. if (this.options.fields[field].validators[validatorName].enabled === false) {
  1178. continue;
  1179. }
  1180. status = $field.data(ns + '.result.' + validatorName);
  1181. if (status === this.STATUS_VALIDATING || status === this.STATUS_NOT_VALIDATED) {
  1182. return null;
  1183. } else if (status === this.STATUS_INVALID) {
  1184. return false;
  1185. }
  1186. }
  1187. }
  1188. return true;
  1189. },
  1190. /**
  1191. * Detach a handler function for a field live change event
  1192. *
  1193. * @param {jQuery[]} $fields The field elements
  1194. * @param {String} namespace The event namespace
  1195. * @returns {FormValidation.Base}
  1196. */
  1197. offLiveChange: function($fields, namespace) {
  1198. if ($fields === null || $fields.length === 0) {
  1199. return this;
  1200. }
  1201. var ns = this._namespace,
  1202. trigger = this._getFieldTrigger($fields.eq(0)),
  1203. events = $.map(trigger, function(item) {
  1204. return item + '.' + namespace + '.' + ns;
  1205. }).join(' ');
  1206. $fields.off(events);
  1207. return this;
  1208. },
  1209. /**
  1210. * Attach a handler function for a field live change event
  1211. *
  1212. * @param {jQuery[]} $fields The field elements
  1213. * @param {String} namespace The event namespace
  1214. * @param {Function} handler The handler function
  1215. * @returns {FormValidation.Base}
  1216. */
  1217. onLiveChange: function($fields, namespace, handler) {
  1218. if ($fields === null || $fields.length === 0) {
  1219. return this;
  1220. }
  1221. var ns = this._namespace,
  1222. trigger = this._getFieldTrigger($fields.eq(0)),
  1223. events = $.map(trigger, function(item) {
  1224. return item + '.' + namespace + '.' + ns;
  1225. }).join(' ');
  1226. switch (this.options.live) {
  1227. case 'submitted':
  1228. break;
  1229. case 'disabled':
  1230. $fields.off(events);
  1231. break;
  1232. case 'enabled':
  1233. /* falls through */
  1234. default:
  1235. $fields.off(events).on(events, function(e) {
  1236. handler.apply(this, arguments);
  1237. });
  1238. break;
  1239. }
  1240. return this;
  1241. },
  1242. /**
  1243. * Update the error message
  1244. *
  1245. * @param {String|jQuery} field The field name or field element
  1246. * @param {String} validator The validator name
  1247. * @param {String} message The message
  1248. * @returns {FormValidation.Base}
  1249. */
  1250. updateMessage: function(field, validator, message) {
  1251. var ns = this._namespace,
  1252. $fields = $([]);
  1253. switch (typeof field) {
  1254. case 'object':
  1255. $fields = field;
  1256. field = field.attr('data-' + ns + '-field');
  1257. break;
  1258. case 'string':
  1259. $fields = this.getFieldElements(field);
  1260. break;
  1261. default:
  1262. break;
  1263. }
  1264. var clazz = this.options.err.clazz.split(' ').join('.');
  1265. $fields.each(function() {
  1266. $(this)
  1267. .data(ns + '.messages')
  1268. .find('.' + clazz + '[data-' + ns + '-validator="' + validator + '"][data-' + ns + '-for="' + field + '"]').html(message);
  1269. });
  1270. return this;
  1271. },
  1272. /**
  1273. * Update all validating results of field
  1274. *
  1275. * @param {String|jQuery} field The field name or field element
  1276. * @param {String} status The status. Can be 'NOT_VALIDATED', 'VALIDATING', 'INVALID', 'VALID' or 'IGNORED'
  1277. * @param {String} [validatorName] The validator name. If null, the method updates validity result for all validators
  1278. * @returns {FormValidation.Base}
  1279. */
  1280. updateStatus: function(field, status, validatorName) {
  1281. var ns = this._namespace,
  1282. fields = $([]);
  1283. switch (typeof field) {
  1284. case 'object':
  1285. fields = field;
  1286. field = field.attr('data-' + ns + '-field');
  1287. break;
  1288. case 'string':
  1289. fields = this.getFieldElements(field);
  1290. break;
  1291. default:
  1292. break;
  1293. }
  1294. if (!field || !this.options.fields[field]) {
  1295. return this;
  1296. }
  1297. if (status === this.STATUS_NOT_VALIDATED) {
  1298. // Reset the flag
  1299. // To prevent the form from doing submit when a deferred validator returns true while typing
  1300. this._submitIfValid = false;
  1301. }
  1302. var that = this,
  1303. type = fields.attr('type'),
  1304. row = this.options.fields[field].row || this.options.row.selector,
  1305. total = ('radio' === type || 'checkbox' === type) ? 1 : fields.length,
  1306. clazz = this.options.err.clazz.split(' ').join('.');
  1307. for (var i = 0; i < total; i++) {
  1308. var $field = fields.eq(i);
  1309. if (this._isExcluded($field)) {
  1310. continue;
  1311. }
  1312. var $parent = $field.closest(row),
  1313. $message = $field.data(ns + '.messages'),
  1314. $allErrors = $message.find('.' + clazz + '[data-' + ns + '-validator][data-' + ns + '-for="' + field + '"]'),
  1315. $errors = validatorName ? $allErrors.filter('[data-' + ns + '-validator="' + validatorName + '"]') : $allErrors,
  1316. $icon = $field.data(ns + '.icon'),
  1317. // Support backward
  1318. container = ('function' === typeof (this.options.fields[field].container || this.options.fields[field].err || this.options.err.container))
  1319. ? (this.options.fields[field].container || this.options.fields[field].err || this.options.err.container).call(this, $field, this)
  1320. : (this.options.fields[field].container || this.options.fields[field].err || this.options.err.container),
  1321. isValidField = null,
  1322. isValidating,
  1323. isNotValidated;
  1324. // Update status
  1325. if (validatorName) {
  1326. $field.data(ns + '.result.' + validatorName, status);
  1327. } else {
  1328. for (var v in this.options.fields[field].validators) {
  1329. $field.data(ns + '.result.' + v, status);
  1330. }
  1331. }
  1332. // Show/hide error elements and feedback icons
  1333. $errors.attr('data-' + ns + '-result', status);
  1334. switch (status) {
  1335. case this.STATUS_VALIDATING:
  1336. isValidField = null;
  1337. this.disableSubmitButtons(true);
  1338. $field.removeClass(this.options.control.valid).removeClass(this.options.control.invalid);
  1339. $parent.removeClass(this.options.row.valid).removeClass(this.options.row.invalid);
  1340. if ($icon) {
  1341. $icon.removeClass(this.options.icon.valid).removeClass(this.options.icon.invalid).addClass(this.options.icon.validating).show();
  1342. }
  1343. break;
  1344. case this.STATUS_INVALID:
  1345. isValidField = false;
  1346. this.disableSubmitButtons(true);
  1347. $field.removeClass(this.options.control.valid).addClass(this.options.control.invalid);
  1348. $parent.removeClass(this.options.row.valid).addClass(this.options.row.invalid);
  1349. if ($icon) {
  1350. $icon.removeClass(this.options.icon.valid).removeClass(this.options.icon.validating).addClass(this.options.icon.invalid).show();
  1351. }
  1352. break;
  1353. case this.STATUS_IGNORED: // Treat ignored fields like they are valid with some specialties
  1354. case this.STATUS_VALID:
  1355. isValidating = ($allErrors.filter('[data-' + ns + '-result="' + this.STATUS_VALIDATING +'"]').length > 0);
  1356. isNotValidated = ($allErrors.filter('[data-' + ns + '-result="' + this.STATUS_NOT_VALIDATED +'"]').length > 0);
  1357. var numIgnored = $allErrors.filter('[data-' + ns + '-result="' + this.STATUS_IGNORED +'"]').length;
  1358. // If the field is valid (passes all validators)
  1359. isValidField = (isValidating || isNotValidated) // There are some validators that have not done
  1360. ? null
  1361. // All validators are completed
  1362. : ($allErrors.filter('[data-' + ns + '-result="' + this.STATUS_VALID +'"]').length + numIgnored === $allErrors.length);
  1363. $field.removeClass(this.options.control.valid).removeClass(this.options.control.invalid);
  1364. if (isValidField === true) {
  1365. this.disableSubmitButtons(this.isValid() === false);
  1366. if (status === this.STATUS_VALID) {
  1367. $field.addClass(this.options.control.valid);
  1368. }
  1369. } else if (isValidField === false) {
  1370. this.disableSubmitButtons(true);
  1371. if (status === this.STATUS_VALID) {
  1372. $field.addClass(this.options.control.invalid);
  1373. }
  1374. }
  1375. if ($icon) {
  1376. $icon.removeClass(this.options.icon.invalid).removeClass(this.options.icon.validating).removeClass(this.options.icon.valid);
  1377. if (status === this.STATUS_VALID || numIgnored !== $allErrors.length) {
  1378. $icon.addClass(isValidating
  1379. ? this.options.icon.validating
  1380. : (isValidField === null ? '' : (isValidField ? this.options.icon.valid : this.options.icon.invalid)))
  1381. .show();
  1382. }
  1383. }
  1384. var isValidContainer = this.isValidContainer($parent);
  1385. if (isValidContainer !== null) {
  1386. $parent.removeClass(this.options.row.valid).removeClass(this.options.row.invalid);
  1387. if (status === this.STATUS_VALID || numIgnored !== $allErrors.length) {
  1388. $parent.addClass(isValidContainer ? this.options.row.valid : this.options.row.invalid);
  1389. }
  1390. }
  1391. break;
  1392. case this.STATUS_NOT_VALIDATED:
  1393. /* falls through */
  1394. default:
  1395. isValidField = null;
  1396. this.disableSubmitButtons(false);
  1397. $field.removeClass(this.options.control.valid).removeClass(this.options.control.invalid);
  1398. $parent.removeClass(this.options.row.valid).removeClass(this.options.row.invalid);
  1399. if ($icon) {
  1400. $icon.removeClass(this.options.icon.valid).removeClass(this.options.icon.invalid).removeClass(this.options.icon.validating).hide();
  1401. }
  1402. break;
  1403. }
  1404. if ($icon && ('tooltip' === container || 'popover' === container)) {
  1405. (isValidField === false)
  1406. // Only show the first error message
  1407. ? this._createTooltip($field, $allErrors.filter('[data-' + ns + '-result="' + that.STATUS_INVALID + '"]').eq(0).html(), container)
  1408. : this._destroyTooltip($field, container);
  1409. } else {
  1410. (status === this.STATUS_INVALID) ? $errors.show() : $errors.hide();
  1411. }
  1412. // Trigger an event
  1413. $field.trigger($.Event(this.options.events.fieldStatus), {
  1414. bv: this, // Support backward
  1415. fv: this,
  1416. field: field,
  1417. element: $field,
  1418. status: status,
  1419. validator: validatorName
  1420. });
  1421. this._onFieldValidated($field, validatorName);
  1422. }
  1423. return this;
  1424. },
  1425. /**
  1426. * Validate the form
  1427. *
  1428. * @returns {FormValidation.Base}
  1429. */
  1430. validate: function() {
  1431. if ($.isEmptyObject(this.options.fields)) {
  1432. this._submit();
  1433. return this;
  1434. }
  1435. this.$form.trigger($.Event(this.options.events.formPreValidate));
  1436. this.disableSubmitButtons(true);
  1437. this._submitIfValid = false;
  1438. for (var field in this.options.fields) {
  1439. this.validateField(field);
  1440. }
  1441. this._submit();
  1442. this._submitIfValid = true;
  1443. return this;
  1444. },
  1445. /**
  1446. * Validate given field
  1447. *
  1448. * @param {String|jQuery} field The field name or field element
  1449. * @returns {FormValidation.Base}
  1450. */
  1451. validateField: function(field) {
  1452. var ns = this._namespace,
  1453. fields = $([]);
  1454. switch (typeof field) {
  1455. case 'object':
  1456. fields = field;
  1457. field = field.attr('data-' + ns + '-field');
  1458. break;
  1459. case 'string':
  1460. fields = this.getFieldElements(field);
  1461. break;
  1462. default:
  1463. break;
  1464. }
  1465. if (fields.length === 0 || !this.options.fields[field]
  1466. || this.options.fields[field].enabled === 'false' || this.options.fields[field].enabled === false)
  1467. {
  1468. return this;
  1469. }
  1470. var that = this,
  1471. type = fields.attr('type'),
  1472. total = (('radio' === type || 'checkbox' === type) && this.options.live !== 'disabled') ? 1 : fields.length,
  1473. updateAll = ('radio' === type || 'checkbox' === type),
  1474. validators = this.options.fields[field].validators,
  1475. verbose = this.isOptionEnabled(field, 'verbose'),
  1476. validatorName,
  1477. alias,
  1478. validateResult;
  1479. for (var i = 0; i < total; i++) {
  1480. var $field = fields.eq(i);
  1481. if (this._isExcluded($field)) {
  1482. continue;
  1483. }
  1484. var stop = false,
  1485. sortedByPriority = $field.data(ns + '.validators'),
  1486. numValidators = sortedByPriority.length;
  1487. for (var j = 0; j < numValidators; j++) {
  1488. validatorName = sortedByPriority[j].validator;
  1489. if ($field.data(ns + '.dfs.' + validatorName)) {
  1490. $field.data(ns + '.dfs.' + validatorName).reject();
  1491. }
  1492. if (stop) {
  1493. break;
  1494. }
  1495. // Don't validate field if it is already done
  1496. var result = $field.data(ns + '.result.' + validatorName);
  1497. if (result === this.STATUS_VALID || result === this.STATUS_INVALID) {
  1498. this._onFieldValidated($field, validatorName);
  1499. continue;
  1500. } else if (validators[validatorName].enabled === false) {
  1501. // Changed in v0.6.2:
  1502. // When the field validator is disabled, it should be treated as STATUS_IGNORED instead of STATUS_VALID
  1503. // By doing that, the field with only disabled and ignored validators will not have success/error class
  1504. this.updateStatus(updateAll ? field : $field, this.STATUS_IGNORED, validatorName);
  1505. continue;
  1506. }
  1507. $field.data(ns + '.result.' + validatorName, this.STATUS_VALIDATING);
  1508. // Check whether or not the validator is just an alias of another
  1509. alias = validators[validatorName].alias || validatorName;
  1510. validateResult = FormValidation.Validator[alias].validate(this, $field, validators[validatorName], validatorName);
  1511. // validateResult can be a $.Deferred object ...
  1512. if ('object' === typeof validateResult && validateResult.resolve) {
  1513. this.updateStatus(updateAll ? field : $field, this.STATUS_VALIDATING, validatorName);
  1514. $field.data(ns + '.dfs.' + validatorName, validateResult);
  1515. validateResult.done(function($f, v, response) {
  1516. // v is validator name
  1517. $f.removeData(ns + '.dfs.' + v).data(ns + '.response.' + v, response);
  1518. if (response.message) {
  1519. that.updateMessage($f, v, response.message);
  1520. }
  1521. that.updateStatus(updateAll ? $f.attr('data-' + ns + '-field') : $f,
  1522. response.valid === true ? that.STATUS_VALID : (response.valid === false ? that.STATUS_INVALID : that.STATUS_IGNORED),
  1523. v);
  1524. if (response.valid && that._submitIfValid === true) {
  1525. // If a remote validator returns true and the form is ready to submit, then do it
  1526. that._submit();
  1527. } else if (response.valid === false && !verbose) {
  1528. stop = true;
  1529. }
  1530. });
  1531. }
  1532. // ... or object { valid: true/false/null, message: 'dynamic message', otherKey: value, ... }
  1533. else if ('object' === typeof validateResult && validateResult.valid !== undefined) {
  1534. $field.data(ns + '.response.' + validatorName, validateResult);
  1535. if (validateResult.message) {
  1536. this.updateMessage(updateAll ? field : $field, validatorName, validateResult.message);
  1537. }
  1538. this.updateStatus(updateAll ? field : $field,
  1539. validateResult.valid === true ? this.STATUS_VALID : (validateResult.valid === false ? this.STATUS_INVALID : this.STATUS_IGNORED),
  1540. validatorName);
  1541. if (validateResult.valid === false && !verbose) {
  1542. break;
  1543. }
  1544. }
  1545. // ... or a boolean value
  1546. else if ('boolean' === typeof validateResult) {
  1547. $field.data(ns + '.response.' + validatorName, validateResult);
  1548. this.updateStatus(updateAll ? field : $field, validateResult ? this.STATUS_VALID : this.STATUS_INVALID, validatorName);
  1549. if (!validateResult && !verbose) {
  1550. break;
  1551. }
  1552. }
  1553. // ... or null
  1554. // to indicate that the field should be ignored for current validator
  1555. else if (null === validateResult) {
  1556. $field.data(ns + '.response.' + validatorName, validateResult);
  1557. this.updateStatus(updateAll ? field : $field, this.STATUS_IGNORED, validatorName);
  1558. }
  1559. }
  1560. }
  1561. return this;
  1562. },
  1563. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1564. // Useful APIs which aren't used internally
  1565. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1566. /**
  1567. * Add a new field
  1568. *
  1569. * @param {String|jQuery} field The field name or field element
  1570. * @param {Object} [options] The validator rules
  1571. * @returns {FormValidation.Base}
  1572. */
  1573. addField: function(field, options) {
  1574. var ns = this._namespace,
  1575. fields = $([]);
  1576. switch (typeof field) {
  1577. case 'object':
  1578. fields = field;
  1579. field = field.attr('data-' + ns + '-field') || field.attr('name');
  1580. break;
  1581. case 'string':
  1582. delete this._cacheFields[field];
  1583. fields = this.getFieldElements(field);
  1584. break;
  1585. default:
  1586. break;
  1587. }
  1588. fields.attr('data-' + ns + '-field', field);
  1589. var type = fields.attr('type'),
  1590. total = ('radio' === type || 'checkbox' === type) ? 1 : fields.length;
  1591. for (var i = 0; i < total; i++) {
  1592. var $field = fields.eq(i);
  1593. // Try to parse the options from HTML attributes
  1594. var opts = this._parseOptions($field);
  1595. opts = (opts === null) ? options : $.extend(true, opts, options);
  1596. this.options.fields[field] = $.extend(true, this.options.fields[field], opts);
  1597. // Update the cache
  1598. this._cacheFields[field] = this._cacheFields[field] ? this._cacheFields[field].add($field) : $field;
  1599. // Init the element
  1600. this._initField(('checkbox' === type || 'radio' === type) ? field : $field);
  1601. }
  1602. this.disableSubmitButtons(false);
  1603. // Trigger an event
  1604. this.$form.trigger($.Event(this.options.events.fieldAdded), {
  1605. field: field,
  1606. element: fields,
  1607. options: this.options.fields[field]
  1608. });
  1609. return this;
  1610. },
  1611. /**
  1612. * Destroy the plugin
  1613. * It will remove all error messages, feedback icons and turn off the events
  1614. */
  1615. destroy: function() {
  1616. var ns = this._namespace, i, field, fields, $field, validator, $icon, row, alias;
  1617. // Destroy the validators first
  1618. for (field in this.options.fields) {
  1619. fields = this.getFieldElements(field);
  1620. for (i = 0; i < fields.length; i++) {
  1621. $field = fields.eq(i);
  1622. for (validator in this.options.fields[field].validators) {
  1623. if ($field.data(ns + '.dfs.' + validator)) {
  1624. $field.data(ns + '.dfs.' + validator).reject();
  1625. }
  1626. $field.removeData(ns + '.result.' + validator)
  1627. .removeData(ns + '.response.' + validator)
  1628. .removeData(ns + '.dfs.' + validator);
  1629. // Destroy the validator
  1630. alias = this.options.fields[field].validators[validator].alias || validator;
  1631. if ('function' === typeof FormValidation.Validator[alias].destroy) {
  1632. FormValidation.Validator[alias].destroy(this, $field, this.options.fields[field].validators[validator], validator);
  1633. }
  1634. }
  1635. }
  1636. }
  1637. // Remove messages and icons
  1638. var clazz = this.options.err.clazz.split(' ').join('.');
  1639. for (field in this.options.fields) {
  1640. fields = this.getFieldElements(field);
  1641. row = this.options.fields[field].row || this.options.row.selector;
  1642. for (i = 0; i < fields.length; i++) {
  1643. $field = fields.eq(i);
  1644. // Remove all error messages
  1645. var $messages = $field.data(ns + '.messages');
  1646. if ($messages) {
  1647. $messages.find('.' + clazz + '[data-' + ns + '-validator][data-' + ns + '-for="' + field + '"]').remove();
  1648. }
  1649. $field
  1650. .removeData(ns + '.messages')
  1651. .removeData(ns + '.validators')
  1652. // Remove feedback classes
  1653. .closest(row)
  1654. .removeClass(this.options.row.valid)
  1655. .removeClass(this.options.row.invalid)
  1656. .removeClass(this.options.row.feedback)
  1657. .end()
  1658. // Turn off events
  1659. .off('.' + ns)
  1660. .removeAttr('data-' + ns + '-field');
  1661. // Remove feedback icons, tooltip/popover container
  1662. // Support backward
  1663. var container = ('function' === typeof (this.options.fields[field].container || this.options.fields[field].err || this.options.err.container))
  1664. ? (this.options.fields[field].container || this.options.fields[field].err || this.options.err.container).call(this, $field, this)
  1665. : (this.options.fields[field].container || this.options.fields[field].err || this.options.err.container);
  1666. if ('tooltip' === container || 'popover' === container) {
  1667. this._destroyTooltip($field, container);
  1668. }
  1669. $icon = $field.data(ns + '.icon');
  1670. if ($icon) {
  1671. $icon.remove();
  1672. }
  1673. $field.removeData(ns + '.icon')
  1674. // It's safe to remove trigger data here, because it might be used when destroying the validator
  1675. .removeData(ns + '.trigger');
  1676. }
  1677. }
  1678. // Destroy the add-ons
  1679. for (var addOn in this.options.addOns) {
  1680. if ('function' === typeof FormValidation.AddOn[addOn].destroy) {
  1681. FormValidation.AddOn[addOn].destroy(this, this.options.addOns[addOn]);
  1682. }
  1683. }
  1684. this.disableSubmitButtons(false); // Enable submit buttons
  1685. this.$hiddenButton.remove(); // Remove the hidden button
  1686. this.$form
  1687. .removeClass(this.options.elementClass)
  1688. .off('.' + ns)
  1689. .removeData('bootstrapValidator') // Support backward
  1690. .removeData('formValidation')
  1691. // Remove generated hidden elements
  1692. .find('[data-' + ns + '-submit-hidden]').remove().end()
  1693. .find('[type="submit"]')
  1694. .off('click.' + ns);
  1695. },
  1696. /**
  1697. * Enable/Disable all validators to given field
  1698. *
  1699. * @param {String} field The field name
  1700. * @param {Boolean} enabled Enable/Disable field validators
  1701. * @param {String} [validatorName] The validator name. If null, all validators will be enabled/disabled
  1702. * @returns {FormValidation.Base}
  1703. */
  1704. enableFieldValidators: function(field, enabled, validatorName) {
  1705. var validators = this.options.fields[field].validators;
  1706. // Enable/disable particular validator
  1707. if (validatorName
  1708. && validators
  1709. && validators[validatorName] && validators[validatorName].enabled !== enabled)
  1710. {
  1711. this.options.fields[field].validators[validatorName].enabled = enabled;
  1712. this.updateStatus(field, this.STATUS_NOT_VALIDATED, validatorName);
  1713. }
  1714. // Enable/disable all validators
  1715. else if (!validatorName && this.options.fields[field].enabled !== enabled) {
  1716. this.options.fields[field].enabled = enabled;
  1717. for (var v in validators) {
  1718. this.enableFieldValidators(field, enabled, v);
  1719. }
  1720. }
  1721. return this;
  1722. },
  1723. /**
  1724. * Some validators have option which its value is dynamic.
  1725. * For example, the zipCode validator has the country option which might be changed dynamically by a select element.
  1726. *
  1727. * @param {jQuery|String} field The field name or element
  1728. * @param {String|Function} option The option which can be determined by:
  1729. * - a string
  1730. * - name of field which defines the value
  1731. * - name of function which returns the value
  1732. * - a function returns the value
  1733. *
  1734. * The callback function has the format of
  1735. * callback: function(value, validator, $field) {
  1736. * // value is the value of field
  1737. * // validator is the BootstrapValidator instance
  1738. * // $field is the field element
  1739. * }
  1740. *
  1741. * @returns {String}
  1742. */
  1743. getDynamicOption: function(field, option) {
  1744. var $field = ('string' === typeof field) ? this.getFieldElements(field) : field,
  1745. value = $field.val();
  1746. // Option can be determined by
  1747. // ... a function
  1748. if ('function' === typeof option) {
  1749. return FormValidation.Helper.call(option, [value, this, $field]);
  1750. }
  1751. // ... value of other field
  1752. else if ('string' === typeof option) {
  1753. var $f = this.getFieldElements(option);
  1754. if ($f.length) {
  1755. return $f.val();
  1756. }
  1757. // ... return value of callback
  1758. else {
  1759. return FormValidation.Helper.call(option, [value, this, $field]) || option;
  1760. }
  1761. }
  1762. return null;
  1763. },
  1764. /**
  1765. * Get the form element
  1766. *
  1767. * @returns {jQuery}
  1768. */
  1769. getForm: function() {
  1770. return this.$form;
  1771. },
  1772. /**
  1773. * Get the list of invalid fields
  1774. *
  1775. * @returns {jQuery[]}
  1776. */
  1777. getInvalidFields: function() {
  1778. return this.$invalidFields;
  1779. },
  1780. /**
  1781. * Get the current locale
  1782. *
  1783. * @return {String}
  1784. */
  1785. getLocale: function() {
  1786. return this.options.locale;
  1787. },
  1788. /**
  1789. * Get the error messages
  1790. *
  1791. * @param {String|jQuery} [field] The field name or field element
  1792. * If the field is not defined, the method returns all error messages of all fields
  1793. * @param {String} [validator] The name of validator
  1794. * If the validator is not defined, the method returns error messages of all validators
  1795. * @returns {String[]}
  1796. */
  1797. getMessages: function(field, validator) {
  1798. var that = this,
  1799. ns = this._namespace,
  1800. messages = [],
  1801. $fields = $([]);
  1802. switch (true) {
  1803. case (field && 'object' === typeof field):
  1804. $fields = field;
  1805. break;
  1806. case (field && 'string' === typeof field):
  1807. var f = this.getFieldElements(field);
  1808. if (f.length > 0) {
  1809. var type = f.attr('type');
  1810. $fields = ('radio' === type || 'checkbox' === type) ? f.eq(0) : f;
  1811. }
  1812. break;
  1813. default:
  1814. $fields = this.$invalidFields;
  1815. break;
  1816. }
  1817. var filter = validator ? '[data-' + ns + '-validator="' + validator + '"]' : '',
  1818. clazz = this.options.err.clazz.split(' ').join('.');
  1819. $fields.each(function() {
  1820. messages = messages.concat(
  1821. $(this)
  1822. .data(ns + '.messages')
  1823. .find('.' + clazz + '[data-' + ns + '-for="' + $(this).attr('data-' + ns + '-field') + '"][data-' + ns + '-result="' + that.STATUS_INVALID + '"]' + filter)
  1824. .map(function() {
  1825. var v = $(this).attr('data-' + ns + '-validator'),
  1826. f = $(this).attr('data-' + ns + '-for');
  1827. return (that.options.fields[f].validators[v].enabled === false) ? '' : $(this).html();
  1828. })
  1829. .get()
  1830. );
  1831. });
  1832. return messages;
  1833. },
  1834. /**
  1835. * Returns the clicked submit button
  1836. *
  1837. * @returns {jQuery}
  1838. */
  1839. getSubmitButton: function() {
  1840. return this.$submitButton;
  1841. },
  1842. /**
  1843. * Remove a given field
  1844. *
  1845. * @param {String|jQuery} field The field name or field element
  1846. * @returns {FormValidation.Base}
  1847. */
  1848. removeField: function(field) {
  1849. var ns = this._namespace,
  1850. fields = $([]);
  1851. switch (typeof field) {
  1852. case 'object':
  1853. fields = field;
  1854. field = field.attr('data-' + ns + '-field') || field.attr('name');
  1855. fields.attr('data-' + ns + '-field', field);
  1856. break;
  1857. case 'string':
  1858. fields = this.getFieldElements(field);
  1859. break;
  1860. default:
  1861. break;
  1862. }
  1863. if (fields.length === 0) {
  1864. return this;
  1865. }
  1866. var type = fields.attr('type'),
  1867. total = ('radio' === type || 'checkbox' === type) ? 1 : fields.length;
  1868. for (var i = 0; i < total; i++) {
  1869. var $field = fields.eq(i);
  1870. // Remove from the list of invalid fields
  1871. this.$invalidFields = this.$invalidFields.not($field);
  1872. // Update the cache
  1873. this._cacheFields[field] = this._cacheFields[field].not($field);
  1874. }
  1875. if (!this._cacheFields[field] || this._cacheFields[field].length === 0) {
  1876. delete this.options.fields[field];
  1877. }
  1878. if ('checkbox' === type || 'radio' === type) {
  1879. this._initField(field);
  1880. }
  1881. this.disableSubmitButtons(false);
  1882. // Trigger an event
  1883. this.$form.trigger($.Event(this.options.events.fieldRemoved), {
  1884. field: field,
  1885. element: fields
  1886. });
  1887. return this;
  1888. },
  1889. /**
  1890. * Reset given field
  1891. *
  1892. * @param {String|jQuery} field The field name or field element
  1893. * @param {Boolean} [resetValue] If true, the method resets field value to empty or remove checked/selected attribute (for radio/checkbox)
  1894. * @returns {FormValidation.Base}
  1895. */
  1896. resetField: function(field, resetValue) {
  1897. var ns = this._namespace,
  1898. $fields = $([]);
  1899. switch (typeof field) {
  1900. case 'object':
  1901. $fields = field;
  1902. field = field.attr('data-' + ns + '-field');
  1903. break;
  1904. case 'string':
  1905. $fields = this.getFieldElements(field);
  1906. break;
  1907. default:
  1908. break;
  1909. }
  1910. var i = 0,
  1911. total = $fields.length;
  1912. if (this.options.fields[field]) {
  1913. for (i = 0; i < total; i++) {
  1914. for (var validator in this.options.fields[field].validators) {
  1915. $fields.eq(i).removeData(ns + '.dfs.' + validator);
  1916. }
  1917. }
  1918. }
  1919. if (resetValue) {
  1920. var type = $fields.attr('type');
  1921. ('radio' === type || 'checkbox' === type) ? $fields.prop('checked', false).removeAttr('selected') : $fields.val('');
  1922. }
  1923. // Mark field as not validated yet
  1924. this.updateStatus(field, this.STATUS_NOT_VALIDATED);
  1925. for (i = 0; i < total; i++) {
  1926. $fields.eq(i).trigger($.Event(this.options.events.fieldReset), {
  1927. fv: this,
  1928. field: field,
  1929. element: $fields.eq(i),
  1930. resetValue: resetValue
  1931. });
  1932. }
  1933. return this;
  1934. },
  1935. /**
  1936. * Reset the form
  1937. *
  1938. * @param {Boolean} [resetValue] If true, the method resets field value to empty or remove checked/selected attribute (for radio/checkbox)
  1939. * @returns {FormValidation.Base}
  1940. */
  1941. resetForm: function(resetValue) {
  1942. for (var field in this.options.fields) {
  1943. this.resetField(field, resetValue);
  1944. }
  1945. this.$invalidFields = $([]);
  1946. this.$submitButton = null;
  1947. // Enable submit buttons
  1948. this.disableSubmitButtons(false);
  1949. this.$form.trigger($.Event(this.options.events.formReset), {
  1950. fv: this,
  1951. resetValue: resetValue
  1952. });
  1953. return this;
  1954. },
  1955. /**
  1956. * Revalidate given field
  1957. * It's used when you need to revalidate the field which its value is updated by other plugin
  1958. *
  1959. * @param {String|jQuery} field The field name of field element
  1960. * @returns {FormValidation.Base}
  1961. */
  1962. revalidateField: function(field) {
  1963. this.updateStatus(field, this.STATUS_NOT_VALIDATED)
  1964. .validateField(field);
  1965. return this;
  1966. },
  1967. /**
  1968. * Set the locale
  1969. *
  1970. * @param {String} locale The locale in format of countrycode_LANGUAGECODE
  1971. * @returns {FormValidation.Base}
  1972. */
  1973. setLocale: function(locale) {
  1974. this.options.locale = locale;
  1975. this.$form.trigger($.Event(this.options.events.localeChanged), {
  1976. locale: locale,
  1977. bv: this, // Support backward
  1978. fv: this
  1979. });
  1980. return this;
  1981. },
  1982. /**
  1983. * Update the option of a specific validator
  1984. *
  1985. * @param {String|jQuery} field The field name or field element
  1986. * @param {String} validator The validator name
  1987. * @param {String} option The option name
  1988. * @param {String} value The value to set
  1989. * @returns {FormValidation.Base}
  1990. */
  1991. updateOption: function(field, validator, option, value) {
  1992. var ns = this._namespace;
  1993. if ('object' === typeof field) {
  1994. field = field.attr('data-' + ns + '-field');
  1995. }
  1996. if (this.options.fields[field] && this.options.fields[field].validators[validator]) {
  1997. this.options.fields[field].validators[validator][option] = value;
  1998. this.updateStatus(field, this.STATUS_NOT_VALIDATED, validator);
  1999. }
  2000. return this;
  2001. },
  2002. /**
  2003. * Validate given container
  2004. * It can be used with isValidContainer() when you want to work with wizard form
  2005. *
  2006. * @param {String|jQuery} container The container selector or element
  2007. * @returns {FormValidation.Base}
  2008. */
  2009. validateContainer: function(container) {
  2010. var that = this,
  2011. ns = this._namespace,
  2012. fields = [],
  2013. $container = ('string' === typeof container) ? $(container) : container;
  2014. if ($container.length === 0) {
  2015. return this;
  2016. }
  2017. $container.find('[data-' + ns + '-field]').each(function() {
  2018. var $field = $(this);
  2019. if (!that._isExcluded($field)) {
  2020. fields.push($field);
  2021. }
  2022. });
  2023. var total = fields.length;
  2024. for (var i = 0; i < total; i++) {
  2025. this.validateField(fields[i]);
  2026. }
  2027. return this;
  2028. }
  2029. };
  2030. // Plugin definition
  2031. $.fn.formValidation = function(option) {
  2032. var params = arguments;
  2033. return this.each(function() {
  2034. var $this = $(this),
  2035. data = $this.data('formValidation'),
  2036. options = 'object' === typeof option && option;
  2037. if (!data) {
  2038. var framework = (options.framework || $this.attr('data-fv-framework') || 'bootstrap').toLowerCase(),
  2039. clazz = framework.substr(0, 1).toUpperCase() + framework.substr(1);
  2040. if (typeof FormValidation.Framework[clazz] === 'undefined') {
  2041. throw new Error('The class FormValidation.Framework.' + clazz + ' is not implemented');
  2042. }
  2043. data = new FormValidation.Framework[clazz](this, options);
  2044. $this.addClass('fv-form-' + framework)
  2045. .data('formValidation', data);
  2046. }
  2047. // Allow to call plugin method
  2048. if ('string' === typeof option) {
  2049. data[option].apply(data, Array.prototype.slice.call(params, 1));
  2050. }
  2051. });
  2052. };
  2053. $.fn.formValidation.Constructor = FormValidation.Base;
  2054. // Default message
  2055. $.fn.formValidation.DEFAULT_MESSAGE = 'This value is not valid';
  2056. // The default options sorted in alphabetical order
  2057. $.fn.formValidation.DEFAULT_OPTIONS = {
  2058. // The first invalid field will be focused automatically
  2059. autoFocus: true,
  2060. // Support declarative usage (setting options via HTML 5 attributes)
  2061. // Setting to false can improve the performance
  2062. declarative: true,
  2063. // The form CSS class
  2064. elementClass: 'fv-form',
  2065. // Use custom event name to avoid window.onerror being invoked by jQuery
  2066. // See #630
  2067. events: {
  2068. // Support backward
  2069. formInit: 'init.form.fv',
  2070. formPreValidate: 'prevalidate.form.fv',
  2071. formError: 'err.form.fv',
  2072. formReset: 'rst.form.fv',
  2073. formSuccess: 'success.form.fv',
  2074. fieldAdded: 'added.field.fv',
  2075. fieldRemoved: 'removed.field.fv',
  2076. fieldInit: 'init.field.fv',
  2077. fieldError: 'err.field.fv',
  2078. fieldReset: 'rst.field.fv',
  2079. fieldSuccess: 'success.field.fv',
  2080. fieldStatus: 'status.field.fv',
  2081. localeChanged: 'changed.locale.fv',
  2082. validatorError: 'err.validator.fv',
  2083. validatorSuccess: 'success.validator.fv',
  2084. validatorIgnored: 'ignored.validator.fv'
  2085. },
  2086. // Indicate fields which won't be validated
  2087. // By default, the plugin will not validate the following kind of fields:
  2088. // - disabled
  2089. // - hidden
  2090. // - invisible
  2091. //
  2092. // The setting consists of jQuery filters. Accept 3 formats:
  2093. // - A string. Use a comma to separate filter
  2094. // - An array. Each element is a filter
  2095. // - An array. Each element can be a callback function
  2096. // function($field, validator) {
  2097. // $field is jQuery object representing the field element
  2098. // validator is the BootstrapValidator instance
  2099. // return true or false;
  2100. // }
  2101. //
  2102. // The 3 following settings are equivalent:
  2103. //
  2104. // 1) ':disabled, :hidden, :not(:visible)'
  2105. // 2) [':disabled', ':hidden', ':not(:visible)']
  2106. // 3) [':disabled', ':hidden', function($field) {
  2107. // return !$field.is(':visible');
  2108. // }]
  2109. excluded: [':disabled', ':hidden', ':not(:visible)'],
  2110. // Map the field name with validator rules
  2111. fields: null,
  2112. // Live validating option
  2113. // Can be one of 3 values:
  2114. // - enabled: The plugin validates fields as soon as they are changed
  2115. // - disabled: Disable the live validating. The error messages are only shown after the form is submitted
  2116. // - submitted: The live validating is enabled after the form is submitted
  2117. live: 'enabled',
  2118. // Locale in the format of languagecode_COUNTRYCODE
  2119. locale: 'en_US',
  2120. // Default invalid message
  2121. message: null,
  2122. // The field will not be live validated if its length is less than this number of characters
  2123. threshold: null,
  2124. // Whether to be verbose when validating a field or not.
  2125. // Possible values:
  2126. // - true: when a field has multiple validators, all of them will be checked, and respectively - if errors occur in
  2127. // multiple validators, all of them will be displayed to the user
  2128. // - false: when a field has multiple validators, validation for this field will be terminated upon the first encountered error.
  2129. // Thus, only the very first error message related to this field will be displayed to the user
  2130. verbose: true,
  2131. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2132. // These options mostly are overridden by specific framework
  2133. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2134. button: {
  2135. // The submit buttons selector
  2136. // These buttons will be disabled to prevent the valid form from multiple submissions
  2137. // Don't perform validation when clicking on the submit button/input which have formnovalidate attribute
  2138. selector: '[type="submit"]:not([formnovalidate])',
  2139. // The disabled class
  2140. disabled: ''
  2141. },
  2142. control: {
  2143. // The CSS class for valid control
  2144. valid: '',
  2145. // The CSS class for invalid control
  2146. invalid: ''
  2147. },
  2148. err: {
  2149. // The CSS class of each message element
  2150. clazz: '',
  2151. // The error messages container. It can be:
  2152. // - 'tooltip' if you want to use Bootstrap tooltip to show error messages
  2153. // - 'popover' if you want to use Bootstrap popover to show error messages
  2154. // - a CSS selector indicating the container
  2155. // In the first two cases, since the tooltip/popover should be small enough, the plugin only shows only one error message
  2156. // You also can define the message container for particular field
  2157. container: null,
  2158. // Used to determine where the messages are placed
  2159. parent: null
  2160. },
  2161. // Shows ok/error/loading icons based on the field validity.
  2162. icon: {
  2163. valid: null,
  2164. invalid: null,
  2165. validating: null,
  2166. feedback: ''
  2167. },
  2168. row: {
  2169. // The CSS selector for indicating the element consists of the field
  2170. // You should adjust this option if your form group consists of many fields which not all of them need to be validated
  2171. selector: null,
  2172. valid: '',
  2173. invalid: '',
  2174. feedback: ''
  2175. }
  2176. };
  2177. }(jQuery));
  2178. ;(function($) {
  2179. // Helper methods, which can be used in validator class
  2180. FormValidation.Helper = {
  2181. /**
  2182. * Execute a callback function
  2183. *
  2184. * @param {String|Function} functionName Can be
  2185. * - name of global function
  2186. * - name of namespace function (such as A.B.C)
  2187. * - a function
  2188. * @param {Array} args The callback arguments
  2189. */
  2190. call: function(functionName, args) {
  2191. if ('function' === typeof functionName) {
  2192. return functionName.apply(this, args);
  2193. } else if ('string' === typeof functionName) {
  2194. if ('()' === functionName.substring(functionName.length - 2)) {
  2195. functionName = functionName.substring(0, functionName.length - 2);
  2196. }
  2197. var ns = functionName.split('.'),
  2198. func = ns.pop(),
  2199. context = window;
  2200. for (var i = 0; i < ns.length; i++) {
  2201. context = context[ns[i]];
  2202. }
  2203. return (typeof context[func] === 'undefined') ? null : context[func].apply(this, args);
  2204. }
  2205. },
  2206. /**
  2207. * Validate a date
  2208. *
  2209. * @param {Number} year The full year in 4 digits
  2210. * @param {Number} month The month number
  2211. * @param {Number} day The day number
  2212. * @param {Boolean} [notInFuture] If true, the date must not be in the future
  2213. * @returns {Boolean}
  2214. */
  2215. date: function(year, month, day, notInFuture) {
  2216. if (isNaN(year) || isNaN(month) || isNaN(day)) {
  2217. return false;
  2218. }
  2219. if (day.length > 2 || month.length > 2 || year.length > 4) {
  2220. return false;
  2221. }
  2222. day = parseInt(day, 10);
  2223. month = parseInt(month, 10);
  2224. year = parseInt(year, 10);
  2225. if (year < 1000 || year > 9999 || month <= 0 || month > 12) {
  2226. return false;
  2227. }
  2228. var numDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  2229. // Update the number of days in Feb of leap year
  2230. if (year % 400 === 0 || (year % 100 !== 0 && year % 4 === 0)) {
  2231. numDays[1] = 29;
  2232. }
  2233. // Check the day
  2234. if (day <= 0 || day > numDays[month - 1]) {
  2235. return false;
  2236. }
  2237. if (notInFuture === true) {
  2238. var currentDate = new Date(),
  2239. currentYear = currentDate.getFullYear(),
  2240. currentMonth = currentDate.getMonth(),
  2241. currentDay = currentDate.getDate();
  2242. return (year < currentYear
  2243. || (year === currentYear && month - 1 < currentMonth)
  2244. || (year === currentYear && month - 1 === currentMonth && day < currentDay));
  2245. }
  2246. return true;
  2247. },
  2248. /**
  2249. * Format a string
  2250. * It's used to format the error message
  2251. * format('The field must between %s and %s', [10, 20]) = 'The field must between 10 and 20'
  2252. *
  2253. * @param {String} message
  2254. * @param {Array} parameters
  2255. * @returns {String}
  2256. */
  2257. format: function(message, parameters) {
  2258. if (!$.isArray(parameters)) {
  2259. parameters = [parameters];
  2260. }
  2261. for (var i in parameters) {
  2262. message = message.replace('%s', parameters[i]);
  2263. }
  2264. return message;
  2265. },
  2266. /**
  2267. * Implement Luhn validation algorithm
  2268. * Credit to https://gist.github.com/ShirtlessKirk/2134376
  2269. *
  2270. * @see http://en.wikipedia.org/wiki/Luhn
  2271. * @param {String} value
  2272. * @returns {Boolean}
  2273. */
  2274. luhn: function(value) {
  2275. var length = value.length,
  2276. mul = 0,
  2277. prodArr = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]],
  2278. sum = 0;
  2279. while (length--) {
  2280. sum += prodArr[mul][parseInt(value.charAt(length), 10)];
  2281. mul ^= 1;
  2282. }
  2283. return (sum % 10 === 0 && sum > 0);
  2284. },
  2285. /**
  2286. * Implement modulus 11, 10 (ISO 7064) algorithm
  2287. *
  2288. * @param {String} value
  2289. * @returns {Boolean}
  2290. */
  2291. mod11And10: function(value) {
  2292. var check = 5,
  2293. length = value.length;
  2294. for (var i = 0; i < length; i++) {
  2295. check = (((check || 10) * 2) % 11 + parseInt(value.charAt(i), 10)) % 10;
  2296. }
  2297. return (check === 1);
  2298. },
  2299. /**
  2300. * Implements Mod 37, 36 (ISO 7064) algorithm
  2301. * Usages:
  2302. * mod37And36('A12425GABC1234002M')
  2303. * mod37And36('002006673085', '0123456789')
  2304. *
  2305. * @param {String} value
  2306. * @param {String} [alphabet]
  2307. * @returns {Boolean}
  2308. */
  2309. mod37And36: function(value, alphabet) {
  2310. alphabet = alphabet || '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  2311. var modulus = alphabet.length,
  2312. length = value.length,
  2313. check = Math.floor(modulus / 2);
  2314. for (var i = 0; i < length; i++) {
  2315. check = (((check || modulus) * 2) % (modulus + 1) + alphabet.indexOf(value.charAt(i))) % modulus;
  2316. }
  2317. return (check === 1);
  2318. }
  2319. };
  2320. }(jQuery));
  2321. ;(function($) {
  2322. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  2323. 'en_US': {
  2324. base64: {
  2325. 'default': 'Please enter a valid base 64 encoded'
  2326. }
  2327. }
  2328. });
  2329. FormValidation.Validator.base64 = {
  2330. /**
  2331. * Return true if the input value is a base 64 encoded string.
  2332. *
  2333. * @param {FormValidation.Base} validator The validator plugin instance
  2334. * @param {jQuery} $field Field element
  2335. * @param {Object} options Can consist of the following keys:
  2336. * - message: The invalid message
  2337. * @returns {Boolean}
  2338. */
  2339. validate: function(validator, $field, options, validatorName) {
  2340. var value = validator.getFieldValue($field, validatorName);
  2341. if (value === '') {
  2342. return true;
  2343. }
  2344. return /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$/.test(value);
  2345. }
  2346. };
  2347. }(jQuery));
  2348. ;(function($) {
  2349. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  2350. 'en_US': {
  2351. between: {
  2352. 'default': 'Please enter a value between %s and %s',
  2353. notInclusive: 'Please enter a value between %s and %s strictly'
  2354. }
  2355. }
  2356. });
  2357. FormValidation.Validator.between = {
  2358. html5Attributes: {
  2359. message: 'message',
  2360. min: 'min',
  2361. max: 'max',
  2362. inclusive: 'inclusive'
  2363. },
  2364. enableByHtml5: function($field) {
  2365. if ('range' === $field.attr('type')) {
  2366. return {
  2367. min: $field.attr('min'),
  2368. max: $field.attr('max')
  2369. };
  2370. }
  2371. return false;
  2372. },
  2373. /**
  2374. * Return true if the input value is between (strictly or not) two given numbers
  2375. *
  2376. * @param {FormValidation.Base} validator The validator plugin instance
  2377. * @param {jQuery} $field Field element
  2378. * @param {Object} options Can consist of the following keys:
  2379. * - min
  2380. * - max
  2381. *
  2382. * The min, max keys define the number which the field value compares to. min, max can be
  2383. * - A number
  2384. * - Name of field which its value defines the number
  2385. * - Name of callback function that returns the number
  2386. * - A callback function that returns the number
  2387. *
  2388. * - inclusive [optional]: Can be true or false. Default is true
  2389. * - message: The invalid message
  2390. * @returns {Boolean|Object}
  2391. */
  2392. validate: function(validator, $field, options, validatorName) {
  2393. var value = validator.getFieldValue($field, validatorName);
  2394. if (value === '') {
  2395. return true;
  2396. }
  2397. value = this._format(value);
  2398. var locale = validator.getLocale(),
  2399. min = $.isNumeric(options.min) ? options.min : validator.getDynamicOption($field, options.min),
  2400. max = $.isNumeric(options.max) ? options.max : validator.getDynamicOption($field, options.max),
  2401. minValue = this._format(min),
  2402. maxValue = this._format(max);
  2403. return (options.inclusive === true || options.inclusive === undefined)
  2404. ? {
  2405. valid: $.isNumeric(value) && parseFloat(value) >= minValue && parseFloat(value) <= maxValue,
  2406. message: FormValidation.Helper.format(options.message || FormValidation.I18n[locale].between['default'], [min, max])
  2407. }
  2408. : {
  2409. valid: $.isNumeric(value) && parseFloat(value) > minValue && parseFloat(value) < maxValue,
  2410. message: FormValidation.Helper.format(options.message || FormValidation.I18n[locale].between.notInclusive, [min, max])
  2411. };
  2412. },
  2413. _format: function(value) {
  2414. return (value + '').replace(',', '.');
  2415. }
  2416. };
  2417. }(jQuery));
  2418. ;(function($) {
  2419. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  2420. 'en_US': {
  2421. bic: {
  2422. 'default': 'Please enter a valid BIC number'
  2423. }
  2424. }
  2425. });
  2426. FormValidation.Validator.bic = {
  2427. /**
  2428. * Validate an Business Identifier Code (BIC), also known as ISO 9362, SWIFT-BIC, SWIFT ID or SWIFT code
  2429. *
  2430. * For more information see http://en.wikipedia.org/wiki/ISO_9362
  2431. *
  2432. * @todo The 5 and 6 characters are an ISO 3166-1 country code, this could also be validated
  2433. * @param {FormValidation.Base} validator The validator plugin instance
  2434. * @param {jQuery} $field Field element
  2435. * @param {Object} options Can consist of the following keys:
  2436. * - message: The invalid message
  2437. * @returns {Object}
  2438. */
  2439. validate: function(validator, $field, options, validatorName) {
  2440. var value = validator.getFieldValue($field, validatorName);
  2441. if (value === '') {
  2442. return true;
  2443. }
  2444. return /^[a-zA-Z]{6}[a-zA-Z0-9]{2}([a-zA-Z0-9]{3})?$/.test(value);
  2445. }
  2446. };
  2447. }(jQuery));
  2448. ;(function($) {
  2449. FormValidation.Validator.blank = {
  2450. /**
  2451. * Placeholder validator that can be used to display a custom validation message
  2452. * returned from the server
  2453. * Example:
  2454. *
  2455. * (1) a "blank" validator is applied to an input field.
  2456. * (2) data is entered via the UI that is unable to be validated client-side.
  2457. * (3) server returns a 400 with JSON data that contains the field that failed
  2458. * validation and an associated message.
  2459. * (4) ajax 400 call handler does the following:
  2460. *
  2461. * fv.updateMessage(field, 'blank', errorMessage);
  2462. * fv.updateStatus(field, 'INVALID', 'blank');
  2463. *
  2464. * @param {FormValidation.Base} validator The validator plugin instance
  2465. * @param {jQuery} $field Field element
  2466. * @param {Object} options Can consist of the following keys:
  2467. * - message: The invalid message
  2468. * @returns {Boolean}
  2469. */
  2470. validate: function(validator, $field, options, validatorName) {
  2471. return true;
  2472. }
  2473. };
  2474. }(jQuery));
  2475. ;(function($) {
  2476. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  2477. 'en_US': {
  2478. callback: {
  2479. 'default': 'Please enter a valid value'
  2480. }
  2481. }
  2482. });
  2483. FormValidation.Validator.callback = {
  2484. priority: 999,
  2485. html5Attributes: {
  2486. message: 'message',
  2487. callback: 'callback'
  2488. },
  2489. /**
  2490. * Return result from the callback method
  2491. *
  2492. * @param {FormValidation.Base} validator The validator plugin instance
  2493. * @param {jQuery} $field Field element
  2494. * @param {Object} options Can consist of the following keys:
  2495. * - callback: The callback method that passes parameters:
  2496. * callback: function(fieldValue, validator, $field) {
  2497. * // fieldValue is the value of field
  2498. * // validator is instance of FormValidation.Base
  2499. * // $field is the field element
  2500. * }
  2501. * - message: The invalid message
  2502. * @returns {Deferred}
  2503. */
  2504. validate: function(validator, $field, options, validatorName) {
  2505. var value = validator.getFieldValue($field, validatorName),
  2506. dfd = new $.Deferred(),
  2507. result = { valid: true };
  2508. if (options.callback) {
  2509. var response = FormValidation.Helper.call(options.callback, [value, validator, $field]);
  2510. result = ('boolean' === typeof response || null === response) ? { valid: response } : response;
  2511. }
  2512. dfd.resolve($field, validatorName, result);
  2513. return dfd;
  2514. }
  2515. };
  2516. }(jQuery));
  2517. ;(function($) {
  2518. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  2519. 'en_US': {
  2520. choice: {
  2521. 'default': 'Please enter a valid value',
  2522. less: 'Please choose %s options at minimum',
  2523. more: 'Please choose %s options at maximum',
  2524. between: 'Please choose %s - %s options'
  2525. }
  2526. }
  2527. });
  2528. FormValidation.Validator.choice = {
  2529. html5Attributes: {
  2530. message: 'message',
  2531. min: 'min',
  2532. max: 'max'
  2533. },
  2534. /**
  2535. * Check if the number of checked boxes are less or more than a given number
  2536. *
  2537. * @param {FormValidation.Base} validator The validator plugin instance
  2538. * @param {jQuery} $field Field element
  2539. * @param {Object} options Consists of following keys:
  2540. * - min
  2541. * - max
  2542. *
  2543. * At least one of two keys is required
  2544. * The min, max keys define the number which the field value compares to. min, max can be
  2545. * - A number
  2546. * - Name of field which its value defines the number
  2547. * - Name of callback function that returns the number
  2548. * - A callback function that returns the number
  2549. *
  2550. * - message: The invalid message
  2551. * @returns {Object}
  2552. */
  2553. validate: function(validator, $field, options, validatorName) {
  2554. var locale = validator.getLocale(),
  2555. ns = validator.getNamespace(),
  2556. numChoices = $field.is('select')
  2557. ? validator.getFieldElements($field.attr('data-' + ns + '-field')).find('option').filter(':selected').length
  2558. : validator.getFieldElements($field.attr('data-' + ns + '-field')).filter(':checked').length,
  2559. min = options.min ? ($.isNumeric(options.min) ? options.min : validator.getDynamicOption($field, options.min)) : null,
  2560. max = options.max ? ($.isNumeric(options.max) ? options.max : validator.getDynamicOption($field, options.max)) : null,
  2561. isValid = true,
  2562. message = options.message || FormValidation.I18n[locale].choice['default'];
  2563. if ((min && numChoices < parseInt(min, 10)) || (max && numChoices > parseInt(max, 10))) {
  2564. isValid = false;
  2565. }
  2566. switch (true) {
  2567. case (!!min && !!max):
  2568. message = FormValidation.Helper.format(options.message || FormValidation.I18n[locale].choice.between, [parseInt(min, 10), parseInt(max, 10)]);
  2569. break;
  2570. case (!!min):
  2571. message = FormValidation.Helper.format(options.message || FormValidation.I18n[locale].choice.less, parseInt(min, 10));
  2572. break;
  2573. case (!!max):
  2574. message = FormValidation.Helper.format(options.message || FormValidation.I18n[locale].choice.more, parseInt(max, 10));
  2575. break;
  2576. default:
  2577. break;
  2578. }
  2579. return { valid: isValid, message: message };
  2580. }
  2581. };
  2582. }(jQuery));
  2583. ;(function($) {
  2584. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  2585. 'en_US': {
  2586. color: {
  2587. 'default': 'Please enter a valid color'
  2588. }
  2589. }
  2590. });
  2591. FormValidation.Validator.color = {
  2592. html5Attributes: {
  2593. message: 'message',
  2594. type: 'type'
  2595. },
  2596. enableByHtml5: function($field) {
  2597. return ('color' === $field.attr('type'));
  2598. },
  2599. SUPPORTED_TYPES: [
  2600. 'hex', 'rgb', 'rgba', 'hsl', 'hsla', 'keyword'
  2601. ],
  2602. KEYWORD_COLORS: [
  2603. // Colors start with A
  2604. 'aliceblue', 'antiquewhite', 'aqua', 'aquamarine', 'azure',
  2605. // B
  2606. 'beige', 'bisque', 'black', 'blanchedalmond', 'blue', 'blueviolet', 'brown', 'burlywood',
  2607. // C
  2608. 'cadetblue', 'chartreuse', 'chocolate', 'coral', 'cornflowerblue', 'cornsilk', 'crimson', 'cyan',
  2609. // D
  2610. 'darkblue', 'darkcyan', 'darkgoldenrod', 'darkgray', 'darkgreen', 'darkgrey', 'darkkhaki', 'darkmagenta',
  2611. 'darkolivegreen', 'darkorange', 'darkorchid', 'darkred', 'darksalmon', 'darkseagreen', 'darkslateblue',
  2612. 'darkslategray', 'darkslategrey', 'darkturquoise', 'darkviolet', 'deeppink', 'deepskyblue', 'dimgray',
  2613. 'dimgrey', 'dodgerblue',
  2614. // F
  2615. 'firebrick', 'floralwhite', 'forestgreen', 'fuchsia',
  2616. // G
  2617. 'gainsboro', 'ghostwhite', 'gold', 'goldenrod', 'gray', 'green', 'greenyellow', 'grey',
  2618. // H
  2619. 'honeydew', 'hotpink',
  2620. // I
  2621. 'indianred', 'indigo', 'ivory',
  2622. // K
  2623. 'khaki',
  2624. // L
  2625. 'lavender', 'lavenderblush', 'lawngreen', 'lemonchiffon', 'lightblue', 'lightcoral', 'lightcyan',
  2626. 'lightgoldenrodyellow', 'lightgray', 'lightgreen', 'lightgrey', 'lightpink', 'lightsalmon', 'lightseagreen',
  2627. 'lightskyblue', 'lightslategray', 'lightslategrey', 'lightsteelblue', 'lightyellow', 'lime', 'limegreen',
  2628. 'linen',
  2629. // M
  2630. 'magenta', 'maroon', 'mediumaquamarine', 'mediumblue', 'mediumorchid', 'mediumpurple', 'mediumseagreen',
  2631. 'mediumslateblue', 'mediumspringgreen', 'mediumturquoise', 'mediumvioletred', 'midnightblue', 'mintcream',
  2632. 'mistyrose', 'moccasin',
  2633. // N
  2634. 'navajowhite', 'navy',
  2635. // O
  2636. 'oldlace', 'olive', 'olivedrab', 'orange', 'orangered', 'orchid',
  2637. // P
  2638. 'palegoldenrod', 'palegreen', 'paleturquoise', 'palevioletred', 'papayawhip', 'peachpuff', 'peru', 'pink',
  2639. 'plum', 'powderblue', 'purple',
  2640. // R
  2641. 'red', 'rosybrown', 'royalblue',
  2642. // S
  2643. 'saddlebrown', 'salmon', 'sandybrown', 'seagreen', 'seashell', 'sienna', 'silver', 'skyblue', 'slateblue',
  2644. 'slategray', 'slategrey', 'snow', 'springgreen', 'steelblue',
  2645. // T
  2646. 'tan', 'teal', 'thistle', 'tomato', 'transparent', 'turquoise',
  2647. // V
  2648. 'violet',
  2649. // W
  2650. 'wheat', 'white', 'whitesmoke',
  2651. // Y
  2652. 'yellow', 'yellowgreen'
  2653. ],
  2654. /**
  2655. * Return true if the input value is a valid color
  2656. *
  2657. * @param {FormValidation.Base} validator The validator plugin instance
  2658. * @param {jQuery} $field Field element
  2659. * @param {Object} options Can consist of the following keys:
  2660. * - message: The invalid message
  2661. * - type: The array of valid color types
  2662. * @returns {Boolean}
  2663. */
  2664. validate: function(validator, $field, options, validatorName) {
  2665. var value = validator.getFieldValue($field, validatorName);
  2666. if (value === '') {
  2667. return true;
  2668. }
  2669. // Only accept 6 hex character values due to the HTML 5 spec
  2670. // See http://www.w3.org/TR/html-markup/input.color.html#input.color.attrs.value
  2671. if (this.enableByHtml5($field)) {
  2672. return /^#[0-9A-F]{6}$/i.test(value);
  2673. }
  2674. var types = options.type || this.SUPPORTED_TYPES;
  2675. if (!$.isArray(types)) {
  2676. types = types.replace(/s/g, '').split(',');
  2677. }
  2678. var method,
  2679. type,
  2680. isValid = false;
  2681. for (var i = 0; i < types.length; i++) {
  2682. type = types[i];
  2683. method = '_' + type.toLowerCase();
  2684. isValid = isValid || this[method](value);
  2685. if (isValid) {
  2686. return true;
  2687. }
  2688. }
  2689. return false;
  2690. },
  2691. _hex: function(value) {
  2692. return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(value);
  2693. },
  2694. _hsl: function(value) {
  2695. return /^hsl\((\s*(-?\d+)\s*,)(\s*(\b(0?\d{1,2}|100)\b%)\s*,)(\s*(\b(0?\d{1,2}|100)\b%)\s*)\)$/.test(value);
  2696. },
  2697. _hsla: function(value) {
  2698. return /^hsla\((\s*(-?\d+)\s*,)(\s*(\b(0?\d{1,2}|100)\b%)\s*,){2}(\s*(0?(\.\d+)?|1(\.0+)?)\s*)\)$/.test(value);
  2699. },
  2700. _keyword: function(value) {
  2701. return $.inArray(value, this.KEYWORD_COLORS) >= 0;
  2702. },
  2703. _rgb: function(value) {
  2704. var regexInteger = /^rgb\((\s*(\b([01]?\d{1,2}|2[0-4]\d|25[0-5])\b)\s*,){2}(\s*(\b([01]?\d{1,2}|2[0-4]\d|25[0-5])\b)\s*)\)$/,
  2705. regexPercent = /^rgb\((\s*(\b(0?\d{1,2}|100)\b%)\s*,){2}(\s*(\b(0?\d{1,2}|100)\b%)\s*)\)$/;
  2706. return regexInteger.test(value) || regexPercent.test(value);
  2707. },
  2708. _rgba: function(value) {
  2709. var regexInteger = /^rgba\((\s*(\b([01]?\d{1,2}|2[0-4]\d|25[0-5])\b)\s*,){3}(\s*(0?(\.\d+)?|1(\.0+)?)\s*)\)$/,
  2710. regexPercent = /^rgba\((\s*(\b(0?\d{1,2}|100)\b%)\s*,){3}(\s*(0?(\.\d+)?|1(\.0+)?)\s*)\)$/;
  2711. return regexInteger.test(value) || regexPercent.test(value);
  2712. }
  2713. };
  2714. }(jQuery));
  2715. ;(function($) {
  2716. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  2717. 'en_US': {
  2718. creditCard: {
  2719. 'default': 'Please enter a valid credit card number'
  2720. }
  2721. }
  2722. });
  2723. FormValidation.Validator.creditCard = {
  2724. /**
  2725. * Return true if the input value is valid credit card number
  2726. * Based on https://gist.github.com/DiegoSalazar/4075533
  2727. *
  2728. * @param {FormValidation.Base} validator The validator plugin instance
  2729. * @param {jQuery} $field Field element
  2730. * @param {Object} [options] Can consist of the following key:
  2731. * - message: The invalid message
  2732. * @returns {Boolean|Object}
  2733. */
  2734. validate: function(validator, $field, options, validatorName) {
  2735. var value = validator.getFieldValue($field, validatorName);
  2736. if (value === '') {
  2737. return true;
  2738. }
  2739. // Accept only digits, dashes or spaces
  2740. if (/[^0-9-\s]+/.test(value)) {
  2741. return false;
  2742. }
  2743. value = value.replace(/\D/g, '');
  2744. if (!FormValidation.Helper.luhn(value)) {
  2745. return false;
  2746. }
  2747. // Validate the card number based on prefix (IIN ranges) and length
  2748. var cards = {
  2749. AMERICAN_EXPRESS: {
  2750. length: [15],
  2751. prefix: ['34', '37']
  2752. },
  2753. DANKORT: {
  2754. length: [16],
  2755. prefix: ['5019']
  2756. },
  2757. DINERS_CLUB: {
  2758. length: [14],
  2759. prefix: ['300', '301', '302', '303', '304', '305', '36']
  2760. },
  2761. DINERS_CLUB_US: {
  2762. length: [16],
  2763. prefix: ['54', '55']
  2764. },
  2765. DISCOVER: {
  2766. length: [16],
  2767. prefix: ['6011', '622126', '622127', '622128', '622129', '62213',
  2768. '62214', '62215', '62216', '62217', '62218', '62219',
  2769. '6222', '6223', '6224', '6225', '6226', '6227', '6228',
  2770. '62290', '62291', '622920', '622921', '622922', '622923',
  2771. '622924', '622925', '644', '645', '646', '647', '648',
  2772. '649', '65']
  2773. },
  2774. ELO: {
  2775. length: [16],
  2776. prefix: ['4011', '4312', '4389', '4514', '4573', '4576',
  2777. '5041', '5066', '5067', '509',
  2778. '6277', '6362', '6363', '650', '6516', '6550']
  2779. },
  2780. FORBRUGSFORENINGEN: {
  2781. length: [16],
  2782. prefix: ['600722']
  2783. },
  2784. JCB: {
  2785. length: [16],
  2786. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  2787. },
  2788. LASER: {
  2789. length: [16, 17, 18, 19],
  2790. prefix: ['6304', '6706', '6771', '6709']
  2791. },
  2792. MAESTRO: {
  2793. length: [12, 13, 14, 15, 16, 17, 18, 19],
  2794. prefix: ['5018', '5020', '5038', '5868', '6304', '6759', '6761', '6762', '6763', '6764', '6765', '6766']
  2795. },
  2796. MASTERCARD: {
  2797. length: [16],
  2798. prefix: ['51', '52', '53', '54', '55']
  2799. },
  2800. SOLO: {
  2801. length: [16, 18, 19],
  2802. prefix: ['6334', '6767']
  2803. },
  2804. UNIONPAY: {
  2805. length: [16, 17, 18, 19],
  2806. prefix: ['622126', '622127', '622128', '622129', '62213', '62214',
  2807. '62215', '62216', '62217', '62218', '62219', '6222', '6223',
  2808. '6224', '6225', '6226', '6227', '6228', '62290', '62291',
  2809. '622920', '622921', '622922', '622923', '622924', '622925']
  2810. },
  2811. VISA_ELECTRON: {
  2812. length: [16],
  2813. prefix: ['4026', '417500', '4405', '4508', '4844', '4913', '4917']
  2814. },
  2815. VISA: {
  2816. length: [16],
  2817. prefix: ['4']
  2818. }
  2819. };
  2820. var type, i;
  2821. for (type in cards) {
  2822. for (i in cards[type].prefix) {
  2823. if (value.substr(0, cards[type].prefix[i].length) === cards[type].prefix[i] // Check the prefix
  2824. && $.inArray(value.length, cards[type].length) !== -1) // and length
  2825. {
  2826. return {
  2827. valid: true,
  2828. type: type
  2829. };
  2830. }
  2831. }
  2832. }
  2833. return false;
  2834. }
  2835. };
  2836. }(jQuery));
  2837. ;(function($) {
  2838. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  2839. 'en_US': {
  2840. cusip: {
  2841. 'default': 'Please enter a valid CUSIP number'
  2842. }
  2843. }
  2844. });
  2845. FormValidation.Validator.cusip = {
  2846. /**
  2847. * Validate a CUSIP number
  2848. *
  2849. * @see http://en.wikipedia.org/wiki/CUSIP
  2850. * @param {FormValidation.Base} validator The validator plugin instance
  2851. * @param {jQuery} $field Field element
  2852. * @param {Object} [options] Can consist of the following keys:
  2853. * - message: The invalid message
  2854. * @returns {Boolean}
  2855. */
  2856. validate: function(validator, $field, options, validatorName) {
  2857. var value = validator.getFieldValue($field, validatorName);
  2858. if (value === '') {
  2859. return true;
  2860. }
  2861. value = value.toUpperCase();
  2862. if (!/^[0-9A-Z]{9}$/.test(value)) {
  2863. return false;
  2864. }
  2865. var converted = $.map(value.split(''), function(item) {
  2866. var code = item.charCodeAt(0);
  2867. return (code >= 'A'.charCodeAt(0) && code <= 'Z'.charCodeAt(0))
  2868. // Replace A, B, C, ..., Z with 10, 11, ..., 35
  2869. ? (code - 'A'.charCodeAt(0) + 10)
  2870. : item;
  2871. }),
  2872. length = converted.length,
  2873. sum = 0;
  2874. for (var i = 0; i < length - 1; i++) {
  2875. var num = parseInt(converted[i], 10);
  2876. if (i % 2 !== 0) {
  2877. num *= 2;
  2878. }
  2879. if (num > 9) {
  2880. num -= 9;
  2881. }
  2882. sum += num;
  2883. }
  2884. sum = (10 - (sum % 10)) % 10;
  2885. return sum === parseInt(converted[length - 1], 10);
  2886. }
  2887. };
  2888. }(jQuery));
  2889. ;(function($) {
  2890. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  2891. 'en_US': {
  2892. cvv: {
  2893. 'default': 'Please enter a valid CVV number'
  2894. }
  2895. }
  2896. });
  2897. FormValidation.Validator.cvv = {
  2898. html5Attributes: {
  2899. message: 'message',
  2900. ccfield: 'creditCardField'
  2901. },
  2902. /**
  2903. * Bind the validator on the live change of the credit card field
  2904. *
  2905. * @param {FormValidation.Base} validator The validator plugin instance
  2906. * @param {jQuery} $field Field element
  2907. * @param {Object} options Consists of the following key:
  2908. * - creditCardField: The credit card number field
  2909. */
  2910. init: function(validator, $field, options, validatorName) {
  2911. if (options.creditCardField) {
  2912. var creditCardField = validator.getFieldElements(options.creditCardField);
  2913. validator.onLiveChange(creditCardField, 'live_' + validatorName, function() {
  2914. var status = validator.getStatus($field, validatorName);
  2915. if (status !== validator.STATUS_NOT_VALIDATED) {
  2916. validator.revalidateField($field);
  2917. }
  2918. });
  2919. }
  2920. },
  2921. /**
  2922. * Unbind the validator on the live change of the credit card field
  2923. *
  2924. * @param {FormValidation.Base} validator The validator plugin instance
  2925. * @param {jQuery} $field Field element
  2926. * @param {Object} options Consists of the following key:
  2927. * - creditCardField: The credit card number field
  2928. */
  2929. destroy: function(validator, $field, options, validatorName) {
  2930. if (options.creditCardField) {
  2931. var creditCardField = validator.getFieldElements(options.creditCardField);
  2932. validator.offLiveChange(creditCardField, 'live_' + validatorName);
  2933. }
  2934. },
  2935. /**
  2936. * Return true if the input value is a valid CVV number.
  2937. *
  2938. * @param {FormValidation.Base} validator The validator plugin instance
  2939. * @param {jQuery} $field Field element
  2940. * @param {Object} options Can consist of the following keys:
  2941. * - creditCardField: The credit card number field. It can be null
  2942. * - message: The invalid message
  2943. * @returns {Boolean}
  2944. */
  2945. validate: function(validator, $field, options, validatorName) {
  2946. var value = validator.getFieldValue($field, validatorName);
  2947. if (value === '') {
  2948. return true;
  2949. }
  2950. if (!/^[0-9]{3,4}$/.test(value)) {
  2951. return false;
  2952. }
  2953. if (!options.creditCardField) {
  2954. return true;
  2955. }
  2956. // Get the credit card number
  2957. var creditCard = validator.getFieldValue(options.creditCardField, 'creditCard');
  2958. if (creditCard === null || creditCard === '') {
  2959. return true;
  2960. }
  2961. creditCard = creditCard.replace(/\D/g, '');
  2962. // Supported credit card types
  2963. var cards = {
  2964. AMERICAN_EXPRESS: {
  2965. length: [15],
  2966. prefix: ['34', '37']
  2967. },
  2968. DANKORT: {
  2969. length: [16],
  2970. prefix: ['5019']
  2971. },
  2972. DINERS_CLUB: {
  2973. length: [14],
  2974. prefix: ['300', '301', '302', '303', '304', '305', '36']
  2975. },
  2976. DINERS_CLUB_US: {
  2977. length: [16],
  2978. prefix: ['54', '55']
  2979. },
  2980. DISCOVER: {
  2981. length: [16],
  2982. prefix: ['6011', '622126', '622127', '622128', '622129', '62213',
  2983. '62214', '62215', '62216', '62217', '62218', '62219',
  2984. '6222', '6223', '6224', '6225', '6226', '6227', '6228',
  2985. '62290', '62291', '622920', '622921', '622922', '622923',
  2986. '622924', '622925', '644', '645', '646', '647', '648',
  2987. '649', '65']
  2988. },
  2989. ELO: {
  2990. length: [16],
  2991. prefix: ['4011', '4312', '4389', '4514', '4573', '4576',
  2992. '5041', '5066', '5067', '509',
  2993. '6277', '6362', '6363', '650', '6516', '6550']
  2994. },
  2995. FORBRUGSFORENINGEN: {
  2996. length: [16],
  2997. prefix: ['600722']
  2998. },
  2999. JCB: {
  3000. length: [16],
  3001. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  3002. },
  3003. LASER: {
  3004. length: [16, 17, 18, 19],
  3005. prefix: ['6304', '6706', '6771', '6709']
  3006. },
  3007. MAESTRO: {
  3008. length: [12, 13, 14, 15, 16, 17, 18, 19],
  3009. prefix: ['5018', '5020', '5038', '5868', '6304', '6759', '6761', '6762', '6763', '6764', '6765', '6766']
  3010. },
  3011. MASTERCARD: {
  3012. length: [16],
  3013. prefix: ['51', '52', '53', '54', '55']
  3014. },
  3015. SOLO: {
  3016. length: [16, 18, 19],
  3017. prefix: ['6334', '6767']
  3018. },
  3019. UNIONPAY: {
  3020. length: [16, 17, 18, 19],
  3021. prefix: ['622126', '622127', '622128', '622129', '62213', '62214',
  3022. '62215', '62216', '62217', '62218', '62219', '6222', '6223',
  3023. '6224', '6225', '6226', '6227', '6228', '62290', '62291',
  3024. '622920', '622921', '622922', '622923', '622924', '622925']
  3025. },
  3026. VISA_ELECTRON: {
  3027. length: [16],
  3028. prefix: ['4026', '417500', '4405', '4508', '4844', '4913', '4917']
  3029. },
  3030. VISA: {
  3031. length: [16],
  3032. prefix: ['4']
  3033. }
  3034. };
  3035. var type, i, creditCardType = null;
  3036. for (type in cards) {
  3037. for (i in cards[type].prefix) {
  3038. if (creditCard.substr(0, cards[type].prefix[i].length) === cards[type].prefix[i] // Check the prefix
  3039. && $.inArray(creditCard.length, cards[type].length) !== -1) // and length
  3040. {
  3041. creditCardType = type;
  3042. break;
  3043. }
  3044. }
  3045. }
  3046. return (creditCardType === null)
  3047. ? false
  3048. : (('AMERICAN_EXPRESS' === creditCardType) ? (value.length === 4) : (value.length === 3));
  3049. }
  3050. };
  3051. }(jQuery));
  3052. ;(function($) {
  3053. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  3054. 'en_US': {
  3055. date: {
  3056. 'default': 'Please enter a valid date',
  3057. min: 'Please enter a date after %s',
  3058. max: 'Please enter a date before %s',
  3059. range: 'Please enter a date in the range %s - %s'
  3060. }
  3061. }
  3062. });
  3063. FormValidation.Validator.date = {
  3064. html5Attributes: {
  3065. message: 'message',
  3066. format: 'format',
  3067. min: 'min',
  3068. max: 'max',
  3069. separator: 'separator'
  3070. },
  3071. /**
  3072. * Return true if the input value is valid date
  3073. *
  3074. * @param {FormValidation.Base} validator The validator plugin instance
  3075. * @param {jQuery} $field Field element
  3076. * @param {Object} options Can consist of the following keys:
  3077. * - message: The invalid message
  3078. * - min: the minimum date
  3079. * - max: the maximum date
  3080. * - separator: Use to separate the date, month, and year.
  3081. * By default, it is /
  3082. * - format: The date format. Default is MM/DD/YYYY
  3083. * The format can be:
  3084. *
  3085. * i) date: Consist of DD, MM, YYYY parts which are separated by the separator option
  3086. * ii) date and time:
  3087. * The time can consist of h, m, s parts which are separated by :
  3088. * ii) date, time and A (indicating AM or PM)
  3089. * @returns {Boolean|Object}
  3090. */
  3091. validate: function(validator, $field, options, validatorName) {
  3092. var value = validator.getFieldValue($field, validatorName);
  3093. if (value === '') {
  3094. return true;
  3095. }
  3096. options.format = options.format || 'MM/DD/YYYY';
  3097. // #683: Force the format to YYYY-MM-DD as the default browser behaviour when using type="date" attribute
  3098. if ($field.attr('type') === 'date') {
  3099. options.format = 'YYYY-MM-DD';
  3100. }
  3101. var locale = validator.getLocale(),
  3102. message = options.message || FormValidation.I18n[locale].date['default'],
  3103. formats = options.format.split(' '),
  3104. dateFormat = formats[0],
  3105. timeFormat = (formats.length > 1) ? formats[1] : null,
  3106. amOrPm = (formats.length > 2) ? formats[2] : null,
  3107. sections = value.split(' '),
  3108. date = sections[0],
  3109. time = (sections.length > 1) ? sections[1] : null;
  3110. if (formats.length !== sections.length) {
  3111. return {
  3112. valid: false,
  3113. message: message
  3114. };
  3115. }
  3116. // Determine the separator
  3117. var separator = options.separator;
  3118. if (!separator) {
  3119. separator = (date.indexOf('/') !== -1)
  3120. ? '/'
  3121. : ((date.indexOf('-') !== -1) ? '-' : ((date.indexOf('.') !== -1) ? '.' : null));
  3122. }
  3123. if (separator === null || date.indexOf(separator) === -1) {
  3124. return {
  3125. valid: false,
  3126. message: message
  3127. };
  3128. }
  3129. // Determine the date
  3130. date = date.split(separator);
  3131. dateFormat = dateFormat.split(separator);
  3132. if (date.length !== dateFormat.length) {
  3133. return {
  3134. valid: false,
  3135. message: message
  3136. };
  3137. }
  3138. var year = date[$.inArray('YYYY', dateFormat)],
  3139. month = date[$.inArray('MM', dateFormat)],
  3140. day = date[$.inArray('DD', dateFormat)];
  3141. if (!year || !month || !day || year.length !== 4) {
  3142. return {
  3143. valid: false,
  3144. message: message
  3145. };
  3146. }
  3147. // Determine the time
  3148. var minutes = null, hours = null, seconds = null;
  3149. if (timeFormat) {
  3150. timeFormat = timeFormat.split(':');
  3151. time = time.split(':');
  3152. if (timeFormat.length !== time.length) {
  3153. return {
  3154. valid: false,
  3155. message: message
  3156. };
  3157. }
  3158. hours = time.length > 0 ? time[0] : null;
  3159. minutes = time.length > 1 ? time[1] : null;
  3160. seconds = time.length > 2 ? time[2] : null;
  3161. if (hours === '' || minutes === '' || seconds === '') {
  3162. return {
  3163. valid: false,
  3164. message: message
  3165. };
  3166. }
  3167. // Validate seconds
  3168. if (seconds) {
  3169. if (isNaN(seconds) || seconds.length > 2) {
  3170. return {
  3171. valid: false,
  3172. message: message
  3173. };
  3174. }
  3175. seconds = parseInt(seconds, 10);
  3176. if (seconds < 0 || seconds > 60) {
  3177. return {
  3178. valid: false,
  3179. message: message
  3180. };
  3181. }
  3182. }
  3183. // Validate hours
  3184. if (hours) {
  3185. if (isNaN(hours) || hours.length > 2) {
  3186. return {
  3187. valid: false,
  3188. message: message
  3189. };
  3190. }
  3191. hours = parseInt(hours, 10);
  3192. if (hours < 0 || hours >= 24 || (amOrPm && hours > 12)) {
  3193. return {
  3194. valid: false,
  3195. message: message
  3196. };
  3197. }
  3198. }
  3199. // Validate minutes
  3200. if (minutes) {
  3201. if (isNaN(minutes) || minutes.length > 2) {
  3202. return {
  3203. valid: false,
  3204. message: message
  3205. };
  3206. }
  3207. minutes = parseInt(minutes, 10);
  3208. if (minutes < 0 || minutes > 59) {
  3209. return {
  3210. valid: false,
  3211. message: message
  3212. };
  3213. }
  3214. }
  3215. }
  3216. // Validate day, month, and year
  3217. var valid = FormValidation.Helper.date(year, month, day),
  3218. min = null,
  3219. max = null,
  3220. minOption = options.min,
  3221. maxOption = options.max;
  3222. if (minOption) {
  3223. min = (minOption instanceof Date)
  3224. ? minOption
  3225. : (this._parseDate(minOption, dateFormat, separator) ||
  3226. this._parseDate(validator.getDynamicOption($field, minOption), dateFormat, separator));
  3227. // In order to avoid displaying a date string like "Mon Dec 08 2014 19:14:12 GMT+0000 (WET)"
  3228. minOption = this._formatDate(min, options.format);
  3229. }
  3230. if (maxOption) {
  3231. max = (maxOption instanceof Date)
  3232. ? maxOption
  3233. : (this._parseDate(maxOption, dateFormat, separator) ||
  3234. this._parseDate(validator.getDynamicOption($field, maxOption), dateFormat, separator));
  3235. // In order to avoid displaying a date string like "Mon Dec 08 2014 19:14:12 GMT+0000 (WET)"
  3236. maxOption = this._formatDate(max, options.format);
  3237. }
  3238. date = new Date(year, month -1, day, hours, minutes, seconds);
  3239. switch (true) {
  3240. case (minOption && !maxOption && valid):
  3241. valid = date.getTime() >= min.getTime();
  3242. message = options.message || FormValidation.Helper.format(FormValidation.I18n[locale].date.min, minOption);
  3243. break;
  3244. case (maxOption && !minOption && valid):
  3245. valid = date.getTime() <= max.getTime();
  3246. message = options.message || FormValidation.Helper.format(FormValidation.I18n[locale].date.max, maxOption);
  3247. break;
  3248. case (maxOption && minOption && valid):
  3249. valid = date.getTime() <= max.getTime() && date.getTime() >= min.getTime();
  3250. message = options.message || FormValidation.Helper.format(FormValidation.I18n[locale].date.range, [minOption, maxOption]);
  3251. break;
  3252. default:
  3253. break;
  3254. }
  3255. return {
  3256. valid: valid,
  3257. date: date,
  3258. message: message
  3259. };
  3260. },
  3261. /**
  3262. * Return a date object after parsing the date string
  3263. *
  3264. * @param {Date|String} date The date string to parse
  3265. * @param {String} format The date format
  3266. * The format can be:
  3267. * - date: Consist of DD, MM, YYYY parts which are separated by the separator option
  3268. * - date and time:
  3269. * The time can consist of h, m, s parts which are separated by :
  3270. * @param {String} separator The separator used to separate the date, month, and year
  3271. * @returns {Date}
  3272. */
  3273. _parseDate: function(date, format, separator) {
  3274. if (date instanceof Date) {
  3275. return date;
  3276. }
  3277. if (typeof date !== 'string') {
  3278. return null;
  3279. }
  3280. // Ensure that the format must consist of year, month and day patterns
  3281. var yearIndex = $.inArray('YYYY', format),
  3282. monthIndex = $.inArray('MM', format),
  3283. dayIndex = $.inArray('DD', format);
  3284. if (yearIndex === -1 || monthIndex === -1 || dayIndex === -1) {
  3285. return null;
  3286. }
  3287. var minutes = 0, hours = 0, seconds = 0,
  3288. sections = date.split(' '),
  3289. dateSection = sections[0].split(separator);
  3290. if (dateSection.length < 3) {
  3291. return null;
  3292. }
  3293. if (sections.length > 1) {
  3294. var timeSection = sections[1].split(':');
  3295. hours = timeSection.length > 0 ? timeSection[0] : null;
  3296. minutes = timeSection.length > 1 ? timeSection[1] : null;
  3297. seconds = timeSection.length > 2 ? timeSection[2] : null;
  3298. }
  3299. return new Date(dateSection[yearIndex], dateSection[monthIndex] - 1, dateSection[dayIndex],
  3300. hours, minutes, seconds);
  3301. },
  3302. /**
  3303. * Format date
  3304. *
  3305. * @param {Date} date The date object to format
  3306. * @param {String} format The date format
  3307. * The format can consist of the following tokens:
  3308. * d Day of the month without leading zeros (1 through 31)
  3309. * dd Day of the month with leading zeros (01 through 31)
  3310. * m Month without leading zeros (1 through 12)
  3311. * mm Month with leading zeros (01 through 12)
  3312. * yy Last two digits of year (for example: 14)
  3313. * yyyy Full four digits of year (for example: 2014)
  3314. * h Hours without leading zeros (1 through 12)
  3315. * hh Hours with leading zeros (01 through 12)
  3316. * H Hours without leading zeros (0 through 23)
  3317. * HH Hours with leading zeros (00 through 23)
  3318. * M Minutes without leading zeros (0 through 59)
  3319. * MM Minutes with leading zeros (00 through 59)
  3320. * s Seconds without leading zeros (0 through 59)
  3321. * ss Seconds with leading zeros (00 through 59)
  3322. * @returns {String}
  3323. */
  3324. _formatDate: function(date, format) {
  3325. format = format
  3326. .replace(/Y/g, 'y')
  3327. .replace(/M/g, 'm')
  3328. .replace(/D/g, 'd')
  3329. .replace(/:m/g, ':M')
  3330. .replace(/:mm/g, ':MM')
  3331. .replace(/:S/, ':s')
  3332. .replace(/:SS/, ':ss');
  3333. var replacer = {
  3334. d: function(date) {
  3335. return date.getDate();
  3336. },
  3337. dd: function(date) {
  3338. var d = date.getDate();
  3339. return (d < 10) ? '0' + d : d;
  3340. },
  3341. m: function(date) {
  3342. return date.getMonth() + 1;
  3343. },
  3344. mm: function(date) {
  3345. var m = date.getMonth() + 1;
  3346. return m < 10 ? '0' + m : m;
  3347. },
  3348. yy: function(date) {
  3349. return ('' + date.getFullYear()).substr(2);
  3350. },
  3351. yyyy: function(date) {
  3352. return date.getFullYear();
  3353. },
  3354. h: function(date) {
  3355. return date.getHours() % 12 || 12;
  3356. },
  3357. hh: function(date) {
  3358. var h = date.getHours() % 12 || 12;
  3359. return h < 10 ? '0' + h : h;
  3360. },
  3361. H: function(date) {
  3362. return date.getHours();
  3363. },
  3364. HH: function(date) {
  3365. var H = date.getHours();
  3366. return H < 10 ? '0' + H : H;
  3367. },
  3368. M: function(date) {
  3369. return date.getMinutes();
  3370. },
  3371. MM: function(date) {
  3372. var M = date.getMinutes();
  3373. return M < 10 ? '0' + M : M;
  3374. },
  3375. s: function(date) {
  3376. return date.getSeconds();
  3377. },
  3378. ss: function(date) {
  3379. var s = date.getSeconds();
  3380. return s < 10 ? '0' + s : s;
  3381. }
  3382. };
  3383. return format.replace(/d{1,4}|m{1,4}|yy(?:yy)?|([HhMs])\1?|"[^"]*"|'[^']*'/g, function(match) {
  3384. return replacer[match] ? replacer[match](date) : match.slice(1, match.length - 1);
  3385. });
  3386. }
  3387. };
  3388. }(jQuery));
  3389. ;(function($) {
  3390. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  3391. 'en_US': {
  3392. different: {
  3393. 'default': 'Please enter a different value'
  3394. }
  3395. }
  3396. });
  3397. FormValidation.Validator.different = {
  3398. html5Attributes: {
  3399. message: 'message',
  3400. field: 'field'
  3401. },
  3402. /**
  3403. * Bind the validator on the live change of the field to compare with current one
  3404. *
  3405. * @param {FormValidation.Base} validator The validator plugin instance
  3406. * @param {jQuery} $field Field element
  3407. * @param {Object} options Consists of the following key:
  3408. * - field: The name of field that will be used to compare with current one
  3409. */
  3410. init: function(validator, $field, options, validatorName) {
  3411. var fields = options.field.split(',');
  3412. for (var i = 0; i < fields.length; i++) {
  3413. var compareWith = validator.getFieldElements($.trim(fields[i]));
  3414. validator.onLiveChange(compareWith, 'live_' + validatorName, function() {
  3415. var status = validator.getStatus($field, validatorName);
  3416. if (status !== validator.STATUS_NOT_VALIDATED) {
  3417. validator.revalidateField($field);
  3418. }
  3419. });
  3420. }
  3421. },
  3422. /**
  3423. * Unbind the validator on the live change of the field to compare with current one
  3424. *
  3425. * @param {FormValidation.Base} validator The validator plugin instance
  3426. * @param {jQuery} $field Field element
  3427. * @param {Object} options Consists of the following key:
  3428. * - field: The name of field that will be used to compare with current one
  3429. */
  3430. destroy: function(validator, $field, options, validatorName) {
  3431. var fields = options.field.split(',');
  3432. for (var i = 0; i < fields.length; i++) {
  3433. var compareWith = validator.getFieldElements($.trim(fields[i]));
  3434. validator.offLiveChange(compareWith, 'live_' + validatorName);
  3435. }
  3436. },
  3437. /**
  3438. * Return true if the input value is different with given field's value
  3439. *
  3440. * @param {FormValidation.Base} validator The validator plugin instance
  3441. * @param {jQuery} $field Field element
  3442. * @param {Object} options Consists of the following key:
  3443. * - field: The name of field that will be used to compare with current one
  3444. * - message: The invalid message
  3445. * @returns {Boolean}
  3446. */
  3447. validate: function(validator, $field, options, validatorName) {
  3448. var value = validator.getFieldValue($field, validatorName);
  3449. if (value === '') {
  3450. return true;
  3451. }
  3452. var fields = options.field.split(','),
  3453. isValid = true;
  3454. for (var i = 0; i < fields.length; i++) {
  3455. var compareWith = validator.getFieldElements($.trim(fields[i]));
  3456. if (compareWith == null || compareWith.length === 0) {
  3457. continue;
  3458. }
  3459. var compareValue = validator.getFieldValue(compareWith, validatorName);
  3460. if (value === compareValue) {
  3461. isValid = false;
  3462. } else if (compareValue !== '') {
  3463. validator.updateStatus(compareWith, validator.STATUS_VALID, validatorName);
  3464. }
  3465. }
  3466. return isValid;
  3467. }
  3468. };
  3469. }(jQuery));
  3470. ;(function($) {
  3471. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  3472. 'en_US': {
  3473. digits: {
  3474. 'default': 'Please enter only digits'
  3475. }
  3476. }
  3477. });
  3478. FormValidation.Validator.digits = {
  3479. /**
  3480. * Return true if the input value contains digits only
  3481. *
  3482. * @param {FormValidation.Base} validator Validate plugin instance
  3483. * @param {jQuery} $field Field element
  3484. * @param {Object} [options]
  3485. * @returns {Boolean}
  3486. */
  3487. validate: function(validator, $field, options, validatorName) {
  3488. var value = validator.getFieldValue($field, validatorName);
  3489. if (value === '') {
  3490. return true;
  3491. }
  3492. return /^\d+$/.test(value);
  3493. }
  3494. };
  3495. }(jQuery));
  3496. ;(function($) {
  3497. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  3498. 'en_US': {
  3499. ean: {
  3500. 'default': 'Please enter a valid EAN number'
  3501. }
  3502. }
  3503. });
  3504. FormValidation.Validator.ean = {
  3505. /**
  3506. * Validate EAN (International Article Number)
  3507. *
  3508. * @see http://en.wikipedia.org/wiki/European_Article_Number
  3509. * @param {FormValidation.Base} validator The validator plugin instance
  3510. * @param {jQuery} $field Field element
  3511. * @param {Object} options Can consist of the following keys:
  3512. * - message: The invalid message
  3513. * @returns {Boolean}
  3514. */
  3515. validate: function(validator, $field, options, validatorName) {
  3516. var value = validator.getFieldValue($field, validatorName);
  3517. if (value === '') {
  3518. return true;
  3519. }
  3520. if (!/^(\d{8}|\d{12}|\d{13})$/.test(value)) {
  3521. return false;
  3522. }
  3523. var length = value.length,
  3524. sum = 0,
  3525. weight = (length === 8) ? [3, 1] : [1, 3];
  3526. for (var i = 0; i < length - 1; i++) {
  3527. sum += parseInt(value.charAt(i), 10) * weight[i % 2];
  3528. }
  3529. sum = (10 - sum % 10) % 10;
  3530. return (sum + '' === value.charAt(length - 1));
  3531. }
  3532. };
  3533. }(jQuery));
  3534. ;(function($) {
  3535. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  3536. 'en_US': {
  3537. ein: {
  3538. 'default': 'Please enter a valid EIN number'
  3539. }
  3540. }
  3541. });
  3542. FormValidation.Validator.ein = {
  3543. // The first two digits are called campus
  3544. // See http://en.wikipedia.org/wiki/Employer_Identification_Number
  3545. // http://www.irs.gov/Businesses/Small-Businesses-&-Self-Employed/How-EINs-are-Assigned-and-Valid-EIN-Prefixes
  3546. CAMPUS: {
  3547. ANDOVER: ['10', '12'],
  3548. ATLANTA: ['60', '67'],
  3549. AUSTIN: ['50', '53'],
  3550. BROOKHAVEN: ['01', '02', '03', '04', '05', '06', '11', '13', '14', '16', '21', '22', '23', '25', '34', '51', '52', '54', '55', '56', '57', '58', '59', '65'],
  3551. CINCINNATI: ['30', '32', '35', '36', '37', '38', '61'],
  3552. FRESNO: ['15', '24'],
  3553. KANSAS_CITY: ['40', '44'],
  3554. MEMPHIS: ['94', '95'],
  3555. OGDEN: ['80', '90'],
  3556. PHILADELPHIA: ['33', '39', '41', '42', '43', '48', '62', '63', '64', '66', '68', '71', '72', '73', '74', '75', '76', '77', '81', '82', '83', '84', '85', '86', '87', '88', '91', '92', '93', '98', '99'],
  3557. INTERNET: ['20', '26', '27', '45', '46', '47'],
  3558. SMALL_BUSINESS_ADMINISTRATION: ['31']
  3559. },
  3560. /**
  3561. * Validate EIN (Employer Identification Number) which is also known as
  3562. * Federal Employer Identification Number (FEIN) or Federal Tax Identification Number
  3563. *
  3564. * @param {FormValidation.Base} validator The validator plugin instance
  3565. * @param {jQuery} $field Field element
  3566. * @param {Object} options Can consist of the following keys:
  3567. * - message: The invalid message
  3568. * @returns {Object|Boolean}
  3569. */
  3570. validate: function(validator, $field, options, validatorName) {
  3571. var value = validator.getFieldValue($field, validatorName);
  3572. if (value === '') {
  3573. return true;
  3574. }
  3575. if (!/^[0-9]{2}-?[0-9]{7}$/.test(value)) {
  3576. return false;
  3577. }
  3578. // Check the first two digits
  3579. var campus = value.substr(0, 2) + '';
  3580. for (var key in this.CAMPUS) {
  3581. if ($.inArray(campus, this.CAMPUS[key]) !== -1) {
  3582. return {
  3583. valid: true,
  3584. campus: key
  3585. };
  3586. }
  3587. }
  3588. return false;
  3589. }
  3590. };
  3591. }(jQuery));
  3592. ;(function($) {
  3593. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  3594. 'en_US': {
  3595. emailAddress: {
  3596. 'default': 'Please enter a valid email address'
  3597. }
  3598. }
  3599. });
  3600. FormValidation.Validator.emailAddress = {
  3601. html5Attributes: {
  3602. message: 'message',
  3603. multiple: 'multiple',
  3604. separator: 'separator'
  3605. },
  3606. enableByHtml5: function($field) {
  3607. return ('email' === $field.attr('type'));
  3608. },
  3609. /**
  3610. * Return true if and only if the input value is a valid email address
  3611. *
  3612. * @param {FormValidation.Base} validator Validate plugin instance
  3613. * @param {jQuery} $field Field element
  3614. * @param {Object} [options]
  3615. * - multiple: Allow multiple email addresses, separated by a comma or semicolon; default is false.
  3616. * - separator: Regex for character or characters expected as separator between addresses; default is comma /[,;]/, i.e. comma or semicolon.
  3617. * @returns {Boolean}
  3618. */
  3619. validate: function(validator, $field, options, validatorName) {
  3620. var value = validator.getFieldValue($field, validatorName);
  3621. if (value === '') {
  3622. return true;
  3623. }
  3624. // Email address regular expression
  3625. // http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
  3626. 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])?)*$/,
  3627. allowMultiple = options.multiple === true || options.multiple === 'true';
  3628. if (allowMultiple) {
  3629. var separator = options.separator || /[,;]/,
  3630. addresses = this._splitEmailAddresses(value, separator);
  3631. for (var i = 0; i < addresses.length; i++) {
  3632. if (!emailRegExp.test(addresses[i])) {
  3633. return false;
  3634. }
  3635. }
  3636. return true;
  3637. } else {
  3638. return emailRegExp.test(value);
  3639. }
  3640. },
  3641. _splitEmailAddresses: function(emailAddresses, separator) {
  3642. var quotedFragments = emailAddresses.split(/"/),
  3643. quotedFragmentCount = quotedFragments.length,
  3644. emailAddressArray = [],
  3645. nextEmailAddress = '';
  3646. for (var i = 0; i < quotedFragmentCount; i++) {
  3647. if (i % 2 === 0) {
  3648. var splitEmailAddressFragments = quotedFragments[i].split(separator),
  3649. splitEmailAddressFragmentCount = splitEmailAddressFragments.length;
  3650. if (splitEmailAddressFragmentCount === 1) {
  3651. nextEmailAddress += splitEmailAddressFragments[0];
  3652. } else {
  3653. emailAddressArray.push(nextEmailAddress + splitEmailAddressFragments[0]);
  3654. for (var j = 1; j < splitEmailAddressFragmentCount - 1; j++) {
  3655. emailAddressArray.push(splitEmailAddressFragments[j]);
  3656. }
  3657. nextEmailAddress = splitEmailAddressFragments[splitEmailAddressFragmentCount - 1];
  3658. }
  3659. } else {
  3660. nextEmailAddress += '"' + quotedFragments[i];
  3661. if (i < quotedFragmentCount - 1) {
  3662. nextEmailAddress += '"';
  3663. }
  3664. }
  3665. }
  3666. emailAddressArray.push(nextEmailAddress);
  3667. return emailAddressArray;
  3668. }
  3669. };
  3670. }(jQuery));
  3671. ;(function($) {
  3672. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  3673. 'en_US': {
  3674. file: {
  3675. 'default': 'Please choose a valid file'
  3676. }
  3677. }
  3678. });
  3679. FormValidation.Validator.file = {
  3680. Error: {
  3681. EXTENSION: 'EXTENSION',
  3682. MAX_FILES: 'MAX_FILES',
  3683. MAX_SIZE: 'MAX_SIZE',
  3684. MAX_TOTAL_SIZE: 'MAX_TOTAL_SIZE',
  3685. MIN_FILES: 'MIN_FILES',
  3686. MIN_SIZE: 'MIN_SIZE',
  3687. MIN_TOTAL_SIZE: 'MIN_TOTAL_SIZE',
  3688. TYPE: 'TYPE'
  3689. },
  3690. html5Attributes: {
  3691. extension: 'extension',
  3692. maxfiles: 'maxFiles',
  3693. minfiles: 'minFiles',
  3694. maxsize: 'maxSize',
  3695. minsize: 'minSize',
  3696. maxtotalsize: 'maxTotalSize',
  3697. mintotalsize: 'minTotalSize',
  3698. message: 'message',
  3699. type: 'type'
  3700. },
  3701. /**
  3702. * Validate upload file. Use HTML 5 API if the browser supports
  3703. *
  3704. * @param {FormValidation.Base} validator The validator plugin instance
  3705. * @param {jQuery} $field Field element
  3706. * @param {Object} options Can consist of the following keys:
  3707. * - extension: The allowed extensions, separated by a comma
  3708. * - maxFiles: The maximum number of files
  3709. * - minFiles: The minimum number of files
  3710. * - maxSize: The maximum size in bytes
  3711. * - minSize: The minimum size in bytes
  3712. * - maxTotalSize: The maximum size in bytes for all files
  3713. * - minTotalSize: The minimum size in bytes for all files
  3714. * - message: The invalid message
  3715. * - type: The allowed MIME type, separated by a comma
  3716. * @returns {Boolean|Object}
  3717. */
  3718. validate: function(validator, $field, options, validatorName) {
  3719. var value = validator.getFieldValue($field, validatorName);
  3720. if (value === '') {
  3721. return true;
  3722. }
  3723. var ext,
  3724. extensions = options.extension ? options.extension.toLowerCase().split(',') : null,
  3725. types = options.type ? options.type.toLowerCase().split(',') : null,
  3726. html5 = (window.File && window.FileList && window.FileReader);
  3727. if (html5) {
  3728. // Get FileList instance
  3729. var files = $field.get(0).files,
  3730. total = files.length,
  3731. totalSize = 0;
  3732. // Check the maxFiles
  3733. if (options.maxFiles && total > parseInt(options.maxFiles, 10)) {
  3734. return {
  3735. valid: false,
  3736. error: this.Error.MAX_FILES
  3737. };
  3738. }
  3739. // Check the minFiles
  3740. if (options.minFiles && total < parseInt(options.minFiles, 10)) {
  3741. return {
  3742. valid: false,
  3743. error: this.Error.MIN_FILES
  3744. };
  3745. }
  3746. var metaData = {};
  3747. for (var i = 0; i < total; i++) {
  3748. totalSize += files[i].size;
  3749. ext = files[i].name.substr(files[i].name.lastIndexOf('.') + 1);
  3750. metaData = {
  3751. file: files[i],
  3752. size: files[i].size,
  3753. ext: ext,
  3754. type: files[i].type
  3755. };
  3756. // Check the minSize
  3757. if (options.minSize && files[i].size < parseInt(options.minSize, 10)) {
  3758. return {
  3759. valid: false,
  3760. error: this.Error.MIN_SIZE,
  3761. metaData: metaData
  3762. };
  3763. }
  3764. // Check the maxSize
  3765. if (options.maxSize && files[i].size > parseInt(options.maxSize, 10)) {
  3766. return {
  3767. valid: false,
  3768. error: this.Error.MAX_SIZE,
  3769. metaData: metaData
  3770. };
  3771. }
  3772. // Check file extension
  3773. if (extensions && $.inArray(ext.toLowerCase(), extensions) === -1) {
  3774. return {
  3775. valid: false,
  3776. error: this.Error.EXTENSION,
  3777. metaData: metaData
  3778. };
  3779. }
  3780. // Check file type
  3781. if (files[i].type && types && $.inArray(files[i].type.toLowerCase(), types) === -1) {
  3782. return {
  3783. valid: false,
  3784. error: this.Error.TYPE,
  3785. metaData: metaData
  3786. };
  3787. }
  3788. }
  3789. // Check the maxTotalSize
  3790. if (options.maxTotalSize && totalSize > parseInt(options.maxTotalSize, 10)) {
  3791. return {
  3792. valid: false,
  3793. error: this.Error.MAX_TOTAL_SIZE,
  3794. metaData: {
  3795. totalSize: totalSize
  3796. }
  3797. };
  3798. }
  3799. // Check the minTotalSize
  3800. if (options.minTotalSize && totalSize < parseInt(options.minTotalSize, 10)) {
  3801. return {
  3802. valid: false,
  3803. error: this.Error.MIN_TOTAL_SIZE,
  3804. metaData: {
  3805. totalSize: totalSize
  3806. }
  3807. };
  3808. }
  3809. } else {
  3810. // Check file extension
  3811. ext = value.substr(value.lastIndexOf('.') + 1);
  3812. if (extensions && $.inArray(ext.toLowerCase(), extensions) === -1) {
  3813. return {
  3814. valid: false,
  3815. error: this.Error.EXTENSION,
  3816. metaData: {
  3817. ext: ext
  3818. }
  3819. };
  3820. }
  3821. }
  3822. return true;
  3823. }
  3824. };
  3825. }(jQuery));
  3826. ;(function($) {
  3827. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  3828. 'en_US': {
  3829. greaterThan: {
  3830. 'default': 'Please enter a value greater than or equal to %s',
  3831. notInclusive: 'Please enter a value greater than %s'
  3832. }
  3833. }
  3834. });
  3835. FormValidation.Validator.greaterThan = {
  3836. html5Attributes: {
  3837. message: 'message',
  3838. value: 'value',
  3839. inclusive: 'inclusive'
  3840. },
  3841. enableByHtml5: function($field) {
  3842. var type = $field.attr('type'),
  3843. min = $field.attr('min');
  3844. if (min && type !== 'date') {
  3845. return {
  3846. value: min
  3847. };
  3848. }
  3849. return false;
  3850. },
  3851. /**
  3852. * Return true if the input value is greater than or equals to given number
  3853. *
  3854. * @param {FormValidation.Base} validator Validate plugin instance
  3855. * @param {jQuery} $field Field element
  3856. * @param {Object} options Can consist of the following keys:
  3857. * - value: Define the number to compare with. It can be
  3858. * - A number
  3859. * - Name of field which its value defines the number
  3860. * - Name of callback function that returns the number
  3861. * - A callback function that returns the number
  3862. *
  3863. * - inclusive [optional]: Can be true or false. Default is true
  3864. * - message: The invalid message
  3865. * @returns {Boolean|Object}
  3866. */
  3867. validate: function(validator, $field, options, validatorName) {
  3868. var value = validator.getFieldValue($field, validatorName);
  3869. if (value === '') {
  3870. return true;
  3871. }
  3872. value = this._format(value);
  3873. var locale = validator.getLocale(),
  3874. compareTo = $.isNumeric(options.value) ? options.value : validator.getDynamicOption($field, options.value),
  3875. compareToValue = this._format(compareTo);
  3876. return (options.inclusive === true || options.inclusive === undefined)
  3877. ? {
  3878. valid: $.isNumeric(value) && parseFloat(value) >= compareToValue,
  3879. message: FormValidation.Helper.format(options.message || FormValidation.I18n[locale].greaterThan['default'], compareTo)
  3880. }
  3881. : {
  3882. valid: $.isNumeric(value) && parseFloat(value) > compareToValue,
  3883. message: FormValidation.Helper.format(options.message || FormValidation.I18n[locale].greaterThan.notInclusive, compareTo)
  3884. };
  3885. },
  3886. _format: function(value) {
  3887. return (value + '').replace(',', '.');
  3888. }
  3889. };
  3890. }(jQuery));
  3891. ;(function($) {
  3892. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  3893. 'en_US': {
  3894. grid: {
  3895. 'default': 'Please enter a valid GRId number'
  3896. }
  3897. }
  3898. });
  3899. FormValidation.Validator.grid = {
  3900. /**
  3901. * Validate GRId (Global Release Identifier)
  3902. *
  3903. * @see http://en.wikipedia.org/wiki/Global_Release_Identifier
  3904. * @param {FormValidation.Base} validator The validator plugin instance
  3905. * @param {jQuery} $field Field element
  3906. * @param {Object} options Can consist of the following keys:
  3907. * - message: The invalid message
  3908. * @returns {Boolean}
  3909. */
  3910. validate: function(validator, $field, options, validatorName) {
  3911. var value = validator.getFieldValue($field, validatorName);
  3912. if (value === '') {
  3913. return true;
  3914. }
  3915. value = value.toUpperCase();
  3916. if (!/^[GRID:]*([0-9A-Z]{2})[-\s]*([0-9A-Z]{5})[-\s]*([0-9A-Z]{10})[-\s]*([0-9A-Z]{1})$/g.test(value)) {
  3917. return false;
  3918. }
  3919. value = value.replace(/\s/g, '').replace(/-/g, '');
  3920. if ('GRID:' === value.substr(0, 5)) {
  3921. value = value.substr(5);
  3922. }
  3923. return FormValidation.Helper.mod37And36(value);
  3924. }
  3925. };
  3926. }(jQuery));
  3927. ;(function($) {
  3928. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  3929. 'en_US': {
  3930. hex: {
  3931. 'default': 'Please enter a valid hexadecimal number'
  3932. }
  3933. }
  3934. });
  3935. FormValidation.Validator.hex = {
  3936. /**
  3937. * Return true if and only if the input value is a valid hexadecimal number
  3938. *
  3939. * @param {FormValidation.Base} validator The validator plugin instance
  3940. * @param {jQuery} $field Field element
  3941. * @param {Object} options Consist of key:
  3942. * - message: The invalid message
  3943. * @returns {Boolean}
  3944. */
  3945. validate: function(validator, $field, options, validatorName) {
  3946. var value = validator.getFieldValue($field, validatorName);
  3947. if (value === '') {
  3948. return true;
  3949. }
  3950. return /^[0-9a-fA-F]+$/.test(value);
  3951. }
  3952. };
  3953. }(jQuery));
  3954. ;(function($) {
  3955. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  3956. 'en_US': {
  3957. iban: {
  3958. 'default': 'Please enter a valid IBAN number',
  3959. country: 'Please enter a valid IBAN number in %s',
  3960. countries: {
  3961. AD: 'Andorra',
  3962. AE: 'United Arab Emirates',
  3963. AL: 'Albania',
  3964. AO: 'Angola',
  3965. AT: 'Austria',
  3966. AZ: 'Azerbaijan',
  3967. BA: 'Bosnia and Herzegovina',
  3968. BE: 'Belgium',
  3969. BF: 'Burkina Faso',
  3970. BG: 'Bulgaria',
  3971. BH: 'Bahrain',
  3972. BI: 'Burundi',
  3973. BJ: 'Benin',
  3974. BR: 'Brazil',
  3975. CH: 'Switzerland',
  3976. CI: 'Ivory Coast',
  3977. CM: 'Cameroon',
  3978. CR: 'Costa Rica',
  3979. CV: 'Cape Verde',
  3980. CY: 'Cyprus',
  3981. CZ: 'Czech Republic',
  3982. DE: 'Germany',
  3983. DK: 'Denmark',
  3984. DO: 'Dominican Republic',
  3985. DZ: 'Algeria',
  3986. EE: 'Estonia',
  3987. ES: 'Spain',
  3988. FI: 'Finland',
  3989. FO: 'Faroe Islands',
  3990. FR: 'France',
  3991. GB: 'United Kingdom',
  3992. GE: 'Georgia',
  3993. GI: 'Gibraltar',
  3994. GL: 'Greenland',
  3995. GR: 'Greece',
  3996. GT: 'Guatemala',
  3997. HR: 'Croatia',
  3998. HU: 'Hungary',
  3999. IE: 'Ireland',
  4000. IL: 'Israel',
  4001. IR: 'Iran',
  4002. IS: 'Iceland',
  4003. IT: 'Italy',
  4004. JO: 'Jordan',
  4005. KW: 'Kuwait',
  4006. KZ: 'Kazakhstan',
  4007. LB: 'Lebanon',
  4008. LI: 'Liechtenstein',
  4009. LT: 'Lithuania',
  4010. LU: 'Luxembourg',
  4011. LV: 'Latvia',
  4012. MC: 'Monaco',
  4013. MD: 'Moldova',
  4014. ME: 'Montenegro',
  4015. MG: 'Madagascar',
  4016. MK: 'Macedonia',
  4017. ML: 'Mali',
  4018. MR: 'Mauritania',
  4019. MT: 'Malta',
  4020. MU: 'Mauritius',
  4021. MZ: 'Mozambique',
  4022. NL: 'Netherlands',
  4023. NO: 'Norway',
  4024. PK: 'Pakistan',
  4025. PL: 'Poland',
  4026. PS: 'Palestine',
  4027. PT: 'Portugal',
  4028. QA: 'Qatar',
  4029. RO: 'Romania',
  4030. RS: 'Serbia',
  4031. SA: 'Saudi Arabia',
  4032. SE: 'Sweden',
  4033. SI: 'Slovenia',
  4034. SK: 'Slovakia',
  4035. SM: 'San Marino',
  4036. SN: 'Senegal',
  4037. TL: 'East Timor',
  4038. TN: 'Tunisia',
  4039. TR: 'Turkey',
  4040. VG: 'Virgin Islands, British',
  4041. XK: 'Republic of Kosovo'
  4042. }
  4043. }
  4044. }
  4045. });
  4046. FormValidation.Validator.iban = {
  4047. html5Attributes: {
  4048. message: 'message',
  4049. country: 'country',
  4050. sepa: 'sepa'
  4051. },
  4052. // http://www.swift.com/dsp/resources/documents/IBAN_Registry.pdf
  4053. // http://en.wikipedia.org/wiki/International_Bank_Account_Number#IBAN_formats_by_country
  4054. REGEX: {
  4055. AD: 'AD[0-9]{2}[0-9]{4}[0-9]{4}[A-Z0-9]{12}', // Andorra
  4056. AE: 'AE[0-9]{2}[0-9]{3}[0-9]{16}', // United Arab Emirates
  4057. AL: 'AL[0-9]{2}[0-9]{8}[A-Z0-9]{16}', // Albania
  4058. AO: 'AO[0-9]{2}[0-9]{21}', // Angola
  4059. AT: 'AT[0-9]{2}[0-9]{5}[0-9]{11}', // Austria
  4060. AZ: 'AZ[0-9]{2}[A-Z]{4}[A-Z0-9]{20}', // Azerbaijan
  4061. BA: 'BA[0-9]{2}[0-9]{3}[0-9]{3}[0-9]{8}[0-9]{2}', // Bosnia and Herzegovina
  4062. BE: 'BE[0-9]{2}[0-9]{3}[0-9]{7}[0-9]{2}', // Belgium
  4063. BF: 'BF[0-9]{2}[0-9]{23}', // Burkina Faso
  4064. BG: 'BG[0-9]{2}[A-Z]{4}[0-9]{4}[0-9]{2}[A-Z0-9]{8}', // Bulgaria
  4065. BH: 'BH[0-9]{2}[A-Z]{4}[A-Z0-9]{14}', // Bahrain
  4066. BI: 'BI[0-9]{2}[0-9]{12}', // Burundi
  4067. BJ: 'BJ[0-9]{2}[A-Z]{1}[0-9]{23}', // Benin
  4068. BR: 'BR[0-9]{2}[0-9]{8}[0-9]{5}[0-9]{10}[A-Z][A-Z0-9]', // Brazil
  4069. CH: 'CH[0-9]{2}[0-9]{5}[A-Z0-9]{12}', // Switzerland
  4070. CI: 'CI[0-9]{2}[A-Z]{1}[0-9]{23}', // Ivory Coast
  4071. CM: 'CM[0-9]{2}[0-9]{23}', // Cameroon
  4072. CR: 'CR[0-9]{2}[0-9]{3}[0-9]{14}', // Costa Rica
  4073. CV: 'CV[0-9]{2}[0-9]{21}', // Cape Verde
  4074. CY: 'CY[0-9]{2}[0-9]{3}[0-9]{5}[A-Z0-9]{16}', // Cyprus
  4075. CZ: 'CZ[0-9]{2}[0-9]{20}', // Czech Republic
  4076. DE: 'DE[0-9]{2}[0-9]{8}[0-9]{10}', // Germany
  4077. DK: 'DK[0-9]{2}[0-9]{14}', // Denmark
  4078. DO: 'DO[0-9]{2}[A-Z0-9]{4}[0-9]{20}', // Dominican Republic
  4079. DZ: 'DZ[0-9]{2}[0-9]{20}', // Algeria
  4080. EE: 'EE[0-9]{2}[0-9]{2}[0-9]{2}[0-9]{11}[0-9]{1}', // Estonia
  4081. ES: 'ES[0-9]{2}[0-9]{4}[0-9]{4}[0-9]{1}[0-9]{1}[0-9]{10}', // Spain
  4082. FI: 'FI[0-9]{2}[0-9]{6}[0-9]{7}[0-9]{1}', // Finland
  4083. FO: 'FO[0-9]{2}[0-9]{4}[0-9]{9}[0-9]{1}', // Faroe Islands
  4084. FR: 'FR[0-9]{2}[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}', // France
  4085. GB: 'GB[0-9]{2}[A-Z]{4}[0-9]{6}[0-9]{8}', // United Kingdom
  4086. GE: 'GE[0-9]{2}[A-Z]{2}[0-9]{16}', // Georgia
  4087. GI: 'GI[0-9]{2}[A-Z]{4}[A-Z0-9]{15}', // Gibraltar
  4088. GL: 'GL[0-9]{2}[0-9]{4}[0-9]{9}[0-9]{1}', // Greenland
  4089. GR: 'GR[0-9]{2}[0-9]{3}[0-9]{4}[A-Z0-9]{16}', // Greece
  4090. GT: 'GT[0-9]{2}[A-Z0-9]{4}[A-Z0-9]{20}', // Guatemala
  4091. HR: 'HR[0-9]{2}[0-9]{7}[0-9]{10}', // Croatia
  4092. HU: 'HU[0-9]{2}[0-9]{3}[0-9]{4}[0-9]{1}[0-9]{15}[0-9]{1}', // Hungary
  4093. IE: 'IE[0-9]{2}[A-Z]{4}[0-9]{6}[0-9]{8}', // Ireland
  4094. IL: 'IL[0-9]{2}[0-9]{3}[0-9]{3}[0-9]{13}', // Israel
  4095. IR: 'IR[0-9]{2}[0-9]{22}', // Iran
  4096. IS: 'IS[0-9]{2}[0-9]{4}[0-9]{2}[0-9]{6}[0-9]{10}', // Iceland
  4097. IT: 'IT[0-9]{2}[A-Z]{1}[0-9]{5}[0-9]{5}[A-Z0-9]{12}', // Italy
  4098. JO: 'JO[0-9]{2}[A-Z]{4}[0-9]{4}[0]{8}[A-Z0-9]{10}', // Jordan
  4099. KW: 'KW[0-9]{2}[A-Z]{4}[0-9]{22}', // Kuwait
  4100. KZ: 'KZ[0-9]{2}[0-9]{3}[A-Z0-9]{13}', // Kazakhstan
  4101. LB: 'LB[0-9]{2}[0-9]{4}[A-Z0-9]{20}', // Lebanon
  4102. LI: 'LI[0-9]{2}[0-9]{5}[A-Z0-9]{12}', // Liechtenstein
  4103. LT: 'LT[0-9]{2}[0-9]{5}[0-9]{11}', // Lithuania
  4104. LU: 'LU[0-9]{2}[0-9]{3}[A-Z0-9]{13}', // Luxembourg
  4105. LV: 'LV[0-9]{2}[A-Z]{4}[A-Z0-9]{13}', // Latvia
  4106. MC: 'MC[0-9]{2}[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}', // Monaco
  4107. MD: 'MD[0-9]{2}[A-Z0-9]{20}', // Moldova
  4108. ME: 'ME[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}', // Montenegro
  4109. MG: 'MG[0-9]{2}[0-9]{23}', // Madagascar
  4110. MK: 'MK[0-9]{2}[0-9]{3}[A-Z0-9]{10}[0-9]{2}', // Macedonia
  4111. ML: 'ML[0-9]{2}[A-Z]{1}[0-9]{23}', // Mali
  4112. MR: 'MR13[0-9]{5}[0-9]{5}[0-9]{11}[0-9]{2}', // Mauritania
  4113. MT: 'MT[0-9]{2}[A-Z]{4}[0-9]{5}[A-Z0-9]{18}', // Malta
  4114. MU: 'MU[0-9]{2}[A-Z]{4}[0-9]{2}[0-9]{2}[0-9]{12}[0-9]{3}[A-Z]{3}', // Mauritius
  4115. MZ: 'MZ[0-9]{2}[0-9]{21}', // Mozambique
  4116. NL: 'NL[0-9]{2}[A-Z]{4}[0-9]{10}', // Netherlands
  4117. NO: 'NO[0-9]{2}[0-9]{4}[0-9]{6}[0-9]{1}', // Norway
  4118. PK: 'PK[0-9]{2}[A-Z]{4}[A-Z0-9]{16}', // Pakistan
  4119. PL: 'PL[0-9]{2}[0-9]{8}[0-9]{16}', // Poland
  4120. PS: 'PS[0-9]{2}[A-Z]{4}[A-Z0-9]{21}', // Palestinian
  4121. PT: 'PT[0-9]{2}[0-9]{4}[0-9]{4}[0-9]{11}[0-9]{2}', // Portugal
  4122. QA: 'QA[0-9]{2}[A-Z]{4}[A-Z0-9]{21}', // Qatar
  4123. RO: 'RO[0-9]{2}[A-Z]{4}[A-Z0-9]{16}', // Romania
  4124. RS: 'RS[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}', // Serbia
  4125. SA: 'SA[0-9]{2}[0-9]{2}[A-Z0-9]{18}', // Saudi Arabia
  4126. SE: 'SE[0-9]{2}[0-9]{3}[0-9]{16}[0-9]{1}', // Sweden
  4127. SI: 'SI[0-9]{2}[0-9]{5}[0-9]{8}[0-9]{2}', // Slovenia
  4128. SK: 'SK[0-9]{2}[0-9]{4}[0-9]{6}[0-9]{10}', // Slovakia
  4129. SM: 'SM[0-9]{2}[A-Z]{1}[0-9]{5}[0-9]{5}[A-Z0-9]{12}', // San Marino
  4130. SN: 'SN[0-9]{2}[A-Z]{1}[0-9]{23}', // Senegal
  4131. TL: 'TL38[0-9]{3}[0-9]{14}[0-9]{2}', // East Timor
  4132. TN: 'TN59[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}', // Tunisia
  4133. TR: 'TR[0-9]{2}[0-9]{5}[A-Z0-9]{1}[A-Z0-9]{16}', // Turkey
  4134. VG: 'VG[0-9]{2}[A-Z]{4}[0-9]{16}', // Virgin Islands, British
  4135. XK: 'XK[0-9]{2}[0-9]{4}[0-9]{10}[0-9]{2}' // Republic of Kosovo
  4136. },
  4137. // List of SEPA country codes
  4138. SEPA_COUNTRIES: [
  4139. 'AT', 'BE', 'BG', 'CH', 'CY', 'CZ', 'DE', 'DK', 'EE', 'ES',
  4140. 'FI', 'FR', 'GB', 'GI', 'GR', 'HR', 'HU', 'IE', 'IS', 'IT',
  4141. 'LI', 'LT', 'LU', 'LV', 'MC', 'MT', 'NL', 'NO', 'PL', 'PT',
  4142. 'RO', 'SE', 'SI', 'SK', 'SM'
  4143. ],
  4144. /**
  4145. * Validate an International Bank Account Number (IBAN)
  4146. * To test it, take the sample IBAN from
  4147. * http://www.nordea.com/Our+services/International+products+and+services/Cash+Management/IBAN+countries/908462.html
  4148. *
  4149. * @param {FormValidation.Base} validator The validator plugin instance
  4150. * @param {jQuery} $field Field element
  4151. * @param {Object} options Can consist of the following keys:
  4152. * - message: The invalid message
  4153. * - country: The ISO 3166-1 country code. It can be
  4154. * - A country code
  4155. * - Name of field which its value defines the country code
  4156. * - Name of callback function that returns the country code
  4157. * - A callback function that returns the country code
  4158. * - sepa: Set it to true (false) to indicate that the IBAN number must be (not be) from SEPA countries
  4159. * By default, this option is not defined
  4160. * @returns {Boolean|Object}
  4161. */
  4162. validate: function(validator, $field, options, validatorName) {
  4163. var value = validator.getFieldValue($field, validatorName);
  4164. if (value === '') {
  4165. return true;
  4166. }
  4167. value = value.replace(/[^a-zA-Z0-9]/g, '').toUpperCase();
  4168. var country = options.country;
  4169. if (!country) {
  4170. country = value.substr(0, 2);
  4171. } else if (typeof country !== 'string' || !this.REGEX[country]) {
  4172. // Determine the country code
  4173. country = validator.getDynamicOption($field, country);
  4174. }
  4175. var locale = validator.getLocale();
  4176. if (!this.REGEX[country]) {
  4177. return false;
  4178. }
  4179. // Check whether or not the sepa option is enabled
  4180. if (typeof options.sepa !== undefined) {
  4181. var isSepaCountry = $.inArray(country, this.SEPA_COUNTRIES) !== -1;
  4182. if (((options.sepa === 'true' || options.sepa === true) && !isSepaCountry)
  4183. || ((options.sepa === 'false' || options.sepa === false) && isSepaCountry))
  4184. {
  4185. return false;
  4186. }
  4187. }
  4188. if (!(new RegExp('^' + this.REGEX[country] + '$')).test(value)) {
  4189. return {
  4190. valid: false,
  4191. message: FormValidation.Helper.format(options.message || FormValidation.I18n[locale].iban.country, FormValidation.I18n[locale].iban.countries[country])
  4192. };
  4193. }
  4194. value = value.substr(4) + value.substr(0, 4);
  4195. value = $.map(value.split(''), function(n) {
  4196. var code = n.charCodeAt(0);
  4197. return (code >= 'A'.charCodeAt(0) && code <= 'Z'.charCodeAt(0))
  4198. // Replace A, B, C, ..., Z with 10, 11, ..., 35
  4199. ? (code - 'A'.charCodeAt(0) + 10)
  4200. : n;
  4201. });
  4202. value = value.join('');
  4203. var temp = parseInt(value.substr(0, 1), 10),
  4204. length = value.length;
  4205. for (var i = 1; i < length; ++i) {
  4206. temp = (temp * 10 + parseInt(value.substr(i, 1), 10)) % 97;
  4207. }
  4208. return {
  4209. valid: (temp === 1),
  4210. message: FormValidation.Helper.format(options.message || FormValidation.I18n[locale].iban.country, FormValidation.I18n[locale].iban.countries[country])
  4211. };
  4212. }
  4213. };
  4214. }(jQuery));
  4215. ;(function($) {
  4216. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  4217. 'en_US': {
  4218. id: {
  4219. 'default': 'Please enter a valid identification number',
  4220. country: 'Please enter a valid identification number in %s',
  4221. countries: {
  4222. BA: 'Bosnia and Herzegovina',
  4223. BG: 'Bulgaria',
  4224. BR: 'Brazil',
  4225. CH: 'Switzerland',
  4226. CL: 'Chile',
  4227. CN: 'China',
  4228. CZ: 'Czech Republic',
  4229. DK: 'Denmark',
  4230. EE: 'Estonia',
  4231. ES: 'Spain',
  4232. FI: 'Finland',
  4233. HR: 'Croatia',
  4234. IE: 'Ireland',
  4235. IS: 'Iceland',
  4236. LT: 'Lithuania',
  4237. LV: 'Latvia',
  4238. ME: 'Montenegro',
  4239. MK: 'Macedonia',
  4240. NL: 'Netherlands',
  4241. PL: 'Poland',
  4242. RO: 'Romania',
  4243. RS: 'Serbia',
  4244. SE: 'Sweden',
  4245. SI: 'Slovenia',
  4246. SK: 'Slovakia',
  4247. SM: 'San Marino',
  4248. TH: 'Thailand',
  4249. TR: 'Turkey',
  4250. ZA: 'South Africa'
  4251. }
  4252. }
  4253. }
  4254. });
  4255. FormValidation.Validator.id = {
  4256. html5Attributes: {
  4257. message: 'message',
  4258. country: 'country'
  4259. },
  4260. // Supported country codes
  4261. COUNTRY_CODES: [
  4262. 'BA', 'BG', 'BR', 'CH', 'CL', 'CN', 'CZ', 'DK', 'EE', 'ES', 'FI', 'HR', 'IE', 'IS', 'LT', 'LV', 'ME', 'MK', 'NL',
  4263. 'PL', 'RO', 'RS', 'SE', 'SI', 'SK', 'SM', 'TH', 'TR', 'ZA'
  4264. ],
  4265. /**
  4266. * Validate identification number in different countries
  4267. *
  4268. * @see http://en.wikipedia.org/wiki/National_identification_number
  4269. * @param {FormValidation.Base} validator The validator plugin instance
  4270. * @param {jQuery} $field Field element
  4271. * @param {Object} options Consist of key:
  4272. * - message: The invalid message
  4273. * - country: The ISO 3166-1 country code. It can be
  4274. * - One of country code defined in COUNTRY_CODES
  4275. * - Name of field which its value defines the country code
  4276. * - Name of callback function that returns the country code
  4277. * - A callback function that returns the country code
  4278. * @returns {Object}
  4279. */
  4280. validate: function(validator, $field, options, validatorName) {
  4281. var value = validator.getFieldValue($field, validatorName);
  4282. if (value === '') {
  4283. return true;
  4284. }
  4285. var locale = validator.getLocale(),
  4286. country = options.country;
  4287. if (!country) {
  4288. country = value.substr(0, 2);
  4289. } else if (typeof country !== 'string' || $.inArray(country.toUpperCase(), this.COUNTRY_CODES) === -1) {
  4290. // Determine the country code
  4291. country = validator.getDynamicOption($field, country);
  4292. }
  4293. if ($.inArray(country, this.COUNTRY_CODES) === -1) {
  4294. return true;
  4295. }
  4296. var method = ['_', country.toLowerCase()].join(''),
  4297. result = this[method](value);
  4298. result = (result === true || result === false) ? { valid: result } : result;
  4299. result.message = FormValidation.Helper.format(options.message || FormValidation.I18n[locale].id.country, FormValidation.I18n[locale].id.countries[country.toUpperCase()]);
  4300. return result;
  4301. },
  4302. /**
  4303. * Validate Unique Master Citizen Number which uses in
  4304. * - Bosnia and Herzegovina (country code: BA)
  4305. * - Macedonia (MK)
  4306. * - Montenegro (ME)
  4307. * - Serbia (RS)
  4308. * - Slovenia (SI)
  4309. *
  4310. * @see http://en.wikipedia.org/wiki/Unique_Master_Citizen_Number
  4311. * @param {String} value The ID
  4312. * @param {String} countryCode The ISO country code, can be BA, MK, ME, RS, SI
  4313. * @returns {Boolean}
  4314. */
  4315. _validateJMBG: function(value, countryCode) {
  4316. if (!/^\d{13}$/.test(value)) {
  4317. return false;
  4318. }
  4319. var day = parseInt(value.substr(0, 2), 10),
  4320. month = parseInt(value.substr(2, 2), 10),
  4321. year = parseInt(value.substr(4, 3), 10),
  4322. rr = parseInt(value.substr(7, 2), 10),
  4323. k = parseInt(value.substr(12, 1), 10);
  4324. // Validate date of birth
  4325. // FIXME: Validate the year of birth
  4326. if (day > 31 || month > 12) {
  4327. return false;
  4328. }
  4329. // Validate checksum
  4330. var sum = 0;
  4331. for (var i = 0; i < 6; i++) {
  4332. sum += (7 - i) * (parseInt(value.charAt(i), 10) + parseInt(value.charAt(i + 6), 10));
  4333. }
  4334. sum = 11 - sum % 11;
  4335. if (sum === 10 || sum === 11) {
  4336. sum = 0;
  4337. }
  4338. if (sum !== k) {
  4339. return false;
  4340. }
  4341. // Validate political region
  4342. // rr is the political region of birth, which can be in ranges:
  4343. // 10-19: Bosnia and Herzegovina
  4344. // 20-29: Montenegro
  4345. // 30-39: Croatia (not used anymore)
  4346. // 41-49: Macedonia
  4347. // 50-59: Slovenia (only 50 is used)
  4348. // 70-79: Central Serbia
  4349. // 80-89: Serbian province of Vojvodina
  4350. // 90-99: Kosovo
  4351. switch (countryCode.toUpperCase()) {
  4352. case 'BA':
  4353. return (10 <= rr && rr <= 19);
  4354. case 'MK':
  4355. return (41 <= rr && rr <= 49);
  4356. case 'ME':
  4357. return (20 <= rr && rr <= 29);
  4358. case 'RS':
  4359. return (70 <= rr && rr <= 99);
  4360. case 'SI':
  4361. return (50 <= rr && rr <= 59);
  4362. default:
  4363. return true;
  4364. }
  4365. },
  4366. _ba: function(value) {
  4367. return this._validateJMBG(value, 'BA');
  4368. },
  4369. _mk: function(value) {
  4370. return this._validateJMBG(value, 'MK');
  4371. },
  4372. _me: function(value) {
  4373. return this._validateJMBG(value, 'ME');
  4374. },
  4375. _rs: function(value) {
  4376. return this._validateJMBG(value, 'RS');
  4377. },
  4378. /**
  4379. * Examples: 0101006500006
  4380. */
  4381. _si: function(value) {
  4382. return this._validateJMBG(value, 'SI');
  4383. },
  4384. /**
  4385. * Validate Bulgarian national identification number (EGN)
  4386. *
  4387. * @see http://en.wikipedia.org/wiki/Uniform_civil_number
  4388. * @param {String} value The ID
  4389. * @returns {Boolean}
  4390. */
  4391. _bg: function(value) {
  4392. if (!/^\d{10}$/.test(value) && !/^\d{6}\s\d{3}\s\d{1}$/.test(value)) {
  4393. return false;
  4394. }
  4395. value = value.replace(/\s/g, '');
  4396. // Check the birth date
  4397. var year = parseInt(value.substr(0, 2), 10) + 1900,
  4398. month = parseInt(value.substr(2, 2), 10),
  4399. day = parseInt(value.substr(4, 2), 10);
  4400. if (month > 40) {
  4401. year += 100;
  4402. month -= 40;
  4403. } else if (month > 20) {
  4404. year -= 100;
  4405. month -= 20;
  4406. }
  4407. if (!FormValidation.Helper.date(year, month, day)) {
  4408. return false;
  4409. }
  4410. var sum = 0,
  4411. weight = [2, 4, 8, 5, 10, 9, 7, 3, 6];
  4412. for (var i = 0; i < 9; i++) {
  4413. sum += parseInt(value.charAt(i), 10) * weight[i];
  4414. }
  4415. sum = (sum % 11) % 10;
  4416. return (sum + '' === value.substr(9, 1));
  4417. },
  4418. /**
  4419. * Validate Brazilian national identification number (CPF)
  4420. *
  4421. * @see http://en.wikipedia.org/wiki/Cadastro_de_Pessoas_F%C3%ADsicas
  4422. * @param {String} value The ID
  4423. * @returns {Boolean}
  4424. */
  4425. _br: function(value) {
  4426. value = value.replace(/\D/g, '');
  4427. if (!/^\d{11}$/.test(value) || /^1{11}|2{11}|3{11}|4{11}|5{11}|6{11}|7{11}|8{11}|9{11}|0{11}$/.test(value)) {
  4428. return false;
  4429. }
  4430. var d1 = 0;
  4431. for (var i = 0; i < 9; i++) {
  4432. d1 += (10 - i) * parseInt(value.charAt(i), 10);
  4433. }
  4434. d1 = 11 - d1 % 11;
  4435. if (d1 === 10 || d1 === 11) {
  4436. d1 = 0;
  4437. }
  4438. if (d1 + '' !== value.charAt(9)) {
  4439. return false;
  4440. }
  4441. var d2 = 0;
  4442. for (i = 0; i < 10; i++) {
  4443. d2 += (11 - i) * parseInt(value.charAt(i), 10);
  4444. }
  4445. d2 = 11 - d2 % 11;
  4446. if (d2 === 10 || d2 === 11) {
  4447. d2 = 0;
  4448. }
  4449. return (d2 + '' === value.charAt(10));
  4450. },
  4451. /**
  4452. * Validate Swiss Social Security Number (AHV-Nr/No AVS)
  4453. *
  4454. * @see http://en.wikipedia.org/wiki/National_identification_number#Switzerland
  4455. * @see http://www.bsv.admin.ch/themen/ahv/00011/02185/index.html?lang=de
  4456. * @param {String} value The ID
  4457. * @returns {Boolean}
  4458. */
  4459. _ch: function(value) {
  4460. if (!/^756[\.]{0,1}[0-9]{4}[\.]{0,1}[0-9]{4}[\.]{0,1}[0-9]{2}$/.test(value)) {
  4461. return false;
  4462. }
  4463. value = value.replace(/\D/g, '').substr(3);
  4464. var length = value.length,
  4465. sum = 0,
  4466. weight = (length === 8) ? [3, 1] : [1, 3];
  4467. for (var i = 0; i < length - 1; i++) {
  4468. sum += parseInt(value.charAt(i), 10) * weight[i % 2];
  4469. }
  4470. sum = 10 - sum % 10;
  4471. return (sum + '' === value.charAt(length - 1));
  4472. },
  4473. /**
  4474. * Validate Chilean national identification number (RUN/RUT)
  4475. *
  4476. * @see http://en.wikipedia.org/wiki/National_identification_number#Chile
  4477. * @see https://palena.sii.cl/cvc/dte/ee_empresas_emisoras.html for samples
  4478. * @param {String} value The ID
  4479. * @returns {Boolean}
  4480. */
  4481. _cl: function(value) {
  4482. if (!/^\d{7,8}[-]{0,1}[0-9K]$/i.test(value)) {
  4483. return false;
  4484. }
  4485. value = value.replace(/\-/g, '');
  4486. while (value.length < 9) {
  4487. value = '0' + value;
  4488. }
  4489. var sum = 0,
  4490. weight = [3, 2, 7, 6, 5, 4, 3, 2];
  4491. for (var i = 0; i < 8; i++) {
  4492. sum += parseInt(value.charAt(i), 10) * weight[i];
  4493. }
  4494. sum = 11 - sum % 11;
  4495. if (sum === 11) {
  4496. sum = 0;
  4497. } else if (sum === 10) {
  4498. sum = 'K';
  4499. }
  4500. return sum + '' === value.charAt(8).toUpperCase();
  4501. },
  4502. /**
  4503. * Validate Chinese citizen identification number
  4504. *
  4505. * Rules:
  4506. * - For current 18-digit system (since 1st Oct 1999, defined by GB11643—1999 national standard):
  4507. * - Digit 0-5: Must be a valid administrative division code of China PR.
  4508. * - Digit 6-13: Must be a valid YYYYMMDD date of birth. A future date is tolerated.
  4509. * - Digit 14-16: Order code, any integer.
  4510. * - Digit 17: An ISO 7064:1983, MOD 11-2 checksum.
  4511. * Both upper/lower case of X are tolerated.
  4512. * - For deprecated 15-digit system:
  4513. * - Digit 0-5: Must be a valid administrative division code of China PR.
  4514. * - Digit 6-11: Must be a valid YYMMDD date of birth, indicating the year of 19XX.
  4515. * - Digit 12-14: Order code, any integer.
  4516. * Lists of valid administrative division codes of China PR can be seen here:
  4517. * <http://www.stats.gov.cn/tjsj/tjbz/xzqhdm/>
  4518. * Published and maintained by National Bureau of Statistics of China PR.
  4519. * NOTE: Current and deprecated codes MUST BOTH be considered valid.
  4520. * Many Chinese citizens born in once existed administrative divisions!
  4521. *
  4522. * @see http://en.wikipedia.org/wiki/Resident_Identity_Card#Identity_card_number
  4523. * @param {String} value The ID
  4524. * @returns {Boolean}
  4525. */
  4526. _cn: function(value) {
  4527. // Basic format check (18 or 15 digits, considering X in checksum)
  4528. value = value.trim();
  4529. if (!/^\d{15}$/.test(value) && !/^\d{17}[\dXx]{1}$/.test(value)) {
  4530. return false;
  4531. }
  4532. // Check China PR Administrative division code
  4533. var adminDivisionCodes = {
  4534. 11: {
  4535. 0: [0],
  4536. 1: [[0, 9], [11, 17]],
  4537. 2: [0, 28, 29]
  4538. },
  4539. 12: {
  4540. 0: [0],
  4541. 1: [[0, 16]],
  4542. 2: [0, 21, 23, 25]
  4543. },
  4544. 13: {
  4545. 0: [0],
  4546. 1: [[0, 5], 7, 8, 21, [23, 33], [81, 85]],
  4547. 2: [[0, 5], [7, 9], [23, 25], 27, 29, 30, 81, 83],
  4548. 3: [[0, 4], [21, 24]],
  4549. 4: [[0, 4], 6, 21, [23, 35], 81],
  4550. 5: [[0, 3], [21, 35], 81, 82],
  4551. 6: [[0, 4], [21, 38], [81, 84]],
  4552. 7: [[0, 3], 5, 6, [21, 33]],
  4553. 8: [[0, 4], [21, 28]],
  4554. 9: [[0, 3], [21, 30], [81, 84]],
  4555. 10: [[0, 3], [22, 26], 28, 81, 82],
  4556. 11: [[0, 2], [21, 28], 81, 82]
  4557. },
  4558. 14: {
  4559. 0: [0],
  4560. 1: [0, 1, [5, 10], [21, 23], 81],
  4561. 2: [[0, 3], 11, 12, [21, 27]],
  4562. 3: [[0, 3], 11, 21, 22],
  4563. 4: [[0, 2], 11, 21, [23, 31], 81],
  4564. 5: [[0, 2], 21, 22, 24, 25, 81],
  4565. 6: [[0, 3], [21, 24]],
  4566. 7: [[0, 2], [21, 29], 81],
  4567. 8: [[0, 2], [21, 30], 81, 82],
  4568. 9: [[0, 2], [21, 32], 81],
  4569. 10: [[0, 2], [21, 34], 81, 82],
  4570. 11: [[0, 2], [21, 30], 81, 82],
  4571. 23: [[0, 3], 22, 23, [25, 30], 32, 33]
  4572. },
  4573. 15: {
  4574. 0: [0],
  4575. 1: [[0, 5], [21, 25]],
  4576. 2: [[0, 7], [21, 23]],
  4577. 3: [[0, 4]],
  4578. 4: [[0, 4], [21, 26], [28, 30]],
  4579. 5: [[0, 2], [21, 26], 81],
  4580. 6: [[0, 2], [21, 27]],
  4581. 7: [[0, 3], [21, 27], [81, 85]],
  4582. 8: [[0, 2], [21, 26]],
  4583. 9: [[0, 2], [21, 29], 81],
  4584. 22: [[0, 2], [21, 24]],
  4585. 25: [[0, 2], [22, 31]],
  4586. 26: [[0, 2], [24, 27], [29, 32], 34],
  4587. 28: [0, 1, [22, 27]],
  4588. 29: [0, [21, 23]]
  4589. },
  4590. 21: {
  4591. 0: [0],
  4592. 1: [[0, 6], [11, 14], [22, 24], 81],
  4593. 2: [[0, 4], [11, 13], 24, [81, 83]],
  4594. 3: [[0, 4], 11, 21, 23, 81],
  4595. 4: [[0, 4], 11, [21, 23]],
  4596. 5: [[0, 5], 21, 22],
  4597. 6: [[0, 4], 24, 81, 82],
  4598. 7: [[0, 3], 11, 26, 27, 81, 82],
  4599. 8: [[0, 4], 11, 81, 82],
  4600. 9: [[0, 5], 11, 21, 22],
  4601. 10: [[0, 5], 11, 21, 81],
  4602. 11: [[0, 3], 21, 22],
  4603. 12: [[0, 2], 4, 21, 23, 24, 81, 82],
  4604. 13: [[0, 3], 21, 22, 24, 81, 82],
  4605. 14: [[0, 4], 21, 22, 81]
  4606. },
  4607. 22: {
  4608. 0: [0],
  4609. 1: [[0, 6], 12, 22, [81, 83]],
  4610. 2: [[0, 4], 11, 21, [81, 84]],
  4611. 3: [[0, 3], 22, 23, 81, 82],
  4612. 4: [[0, 3], 21, 22],
  4613. 5: [[0, 3], 21, 23, 24, 81, 82],
  4614. 6: [[0, 2], 4, 5, [21, 23], 25, 81],
  4615. 7: [[0, 2], [21, 24], 81],
  4616. 8: [[0, 2], 21, 22, 81, 82],
  4617. 24: [[0, 6], 24, 26]
  4618. },
  4619. 23: {
  4620. 0: [0],
  4621. 1: [[0, 12], 21, [23, 29], [81, 84]],
  4622. 2: [[0, 8], 21, [23, 25], 27, [29, 31], 81],
  4623. 3: [[0, 7], 21, 81, 82],
  4624. 4: [[0, 7], 21, 22],
  4625. 5: [[0, 3], 5, 6, [21, 24]],
  4626. 6: [[0, 6], [21, 24]],
  4627. 7: [[0, 16], 22, 81],
  4628. 8: [[0, 5], 11, 22, 26, 28, 33, 81, 82],
  4629. 9: [[0, 4], 21],
  4630. 10: [[0, 5], 24, 25, 81, [83, 85]],
  4631. 11: [[0, 2], 21, 23, 24, 81, 82],
  4632. 12: [[0, 2], [21, 26], [81, 83]],
  4633. 27: [[0, 4], [21, 23]]
  4634. },
  4635. 31: {
  4636. 0: [0],
  4637. 1: [0, 1, [3, 10], [12, 20]],
  4638. 2: [0, 30]
  4639. },
  4640. 32: {
  4641. 0: [0],
  4642. 1: [[0, 7], 11, [13, 18], 24, 25],
  4643. 2: [[0, 6], 11, 81, 82],
  4644. 3: [[0, 5], 11, 12, [21, 24], 81, 82],
  4645. 4: [[0, 2], 4, 5, 11, 12, 81, 82],
  4646. 5: [[0, 9], [81, 85]],
  4647. 6: [[0, 2], 11, 12, 21, 23, [81, 84]],
  4648. 7: [0, 1, 3, 5, 6, [21, 24]],
  4649. 8: [[0, 4], 11, 26, [29, 31]],
  4650. 9: [[0, 3], [21, 25], 28, 81, 82],
  4651. 10: [[0, 3], 11, 12, 23, 81, 84, 88],
  4652. 11: [[0, 2], 11, 12, [81, 83]],
  4653. 12: [[0, 4], [81, 84]],
  4654. 13: [[0, 2], 11, [21, 24]]
  4655. },
  4656. 33: {
  4657. 0: [0],
  4658. 1: [[0, 6], [8, 10], 22, 27, 82, 83, 85],
  4659. 2: [0, 1, [3, 6], 11, 12, 25, 26, [81, 83]],
  4660. 3: [[0, 4], 22, 24, [26, 29], 81, 82],
  4661. 4: [[0, 2], 11, 21, 24, [81, 83]],
  4662. 5: [[0, 3], [21, 23]],
  4663. 6: [[0, 2], 21, 24, [81, 83]],
  4664. 7: [[0, 3], 23, 26, 27, [81, 84]],
  4665. 8: [[0, 3], 22, 24, 25, 81],
  4666. 9: [[0, 3], 21, 22],
  4667. 10: [[0, 4], [21, 24], 81, 82],
  4668. 11: [[0, 2], [21, 27], 81]
  4669. },
  4670. 34: {
  4671. 0: [0],
  4672. 1: [[0, 4], 11, [21, 24], 81],
  4673. 2: [[0, 4], 7, 8, [21, 23], 25],
  4674. 3: [[0, 4], 11, [21, 23]],
  4675. 4: [[0, 6], 21],
  4676. 5: [[0, 4], 6, [21, 23]],
  4677. 6: [[0, 4], 21],
  4678. 7: [[0, 3], 11, 21],
  4679. 8: [[0, 3], 11, [22, 28], 81],
  4680. 10: [[0, 4], [21, 24]],
  4681. 11: [[0, 3], 22, [24, 26], 81, 82],
  4682. 12: [[0, 4], 21, 22, 25, 26, 82],
  4683. 13: [[0, 2], [21, 24]],
  4684. 14: [[0, 2], [21, 24]],
  4685. 15: [[0, 3], [21, 25]],
  4686. 16: [[0, 2], [21, 23]],
  4687. 17: [[0, 2], [21, 23]],
  4688. 18: [[0, 2], [21, 25], 81]
  4689. },
  4690. 35: {
  4691. 0: [0],
  4692. 1: [[0, 5], 11, [21, 25], 28, 81, 82],
  4693. 2: [[0, 6], [11, 13]],
  4694. 3: [[0, 5], 22],
  4695. 4: [[0, 3], 21, [23, 30], 81],
  4696. 5: [[0, 5], 21, [24, 27], [81, 83]],
  4697. 6: [[0, 3], [22, 29], 81],
  4698. 7: [[0, 2], [21, 25], [81, 84]],
  4699. 8: [[0, 2], [21, 25], 81],
  4700. 9: [[0, 2], [21, 26], 81, 82]
  4701. },
  4702. 36: {
  4703. 0: [0],
  4704. 1: [[0, 5], 11, [21, 24]],
  4705. 2: [[0, 3], 22, 81],
  4706. 3: [[0, 2], 13, [21, 23]],
  4707. 4: [[0, 3], 21, [23, 30], 81, 82],
  4708. 5: [[0, 2], 21],
  4709. 6: [[0, 2], 22, 81],
  4710. 7: [[0, 2], [21, 35], 81, 82],
  4711. 8: [[0, 3], [21, 30], 81],
  4712. 9: [[0, 2], [21, 26], [81, 83]],
  4713. 10: [[0, 2], [21, 30]],
  4714. 11: [[0, 2], [21, 30], 81]
  4715. },
  4716. 37: {
  4717. 0: [0],
  4718. 1: [[0, 5], 12, 13, [24, 26], 81],
  4719. 2: [[0, 3], 5, [11, 14], [81, 85]],
  4720. 3: [[0, 6], [21, 23]],
  4721. 4: [[0, 6], 81],
  4722. 5: [[0, 3], [21, 23]],
  4723. 6: [[0, 2], [11, 13], 34, [81, 87]],
  4724. 7: [[0, 5], 24, 25, [81, 86]],
  4725. 8: [[0, 2], 11, [26, 32], [81, 83]],
  4726. 9: [[0, 3], 11, 21, 23, 82, 83],
  4727. 10: [[0, 2], [81, 83]],
  4728. 11: [[0, 3], 21, 22],
  4729. 12: [[0, 3]],
  4730. 13: [[0, 2], 11, 12, [21, 29]],
  4731. 14: [[0, 2], [21, 28], 81, 82],
  4732. 15: [[0, 2], [21, 26], 81],
  4733. 16: [[0, 2], [21, 26]],
  4734. 17: [[0, 2], [21, 28]]
  4735. },
  4736. 41: {
  4737. 0: [0],
  4738. 1: [[0, 6], 8, 22, [81, 85]],
  4739. 2: [[0, 5], 11, [21, 25]],
  4740. 3: [[0, 7], 11, [22, 29], 81],
  4741. 4: [[0, 4], 11, [21, 23], 25, 81, 82],
  4742. 5: [[0, 3], 5, 6, 22, 23, 26, 27, 81],
  4743. 6: [[0, 3], 11, 21, 22],
  4744. 7: [[0, 4], 11, 21, [24, 28], 81, 82],
  4745. 8: [[0, 4], 11, [21, 23], 25, [81, 83]],
  4746. 9: [[0, 2], 22, 23, [26, 28]],
  4747. 10: [[0, 2], [23, 25], 81, 82],
  4748. 11: [[0, 4], [21, 23]],
  4749. 12: [[0, 2], 21, 22, 24, 81, 82],
  4750. 13: [[0, 3], [21, 30], 81],
  4751. 14: [[0, 3], [21, 26], 81],
  4752. 15: [[0, 3], [21, 28]],
  4753. 16: [[0, 2], [21, 28], 81],
  4754. 17: [[0, 2], [21, 29]],
  4755. 90: [0, 1]
  4756. },
  4757. 42: {
  4758. 0: [0],
  4759. 1: [[0, 7], [11, 17]],
  4760. 2: [[0, 5], 22, 81],
  4761. 3: [[0, 3], [21, 25], 81],
  4762. 5: [[0, 6], [25, 29], [81, 83]],
  4763. 6: [[0, 2], 6, 7, [24, 26], [82, 84]],
  4764. 7: [[0, 4]],
  4765. 8: [[0, 2], 4, 21, 22, 81],
  4766. 9: [[0, 2], [21, 23], 81, 82, 84],
  4767. 10: [[0, 3], [22, 24], 81, 83, 87],
  4768. 11: [[0, 2], [21, 27], 81, 82],
  4769. 12: [[0, 2], [21, 24], 81],
  4770. 13: [[0, 3], 21, 81],
  4771. 28: [[0, 2], 22, 23, [25, 28]],
  4772. 90: [0, [4, 6], 21]
  4773. },
  4774. 43: {
  4775. 0: [0],
  4776. 1: [[0, 5], 11, 12, 21, 22, 24, 81],
  4777. 2: [[0, 4], 11, 21, [23, 25], 81],
  4778. 3: [[0, 2], 4, 21, 81, 82],
  4779. 4: [0, 1, [5, 8], 12, [21, 24], 26, 81, 82],
  4780. 5: [[0, 3], 11, [21, 25], [27, 29], 81],
  4781. 6: [[0, 3], 11, 21, 23, 24, 26, 81, 82],
  4782. 7: [[0, 3], [21, 26], 81],
  4783. 8: [[0, 2], 11, 21, 22],
  4784. 9: [[0, 3], [21, 23], 81],
  4785. 10: [[0, 3], [21, 28], 81],
  4786. 11: [[0, 3], [21, 29]],
  4787. 12: [[0, 2], [21, 30], 81],
  4788. 13: [[0, 2], 21, 22, 81, 82],
  4789. 31: [0, 1, [22, 27], 30]
  4790. },
  4791. 44: {
  4792. 0: [0],
  4793. 1: [[0, 7], [11, 16], 83, 84],
  4794. 2: [[0, 5], 21, 22, 24, 29, 32, 33, 81, 82],
  4795. 3: [0, 1, [3, 8]],
  4796. 4: [[0, 4]],
  4797. 5: [0, 1, [6, 15], 23, 82, 83],
  4798. 6: [0, 1, [4, 8]],
  4799. 7: [0, 1, [3, 5], 81, [83, 85]],
  4800. 8: [[0, 4], 11, 23, 25, [81, 83]],
  4801. 9: [[0, 3], 23, [81, 83]],
  4802. 12: [[0, 3], [23, 26], 83, 84],
  4803. 13: [[0, 3], [22, 24], 81],
  4804. 14: [[0, 2], [21, 24], 26, 27, 81],
  4805. 15: [[0, 2], 21, 23, 81],
  4806. 16: [[0, 2], [21, 25]],
  4807. 17: [[0, 2], 21, 23, 81],
  4808. 18: [[0, 3], 21, 23, [25, 27], 81, 82],
  4809. 19: [0],
  4810. 20: [0],
  4811. 51: [[0, 3], 21, 22],
  4812. 52: [[0, 3], 21, 22, 24, 81],
  4813. 53: [[0, 2], [21, 23], 81]
  4814. },
  4815. 45: {
  4816. 0: [0],
  4817. 1: [[0, 9], [21, 27]],
  4818. 2: [[0, 5], [21, 26]],
  4819. 3: [[0, 5], 11, 12, [21, 32]],
  4820. 4: [0, 1, [3, 6], 11, [21, 23], 81],
  4821. 5: [[0, 3], 12, 21],
  4822. 6: [[0, 3], 21, 81],
  4823. 7: [[0, 3], 21, 22],
  4824. 8: [[0, 4], 21, 81],
  4825. 9: [[0, 3], [21, 24], 81],
  4826. 10: [[0, 2], [21, 31]],
  4827. 11: [[0, 2], [21, 23]],
  4828. 12: [[0, 2], [21, 29], 81],
  4829. 13: [[0, 2], [21, 24], 81],
  4830. 14: [[0, 2], [21, 25], 81]
  4831. },
  4832. 46: {
  4833. 0: [0],
  4834. 1: [0, 1, [5, 8]],
  4835. 2: [0, 1],
  4836. 3: [0, [21, 23]],
  4837. 90: [[0, 3], [5, 7], [21, 39]]
  4838. },
  4839. 50: {
  4840. 0: [0],
  4841. 1: [[0, 19]],
  4842. 2: [0, [22, 38], [40, 43]],
  4843. 3: [0, [81, 84]]
  4844. },
  4845. 51: {
  4846. 0: [0],
  4847. 1: [0, 1, [4, 8], [12, 15], [21, 24], 29, 31, 32, [81, 84]],
  4848. 3: [[0, 4], 11, 21, 22],
  4849. 4: [[0, 3], 11, 21, 22],
  4850. 5: [[0, 4], 21, 22, 24, 25],
  4851. 6: [0, 1, 3, 23, 26, [81, 83]],
  4852. 7: [0, 1, 3, 4, [22, 27], 81],
  4853. 8: [[0, 2], 11, 12, [21, 24]],
  4854. 9: [[0, 4], [21, 23]],
  4855. 10: [[0, 2], 11, 24, 25, 28],
  4856. 11: [[0, 2], [11, 13], 23, 24, 26, 29, 32, 33, 81],
  4857. 13: [[0, 4], [21, 25], 81],
  4858. 14: [[0, 2], [21, 25]],
  4859. 15: [[0, 3], [21, 29]],
  4860. 16: [[0, 3], [21, 23], 81],
  4861. 17: [[0, 3], [21, 25], 81],
  4862. 18: [[0, 3], [21, 27]],
  4863. 19: [[0, 3], [21, 23]],
  4864. 20: [[0, 2], 21, 22, 81],
  4865. 32: [0, [21, 33]],
  4866. 33: [0, [21, 38]],
  4867. 34: [0, 1, [22, 37]]
  4868. },
  4869. 52: {
  4870. 0: [0],
  4871. 1: [[0, 3], [11, 15], [21, 23], 81],
  4872. 2: [0, 1, 3, 21, 22],
  4873. 3: [[0, 3], [21, 30], 81, 82],
  4874. 4: [[0, 2], [21, 25]],
  4875. 5: [[0, 2], [21, 27]],
  4876. 6: [[0, 3], [21, 28]],
  4877. 22: [0, 1, [22, 30]],
  4878. 23: [0, 1, [22, 28]],
  4879. 24: [0, 1, [22, 28]],
  4880. 26: [0, 1, [22, 36]],
  4881. 27: [[0, 2], 22, 23, [25, 32]]
  4882. },
  4883. 53: {
  4884. 0: [0],
  4885. 1: [[0, 3], [11, 14], 21, 22, [24, 29], 81],
  4886. 3: [[0, 2], [21, 26], 28, 81],
  4887. 4: [[0, 2], [21, 28]],
  4888. 5: [[0, 2], [21, 24]],
  4889. 6: [[0, 2], [21, 30]],
  4890. 7: [[0, 2], [21, 24]],
  4891. 8: [[0, 2], [21, 29]],
  4892. 9: [[0, 2], [21, 27]],
  4893. 23: [0, 1, [22, 29], 31],
  4894. 25: [[0, 4], [22, 32]],
  4895. 26: [0, 1, [21, 28]],
  4896. 27: [0, 1, [22, 30]], 28: [0, 1, 22, 23],
  4897. 29: [0, 1, [22, 32]],
  4898. 31: [0, 2, 3, [22, 24]],
  4899. 34: [0, [21, 23]],
  4900. 33: [0, 21, [23, 25]],
  4901. 35: [0, [21, 28]]
  4902. },
  4903. 54: {
  4904. 0: [0],
  4905. 1: [[0, 2], [21, 27]],
  4906. 21: [0, [21, 29], 32, 33],
  4907. 22: [0, [21, 29], [31, 33]],
  4908. 23: [0, 1, [22, 38]],
  4909. 24: [0, [21, 31]],
  4910. 25: [0, [21, 27]],
  4911. 26: [0, [21, 27]]
  4912. },
  4913. 61: {
  4914. 0: [0],
  4915. 1: [[0, 4], [11, 16], 22, [24, 26]],
  4916. 2: [[0, 4], 22],
  4917. 3: [[0, 4], [21, 24], [26, 31]],
  4918. 4: [[0, 4], [22, 31], 81],
  4919. 5: [[0, 2], [21, 28], 81, 82],
  4920. 6: [[0, 2], [21, 32]],
  4921. 7: [[0, 2], [21, 30]],
  4922. 8: [[0, 2], [21, 31]],
  4923. 9: [[0, 2], [21, 29]],
  4924. 10: [[0, 2], [21, 26]]
  4925. },
  4926. 62: {
  4927. 0: [0],
  4928. 1: [[0, 5], 11, [21, 23]],
  4929. 2: [0, 1],
  4930. 3: [[0, 2], 21],
  4931. 4: [[0, 3], [21, 23]],
  4932. 5: [[0, 3], [21, 25]],
  4933. 6: [[0, 2], [21, 23]],
  4934. 7: [[0, 2], [21, 25]],
  4935. 8: [[0, 2], [21, 26]],
  4936. 9: [[0, 2], [21, 24], 81, 82],
  4937. 10: [[0, 2], [21, 27]],
  4938. 11: [[0, 2], [21, 26]],
  4939. 12: [[0, 2], [21, 28]],
  4940. 24: [0, 21, [24, 29]],
  4941. 26: [0, 21, [23, 30]],
  4942. 29: [0, 1, [21, 27]],
  4943. 30: [0, 1, [21, 27]]
  4944. },
  4945. 63: {
  4946. 0: [0],
  4947. 1: [[0, 5], [21, 23]],
  4948. 2: [0, 2, [21, 25]],
  4949. 21: [0, [21, 23], [26, 28]],
  4950. 22: [0, [21, 24]],
  4951. 23: [0, [21, 24]],
  4952. 25: [0, [21, 25]],
  4953. 26: [0, [21, 26]],
  4954. 27: [0, 1, [21, 26]],
  4955. 28: [[0, 2], [21, 23]]
  4956. },
  4957. 64: {
  4958. 0: [0],
  4959. 1: [0, 1, [4, 6], 21, 22, 81],
  4960. 2: [[0, 3], 5, [21, 23]],
  4961. 3: [[0, 3], [21, 24], 81],
  4962. 4: [[0, 2], [21, 25]],
  4963. 5: [[0, 2], 21, 22]
  4964. },
  4965. 65: {
  4966. 0: [0],
  4967. 1: [[0, 9], 21],
  4968. 2: [[0, 5]],
  4969. 21: [0, 1, 22, 23],
  4970. 22: [0, 1, 22, 23],
  4971. 23: [[0, 3], [23, 25], 27, 28],
  4972. 28: [0, 1, [22, 29]],
  4973. 29: [0, 1, [22, 29]],
  4974. 30: [0, 1, [22, 24]], 31: [0, 1, [21, 31]],
  4975. 32: [0, 1, [21, 27]],
  4976. 40: [0, 2, 3, [21, 28]],
  4977. 42: [[0, 2], 21, [23, 26]],
  4978. 43: [0, 1, [21, 26]],
  4979. 90: [[0, 4]], 27: [[0, 2], 22, 23]
  4980. },
  4981. 71: { 0: [0] },
  4982. 81: { 0: [0] },
  4983. 82: { 0: [0] }
  4984. };
  4985. var provincial = parseInt(value.substr(0, 2), 10),
  4986. prefectural = parseInt(value.substr(2, 2), 10),
  4987. county = parseInt(value.substr(4, 2), 10);
  4988. if (!adminDivisionCodes[provincial] || !adminDivisionCodes[provincial][prefectural]) {
  4989. return false;
  4990. }
  4991. var inRange = false,
  4992. rangeDef = adminDivisionCodes[provincial][prefectural];
  4993. for (var i = 0; i < rangeDef.length; i++) {
  4994. if (($.isArray(rangeDef[i]) && rangeDef[i][0] <= county && county <= rangeDef[i][1])
  4995. || (!$.isArray(rangeDef[i]) && county === rangeDef[i]))
  4996. {
  4997. inRange = true;
  4998. break;
  4999. }
  5000. }
  5001. if (!inRange) {
  5002. return false;
  5003. }
  5004. // Check date of birth
  5005. var dob;
  5006. if (value.length === 18) {
  5007. dob = value.substr(6, 8);
  5008. } else /* length == 15 */ {
  5009. dob = '19' + value.substr(6, 6);
  5010. }
  5011. var year = parseInt(dob.substr(0, 4), 10),
  5012. month = parseInt(dob.substr(4, 2), 10),
  5013. day = parseInt(dob.substr(6, 2), 10);
  5014. if (!FormValidation.Helper.date(year, month, day)) {
  5015. return false;
  5016. }
  5017. // Check checksum (18-digit system only)
  5018. if (value.length === 18) {
  5019. var sum = 0,
  5020. weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
  5021. for (i = 0; i < 17; i++) {
  5022. sum += parseInt(value.charAt(i), 10) * weight[i];
  5023. }
  5024. sum = (12 - (sum % 11)) % 11;
  5025. var checksum = (value.charAt(17).toUpperCase() !== 'X') ? parseInt(value.charAt(17), 10) : 10;
  5026. return checksum === sum;
  5027. }
  5028. return true;
  5029. },
  5030. /**
  5031. * Validate Czech national identification number (RC)
  5032. *
  5033. * @param {String} value The ID
  5034. * @returns {Boolean}
  5035. */
  5036. _cz: function(value) {
  5037. if (!/^\d{9,10}$/.test(value)) {
  5038. return false;
  5039. }
  5040. var year = 1900 + parseInt(value.substr(0, 2), 10),
  5041. month = parseInt(value.substr(2, 2), 10) % 50 % 20,
  5042. day = parseInt(value.substr(4, 2), 10);
  5043. if (value.length === 9) {
  5044. if (year >= 1980) {
  5045. year -= 100;
  5046. }
  5047. if (year > 1953) {
  5048. return false;
  5049. }
  5050. } else if (year < 1954) {
  5051. year += 100;
  5052. }
  5053. if (!FormValidation.Helper.date(year, month, day)) {
  5054. return false;
  5055. }
  5056. // Check that the birth date is not in the future
  5057. if (value.length === 10) {
  5058. var check = parseInt(value.substr(0, 9), 10) % 11;
  5059. if (year < 1985) {
  5060. check = check % 10;
  5061. }
  5062. return (check + '' === value.substr(9, 1));
  5063. }
  5064. return true;
  5065. },
  5066. /**
  5067. * Validate Danish Personal Identification number (CPR)
  5068. *
  5069. * @see https://en.wikipedia.org/wiki/Personal_identification_number_(Denmark)
  5070. * @param {String} value The ID
  5071. * @returns {Boolean}
  5072. */
  5073. _dk: function(value) {
  5074. if (!/^[0-9]{6}[-]{0,1}[0-9]{4}$/.test(value)) {
  5075. return false;
  5076. }
  5077. value = value.replace(/-/g, '');
  5078. var day = parseInt(value.substr(0, 2), 10),
  5079. month = parseInt(value.substr(2, 2), 10),
  5080. year = parseInt(value.substr(4, 2), 10);
  5081. switch (true) {
  5082. case ('5678'.indexOf(value.charAt(6)) !== -1 && year >= 58):
  5083. year += 1800;
  5084. break;
  5085. case ('0123'.indexOf(value.charAt(6)) !== -1):
  5086. case ('49'.indexOf(value.charAt(6)) !== -1 && year >= 37):
  5087. year += 1900;
  5088. break;
  5089. default:
  5090. year += 2000;
  5091. break;
  5092. }
  5093. return FormValidation.Helper.date(year, month, day);
  5094. },
  5095. /**
  5096. * Validate Estonian Personal Identification Code (isikukood)
  5097. *
  5098. * @see http://et.wikipedia.org/wiki/Isikukood
  5099. * @param {String} value The ID
  5100. * @returns {Boolean}
  5101. */
  5102. _ee: function(value) {
  5103. // Use the same format as Lithuanian Personal Code
  5104. return this._lt(value);
  5105. },
  5106. /**
  5107. * Validate Spanish personal identity code (DNI)
  5108. * Support DNI (for Spanish citizens), NIE (for foreign people) and CIF (for legal entities)
  5109. *
  5110. * @see https://en.wikipedia.org/wiki/National_identification_number#Spain
  5111. * @param {String} value The ID
  5112. * @returns {Boolean|Object}
  5113. */
  5114. _es: function(value) {
  5115. var isDNI = /^[0-9]{8}[-]{0,1}[A-HJ-NP-TV-Z]$/.test(value),
  5116. isNIE = /^[XYZ][-]{0,1}[0-9]{7}[-]{0,1}[A-HJ-NP-TV-Z]$/.test(value),
  5117. isCIF = /^[A-HNPQS][-]{0,1}[0-9]{7}[-]{0,1}[0-9A-J]$/.test(value);
  5118. if (!isDNI && !isNIE && !isCIF) {
  5119. return false;
  5120. }
  5121. value = value.replace(/-/g, '');
  5122. var check, type, isValid = true;
  5123. if (isDNI || isNIE) {
  5124. type = 'DNI';
  5125. var index = 'XYZ'.indexOf(value.charAt(0));
  5126. if (index !== -1) {
  5127. // It is NIE number
  5128. value = index + value.substr(1) + '';
  5129. type = 'NIE';
  5130. }
  5131. check = parseInt(value.substr(0, 8), 10);
  5132. check = 'TRWAGMYFPDXBNJZSQVHLCKE'[check % 23];
  5133. return {
  5134. valid: (check === value.substr(8, 1)),
  5135. type: type
  5136. };
  5137. } else {
  5138. check = value.substr(1, 7);
  5139. type = 'CIF';
  5140. var letter = value[0],
  5141. control = value.substr(-1),
  5142. sum = 0;
  5143. // The digits in the even positions are added to the sum directly.
  5144. // The ones in the odd positions are multiplied by 2 and then added to the sum.
  5145. // If the result of multiplying by 2 is 10 or higher, add the two digits
  5146. // together and add that to the sum instead
  5147. for (var i = 0; i < check.length; i++) {
  5148. if (i % 2 !== 0) {
  5149. sum += parseInt(check[i], 10);
  5150. } else {
  5151. var tmp = '' + (parseInt(check[i], 10) * 2);
  5152. sum += parseInt(tmp[0], 10);
  5153. if (tmp.length === 2) {
  5154. sum += parseInt(tmp[1], 10);
  5155. }
  5156. }
  5157. }
  5158. // The control digit is calculated from the last digit of the sum.
  5159. // If that last digit is not 0, subtract it from 10
  5160. var lastDigit = sum - (Math.floor(sum / 10) * 10);
  5161. if (lastDigit !== 0) {
  5162. lastDigit = 10 - lastDigit;
  5163. }
  5164. if ('KQS'.indexOf(letter) !== -1) {
  5165. // If the CIF starts with a K, Q or S, the control digit must be a letter
  5166. isValid = (control === 'JABCDEFGHI'[lastDigit]);
  5167. } else if ('ABEH'.indexOf(letter) !== -1) {
  5168. // If it starts with A, B, E or H, it has to be a number
  5169. isValid = (control === ('' + lastDigit));
  5170. } else {
  5171. // In any other case, it doesn't matter
  5172. isValid = (control === ('' + lastDigit) || control === 'JABCDEFGHI'[lastDigit]);
  5173. }
  5174. return {
  5175. valid: isValid,
  5176. type: type
  5177. };
  5178. }
  5179. },
  5180. /**
  5181. * Validate Finnish Personal Identity Code (HETU)
  5182. *
  5183. * @param {String} value The ID
  5184. * @returns {Boolean}
  5185. */
  5186. _fi: function(value) {
  5187. if (!/^[0-9]{6}[-+A][0-9]{3}[0-9ABCDEFHJKLMNPRSTUVWXY]$/.test(value)) {
  5188. return false;
  5189. }
  5190. var day = parseInt(value.substr(0, 2), 10),
  5191. month = parseInt(value.substr(2, 2), 10),
  5192. year = parseInt(value.substr(4, 2), 10),
  5193. centuries = {
  5194. '+': 1800,
  5195. '-': 1900,
  5196. 'A': 2000
  5197. };
  5198. year = centuries[value.charAt(6)] + year;
  5199. if (!FormValidation.Helper.date(year, month, day)) {
  5200. return false;
  5201. }
  5202. var individual = parseInt(value.substr(7, 3), 10);
  5203. if (individual < 2) {
  5204. return false;
  5205. }
  5206. var n = value.substr(0, 6) + value.substr(7, 3) + '';
  5207. n = parseInt(n, 10);
  5208. return '0123456789ABCDEFHJKLMNPRSTUVWXY'.charAt(n % 31) === value.charAt(10);
  5209. },
  5210. /**
  5211. * Validate Croatian personal identification number (OIB)
  5212. *
  5213. * @param {String} value The ID
  5214. * @returns {Boolean}
  5215. */
  5216. _hr: function(value) {
  5217. if (!/^[0-9]{11}$/.test(value)) {
  5218. return false;
  5219. }
  5220. return FormValidation.Helper.mod11And10(value);
  5221. },
  5222. /**
  5223. * Validate Irish Personal Public Service Number (PPS)
  5224. *
  5225. * @see https://en.wikipedia.org/wiki/Personal_Public_Service_Number
  5226. * @param {String} value The ID
  5227. * @returns {Boolean}
  5228. */
  5229. _ie: function(value) {
  5230. if (!/^\d{7}[A-W][AHWTX]?$/.test(value)) {
  5231. return false;
  5232. }
  5233. var getCheckDigit = function(value) {
  5234. while (value.length < 7) {
  5235. value = '0' + value;
  5236. }
  5237. var alphabet = 'WABCDEFGHIJKLMNOPQRSTUV',
  5238. sum = 0;
  5239. for (var i = 0; i < 7; i++) {
  5240. sum += parseInt(value.charAt(i), 10) * (8 - i);
  5241. }
  5242. sum += 9 * alphabet.indexOf(value.substr(7));
  5243. return alphabet[sum % 23];
  5244. };
  5245. // 2013 format
  5246. if (value.length === 9 && ('A' === value.charAt(8) || 'H' === value.charAt(8))) {
  5247. return value.charAt(7) === getCheckDigit(value.substr(0, 7) + value.substr(8) + '');
  5248. }
  5249. // The old format
  5250. else {
  5251. return value.charAt(7) === getCheckDigit(value.substr(0, 7));
  5252. }
  5253. },
  5254. /**
  5255. * Validate Iceland national identification number (Kennitala)
  5256. *
  5257. * @see http://en.wikipedia.org/wiki/Kennitala
  5258. * @param {String} value The ID
  5259. * @returns {Boolean}
  5260. */
  5261. _is: function(value) {
  5262. if (!/^[0-9]{6}[-]{0,1}[0-9]{4}$/.test(value)) {
  5263. return false;
  5264. }
  5265. value = value.replace(/-/g, '');
  5266. var day = parseInt(value.substr(0, 2), 10),
  5267. month = parseInt(value.substr(2, 2), 10),
  5268. year = parseInt(value.substr(4, 2), 10),
  5269. century = parseInt(value.charAt(9), 10);
  5270. year = (century === 9) ? (1900 + year) : ((20 + century) * 100 + year);
  5271. if (!FormValidation.Helper.date(year, month, day, true)) {
  5272. return false;
  5273. }
  5274. // Validate the check digit
  5275. var sum = 0,
  5276. weight = [3, 2, 7, 6, 5, 4, 3, 2];
  5277. for (var i = 0; i < 8; i++) {
  5278. sum += parseInt(value.charAt(i), 10) * weight[i];
  5279. }
  5280. sum = 11 - sum % 11;
  5281. return (sum + '' === value.charAt(8));
  5282. },
  5283. /**
  5284. * Validate Lithuanian Personal Code (Asmens kodas)
  5285. *
  5286. * @see http://en.wikipedia.org/wiki/National_identification_number#Lithuania
  5287. * @see http://www.adomas.org/midi2007/pcode.html
  5288. * @param {String} value The ID
  5289. * @returns {Boolean}
  5290. */
  5291. _lt: function(value) {
  5292. if (!/^[0-9]{11}$/.test(value)) {
  5293. return false;
  5294. }
  5295. var gender = parseInt(value.charAt(0), 10),
  5296. year = parseInt(value.substr(1, 2), 10),
  5297. month = parseInt(value.substr(3, 2), 10),
  5298. day = parseInt(value.substr(5, 2), 10),
  5299. century = (gender % 2 === 0) ? (17 + gender / 2) : (17 + (gender + 1) / 2);
  5300. year = century * 100 + year;
  5301. if (!FormValidation.Helper.date(year, month, day, true)) {
  5302. return false;
  5303. }
  5304. // Validate the check digit
  5305. var sum = 0,
  5306. weight = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1];
  5307. for (var i = 0; i < 10; i++) {
  5308. sum += parseInt(value.charAt(i), 10) * weight[i];
  5309. }
  5310. sum = sum % 11;
  5311. if (sum !== 10) {
  5312. return sum + '' === value.charAt(10);
  5313. }
  5314. // Re-calculate the check digit
  5315. sum = 0;
  5316. weight = [3, 4, 5, 6, 7, 8, 9, 1, 2, 3];
  5317. for (i = 0; i < 10; i++) {
  5318. sum += parseInt(value.charAt(i), 10) * weight[i];
  5319. }
  5320. sum = sum % 11;
  5321. if (sum === 10) {
  5322. sum = 0;
  5323. }
  5324. return (sum + '' === value.charAt(10));
  5325. },
  5326. /**
  5327. * Validate Latvian Personal Code (Personas kods)
  5328. *
  5329. * @see http://laacz.lv/2006/11/25/pk-parbaudes-algoritms/
  5330. * @param {String} value The ID
  5331. * @returns {Boolean}
  5332. */
  5333. _lv: function(value) {
  5334. if (!/^[0-9]{6}[-]{0,1}[0-9]{5}$/.test(value)) {
  5335. return false;
  5336. }
  5337. value = value.replace(/\D/g, '');
  5338. // Check birth date
  5339. var day = parseInt(value.substr(0, 2), 10),
  5340. month = parseInt(value.substr(2, 2), 10),
  5341. year = parseInt(value.substr(4, 2), 10);
  5342. year = year + 1800 + parseInt(value.charAt(6), 10) * 100;
  5343. if (!FormValidation.Helper.date(year, month, day, true)) {
  5344. return false;
  5345. }
  5346. // Check personal code
  5347. var sum = 0,
  5348. weight = [10, 5, 8, 4, 2, 1, 6, 3, 7, 9];
  5349. for (var i = 0; i < 10; i++) {
  5350. sum += parseInt(value.charAt(i), 10) * weight[i];
  5351. }
  5352. sum = (sum + 1) % 11 % 10;
  5353. return (sum + '' === value.charAt(10));
  5354. },
  5355. /**
  5356. * Validate Dutch national identification number (BSN)
  5357. *
  5358. * @see https://nl.wikipedia.org/wiki/Burgerservicenummer
  5359. * @param {String} value The ID
  5360. * @returns {Boolean}
  5361. */
  5362. _nl: function(value) {
  5363. if (value.length < 8) {
  5364. return false;
  5365. }
  5366. if (value.length === 8) {
  5367. value = '0' + value;
  5368. }
  5369. if (!/^[0-9]{4}[.]{0,1}[0-9]{2}[.]{0,1}[0-9]{3}$/.test(value)) {
  5370. return false;
  5371. }
  5372. value = value.replace(/\./g, '');
  5373. if (parseInt(value, 10) === 0) {
  5374. return false;
  5375. }
  5376. var sum = 0,
  5377. length = value.length;
  5378. for (var i = 0; i < length - 1; i++) {
  5379. sum += (9 - i) * parseInt(value.charAt(i), 10);
  5380. }
  5381. sum = sum % 11;
  5382. if (sum === 10) {
  5383. sum = 0;
  5384. }
  5385. return (sum + '' === value.charAt(length - 1));
  5386. },
  5387. /**
  5388. * Validate Poland citizen number (PESEL)
  5389. *
  5390. * @see http://en.wikipedia.org/wiki/National_identification_number#Poland
  5391. * @see http://en.wikipedia.org/wiki/PESEL
  5392. * @param {String} value The ID
  5393. * @returns {Boolean}
  5394. */
  5395. _pl: function(value) {
  5396. if (!/^[0-9]{11}$/.test(value)) {
  5397. return false;
  5398. }
  5399. var sum = 0,
  5400. length = value.length,
  5401. weight = [1, 3, 7, 9, 1, 3, 7, 9, 1, 3, 7];
  5402. for (var i = 0; i < length - 1; i++) {
  5403. sum += weight[i] * parseInt(value.charAt(i), 10);
  5404. }
  5405. sum = sum % 10;
  5406. if (sum === 0) {
  5407. sum = 10;
  5408. }
  5409. sum = 10 - sum;
  5410. return (sum + '' === value.charAt(length - 1));
  5411. },
  5412. /**
  5413. * Validate Romanian numerical personal code (CNP)
  5414. *
  5415. * @see http://en.wikipedia.org/wiki/National_identification_number#Romania
  5416. * @param {String} value The ID
  5417. * @returns {Boolean}
  5418. */
  5419. _ro: function(value) {
  5420. if (!/^[0-9]{13}$/.test(value)) {
  5421. return false;
  5422. }
  5423. var gender = parseInt(value.charAt(0), 10);
  5424. if (gender === 0 || gender === 7 || gender === 8) {
  5425. return false;
  5426. }
  5427. // Determine the date of birth
  5428. var year = parseInt(value.substr(1, 2), 10),
  5429. month = parseInt(value.substr(3, 2), 10),
  5430. day = parseInt(value.substr(5, 2), 10),
  5431. // The year of date is determined base on the gender
  5432. centuries = {
  5433. '1': 1900, // Male born between 1900 and 1999
  5434. '2': 1900, // Female born between 1900 and 1999
  5435. '3': 1800, // Male born between 1800 and 1899
  5436. '4': 1800, // Female born between 1800 and 1899
  5437. '5': 2000, // Male born after 2000
  5438. '6': 2000 // Female born after 2000
  5439. };
  5440. if (day > 31 && month > 12) {
  5441. return false;
  5442. }
  5443. if (gender !== 9) {
  5444. year = centuries[gender + ''] + year;
  5445. if (!FormValidation.Helper.date(year, month, day)) {
  5446. return false;
  5447. }
  5448. }
  5449. // Validate the check digit
  5450. var sum = 0,
  5451. weight = [2, 7, 9, 1, 4, 6, 3, 5, 8, 2, 7, 9],
  5452. length = value.length;
  5453. for (var i = 0; i < length - 1; i++) {
  5454. sum += parseInt(value.charAt(i), 10) * weight[i];
  5455. }
  5456. sum = sum % 11;
  5457. if (sum === 10) {
  5458. sum = 1;
  5459. }
  5460. return (sum + '' === value.charAt(length - 1));
  5461. },
  5462. /**
  5463. * Validate Swedish personal identity number (personnummer)
  5464. *
  5465. * @see http://en.wikipedia.org/wiki/Personal_identity_number_(Sweden)
  5466. * @param {String} value The ID
  5467. * @returns {Boolean}
  5468. */
  5469. _se: function(value) {
  5470. if (!/^[0-9]{10}$/.test(value) && !/^[0-9]{6}[-|+][0-9]{4}$/.test(value)) {
  5471. return false;
  5472. }
  5473. value = value.replace(/[^0-9]/g, '');
  5474. var year = parseInt(value.substr(0, 2), 10) + 1900,
  5475. month = parseInt(value.substr(2, 2), 10),
  5476. day = parseInt(value.substr(4, 2), 10);
  5477. if (!FormValidation.Helper.date(year, month, day)) {
  5478. return false;
  5479. }
  5480. // Validate the last check digit
  5481. return FormValidation.Helper.luhn(value);
  5482. },
  5483. /**
  5484. * Validate Slovak national identifier number (RC)
  5485. *
  5486. * @param {String} value The ID
  5487. * @returns {Boolean}
  5488. */
  5489. _sk: function(value) {
  5490. // Slovakia uses the same format as Czech Republic
  5491. return this._cz(value);
  5492. },
  5493. /**
  5494. * Validate San Marino citizen number
  5495. *
  5496. * @see http://en.wikipedia.org/wiki/National_identification_number#San_Marino
  5497. * @param {String} value The ID
  5498. * @returns {Boolean}
  5499. */
  5500. _sm: function(value) {
  5501. return /^\d{5}$/.test(value);
  5502. },
  5503. /**
  5504. * Validate Thailand citizen number
  5505. *
  5506. * @see http://en.wikipedia.org/wiki/National_identification_number#Thailand
  5507. * @param {String} value The ID
  5508. * @returns {Boolean}
  5509. */
  5510. _th: function(value) {
  5511. if (value.length !== 13) {
  5512. return false;
  5513. }
  5514. var sum = 0;
  5515. for (var i = 0; i < 12; i++) {
  5516. sum += parseInt(value.charAt(i), 10) * (13 - i);
  5517. }
  5518. return (11 - sum % 11) % 10 === parseInt(value.charAt(12), 10);
  5519. },
  5520. /**
  5521. * Validate Turkish Identification Number
  5522. *
  5523. * @see https://en.wikipedia.org/wiki/Turkish_Identification_Number
  5524. * @param {String} value The ID
  5525. * @returns {Boolean}
  5526. */
  5527. _tr: function(value) {
  5528. if (value.length !== 11) {
  5529. return false;
  5530. }
  5531. var sum = 0;
  5532. for (var i = 0; i < 10; i++) {
  5533. sum += parseInt(value.charAt(i), 10);
  5534. }
  5535. return (sum % 10) === parseInt(value.charAt(10), 10);
  5536. },
  5537. /**
  5538. * Validate South African ID
  5539. *
  5540. * @see http://en.wikipedia.org/wiki/National_identification_number#South_Africa
  5541. * @param {String} value The ID
  5542. * @returns {Boolean}
  5543. */
  5544. _za: function(value) {
  5545. if (!/^[0-9]{10}[0|1][8|9][0-9]$/.test(value)) {
  5546. return false;
  5547. }
  5548. var year = parseInt(value.substr(0, 2), 10),
  5549. currentYear = new Date().getFullYear() % 100,
  5550. month = parseInt(value.substr(2, 2), 10),
  5551. day = parseInt(value.substr(4, 2), 10);
  5552. year = (year >= currentYear) ? (year + 1900) : (year + 2000);
  5553. if (!FormValidation.Helper.date(year, month, day)) {
  5554. return false;
  5555. }
  5556. // Validate the last check digit
  5557. return FormValidation.Helper.luhn(value);
  5558. }
  5559. };
  5560. }(jQuery));
  5561. ;(function($) {
  5562. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  5563. 'en_US': {
  5564. identical: {
  5565. 'default': 'Please enter the same value'
  5566. }
  5567. }
  5568. });
  5569. FormValidation.Validator.identical = {
  5570. html5Attributes: {
  5571. message: 'message',
  5572. field: 'field'
  5573. },
  5574. /**
  5575. * Bind the validator on the live change of the field to compare with current one
  5576. *
  5577. * @param {FormValidation.Base} validator The validator plugin instance
  5578. * @param {jQuery} $field Field element
  5579. * @param {Object} options Consists of the following key:
  5580. * - field: The name of field that will be used to compare with current one
  5581. */
  5582. init: function(validator, $field, options, validatorName) {
  5583. var compareWith = validator.getFieldElements(options.field);
  5584. validator.onLiveChange(compareWith, 'live_' + validatorName, function() {
  5585. var status = validator.getStatus($field, validatorName);
  5586. if (status !== validator.STATUS_NOT_VALIDATED) {
  5587. validator.revalidateField($field);
  5588. }
  5589. });
  5590. },
  5591. /**
  5592. * Unbind the validator on the live change of the field to compare with current one
  5593. *
  5594. * @param {FormValidation.Base} validator The validator plugin instance
  5595. * @param {jQuery} $field Field element
  5596. * @param {Object} options Consists of the following key:
  5597. * - field: The name of field that will be used to compare with current one
  5598. */
  5599. destroy: function(validator, $field, options, validatorName) {
  5600. var compareWith = validator.getFieldElements(options.field);
  5601. validator.offLiveChange(compareWith, 'live_' + validatorName);
  5602. },
  5603. /**
  5604. * Check if input value equals to value of particular one
  5605. *
  5606. * @param {FormValidation.Base} validator The validator plugin instance
  5607. * @param {jQuery} $field Field element
  5608. * @param {Object} options Consists of the following key:
  5609. * - field: The name of field that will be used to compare with current one
  5610. * @returns {Boolean}
  5611. */
  5612. validate: function(validator, $field, options, validatorName) {
  5613. var value = validator.getFieldValue($field, validatorName),
  5614. compareWith = validator.getFieldElements(options.field);
  5615. if (compareWith === null || compareWith.length === 0) {
  5616. return true;
  5617. }
  5618. var compareValue = validator.getFieldValue(compareWith, validatorName);
  5619. if (value === compareValue) {
  5620. validator.updateStatus(compareWith, validator.STATUS_VALID, validatorName);
  5621. return true;
  5622. }
  5623. return false;
  5624. }
  5625. };
  5626. }(jQuery));
  5627. ;(function($) {
  5628. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  5629. 'en_US': {
  5630. imei: {
  5631. 'default': 'Please enter a valid IMEI number'
  5632. }
  5633. }
  5634. });
  5635. FormValidation.Validator.imei = {
  5636. /**
  5637. * Validate IMEI (International Mobile Station Equipment Identity)
  5638. *
  5639. * @see http://en.wikipedia.org/wiki/International_Mobile_Station_Equipment_Identity
  5640. * @param {FormValidation.Base} validator The validator plugin instance
  5641. * @param {jQuery} $field Field element
  5642. * @param {Object} options Can consist of the following keys:
  5643. * - message: The invalid message
  5644. * @returns {Boolean}
  5645. */
  5646. validate: function(validator, $field, options, validatorName) {
  5647. var value = validator.getFieldValue($field, validatorName);
  5648. if (value === '') {
  5649. return true;
  5650. }
  5651. switch (true) {
  5652. case /^\d{15}$/.test(value):
  5653. case /^\d{2}-\d{6}-\d{6}-\d{1}$/.test(value):
  5654. case /^\d{2}\s\d{6}\s\d{6}\s\d{1}$/.test(value):
  5655. value = value.replace(/[^0-9]/g, '');
  5656. return FormValidation.Helper.luhn(value);
  5657. case /^\d{14}$/.test(value):
  5658. case /^\d{16}$/.test(value):
  5659. case /^\d{2}-\d{6}-\d{6}(|-\d{2})$/.test(value):
  5660. case /^\d{2}\s\d{6}\s\d{6}(|\s\d{2})$/.test(value):
  5661. return true;
  5662. default:
  5663. return false;
  5664. }
  5665. }
  5666. };
  5667. }(jQuery));
  5668. ;(function($) {
  5669. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  5670. 'en_US': {
  5671. imo: {
  5672. 'default': 'Please enter a valid IMO number'
  5673. }
  5674. }
  5675. });
  5676. FormValidation.Validator.imo = {
  5677. /**
  5678. * Validate IMO (International Maritime Organization)
  5679. *
  5680. * @see http://en.wikipedia.org/wiki/IMO_Number
  5681. * @param {FormValidation.Base} validator The validator plugin instance
  5682. * @param {jQuery} $field Field element
  5683. * @param {Object} options Can consist of the following keys:
  5684. * - message: The invalid message
  5685. * @returns {Boolean}
  5686. */
  5687. validate: function(validator, $field, options, validatorName) {
  5688. var value = validator.getFieldValue($field, validatorName);
  5689. if (value === '') {
  5690. return true;
  5691. }
  5692. if (!/^IMO \d{7}$/i.test(value)) {
  5693. return false;
  5694. }
  5695. // Grab just the digits
  5696. var sum = 0,
  5697. digits = value.replace(/^.*(\d{7})$/, '$1');
  5698. for (var i = 6; i >= 1; i--) {
  5699. sum += (digits.slice((6 - i), -i) * (i + 1));
  5700. }
  5701. return sum % 10 === parseInt(digits.charAt(6), 10);
  5702. }
  5703. };
  5704. }(jQuery));
  5705. ;(function($) {
  5706. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  5707. 'en_US': {
  5708. integer: {
  5709. 'default': 'Please enter a valid number'
  5710. }
  5711. }
  5712. });
  5713. FormValidation.Validator.integer = {
  5714. html5Attributes: {
  5715. message: 'message',
  5716. thousandsseparator: 'thousandsSeparator',
  5717. decimalseparator: 'decimalSeparator'
  5718. },
  5719. enableByHtml5: function($field) {
  5720. return ('number' === $field.attr('type')) && ($field.attr('step') === undefined || $field.attr('step') % 1 === 0);
  5721. },
  5722. /**
  5723. * Return true if the input value is an integer
  5724. *
  5725. * @param {FormValidation.Base} validator The validator plugin instance
  5726. * @param {jQuery} $field Field element
  5727. * @param {Object} options Can consist of the following key:
  5728. * - message: The invalid message
  5729. * - thousandsSeparator: The thousands separator. It's empty by default
  5730. * - decimalSeparator: The decimal separator. It's '.' by default
  5731. * @returns {Boolean}
  5732. */
  5733. validate: function(validator, $field, options, validatorName) {
  5734. if (this.enableByHtml5($field) && $field.get(0).validity && $field.get(0).validity.badInput === true) {
  5735. return false;
  5736. }
  5737. var value = validator.getFieldValue($field, validatorName);
  5738. if (value === '') {
  5739. return true;
  5740. }
  5741. var decimalSeparator = options.decimalSeparator || '.',
  5742. thousandsSeparator = options.thousandsSeparator || '';
  5743. decimalSeparator = (decimalSeparator === '.') ? '\\.' : decimalSeparator;
  5744. thousandsSeparator = (thousandsSeparator === '.') ? '\\.' : thousandsSeparator;
  5745. var testRegexp = new RegExp('^-?[0-9]{1,3}(' + thousandsSeparator + '[0-9]{3})*(' + decimalSeparator + '[0-9]+)?$'),
  5746. thousandsReplacer = new RegExp(thousandsSeparator, 'g');
  5747. if (!testRegexp.test(value)) {
  5748. return false;
  5749. }
  5750. // Replace thousands separator with blank
  5751. if (thousandsSeparator) {
  5752. value = value.replace(thousandsReplacer, '');
  5753. }
  5754. // Replace decimal separator with a dot
  5755. if (decimalSeparator) {
  5756. value = value.replace(decimalSeparator, '.');
  5757. }
  5758. if (isNaN(value) || !isFinite(value)) {
  5759. return false;
  5760. }
  5761. // TODO: Use Number.isInteger() if available
  5762. value = parseFloat(value);
  5763. return Math.floor(value) === value;
  5764. }
  5765. };
  5766. }(jQuery));
  5767. ;(function($) {
  5768. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  5769. 'en_US': {
  5770. ip: {
  5771. 'default': 'Please enter a valid IP address',
  5772. ipv4: 'Please enter a valid IPv4 address',
  5773. ipv6: 'Please enter a valid IPv6 address'
  5774. }
  5775. }
  5776. });
  5777. FormValidation.Validator.ip = {
  5778. html5Attributes: {
  5779. message: 'message',
  5780. ipv4: 'ipv4',
  5781. ipv6: 'ipv6'
  5782. },
  5783. /**
  5784. * Return true if the input value is a IP address.
  5785. *
  5786. * @param {FormValidation.Base} validator The validator plugin instance
  5787. * @param {jQuery} $field Field element
  5788. * @param {Object} options Can consist of the following keys:
  5789. * - ipv4: Enable IPv4 validator, default to true
  5790. * - ipv6: Enable IPv6 validator, default to true
  5791. * - message: The invalid message
  5792. * @returns {Boolean|Object}
  5793. */
  5794. validate: function(validator, $field, options, validatorName) {
  5795. var value = validator.getFieldValue($field, validatorName);
  5796. if (value === '') {
  5797. return true;
  5798. }
  5799. options = $.extend({}, { ipv4: true, ipv6: true }, options);
  5800. var locale = validator.getLocale(),
  5801. 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]?)(\/([0-9]|[1-2][0-9]|3[0-2]))?$/,
  5802. 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*(\/(\d|\d\d|1[0-1]\d|12[0-8]))?$/,
  5803. valid = false,
  5804. message;
  5805. switch (true) {
  5806. case (options.ipv4 && !options.ipv6):
  5807. valid = ipv4Regex.test(value);
  5808. message = options.message || FormValidation.I18n[locale].ip.ipv4;
  5809. break;
  5810. case (!options.ipv4 && options.ipv6):
  5811. valid = ipv6Regex.test(value);
  5812. message = options.message || FormValidation.I18n[locale].ip.ipv6;
  5813. break;
  5814. case (options.ipv4 && options.ipv6):
  5815. /* falls through */
  5816. default:
  5817. valid = ipv4Regex.test(value) || ipv6Regex.test(value);
  5818. message = options.message || FormValidation.I18n[locale].ip['default'];
  5819. break;
  5820. }
  5821. return {
  5822. valid: valid,
  5823. message: message
  5824. };
  5825. }
  5826. };
  5827. }(jQuery));
  5828. ;(function($) {
  5829. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  5830. 'en_US': {
  5831. isbn: {
  5832. 'default': 'Please enter a valid ISBN number'
  5833. }
  5834. }
  5835. });
  5836. FormValidation.Validator.isbn = {
  5837. /**
  5838. * Return true if the input value is a valid ISBN 10 or ISBN 13 number
  5839. *
  5840. * @see http://en.wikipedia.org/wiki/International_Standard_Book_Number
  5841. * @param {FormValidation.Base} validator The validator plugin instance
  5842. * @param {jQuery} $field Field element
  5843. * @param {Object} [options] Can consist of the following keys:
  5844. * - message: The invalid message
  5845. * @returns {Boolean|Object}
  5846. */
  5847. validate: function(validator, $field, options, validatorName) {
  5848. var value = validator.getFieldValue($field, validatorName);
  5849. if (value === '') {
  5850. return true;
  5851. }
  5852. // http://en.wikipedia.org/wiki/International_Standard_Book_Number#Overview
  5853. // Groups are separated by a hyphen or a space
  5854. var type;
  5855. switch (true) {
  5856. case /^\d{9}[\dX]$/.test(value):
  5857. case (value.length === 13 && /^(\d+)-(\d+)-(\d+)-([\dX])$/.test(value)):
  5858. case (value.length === 13 && /^(\d+)\s(\d+)\s(\d+)\s([\dX])$/.test(value)):
  5859. type = 'ISBN10';
  5860. break;
  5861. case /^(978|979)\d{9}[\dX]$/.test(value):
  5862. case (value.length === 17 && /^(978|979)-(\d+)-(\d+)-(\d+)-([\dX])$/.test(value)):
  5863. case (value.length === 17 && /^(978|979)\s(\d+)\s(\d+)\s(\d+)\s([\dX])$/.test(value)):
  5864. type = 'ISBN13';
  5865. break;
  5866. default:
  5867. return false;
  5868. }
  5869. // Replace all special characters except digits and X
  5870. value = value.replace(/[^0-9X]/gi, '');
  5871. var chars = value.split(''),
  5872. length = chars.length,
  5873. sum = 0,
  5874. i,
  5875. checksum;
  5876. switch (type) {
  5877. case 'ISBN10':
  5878. sum = 0;
  5879. for (i = 0; i < length - 1; i++) {
  5880. sum += parseInt(chars[i], 10) * (10 - i);
  5881. }
  5882. checksum = 11 - (sum % 11);
  5883. if (checksum === 11) {
  5884. checksum = 0;
  5885. } else if (checksum === 10) {
  5886. checksum = 'X';
  5887. }
  5888. return {
  5889. type: type,
  5890. valid: (checksum + '' === chars[length - 1])
  5891. };
  5892. case 'ISBN13':
  5893. sum = 0;
  5894. for (i = 0; i < length - 1; i++) {
  5895. sum += ((i % 2 === 0) ? parseInt(chars[i], 10) : (parseInt(chars[i], 10) * 3));
  5896. }
  5897. checksum = 10 - (sum % 10);
  5898. if (checksum === 10) {
  5899. checksum = '0';
  5900. }
  5901. return {
  5902. type: type,
  5903. valid: (checksum + '' === chars[length - 1])
  5904. };
  5905. default:
  5906. return false;
  5907. }
  5908. }
  5909. };
  5910. }(jQuery));
  5911. ;(function($) {
  5912. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  5913. 'en_US': {
  5914. isin: {
  5915. 'default': 'Please enter a valid ISIN number'
  5916. }
  5917. }
  5918. });
  5919. FormValidation.Validator.isin = {
  5920. // Available country codes
  5921. // See http://isin.net/country-codes/
  5922. 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',
  5923. /**
  5924. * Validate an ISIN (International Securities Identification Number)
  5925. *
  5926. * @see http://en.wikipedia.org/wiki/International_Securities_Identifying_Number
  5927. * @param {FormValidation.Base} validator The validator plugin instance
  5928. * @param {jQuery} $field Field element
  5929. * @param {Object} options Can consist of the following keys:
  5930. * - message: The invalid message
  5931. * @returns {Boolean}
  5932. */
  5933. validate: function(validator, $field, options, validatorName) {
  5934. var value = validator.getFieldValue($field, validatorName);
  5935. if (value === '') {
  5936. return true;
  5937. }
  5938. value = value.toUpperCase();
  5939. var regex = new RegExp('^(' + this.COUNTRY_CODES + ')[0-9A-Z]{10}$');
  5940. if (!regex.test(value)) {
  5941. return false;
  5942. }
  5943. var converted = '',
  5944. length = value.length;
  5945. // Convert letters to number
  5946. for (var i = 0; i < length - 1; i++) {
  5947. var c = value.charCodeAt(i);
  5948. converted += ((c > 57) ? (c - 55).toString() : value.charAt(i));
  5949. }
  5950. var digits = '',
  5951. n = converted.length,
  5952. group = (n % 2 !== 0) ? 0 : 1;
  5953. for (i = 0; i < n; i++) {
  5954. digits += (parseInt(converted[i], 10) * ((i % 2) === group ? 2 : 1) + '');
  5955. }
  5956. var sum = 0;
  5957. for (i = 0; i < digits.length; i++) {
  5958. sum += parseInt(digits.charAt(i), 10);
  5959. }
  5960. sum = (10 - (sum % 10)) % 10;
  5961. return sum + '' === value.charAt(length - 1);
  5962. }
  5963. };
  5964. }(jQuery));
  5965. ;(function($) {
  5966. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  5967. 'en_US': {
  5968. ismn: {
  5969. 'default': 'Please enter a valid ISMN number'
  5970. }
  5971. }
  5972. });
  5973. FormValidation.Validator.ismn = {
  5974. /**
  5975. * Validate ISMN (International Standard Music Number)
  5976. *
  5977. * @see http://en.wikipedia.org/wiki/International_Standard_Music_Number
  5978. * @param {FormValidation.Base} validator The validator plugin instance
  5979. * @param {jQuery} $field Field element
  5980. * @param {Object} options Can consist of the following keys:
  5981. * - message: The invalid message
  5982. * @returns {Boolean|Object}
  5983. */
  5984. validate: function(validator, $field, options, validatorName) {
  5985. var value = validator.getFieldValue($field, validatorName);
  5986. if (value === '') {
  5987. return true;
  5988. }
  5989. // Groups are separated by a hyphen or a space
  5990. var type;
  5991. switch (true) {
  5992. case /^M\d{9}$/.test(value):
  5993. case /^M-\d{4}-\d{4}-\d{1}$/.test(value):
  5994. case /^M\s\d{4}\s\d{4}\s\d{1}$/.test(value):
  5995. type = 'ISMN10';
  5996. break;
  5997. case /^9790\d{9}$/.test(value):
  5998. case /^979-0-\d{4}-\d{4}-\d{1}$/.test(value):
  5999. case /^979\s0\s\d{4}\s\d{4}\s\d{1}$/.test(value):
  6000. type = 'ISMN13';
  6001. break;
  6002. default:
  6003. return false;
  6004. }
  6005. if ('ISMN10' === type) {
  6006. value = '9790' + value.substr(1);
  6007. }
  6008. // Replace all special characters except digits
  6009. value = value.replace(/[^0-9]/gi, '');
  6010. var length = value.length,
  6011. sum = 0,
  6012. weight = [1, 3];
  6013. for (var i = 0; i < length - 1; i++) {
  6014. sum += parseInt(value.charAt(i), 10) * weight[i % 2];
  6015. }
  6016. sum = 10 - sum % 10;
  6017. return {
  6018. type: type,
  6019. valid: (sum + '' === value.charAt(length - 1))
  6020. };
  6021. }
  6022. };
  6023. }(jQuery));
  6024. ;(function($) {
  6025. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  6026. 'en_US': {
  6027. issn: {
  6028. 'default': 'Please enter a valid ISSN number'
  6029. }
  6030. }
  6031. });
  6032. FormValidation.Validator.issn = {
  6033. /**
  6034. * Validate ISSN (International Standard Serial Number)
  6035. *
  6036. * @see http://en.wikipedia.org/wiki/International_Standard_Serial_Number
  6037. * @param {FormValidation.Base} validator The validator plugin instance
  6038. * @param {jQuery} $field Field element
  6039. * @param {Object} options Can consist of the following keys:
  6040. * - message: The invalid message
  6041. * @returns {Boolean}
  6042. */
  6043. validate: function(validator, $field, options, validatorName) {
  6044. var value = validator.getFieldValue($field, validatorName);
  6045. if (value === '') {
  6046. return true;
  6047. }
  6048. // Groups are separated by a hyphen or a space
  6049. if (!/^\d{4}\-\d{3}[\dX]$/.test(value)) {
  6050. return false;
  6051. }
  6052. // Replace all special characters except digits and X
  6053. value = value.replace(/[^0-9X]/gi, '');
  6054. var chars = value.split(''),
  6055. length = chars.length,
  6056. sum = 0;
  6057. if (chars[7] === 'X') {
  6058. chars[7] = 10;
  6059. }
  6060. for (var i = 0; i < length; i++) {
  6061. sum += parseInt(chars[i], 10) * (8 - i);
  6062. }
  6063. return (sum % 11 === 0);
  6064. }
  6065. };
  6066. }(jQuery));
  6067. ;(function($) {
  6068. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  6069. 'en_US': {
  6070. lessThan: {
  6071. 'default': 'Please enter a value less than or equal to %s',
  6072. notInclusive: 'Please enter a value less than %s'
  6073. }
  6074. }
  6075. });
  6076. FormValidation.Validator.lessThan = {
  6077. html5Attributes: {
  6078. message: 'message',
  6079. value: 'value',
  6080. inclusive: 'inclusive'
  6081. },
  6082. enableByHtml5: function($field) {
  6083. var type = $field.attr('type'),
  6084. max = $field.attr('max');
  6085. if (max && type !== 'date') {
  6086. return {
  6087. value: max
  6088. };
  6089. }
  6090. return false;
  6091. },
  6092. /**
  6093. * Return true if the input value is less than or equal to given number
  6094. *
  6095. * @param {FormValidation.Base} validator The validator plugin instance
  6096. * @param {jQuery} $field Field element
  6097. * @param {Object} options Can consist of the following keys:
  6098. * - value: The number used to compare to. It can be
  6099. * - A number
  6100. * - Name of field which its value defines the number
  6101. * - Name of callback function that returns the number
  6102. * - A callback function that returns the number
  6103. *
  6104. * - inclusive [optional]: Can be true or false. Default is true
  6105. * - message: The invalid message
  6106. * @returns {Boolean|Object}
  6107. */
  6108. validate: function(validator, $field, options, validatorName) {
  6109. var value = validator.getFieldValue($field, validatorName);
  6110. if (value === '') {
  6111. return true;
  6112. }
  6113. value = this._format(value);
  6114. var locale = validator.getLocale(),
  6115. compareTo = $.isNumeric(options.value) ? options.value : validator.getDynamicOption($field, options.value),
  6116. compareToValue = this._format(compareTo);
  6117. return (options.inclusive === true || options.inclusive === undefined)
  6118. ? {
  6119. valid: $.isNumeric(value) && parseFloat(value) <= compareToValue,
  6120. message: FormValidation.Helper.format(options.message || FormValidation.I18n[locale].lessThan['default'], compareTo)
  6121. }
  6122. : {
  6123. valid: $.isNumeric(value) && parseFloat(value) < compareToValue,
  6124. message: FormValidation.Helper.format(options.message || FormValidation.I18n[locale].lessThan.notInclusive, compareTo)
  6125. };
  6126. },
  6127. _format: function(value) {
  6128. return (value + '').replace(',', '.');
  6129. }
  6130. };
  6131. }(jQuery));
  6132. ;(function($) {
  6133. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  6134. 'en_US': {
  6135. mac: {
  6136. 'default': 'Please enter a valid MAC address'
  6137. }
  6138. }
  6139. });
  6140. FormValidation.Validator.mac = {
  6141. /**
  6142. * Return true if the input value is a MAC address.
  6143. *
  6144. * @param {FormValidation.Base} validator The validator plugin instance
  6145. * @param {jQuery} $field Field element
  6146. * @param {Object} options Can consist of the following keys:
  6147. * - message: The invalid message
  6148. * @returns {Boolean}
  6149. */
  6150. validate: function(validator, $field, options, validatorName) {
  6151. var value = validator.getFieldValue($field, validatorName);
  6152. if (value === '') {
  6153. return true;
  6154. }
  6155. return /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/.test(value) || /^([0-9A-Fa-f]{4}\.){2}([0-9A-Fa-f]{4})$/.test(value);
  6156. }
  6157. };
  6158. }(jQuery));
  6159. ;(function($) {
  6160. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  6161. 'en_US': {
  6162. meid: {
  6163. 'default': 'Please enter a valid MEID number'
  6164. }
  6165. }
  6166. });
  6167. FormValidation.Validator.meid = {
  6168. /**
  6169. * Validate MEID (Mobile Equipment Identifier)
  6170. *
  6171. * @see http://en.wikipedia.org/wiki/Mobile_equipment_identifier
  6172. * @param {FormValidation.Base} validator The validator plugin instance
  6173. * @param {jQuery} $field Field element
  6174. * @param {Object} options Can consist of the following keys:
  6175. * - message: The invalid message
  6176. * @returns {Boolean}
  6177. */
  6178. validate: function(validator, $field, options, validatorName) {
  6179. var value = validator.getFieldValue($field, validatorName);
  6180. if (value === '') {
  6181. return true;
  6182. }
  6183. switch (true) {
  6184. // 14 digit hex representation (no check digit)
  6185. case /^[0-9A-F]{15}$/i.test(value):
  6186. // 14 digit hex representation + dashes or spaces (no check digit)
  6187. case /^[0-9A-F]{2}[- ][0-9A-F]{6}[- ][0-9A-F]{6}[- ][0-9A-F]$/i.test(value):
  6188. // 18 digit decimal representation (no check digit)
  6189. case /^\d{19}$/.test(value):
  6190. // 18 digit decimal representation + dashes or spaces (no check digit)
  6191. case /^\d{5}[- ]\d{5}[- ]\d{4}[- ]\d{4}[- ]\d$/.test(value):
  6192. // Grab the check digit
  6193. var cd = value.charAt(value.length - 1);
  6194. // Strip any non-hex chars
  6195. value = value.replace(/[- ]/g, '');
  6196. // If it's all digits, luhn base 10 is used
  6197. if (value.match(/^\d*$/i)) {
  6198. return FormValidation.Helper.luhn(value);
  6199. }
  6200. // Strip the check digit
  6201. value = value.slice(0, -1);
  6202. // Get every other char, and double it
  6203. var cdCalc = '';
  6204. for (var i = 1; i <= 13; i += 2) {
  6205. cdCalc += (parseInt(value.charAt(i), 16) * 2).toString(16);
  6206. }
  6207. // Get the sum of each char in the string
  6208. var sum = 0;
  6209. for (i = 0; i < cdCalc.length; i++) {
  6210. sum += parseInt(cdCalc.charAt(i), 16);
  6211. }
  6212. // If the last digit of the calc is 0, the check digit is 0
  6213. return (sum % 10 === 0)
  6214. ? (cd === '0')
  6215. // Subtract it from the next highest 10s number (64 goes to 70) and subtract the sum
  6216. // Double it and turn it into a hex char
  6217. : (cd === ((Math.floor((sum + 10) / 10) * 10 - sum) * 2).toString(16));
  6218. // 14 digit hex representation (no check digit)
  6219. case /^[0-9A-F]{14}$/i.test(value):
  6220. // 14 digit hex representation + dashes or spaces (no check digit)
  6221. case /^[0-9A-F]{2}[- ][0-9A-F]{6}[- ][0-9A-F]{6}$/i.test(value):
  6222. // 18 digit decimal representation (no check digit)
  6223. case /^\d{18}$/.test(value):
  6224. // 18 digit decimal representation + dashes or spaces (no check digit)
  6225. case /^\d{5}[- ]\d{5}[- ]\d{4}[- ]\d{4}$/.test(value):
  6226. return true;
  6227. default:
  6228. return false;
  6229. }
  6230. }
  6231. };
  6232. }(jQuery));
  6233. ;(function($) {
  6234. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  6235. 'en_US': {
  6236. notEmpty: {
  6237. 'default': 'Please enter a value'
  6238. }
  6239. }
  6240. });
  6241. FormValidation.Validator.notEmpty = {
  6242. enableByHtml5: function($field) {
  6243. var required = $field.attr('required') + '';
  6244. return ('required' === required || 'true' === required);
  6245. },
  6246. /**
  6247. * Check if input value is empty or not
  6248. *
  6249. * @param {FormValidation.Base} validator The validator plugin instance
  6250. * @param {jQuery} $field Field element
  6251. * @param {Object} options
  6252. * @returns {Boolean}
  6253. */
  6254. validate: function(validator, $field, options, validatorName) {
  6255. var type = $field.attr('type');
  6256. if ('radio' === type || 'checkbox' === type) {
  6257. var ns = validator.getNamespace();
  6258. return validator
  6259. .getFieldElements($field.attr('data-' + ns + '-field'))
  6260. .filter(':checked')
  6261. .length > 0;
  6262. }
  6263. if ('number' === type && $field.get(0).validity && $field.get(0).validity.badInput === true) {
  6264. return true;
  6265. }
  6266. var value = validator.getFieldValue($field, validatorName);
  6267. return $.trim(value) !== '';
  6268. }
  6269. };
  6270. }(jQuery));
  6271. ;(function($) {
  6272. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  6273. 'en_US': {
  6274. numeric: {
  6275. 'default': 'Please enter a valid float number'
  6276. }
  6277. }
  6278. });
  6279. FormValidation.Validator.numeric = {
  6280. html5Attributes: {
  6281. message: 'message',
  6282. separator: 'separator', // deprecated
  6283. thousandsseparator: 'thousandsSeparator',
  6284. decimalseparator: 'decimalSeparator'
  6285. },
  6286. enableByHtml5: function($field) {
  6287. return ('number' === $field.attr('type')) && ($field.attr('step') !== undefined) && ($field.attr('step') % 1 !== 0);
  6288. },
  6289. /**
  6290. * Validate decimal number
  6291. *
  6292. * @param {FormValidation.Base} validator The validator plugin instance
  6293. * @param {jQuery} $field Field element
  6294. * @param {Object} options Consist of key:
  6295. * - message: The invalid message
  6296. * - thousandsSeparator: The thousands separator. It's empty by default
  6297. * - separator, decimalSeparator: The decimal separator. It's '.' by default
  6298. * The separator option is deprecated and should be replaced with decimalSeparator
  6299. * @returns {Boolean}
  6300. */
  6301. validate: function(validator, $field, options, validatorName) {
  6302. if (this.enableByHtml5($field) && $field.get(0).validity && $field.get(0).validity.badInput === true) {
  6303. return false;
  6304. }
  6305. var value = validator.getFieldValue($field, validatorName);
  6306. if (value === '') {
  6307. return true;
  6308. }
  6309. var decimalSeparator = options.separator || options.decimalSeparator || '.',
  6310. thousandsSeparator = options.thousandsSeparator || '';
  6311. // Support preceding zero numbers such as .5, -.5
  6312. if (value.substr(0, 1) === decimalSeparator) {
  6313. value = '0' + decimalSeparator + value.substr(1);
  6314. } else if (value.substr(0, 2) === '-' + decimalSeparator) {
  6315. value = '-0' + decimalSeparator + value.substr(2);
  6316. }
  6317. decimalSeparator = (decimalSeparator === '.') ? '\\.' : decimalSeparator;
  6318. thousandsSeparator = (thousandsSeparator === '.') ? '\\.' : thousandsSeparator;
  6319. var testRegexp = new RegExp('^-?[0-9]{1,3}(' + thousandsSeparator + '[0-9]{3})*(' + decimalSeparator + '[0-9]+)?$'),
  6320. thousandsReplacer = new RegExp(thousandsSeparator, 'g');
  6321. if (!testRegexp.test(value)) {
  6322. return false;
  6323. }
  6324. // Replace thousands separator with blank
  6325. if (thousandsSeparator) {
  6326. value = value.replace(thousandsReplacer, '');
  6327. }
  6328. // Replace decimal separator with a dot
  6329. if (decimalSeparator) {
  6330. value = value.replace(decimalSeparator, '.');
  6331. }
  6332. return !isNaN(parseFloat(value)) && isFinite(value);
  6333. }
  6334. };
  6335. }(jQuery));
  6336. ;(function($) {
  6337. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  6338. 'en_US': {
  6339. phone: {
  6340. 'default': 'Please enter a valid phone number',
  6341. country: 'Please enter a valid phone number in %s',
  6342. countries: {
  6343. AE: 'United Arab Emirates',
  6344. BG: 'Bulgaria',
  6345. BR: 'Brazil',
  6346. CN: 'China',
  6347. CZ: 'Czech Republic',
  6348. DE: 'Germany',
  6349. DK: 'Denmark',
  6350. ES: 'Spain',
  6351. FR: 'France',
  6352. GB: 'United Kingdom',
  6353. IN: 'India',
  6354. MA: 'Morocco',
  6355. NL: 'Netherlands',
  6356. PK: 'Pakistan',
  6357. RO: 'Romania',
  6358. RU: 'Russia',
  6359. SK: 'Slovakia',
  6360. TH: 'Thailand',
  6361. US: 'USA',
  6362. VE: 'Venezuela'
  6363. }
  6364. }
  6365. }
  6366. });
  6367. FormValidation.Validator.phone = {
  6368. html5Attributes: {
  6369. message: 'message',
  6370. country: 'country'
  6371. },
  6372. // The supported countries
  6373. COUNTRY_CODES: ['AE', 'BG', 'BR', 'CN', 'CZ', 'DE', 'DK', 'ES', 'FR', 'GB', 'IN', 'MA', 'NL', 'PK', 'RO', 'RU', 'SK', 'TH', 'US', 'VE'],
  6374. /**
  6375. * Return true if the input value contains a valid phone number for the country
  6376. * selected in the options
  6377. *
  6378. * @param {FormValidation.Base} validator Validate plugin instance
  6379. * @param {jQuery} $field Field element
  6380. * @param {Object} options Consist of key:
  6381. * - message: The invalid message
  6382. * - country: The ISO-3166 country code. It can be
  6383. * - A country code
  6384. * - Name of field which its value defines the country code
  6385. * - Name of callback function that returns the country code
  6386. * - A callback function that returns the country code
  6387. *
  6388. * @returns {Boolean|Object}
  6389. */
  6390. validate: function(validator, $field, options, validatorName) {
  6391. var value = validator.getFieldValue($field, validatorName);
  6392. if (value === '') {
  6393. return true;
  6394. }
  6395. var locale = validator.getLocale(),
  6396. country = options.country;
  6397. if (typeof country !== 'string' || $.inArray(country, this.COUNTRY_CODES) === -1) {
  6398. // Try to determine the country
  6399. country = validator.getDynamicOption($field, country);
  6400. }
  6401. if (!country || $.inArray(country.toUpperCase(), this.COUNTRY_CODES) === -1) {
  6402. return true;
  6403. }
  6404. var isValid = true;
  6405. switch (country.toUpperCase()) {
  6406. case 'AE':
  6407. // http://regexr.com/39tak
  6408. value = $.trim(value);
  6409. isValid = (/^(((\+|00)?971[\s\.-]?(\(0\)[\s\.-]?)?|0)(\(5(0|2|5|6)\)|5(0|2|5|6)|2|3|4|6|7|9)|60)([\s\.-]?[0-9]){7}$/).test(value);
  6410. break;
  6411. case 'BG':
  6412. // https://regex101.com/r/yE6vN4/1
  6413. // See http://en.wikipedia.org/wiki/Telephone_numbers_in_Bulgaria
  6414. value = value.replace(/\+|\s|-|\/|\(|\)/gi,'');
  6415. isValid = (/^(0|359|00)(((700|900)[0-9]{5}|((800)[0-9]{5}|(800)[0-9]{4}))|(87|88|89)([0-9]{7})|((2[0-9]{7})|(([3-9][0-9])(([0-9]{6})|([0-9]{5})))))$/).test(value);
  6416. break;
  6417. case 'BR':
  6418. // http://regexr.com/399m1
  6419. value = $.trim(value);
  6420. 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);
  6421. break;
  6422. case 'CN':
  6423. // http://regexr.com/39dq4
  6424. value = $.trim(value);
  6425. 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);
  6426. break;
  6427. case 'CZ':
  6428. // http://regexr.com/39hhl
  6429. isValid = /^(((00)([- ]?)|\+)(420)([- ]?))?((\d{3})([- ]?)){2}(\d{3})$/.test(value);
  6430. break;
  6431. case 'DE':
  6432. // http://regexr.com/39pkg
  6433. value = $.trim(value);
  6434. isValid = (/^(((((((00|\+)49[ \-/]?)|0)[1-9][0-9]{1,4})[ \-/]?)|((((00|\+)49\()|\(0)[1-9][0-9]{1,4}\)[ \-/]?))[0-9]{1,7}([ \-/]?[0-9]{1,5})?)$/).test(value);
  6435. break;
  6436. case 'DK':
  6437. // Mathing DK phone numbers with country code in 1 of 3 formats and an
  6438. // 8 digit phone number not starting with a 0 or 1. Can have 1 space
  6439. // between each character except inside the country code.
  6440. // http://regex101.com/r/sS8fO4/1
  6441. value = $.trim(value);
  6442. isValid = (/^(\+45|0045|\(45\))?\s?[2-9](\s?\d){7}$/).test(value);
  6443. break;
  6444. case 'ES':
  6445. // http://regex101.com/r/rB9mA9/1
  6446. // Telephone numbers in Spain go like this:
  6447. // 9: Landline phones and special prefixes.
  6448. // 6, 7: Mobile phones.
  6449. // 5: VoIP lines.
  6450. // 8: Premium-rate services.
  6451. // There are also special 5-digit and 3-digit numbers, but
  6452. // maybe it would be overkill to include them all.
  6453. value = $.trim(value);
  6454. isValid = (/^(?:(?:(?:\+|00)34\D?))?(?:5|6|7|8|9)(?:\d\D?){8}$/).test(value);
  6455. break;
  6456. case 'FR':
  6457. // http://regexr.com/39a2p
  6458. value = $.trim(value);
  6459. isValid = (/^(?:(?:(?:\+|00)33[ ]?(?:\(0\)[ ]?)?)|0){1}[1-9]{1}([ .-]?)(?:\d{2}\1?){3}\d{2}$/).test(value);
  6460. break;
  6461. case 'GB':
  6462. // http://aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_GB_Telephone_Numbers#Match_GB_telephone_number_in_any_format
  6463. // http://regexr.com/38uhv
  6464. value = $.trim(value);
  6465. 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);
  6466. break;
  6467. case 'IN':
  6468. // http://stackoverflow.com/questions/18351553/regular-expression-validation-for-indian-phone-number-and-mobile-number
  6469. // http://regex101.com/r/qL6eZ5/1
  6470. // May begin with +91. Supports mobile and land line numbers
  6471. value = $.trim(value);
  6472. isValid = (/((\+?)((0[ -]+)*|(91 )*)(\d{12}|\d{10}))|\d{5}([- ]*)\d{6}/).test(value);
  6473. break;
  6474. case 'MA':
  6475. // http://en.wikipedia.org/wiki/Telephone_numbers_in_Morocco
  6476. // http://regexr.com/399n8
  6477. value = $.trim(value);
  6478. 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);
  6479. break;
  6480. case 'NL':
  6481. // http://en.wikipedia.org/wiki/Telephone_numbers_in_the_Netherlands
  6482. // http://regexr.com/3aevr
  6483. value = $.trim(value);
  6484. isValid = (/^((\+|00(\s|\s?\-\s?)?)31(\s|\s?\-\s?)?(\(0\)[\-\s]?)?|0)[1-9]((\s|\s?\-\s?)?[0-9])((\s|\s?-\s?)?[0-9])((\s|\s?-\s?)?[0-9])\s?[0-9]\s?[0-9]\s?[0-9]\s?[0-9]\s?[0-9]$/gm).test(value);
  6485. break;
  6486. case 'PK':
  6487. // http://regex101.com/r/yH8aV9/2
  6488. value = $.trim(value);
  6489. isValid = (/^0?3[0-9]{2}[0-9]{7}$/).test(value);
  6490. break;
  6491. case 'RO':
  6492. // All mobile network and land line
  6493. // http://regexr.com/39fv1
  6494. 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);
  6495. break;
  6496. case 'RU':
  6497. // http://regex101.com/r/gW7yT5/5
  6498. isValid = (/^((8|\+7|007)[\-\.\/ ]?)?([\(\/\.]?\d{3}[\)\/\.]?[\-\.\/ ]?)?[\d\-\.\/ ]{7,10}$/g).test(value);
  6499. break;
  6500. case 'SK':
  6501. // http://regexr.com/3a95f
  6502. isValid = /^(((00)([- ]?)|\+)(421)([- ]?))?((\d{3})([- ]?)){2}(\d{3})$/.test(value);
  6503. break;
  6504. case 'TH':
  6505. // http://regex101.com/r/vM5mZ4/2
  6506. isValid = (/^0\(?([6|8-9]{2})*-([0-9]{3})*-([0-9]{4})$/).test(value);
  6507. break;
  6508. case 'VE':
  6509. // http://regex101.com/r/eM2yY0/6
  6510. value = $.trim(value);
  6511. 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);
  6512. break;
  6513. case 'US':
  6514. /* falls through */
  6515. default:
  6516. // Make sure US phone numbers have 10 digits
  6517. // May start with 1, +1, or 1-; should discard
  6518. // Area code may be delimited with (), & sections may be delimited with . or -
  6519. // http://regexr.com/38mqi
  6520. isValid = (/^(?:(1\-?)|(\+1 ?))?\(?\d{3}\)?[\-\.\s]?\d{3}[\-\.\s]?\d{4}$/).test(value);
  6521. break;
  6522. }
  6523. return {
  6524. valid: isValid,
  6525. message: FormValidation.Helper.format(options.message || FormValidation.I18n[locale].phone.country, FormValidation.I18n[locale].phone.countries[country])
  6526. };
  6527. }
  6528. };
  6529. }(jQuery));
  6530. ;(function($) {
  6531. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  6532. 'en_US': {
  6533. promise: {
  6534. 'default': 'Please enter a valid value'
  6535. }
  6536. }
  6537. });
  6538. FormValidation.Validator.promise = {
  6539. priority: 999,
  6540. html5Attributes: {
  6541. message: 'message',
  6542. promise: 'promise'
  6543. },
  6544. /**
  6545. * Return result from a jQuery's Deferred object
  6546. *
  6547. * @param {FormValidation.Base} validator The validator plugin instance
  6548. * @param {jQuery} $field Field element
  6549. * @param {Object} options Can consist of the following keys:
  6550. * - promise: The method that passes parameters:
  6551. * promise: function(fieldValue, validator, $field) {
  6552. * // fieldValue is the value of field
  6553. * // validator is instance of FormValidation.Base
  6554. * // $field is the field element
  6555. *
  6556. * var dfd = new $.Deferred();
  6557. *
  6558. * // Do something ...
  6559. *
  6560. * // Resolve when particular task is done
  6561. * dfd.resolve({
  6562. * valid: true or false, // Required
  6563. * message: 'Other message', // Optional
  6564. * key: value // You can attach more data to reuse later
  6565. * });
  6566. *
  6567. * // You can reject if there's error
  6568. * dfd.reject({
  6569. * message: 'Other message', // Optional
  6570. * key: value // You can attach more data to reuse later
  6571. * });
  6572. *
  6573. * return dfd.promise();
  6574. * }
  6575. * - message: The invalid message
  6576. * @returns {Deferred}
  6577. */
  6578. validate: function(validator, $field, options, validatorName) {
  6579. var value = validator.getFieldValue($field, validatorName),
  6580. dfd = new $.Deferred(),
  6581. promise = FormValidation.Helper.call(options.promise, [value, validator, $field]);
  6582. promise
  6583. .done(function(result) {
  6584. dfd.resolve($field, validatorName, result);
  6585. })
  6586. .fail(function(result) {
  6587. result = result || {};
  6588. result.valid = false;
  6589. dfd.resolve($field, validatorName, result);
  6590. });
  6591. return dfd;
  6592. }
  6593. };
  6594. }(jQuery));
  6595. ;(function($) {
  6596. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  6597. 'en_US': {
  6598. regexp: {
  6599. 'default': 'Please enter a value matching the pattern'
  6600. }
  6601. }
  6602. });
  6603. FormValidation.Validator.regexp = {
  6604. html5Attributes: {
  6605. message: 'message',
  6606. flags: 'flags',
  6607. regexp: 'regexp'
  6608. },
  6609. enableByHtml5: function($field) {
  6610. var pattern = $field.attr('pattern');
  6611. if (pattern) {
  6612. return {
  6613. regexp: pattern
  6614. };
  6615. }
  6616. return false;
  6617. },
  6618. /**
  6619. * Check if the element value matches given regular expression
  6620. *
  6621. * @param {FormValidation.Base} validator The validator plugin instance
  6622. * @param {jQuery} $field Field element
  6623. * @param {Object} options Consists of the following key:
  6624. * - regexp: The regular expression you need to check
  6625. * - flags: If specified, flags can have any combination of Javascript regular expression flags such as:
  6626. * g: global match
  6627. * i: ignore case
  6628. * m: multiple line
  6629. * @returns {Boolean}
  6630. */
  6631. validate: function(validator, $field, options, validatorName) {
  6632. var value = validator.getFieldValue($field, validatorName);
  6633. if (value === '') {
  6634. return true;
  6635. }
  6636. var regexp = ('string' === typeof options.regexp)
  6637. ? (options.flags ? new RegExp(options.regexp, options.flags) : new RegExp(options.regexp))
  6638. : options.regexp;
  6639. return regexp.test(value);
  6640. }
  6641. };
  6642. }(jQuery));
  6643. ;(function($) {
  6644. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  6645. 'en_US': {
  6646. remote: {
  6647. 'default': 'Please enter a valid value'
  6648. }
  6649. }
  6650. });
  6651. FormValidation.Validator.remote = {
  6652. priority: 1000,
  6653. html5Attributes: {
  6654. async: 'async',
  6655. crossdomain: 'crossDomain',
  6656. data: 'data',
  6657. datatype: 'dataType',
  6658. delay: 'delay',
  6659. message: 'message',
  6660. name: 'name',
  6661. type: 'type',
  6662. url: 'url',
  6663. validkey: 'validKey'
  6664. },
  6665. /**
  6666. * Destroy the timer when destroying the FormValidation (using validator.destroy() method)
  6667. */
  6668. destroy: function(validator, $field, options, validatorName) {
  6669. var ns = validator.getNamespace(),
  6670. timer = $field.data(ns + '.' + validatorName + '.timer');
  6671. if (timer) {
  6672. clearTimeout(timer);
  6673. $field.removeData(ns + '.' + validatorName + '.timer');
  6674. }
  6675. },
  6676. /**
  6677. * Request a remote server to check the input value
  6678. *
  6679. * @param {FormValidation.Base} validator Plugin instance
  6680. * @param {jQuery} $field Field element
  6681. * @param {Object} options Can consist of the following keys:
  6682. * - async {Boolean} [optional] Indicate the Ajax request will be asynchronous or not. It's true by default
  6683. * - crossDomain {Boolean} [optional]
  6684. * - data {Object|Function} [optional]: By default, it will take the value
  6685. * {
  6686. * <fieldName>: <fieldValue>
  6687. * }
  6688. * - dataType {String} [optional]: The type of data which is returned by remote server.
  6689. * It can be json (default), text, script
  6690. * - delay {Number} [optional]
  6691. * - headers {String[]} [optional]: Additional headers
  6692. * - message {String} [optional]: The invalid message
  6693. * - name {String} [optional]: Override the field name for the request.
  6694. * - type {String} [optional] Can be GET or POST (default)
  6695. * - url {String|Function}
  6696. * - validKey {String} [optional]: The valid key. It's "valid" by default
  6697. * This is useful when connecting to external remote server or APIs provided by 3rd parties
  6698. * @returns {Deferred}
  6699. */
  6700. validate: function(validator, $field, options, validatorName) {
  6701. var ns = validator.getNamespace(),
  6702. value = validator.getFieldValue($field, validatorName),
  6703. dfd = new $.Deferred();
  6704. if (value === '') {
  6705. dfd.resolve($field, validatorName, { valid: true });
  6706. return dfd;
  6707. }
  6708. var name = $field.attr('data-' + ns + '-field'),
  6709. data = options.data || {},
  6710. url = options.url,
  6711. validKey = options.validKey || 'valid';
  6712. // Support dynamic data
  6713. if ('function' === typeof data) {
  6714. data = data.call(this, validator, $field, value);
  6715. }
  6716. // Parse string data from HTML5 attribute
  6717. if ('string' === typeof data) {
  6718. data = JSON.parse(data);
  6719. }
  6720. // Support dynamic url
  6721. if ('function' === typeof url) {
  6722. url = url.call(this, validator, $field, value);
  6723. }
  6724. data[options.name || name] = value;
  6725. var ajaxOptions = {
  6726. async: options.async === null || options.async === true || options.async === 'true',
  6727. data: data,
  6728. dataType: options.dataType || 'json',
  6729. headers: options.headers || {},
  6730. type: options.type || 'GET',
  6731. url: url
  6732. };
  6733. if (options.crossDomain !== null) {
  6734. ajaxOptions.crossDomain = (options.crossDomain === true || options.crossDomain === 'true');
  6735. }
  6736. function runCallback() {
  6737. var xhr = $.ajax(ajaxOptions);
  6738. xhr
  6739. .success(function(response) {
  6740. response.valid = (response[validKey] === true || response[validKey] === 'true')
  6741. ? true
  6742. : (response[validKey] === false || response[validKey] === 'false' ? false : null);
  6743. dfd.resolve($field, validatorName, response);
  6744. })
  6745. .error(function(response) {
  6746. dfd.resolve($field, validatorName, {
  6747. valid: false
  6748. });
  6749. });
  6750. dfd.fail(function() {
  6751. xhr.abort();
  6752. });
  6753. return dfd;
  6754. }
  6755. if (options.delay) {
  6756. // Since the form might have multiple fields with the same name
  6757. // I have to attach the timer to the field element
  6758. if ($field.data(ns + '.' + validatorName + '.timer')) {
  6759. clearTimeout($field.data(ns + '.' + validatorName + '.timer'));
  6760. }
  6761. $field.data(ns + '.' + validatorName + '.timer', setTimeout(runCallback, options.delay));
  6762. return dfd;
  6763. } else {
  6764. return runCallback();
  6765. }
  6766. }
  6767. };
  6768. }(jQuery));
  6769. ;(function($) {
  6770. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  6771. 'en_US': {
  6772. rtn: {
  6773. 'default': 'Please enter a valid RTN number'
  6774. }
  6775. }
  6776. });
  6777. FormValidation.Validator.rtn = {
  6778. /**
  6779. * Validate a RTN (Routing transit number)
  6780. *
  6781. * @see http://en.wikipedia.org/wiki/Routing_transit_number
  6782. * @param {FormValidation.Base} validator The validator plugin instance
  6783. * @param {jQuery} $field Field element
  6784. * @param {Object} options Can consist of the following keys:
  6785. * - message: The invalid message
  6786. * @returns {Boolean}
  6787. */
  6788. validate: function(validator, $field, options, validatorName) {
  6789. var value = validator.getFieldValue($field, validatorName);
  6790. if (value === '') {
  6791. return true;
  6792. }
  6793. if (!/^\d{9}$/.test(value)) {
  6794. return false;
  6795. }
  6796. var sum = 0;
  6797. for (var i = 0; i < value.length; i += 3) {
  6798. sum += parseInt(value.charAt(i), 10) * 3
  6799. + parseInt(value.charAt(i + 1), 10) * 7
  6800. + parseInt(value.charAt(i + 2), 10);
  6801. }
  6802. return (sum !== 0 && sum % 10 === 0);
  6803. }
  6804. };
  6805. }(jQuery));
  6806. ;(function($) {
  6807. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  6808. 'en_US': {
  6809. sedol: {
  6810. 'default': 'Please enter a valid SEDOL number'
  6811. }
  6812. }
  6813. });
  6814. FormValidation.Validator.sedol = {
  6815. /**
  6816. * Validate a SEDOL (Stock Exchange Daily Official List)
  6817. *
  6818. * @see http://en.wikipedia.org/wiki/SEDOL
  6819. * @param {FormValidation.Base} validator The validator plugin instance
  6820. * @param {jQuery} $field Field element
  6821. * @param {Object} options Can consist of the following keys:
  6822. * - message: The invalid message
  6823. * @returns {Boolean}
  6824. */
  6825. validate: function(validator, $field, options, validatorName) {
  6826. var value = validator.getFieldValue($field, validatorName);
  6827. if (value === '') {
  6828. return true;
  6829. }
  6830. value = value.toUpperCase();
  6831. if (!/^[0-9A-Z]{7}$/.test(value)) {
  6832. return false;
  6833. }
  6834. var sum = 0,
  6835. weight = [1, 3, 1, 7, 3, 9, 1],
  6836. length = value.length;
  6837. for (var i = 0; i < length - 1; i++) {
  6838. sum += weight[i] * parseInt(value.charAt(i), 36);
  6839. }
  6840. sum = (10 - sum % 10) % 10;
  6841. return sum + '' === value.charAt(length - 1);
  6842. }
  6843. };
  6844. }(jQuery));
  6845. ;(function($) {
  6846. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  6847. 'en_US': {
  6848. siren: {
  6849. 'default': 'Please enter a valid SIREN number'
  6850. }
  6851. }
  6852. });
  6853. FormValidation.Validator.siren = {
  6854. /**
  6855. * Check if a string is a siren number
  6856. *
  6857. * @param {FormValidation.Base} validator The validator plugin instance
  6858. * @param {jQuery} $field Field element
  6859. * @param {Object} options Consist of key:
  6860. * - message: The invalid message
  6861. * @returns {Boolean}
  6862. */
  6863. validate: function(validator, $field, options, validatorName) {
  6864. var value = validator.getFieldValue($field, validatorName);
  6865. if (value === '') {
  6866. return true;
  6867. }
  6868. if (!/^\d{9}$/.test(value)) {
  6869. return false;
  6870. }
  6871. return FormValidation.Helper.luhn(value);
  6872. }
  6873. };
  6874. }(jQuery));
  6875. ;(function($) {
  6876. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  6877. 'en_US': {
  6878. siret: {
  6879. 'default': 'Please enter a valid SIRET number'
  6880. }
  6881. }
  6882. });
  6883. FormValidation.Validator.siret = {
  6884. /**
  6885. * Check if a string is a siret number
  6886. *
  6887. * @param {FormValidation.Base} validator The validator plugin instance
  6888. * @param {jQuery} $field Field element
  6889. * @param {Object} options Consist of key:
  6890. * - message: The invalid message
  6891. * @returns {Boolean}
  6892. */
  6893. validate: function(validator, $field, options, validatorName) {
  6894. var value = validator.getFieldValue($field, validatorName);
  6895. if (value === '') {
  6896. return true;
  6897. }
  6898. var sum = 0,
  6899. length = value.length,
  6900. tmp;
  6901. for (var i = 0; i < length; i++) {
  6902. tmp = parseInt(value.charAt(i), 10);
  6903. if ((i % 2) === 0) {
  6904. tmp = tmp * 2;
  6905. if (tmp > 9) {
  6906. tmp -= 9;
  6907. }
  6908. }
  6909. sum += tmp;
  6910. }
  6911. return (sum % 10 === 0);
  6912. }
  6913. };
  6914. }(jQuery));
  6915. ;(function($) {
  6916. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  6917. 'en_US': {
  6918. step: {
  6919. 'default': 'Please enter a valid step of %s'
  6920. }
  6921. }
  6922. });
  6923. FormValidation.Validator.step = {
  6924. html5Attributes: {
  6925. message: 'message',
  6926. base: 'baseValue',
  6927. step: 'step'
  6928. },
  6929. /**
  6930. * Return true if the input value is valid step one
  6931. *
  6932. * @param {FormValidation.Base} validator The validator plugin instance
  6933. * @param {jQuery} $field Field element
  6934. * @param {Object} options Can consist of the following keys:
  6935. * - baseValue: The base value
  6936. * - step: The step
  6937. * - message: The invalid message
  6938. * @returns {Boolean|Object}
  6939. */
  6940. validate: function(validator, $field, options, validatorName) {
  6941. var value = validator.getFieldValue($field, validatorName);
  6942. if (value === '') {
  6943. return true;
  6944. }
  6945. options = $.extend({}, { baseValue: 0, step: 1 }, options);
  6946. value = parseFloat(value);
  6947. if (!$.isNumeric(value)) {
  6948. return false;
  6949. }
  6950. var round = function(x, precision) {
  6951. var m = Math.pow(10, precision);
  6952. x = x * m;
  6953. var sign = (x > 0) | -(x < 0),
  6954. isHalf = (x % 1 === 0.5 * sign);
  6955. if (isHalf) {
  6956. return (Math.floor(x) + (sign > 0)) / m;
  6957. } else {
  6958. return Math.round(x) / m;
  6959. }
  6960. },
  6961. floatMod = function(x, y) {
  6962. if (y === 0.0) {
  6963. return 1.0;
  6964. }
  6965. var dotX = (x + '').split('.'),
  6966. dotY = (y + '').split('.'),
  6967. precision = ((dotX.length === 1) ? 0 : dotX[1].length) + ((dotY.length === 1) ? 0 : dotY[1].length);
  6968. return round(x - y * Math.floor(x / y), precision);
  6969. };
  6970. var locale = validator.getLocale(),
  6971. mod = floatMod(value - options.baseValue, options.step);
  6972. return {
  6973. valid: mod === 0.0 || mod === options.step,
  6974. message: FormValidation.Helper.format(options.message || FormValidation.I18n[locale].step['default'], [options.step])
  6975. };
  6976. }
  6977. };
  6978. }(jQuery));
  6979. ;(function($) {
  6980. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  6981. 'en_US': {
  6982. stringCase: {
  6983. 'default': 'Please enter only lowercase characters',
  6984. upper: 'Please enter only uppercase characters'
  6985. }
  6986. }
  6987. });
  6988. FormValidation.Validator.stringCase = {
  6989. html5Attributes: {
  6990. message: 'message',
  6991. 'case': 'case'
  6992. },
  6993. /**
  6994. * Check if a string is a lower or upper case one
  6995. *
  6996. * @param {FormValidation.Base} validator The validator plugin instance
  6997. * @param {jQuery} $field Field element
  6998. * @param {Object} options Consist of key:
  6999. * - message: The invalid message
  7000. * - case: Can be 'lower' (default) or 'upper'
  7001. * @returns {Object}
  7002. */
  7003. validate: function(validator, $field, options, validatorName) {
  7004. var value = validator.getFieldValue($field, validatorName);
  7005. if (value === '') {
  7006. return true;
  7007. }
  7008. var locale = validator.getLocale(),
  7009. stringCase = (options['case'] || 'lower').toLowerCase();
  7010. return {
  7011. valid: ('upper' === stringCase) ? value === value.toUpperCase() : value === value.toLowerCase(),
  7012. message: options.message || (('upper' === stringCase) ? FormValidation.I18n[locale].stringCase.upper : FormValidation.I18n[locale].stringCase['default'])
  7013. };
  7014. }
  7015. };
  7016. }(jQuery));
  7017. ;(function($) {
  7018. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  7019. 'en_US': {
  7020. stringLength: {
  7021. 'default': 'Please enter a value with valid length',
  7022. less: 'Please enter less than %s characters',
  7023. more: 'Please enter more than %s characters',
  7024. between: 'Please enter value between %s and %s characters long'
  7025. }
  7026. }
  7027. });
  7028. FormValidation.Validator.stringLength = {
  7029. html5Attributes: {
  7030. message: 'message',
  7031. min: 'min',
  7032. max: 'max',
  7033. trim: 'trim',
  7034. utf8bytes: 'utf8Bytes'
  7035. },
  7036. enableByHtml5: function($field) {
  7037. var options = {},
  7038. maxLength = $field.attr('maxlength'),
  7039. minLength = $field.attr('minlength');
  7040. if (maxLength) {
  7041. options.max = parseInt(maxLength, 10);
  7042. }
  7043. if (minLength) {
  7044. options.min = parseInt(minLength, 10);
  7045. }
  7046. return $.isEmptyObject(options) ? false : options;
  7047. },
  7048. /**
  7049. * Check if the length of element value is less or more than given number
  7050. *
  7051. * @param {FormValidation.Base} validator The validator plugin instance
  7052. * @param {jQuery} $field Field element
  7053. * @param {Object} options Consists of following keys:
  7054. * - min
  7055. * - max
  7056. * At least one of two keys is required
  7057. * The min, max keys define the number which the field value compares to. min, max can be
  7058. * - A number
  7059. * - Name of field which its value defines the number
  7060. * - Name of callback function that returns the number
  7061. * - A callback function that returns the number
  7062. *
  7063. * - message: The invalid message
  7064. * - trim: Indicate the length will be calculated after trimming the value or not. It is false, by default
  7065. * - utf8bytes: Evaluate string length in UTF-8 bytes, default to false
  7066. * @returns {Object}
  7067. */
  7068. validate: function(validator, $field, options, validatorName) {
  7069. var value = validator.getFieldValue($field, validatorName);
  7070. if (options.trim === true || options.trim === 'true') {
  7071. value = $.trim(value);
  7072. }
  7073. if (value === '') {
  7074. return true;
  7075. }
  7076. var locale = validator.getLocale(),
  7077. min = $.isNumeric(options.min) ? options.min : validator.getDynamicOption($field, options.min),
  7078. max = $.isNumeric(options.max) ? options.max : validator.getDynamicOption($field, options.max),
  7079. // Credit to http://stackoverflow.com/a/23329386 (@lovasoa) for UTF-8 byte length code
  7080. utf8Length = function(str) {
  7081. var s = str.length;
  7082. for (var i = str.length - 1; i >= 0; i--) {
  7083. var code = str.charCodeAt(i);
  7084. if (code > 0x7f && code <= 0x7ff) {
  7085. s++;
  7086. } else if (code > 0x7ff && code <= 0xffff) {
  7087. s += 2;
  7088. }
  7089. if (code >= 0xDC00 && code <= 0xDFFF) {
  7090. i--;
  7091. }
  7092. }
  7093. return s;
  7094. },
  7095. length = options.utf8Bytes ? utf8Length(value) : value.length,
  7096. isValid = true,
  7097. message = options.message || FormValidation.I18n[locale].stringLength['default'];
  7098. if ((min && length < parseInt(min, 10)) || (max && length > parseInt(max, 10))) {
  7099. isValid = false;
  7100. }
  7101. switch (true) {
  7102. case (!!min && !!max):
  7103. message = FormValidation.Helper.format(options.message || FormValidation.I18n[locale].stringLength.between, [parseInt(min, 10), parseInt(max, 10)]);
  7104. break;
  7105. case (!!min):
  7106. message = FormValidation.Helper.format(options.message || FormValidation.I18n[locale].stringLength.more, parseInt(min, 10) - 1);
  7107. break;
  7108. case (!!max):
  7109. message = FormValidation.Helper.format(options.message || FormValidation.I18n[locale].stringLength.less, parseInt(max, 10) + 1);
  7110. break;
  7111. default:
  7112. break;
  7113. }
  7114. return {
  7115. valid: isValid,
  7116. message: message
  7117. };
  7118. }
  7119. };
  7120. }(jQuery));
  7121. ;(function($) {
  7122. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  7123. 'en_US': {
  7124. uri: {
  7125. 'default': 'Please enter a valid URI'
  7126. }
  7127. }
  7128. });
  7129. FormValidation.Validator.uri = {
  7130. html5Attributes: {
  7131. message: 'message',
  7132. allowlocal: 'allowLocal',
  7133. allowemptyprotocol: 'allowEmptyProtocol',
  7134. protocol: 'protocol'
  7135. },
  7136. enableByHtml5: function($field) {
  7137. return ('url' === $field.attr('type'));
  7138. },
  7139. /**
  7140. * Return true if the input value is a valid URL
  7141. *
  7142. * @param {FormValidation.Base} validator The validator plugin instance
  7143. * @param {jQuery} $field Field element
  7144. * @param {Object} options
  7145. * - message: The error message
  7146. * - allowLocal: Allow the private and local network IP. Default to false
  7147. * - allowEmptyProtocol: Allow the URI without protocol. Default to false
  7148. * - protocol: The protocols, separated by a comma. Default to "http, https, ftp"
  7149. * @returns {Boolean}
  7150. */
  7151. validate: function(validator, $field, options, validatorName) {
  7152. var value = validator.getFieldValue($field, validatorName);
  7153. if (value === '') {
  7154. return true;
  7155. }
  7156. // Credit to https://gist.github.com/dperini/729294
  7157. //
  7158. // Regular Expression for URL validation
  7159. //
  7160. // Author: Diego Perini
  7161. // Updated: 2010/12/05
  7162. //
  7163. // the regular expression composed & commented
  7164. // could be easily tweaked for RFC compliance,
  7165. // it was expressly modified to fit & satisfy
  7166. // these test for an URL shortener:
  7167. //
  7168. // http://mathiasbynens.be/demo/url-regex
  7169. //
  7170. // Notes on possible differences from a standard/generic validation:
  7171. //
  7172. // - utf-8 char class take in consideration the full Unicode range
  7173. // - TLDs are mandatory unless `allowLocal` is true
  7174. // - protocols have been restricted to ftp, http and https only as requested
  7175. //
  7176. // Changes:
  7177. //
  7178. // - IP address dotted notation validation, range: 1.0.0.0 - 223.255.255.255
  7179. // first and last IP address of each class is considered invalid
  7180. // (since they are broadcast/network addresses)
  7181. //
  7182. // - Added exclusion of private, reserved and/or local networks ranges
  7183. // unless `allowLocal` is true
  7184. //
  7185. // - Added possibility of choosing a custom protocol
  7186. //
  7187. // - Add option to validate without protocol
  7188. //
  7189. var allowLocal = options.allowLocal === true || options.allowLocal === 'true',
  7190. allowEmptyProtocol = options.allowEmptyProtocol === true || options.allowEmptyProtocol === 'true',
  7191. protocol = (options.protocol || 'http, https, ftp').split(',').join('|').replace(/\s/g, ''),
  7192. urlExp = new RegExp(
  7193. "^" +
  7194. // protocol identifier
  7195. "(?:(?:" + protocol + ")://)" +
  7196. // allow empty protocol
  7197. (allowEmptyProtocol ? '?' : '') +
  7198. // user:pass authentication
  7199. "(?:\\S+(?::\\S*)?@)?" +
  7200. "(?:" +
  7201. // IP address exclusion
  7202. // private & local networks
  7203. (allowLocal
  7204. ? ''
  7205. : ("(?!(?:10|127)(?:\\.\\d{1,3}){3})" +
  7206. "(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})" +
  7207. "(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})")) +
  7208. // IP address dotted notation octets
  7209. // excludes loopback network 0.0.0.0
  7210. // excludes reserved space >= 224.0.0.0
  7211. // excludes network & broadcast addresses
  7212. // (first & last IP address of each class)
  7213. "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
  7214. "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
  7215. "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
  7216. "|" +
  7217. // host name
  7218. "(?:(?:[a-z\\u00a1-\\uffff0-9]-?)*[a-z\\u00a1-\\uffff0-9]+)" +
  7219. // domain name
  7220. "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-?)*[a-z\\u00a1-\\uffff0-9])*" +
  7221. // TLD identifier
  7222. "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" +
  7223. // Allow intranet sites (no TLD) if `allowLocal` is true
  7224. (allowLocal ? '?' : '') +
  7225. ")" +
  7226. // port number
  7227. "(?::\\d{2,5})?" +
  7228. // resource path
  7229. "(?:/[^\\s]*)?" +
  7230. "$", "i"
  7231. );
  7232. return urlExp.test(value);
  7233. }
  7234. };
  7235. }(jQuery));
  7236. ;(function($) {
  7237. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  7238. 'en_US': {
  7239. uuid: {
  7240. 'default': 'Please enter a valid UUID number',
  7241. version: 'Please enter a valid UUID version %s number'
  7242. }
  7243. }
  7244. });
  7245. FormValidation.Validator.uuid = {
  7246. html5Attributes: {
  7247. message: 'message',
  7248. version: 'version'
  7249. },
  7250. /**
  7251. * Return true if and only if the input value is a valid UUID string
  7252. *
  7253. * @see http://en.wikipedia.org/wiki/Universally_unique_identifier
  7254. * @param {FormValidation.Base} validator The validator plugin instance
  7255. * @param {jQuery} $field Field element
  7256. * @param {Object} options Consist of key:
  7257. * - message: The invalid message
  7258. * - version: Can be 3, 4, 5, null
  7259. * @returns {Boolean|Object}
  7260. */
  7261. validate: function(validator, $field, options, validatorName) {
  7262. var value = validator.getFieldValue($field, validatorName);
  7263. if (value === '') {
  7264. return true;
  7265. }
  7266. // See the format at http://en.wikipedia.org/wiki/Universally_unique_identifier#Variants_and_versions
  7267. var locale = validator.getLocale(),
  7268. patterns = {
  7269. '3': /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
  7270. '4': /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
  7271. '5': /^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
  7272. all: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i
  7273. },
  7274. version = options.version ? (options.version + '') : 'all';
  7275. return {
  7276. valid: (null === patterns[version]) ? true : patterns[version].test(value),
  7277. message: options.version
  7278. ? FormValidation.Helper.format(options.message || FormValidation.I18n[locale].uuid.version, options.version)
  7279. : (options.message || FormValidation.I18n[locale].uuid['default'])
  7280. };
  7281. }
  7282. };
  7283. }(jQuery));
  7284. ;(function($) {
  7285. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  7286. 'en_US': {
  7287. vat: {
  7288. 'default': 'Please enter a valid VAT number',
  7289. country: 'Please enter a valid VAT number in %s',
  7290. countries: {
  7291. AT: 'Austria',
  7292. BE: 'Belgium',
  7293. BG: 'Bulgaria',
  7294. BR: 'Brazil',
  7295. CH: 'Switzerland',
  7296. CY: 'Cyprus',
  7297. CZ: 'Czech Republic',
  7298. DE: 'Germany',
  7299. DK: 'Denmark',
  7300. EE: 'Estonia',
  7301. ES: 'Spain',
  7302. FI: 'Finland',
  7303. FR: 'France',
  7304. GB: 'United Kingdom',
  7305. GR: 'Greek',
  7306. EL: 'Greek',
  7307. HU: 'Hungary',
  7308. HR: 'Croatia',
  7309. IE: 'Ireland',
  7310. IS: 'Iceland',
  7311. IT: 'Italy',
  7312. LT: 'Lithuania',
  7313. LU: 'Luxembourg',
  7314. LV: 'Latvia',
  7315. MT: 'Malta',
  7316. NL: 'Netherlands',
  7317. NO: 'Norway',
  7318. PL: 'Poland',
  7319. PT: 'Portugal',
  7320. RO: 'Romania',
  7321. RU: 'Russia',
  7322. RS: 'Serbia',
  7323. SE: 'Sweden',
  7324. SI: 'Slovenia',
  7325. SK: 'Slovakia',
  7326. VE: 'Venezuela',
  7327. ZA: 'South Africa'
  7328. }
  7329. }
  7330. }
  7331. });
  7332. FormValidation.Validator.vat = {
  7333. html5Attributes: {
  7334. message: 'message',
  7335. country: 'country'
  7336. },
  7337. // Supported country codes
  7338. COUNTRY_CODES: [
  7339. 'AT', 'BE', 'BG', 'BR', 'CH', 'CY', 'CZ', 'DE', 'DK', 'EE', 'EL', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU',
  7340. 'IE', 'IS', 'IT', 'LT', 'LU', 'LV', 'MT', 'NL', 'NO', 'PL', 'PT', 'RO', 'RU', 'RS', 'SE', 'SK', 'SI', 'VE',
  7341. 'ZA'
  7342. ],
  7343. /**
  7344. * Validate an European VAT number
  7345. *
  7346. * @param {FormValidation.Base} validator The validator plugin instance
  7347. * @param {jQuery} $field Field element
  7348. * @param {Object} options Consist of key:
  7349. * - message: The invalid message
  7350. * - country: The ISO 3166-1 country code. It can be
  7351. * - One of country code defined in COUNTRY_CODES
  7352. * - Name of field which its value defines the country code
  7353. * - Name of callback function that returns the country code
  7354. * - A callback function that returns the country code
  7355. * @returns {Object}
  7356. */
  7357. validate: function(validator, $field, options, validatorName) {
  7358. var value = validator.getFieldValue($field, validatorName);
  7359. if (value === '') {
  7360. return true;
  7361. }
  7362. var locale = validator.getLocale(),
  7363. country = options.country;
  7364. if (!country) {
  7365. country = value.substr(0, 2);
  7366. } else if (typeof country !== 'string' || $.inArray(country.toUpperCase(), this.COUNTRY_CODES) === -1) {
  7367. // Determine the country code
  7368. country = validator.getDynamicOption($field, country);
  7369. }
  7370. if ($.inArray(country, this.COUNTRY_CODES) === -1) {
  7371. return true;
  7372. }
  7373. var method = ['_', country.toLowerCase()].join(''),
  7374. result = this[method](value);
  7375. result = (result === true || result === false) ? { valid: result } : result;
  7376. result.message = FormValidation.Helper.format(options.message || FormValidation.I18n[locale].vat.country, FormValidation.I18n[locale].vat.countries[country.toUpperCase()]);
  7377. return result;
  7378. },
  7379. // VAT validators
  7380. /**
  7381. * Validate Austrian VAT number
  7382. *
  7383. * @param {String} value VAT number
  7384. * @returns {Boolean}
  7385. */
  7386. _at: function(value) {
  7387. if (/^ATU[0-9]{8}$/.test(value)) {
  7388. value = value.substr(2);
  7389. }
  7390. if (!/^U[0-9]{8}$/.test(value)) {
  7391. return false;
  7392. }
  7393. value = value.substr(1);
  7394. var sum = 0,
  7395. weight = [1, 2, 1, 2, 1, 2, 1],
  7396. temp = 0;
  7397. for (var i = 0; i < 7; i++) {
  7398. temp = parseInt(value.charAt(i), 10) * weight[i];
  7399. if (temp > 9) {
  7400. temp = Math.floor(temp / 10) + temp % 10;
  7401. }
  7402. sum += temp;
  7403. }
  7404. sum = 10 - (sum + 4) % 10;
  7405. if (sum === 10) {
  7406. sum = 0;
  7407. }
  7408. return (sum + '' === value.substr(7, 1));
  7409. },
  7410. /**
  7411. * Validate Belgian VAT number
  7412. *
  7413. * @param {String} value VAT number
  7414. * @returns {Boolean}
  7415. */
  7416. _be: function(value) {
  7417. if (/^BE[0]{0,1}[0-9]{9}$/.test(value)) {
  7418. value = value.substr(2);
  7419. }
  7420. if (!/^[0]{0,1}[0-9]{9}$/.test(value)) {
  7421. return false;
  7422. }
  7423. if (value.length === 9) {
  7424. value = '0' + value;
  7425. }
  7426. if (value.substr(1, 1) === '0') {
  7427. return false;
  7428. }
  7429. var sum = parseInt(value.substr(0, 8), 10) + parseInt(value.substr(8, 2), 10);
  7430. return (sum % 97 === 0);
  7431. },
  7432. /**
  7433. * Validate Bulgarian VAT number
  7434. *
  7435. * @param {String} value VAT number
  7436. * @returns {Boolean}
  7437. */
  7438. _bg: function(value) {
  7439. if (/^BG[0-9]{9,10}$/.test(value)) {
  7440. value = value.substr(2);
  7441. }
  7442. if (!/^[0-9]{9,10}$/.test(value)) {
  7443. return false;
  7444. }
  7445. var sum = 0, i = 0;
  7446. // Legal entities
  7447. if (value.length === 9) {
  7448. for (i = 0; i < 8; i++) {
  7449. sum += parseInt(value.charAt(i), 10) * (i + 1);
  7450. }
  7451. sum = sum % 11;
  7452. if (sum === 10) {
  7453. sum = 0;
  7454. for (i = 0; i < 8; i++) {
  7455. sum += parseInt(value.charAt(i), 10) * (i + 3);
  7456. }
  7457. }
  7458. sum = sum % 10;
  7459. return (sum + '' === value.substr(8));
  7460. }
  7461. // Physical persons, foreigners and others
  7462. else if (value.length === 10) {
  7463. // Validate Bulgarian national identification numbers
  7464. var egn = function(value) {
  7465. // Check the birth date
  7466. var year = parseInt(value.substr(0, 2), 10) + 1900,
  7467. month = parseInt(value.substr(2, 2), 10),
  7468. day = parseInt(value.substr(4, 2), 10);
  7469. if (month > 40) {
  7470. year += 100;
  7471. month -= 40;
  7472. } else if (month > 20) {
  7473. year -= 100;
  7474. month -= 20;
  7475. }
  7476. if (!FormValidation.Helper.date(year, month, day)) {
  7477. return false;
  7478. }
  7479. var sum = 0,
  7480. weight = [2, 4, 8, 5, 10, 9, 7, 3, 6];
  7481. for (var i = 0; i < 9; i++) {
  7482. sum += parseInt(value.charAt(i), 10) * weight[i];
  7483. }
  7484. sum = (sum % 11) % 10;
  7485. return (sum + '' === value.substr(9, 1));
  7486. },
  7487. // Validate Bulgarian personal number of a foreigner
  7488. pnf = function(value) {
  7489. var sum = 0,
  7490. weight = [21, 19, 17, 13, 11, 9, 7, 3, 1];
  7491. for (var i = 0; i < 9; i++) {
  7492. sum += parseInt(value.charAt(i), 10) * weight[i];
  7493. }
  7494. sum = sum % 10;
  7495. return (sum + '' === value.substr(9, 1));
  7496. },
  7497. // Finally, consider it as a VAT number
  7498. vat = function(value) {
  7499. var sum = 0,
  7500. weight = [4, 3, 2, 7, 6, 5, 4, 3, 2];
  7501. for (var i = 0; i < 9; i++) {
  7502. sum += parseInt(value.charAt(i), 10) * weight[i];
  7503. }
  7504. sum = 11 - sum % 11;
  7505. if (sum === 10) {
  7506. return false;
  7507. }
  7508. if (sum === 11) {
  7509. sum = 0;
  7510. }
  7511. return (sum + '' === value.substr(9, 1));
  7512. };
  7513. return (egn(value) || pnf(value) || vat(value));
  7514. }
  7515. return false;
  7516. },
  7517. /**
  7518. * Validate Brazilian VAT number (CNPJ)
  7519. *
  7520. * @param {String} value VAT number
  7521. * @returns {Boolean}
  7522. */
  7523. _br: function(value) {
  7524. if (value === '') {
  7525. return true;
  7526. }
  7527. var cnpj = value.replace(/[^\d]+/g, '');
  7528. if (cnpj === '' || cnpj.length !== 14) {
  7529. return false;
  7530. }
  7531. // Remove invalids CNPJs
  7532. if (cnpj === '00000000000000' || cnpj === '11111111111111' || cnpj === '22222222222222' ||
  7533. cnpj === '33333333333333' || cnpj === '44444444444444' || cnpj === '55555555555555' ||
  7534. cnpj === '66666666666666' || cnpj === '77777777777777' || cnpj === '88888888888888' ||
  7535. cnpj === '99999999999999')
  7536. {
  7537. return false;
  7538. }
  7539. // Validate verification digits
  7540. var length = cnpj.length - 2,
  7541. numbers = cnpj.substring(0, length),
  7542. digits = cnpj.substring(length),
  7543. sum = 0,
  7544. pos = length - 7;
  7545. for (var i = length; i >= 1; i--) {
  7546. sum += parseInt(numbers.charAt(length - i), 10) * pos--;
  7547. if (pos < 2) {
  7548. pos = 9;
  7549. }
  7550. }
  7551. var result = sum % 11 < 2 ? 0 : 11 - sum % 11;
  7552. if (result !== parseInt(digits.charAt(0), 10)) {
  7553. return false;
  7554. }
  7555. length = length + 1;
  7556. numbers = cnpj.substring(0, length);
  7557. sum = 0;
  7558. pos = length - 7;
  7559. for (i = length; i >= 1; i--) {
  7560. sum += parseInt(numbers.charAt(length - i), 10) * pos--;
  7561. if (pos < 2) {
  7562. pos = 9;
  7563. }
  7564. }
  7565. result = sum % 11 < 2 ? 0 : 11 - sum % 11;
  7566. return (result === parseInt(digits.charAt(1), 10));
  7567. },
  7568. /**
  7569. * Validate Swiss VAT number
  7570. *
  7571. * @param {String} value VAT number
  7572. * @returns {Boolean}
  7573. */
  7574. _ch: function(value) {
  7575. if (/^CHE[0-9]{9}(MWST)?$/.test(value)) {
  7576. value = value.substr(2);
  7577. }
  7578. if (!/^E[0-9]{9}(MWST)?$/.test(value)) {
  7579. return false;
  7580. }
  7581. value = value.substr(1);
  7582. var sum = 0,
  7583. weight = [5, 4, 3, 2, 7, 6, 5, 4];
  7584. for (var i = 0; i < 8; i++) {
  7585. sum += parseInt(value.charAt(i), 10) * weight[i];
  7586. }
  7587. sum = 11 - sum % 11;
  7588. if (sum === 10) {
  7589. return false;
  7590. }
  7591. if (sum === 11) {
  7592. sum = 0;
  7593. }
  7594. return (sum + '' === value.substr(8, 1));
  7595. },
  7596. /**
  7597. * Validate Cypriot VAT number
  7598. *
  7599. * @param {String} value VAT number
  7600. * @returns {Boolean}
  7601. */
  7602. _cy: function(value) {
  7603. if (/^CY[0-5|9]{1}[0-9]{7}[A-Z]{1}$/.test(value)) {
  7604. value = value.substr(2);
  7605. }
  7606. if (!/^[0-5|9]{1}[0-9]{7}[A-Z]{1}$/.test(value)) {
  7607. return false;
  7608. }
  7609. // Do not allow to start with "12"
  7610. if (value.substr(0, 2) === '12') {
  7611. return false;
  7612. }
  7613. // Extract the next digit and multiply by the counter.
  7614. var sum = 0,
  7615. translation = {
  7616. '0': 1, '1': 0, '2': 5, '3': 7, '4': 9,
  7617. '5': 13, '6': 15, '7': 17, '8': 19, '9': 21
  7618. };
  7619. for (var i = 0; i < 8; i++) {
  7620. var temp = parseInt(value.charAt(i), 10);
  7621. if (i % 2 === 0) {
  7622. temp = translation[temp + ''];
  7623. }
  7624. sum += temp;
  7625. }
  7626. sum = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[sum % 26];
  7627. return (sum + '' === value.substr(8, 1));
  7628. },
  7629. /**
  7630. * Validate Czech Republic VAT number
  7631. *
  7632. * @param {String} value VAT number
  7633. * @returns {Boolean}
  7634. */
  7635. _cz: function(value) {
  7636. if (/^CZ[0-9]{8,10}$/.test(value)) {
  7637. value = value.substr(2);
  7638. }
  7639. if (!/^[0-9]{8,10}$/.test(value)) {
  7640. return false;
  7641. }
  7642. var sum = 0,
  7643. i = 0;
  7644. if (value.length === 8) {
  7645. // Do not allow to start with '9'
  7646. if (value.charAt(0) + '' === '9') {
  7647. return false;
  7648. }
  7649. sum = 0;
  7650. for (i = 0; i < 7; i++) {
  7651. sum += parseInt(value.charAt(i), 10) * (8 - i);
  7652. }
  7653. sum = 11 - sum % 11;
  7654. if (sum === 10) {
  7655. sum = 0;
  7656. }
  7657. if (sum === 11) {
  7658. sum = 1;
  7659. }
  7660. return (sum + '' === value.substr(7, 1));
  7661. } else if (value.length === 9 && (value.charAt(0) + '' === '6')) {
  7662. sum = 0;
  7663. // Skip the first (which is 6)
  7664. for (i = 0; i < 7; i++) {
  7665. sum += parseInt(value.charAt(i + 1), 10) * (8 - i);
  7666. }
  7667. sum = 11 - sum % 11;
  7668. if (sum === 10) {
  7669. sum = 0;
  7670. }
  7671. if (sum === 11) {
  7672. sum = 1;
  7673. }
  7674. sum = [8, 7, 6, 5, 4, 3, 2, 1, 0, 9, 10][sum - 1];
  7675. return (sum + '' === value.substr(8, 1));
  7676. } else if (value.length === 9 || value.length === 10) {
  7677. // Validate Czech birth number (Rodné číslo), which is also national identifier
  7678. var year = 1900 + parseInt(value.substr(0, 2), 10),
  7679. month = parseInt(value.substr(2, 2), 10) % 50 % 20,
  7680. day = parseInt(value.substr(4, 2), 10);
  7681. if (value.length === 9) {
  7682. if (year >= 1980) {
  7683. year -= 100;
  7684. }
  7685. if (year > 1953) {
  7686. return false;
  7687. }
  7688. } else if (year < 1954) {
  7689. year += 100;
  7690. }
  7691. if (!FormValidation.Helper.date(year, month, day)) {
  7692. return false;
  7693. }
  7694. // Check that the birth date is not in the future
  7695. if (value.length === 10) {
  7696. var check = parseInt(value.substr(0, 9), 10) % 11;
  7697. if (year < 1985) {
  7698. check = check % 10;
  7699. }
  7700. return (check + '' === value.substr(9, 1));
  7701. }
  7702. return true;
  7703. }
  7704. return false;
  7705. },
  7706. /**
  7707. * Validate German VAT number
  7708. *
  7709. * @param {String} value VAT number
  7710. * @returns {Boolean}
  7711. */
  7712. _de: function(value) {
  7713. if (/^DE[0-9]{9}$/.test(value)) {
  7714. value = value.substr(2);
  7715. }
  7716. if (!/^[0-9]{9}$/.test(value)) {
  7717. return false;
  7718. }
  7719. return FormValidation.Helper.mod11And10(value);
  7720. },
  7721. /**
  7722. * Validate Danish VAT number
  7723. *
  7724. * @param {String} value VAT number
  7725. * @returns {Boolean}
  7726. */
  7727. _dk: function(value) {
  7728. if (/^DK[0-9]{8}$/.test(value)) {
  7729. value = value.substr(2);
  7730. }
  7731. if (!/^[0-9]{8}$/.test(value)) {
  7732. return false;
  7733. }
  7734. var sum = 0,
  7735. weight = [2, 7, 6, 5, 4, 3, 2, 1];
  7736. for (var i = 0; i < 8; i++) {
  7737. sum += parseInt(value.charAt(i), 10) * weight[i];
  7738. }
  7739. return (sum % 11 === 0);
  7740. },
  7741. /**
  7742. * Validate Estonian VAT number
  7743. *
  7744. * @param {String} value VAT number
  7745. * @returns {Boolean}
  7746. */
  7747. _ee: function(value) {
  7748. if (/^EE[0-9]{9}$/.test(value)) {
  7749. value = value.substr(2);
  7750. }
  7751. if (!/^[0-9]{9}$/.test(value)) {
  7752. return false;
  7753. }
  7754. var sum = 0,
  7755. weight = [3, 7, 1, 3, 7, 1, 3, 7, 1];
  7756. for (var i = 0; i < 9; i++) {
  7757. sum += parseInt(value.charAt(i), 10) * weight[i];
  7758. }
  7759. return (sum % 10 === 0);
  7760. },
  7761. /**
  7762. * Validate Spanish VAT number (NIF - Número de Identificación Fiscal)
  7763. * Can be:
  7764. * i) DNI (Documento nacional de identidad), for Spaniards
  7765. * ii) NIE (Número de Identificación de Extranjeros), for foreigners
  7766. * iii) CIF (Certificado de Identificación Fiscal), for legal entities and others
  7767. *
  7768. * @param {String} value VAT number
  7769. * @returns {Boolean|Object}
  7770. */
  7771. _es: function(value) {
  7772. if (/^ES[0-9A-Z][0-9]{7}[0-9A-Z]$/.test(value)) {
  7773. value = value.substr(2);
  7774. }
  7775. if (!/^[0-9A-Z][0-9]{7}[0-9A-Z]$/.test(value)) {
  7776. return false;
  7777. }
  7778. var dni = function(value) {
  7779. var check = parseInt(value.substr(0, 8), 10);
  7780. check = 'TRWAGMYFPDXBNJZSQVHLCKE'[check % 23];
  7781. return (check + '' === value.substr(8, 1));
  7782. },
  7783. nie = function(value) {
  7784. var check = ['XYZ'.indexOf(value.charAt(0)), value.substr(1)].join('');
  7785. check = parseInt(check, 10);
  7786. check = 'TRWAGMYFPDXBNJZSQVHLCKE'[check % 23];
  7787. return (check + '' === value.substr(8, 1));
  7788. },
  7789. cif = function(value) {
  7790. var first = value.charAt(0), check;
  7791. if ('KLM'.indexOf(first) !== -1) {
  7792. // K: Spanish younger than 14 year old
  7793. // L: Spanish living outside Spain without DNI
  7794. // M: Granted the tax to foreigners who have no NIE
  7795. check = parseInt(value.substr(1, 8), 10);
  7796. check = 'TRWAGMYFPDXBNJZSQVHLCKE'[check % 23];
  7797. return (check + '' === value.substr(8, 1));
  7798. } else if ('ABCDEFGHJNPQRSUVW'.indexOf(first) !== -1) {
  7799. var sum = 0,
  7800. weight = [2, 1, 2, 1, 2, 1, 2],
  7801. temp = 0;
  7802. for (var i = 0; i < 7; i++) {
  7803. temp = parseInt(value.charAt(i + 1), 10) * weight[i];
  7804. if (temp > 9) {
  7805. temp = Math.floor(temp / 10) + temp % 10;
  7806. }
  7807. sum += temp;
  7808. }
  7809. sum = 10 - sum % 10;
  7810. if (sum === 10) {
  7811. sum = 0;
  7812. }
  7813. return (sum + '' === value.substr(8, 1) || 'JABCDEFGHI'[sum] === value.substr(8, 1));
  7814. }
  7815. return false;
  7816. };
  7817. var first = value.charAt(0);
  7818. if (/^[0-9]$/.test(first)) {
  7819. return {
  7820. valid: dni(value),
  7821. type: 'DNI'
  7822. };
  7823. } else if (/^[XYZ]$/.test(first)) {
  7824. return {
  7825. valid: nie(value),
  7826. type: 'NIE'
  7827. };
  7828. } else {
  7829. return {
  7830. valid: cif(value),
  7831. type: 'CIF'
  7832. };
  7833. }
  7834. },
  7835. /**
  7836. * Validate Finnish VAT number
  7837. *
  7838. * @param {String} value VAT number
  7839. * @returns {Boolean}
  7840. */
  7841. _fi: function(value) {
  7842. if (/^FI[0-9]{8}$/.test(value)) {
  7843. value = value.substr(2);
  7844. }
  7845. if (!/^[0-9]{8}$/.test(value)) {
  7846. return false;
  7847. }
  7848. var sum = 0,
  7849. weight = [7, 9, 10, 5, 8, 4, 2, 1];
  7850. for (var i = 0; i < 8; i++) {
  7851. sum += parseInt(value.charAt(i), 10) * weight[i];
  7852. }
  7853. return (sum % 11 === 0);
  7854. },
  7855. /**
  7856. * Validate French VAT number (TVA - taxe sur la valeur ajoutée)
  7857. * It's constructed by a SIREN number, prefixed by two characters.
  7858. *
  7859. * @param {String} value VAT number
  7860. * @returns {Boolean}
  7861. */
  7862. _fr: function(value) {
  7863. if (/^FR[0-9A-Z]{2}[0-9]{9}$/.test(value)) {
  7864. value = value.substr(2);
  7865. }
  7866. if (!/^[0-9A-Z]{2}[0-9]{9}$/.test(value)) {
  7867. return false;
  7868. }
  7869. if (!FormValidation.Helper.luhn(value.substr(2))) {
  7870. return false;
  7871. }
  7872. if (/^[0-9]{2}$/.test(value.substr(0, 2))) {
  7873. // First two characters are digits
  7874. return value.substr(0, 2) === (parseInt(value.substr(2) + '12', 10) % 97 + '');
  7875. } else {
  7876. // The first characters cann't be O and I
  7877. var alphabet = '0123456789ABCDEFGHJKLMNPQRSTUVWXYZ',
  7878. check;
  7879. // First one is digit
  7880. if (/^[0-9]{1}$/.test(value.charAt(0))) {
  7881. check = alphabet.indexOf(value.charAt(0)) * 24 + alphabet.indexOf(value.charAt(1)) - 10;
  7882. } else {
  7883. check = alphabet.indexOf(value.charAt(0)) * 34 + alphabet.indexOf(value.charAt(1)) - 100;
  7884. }
  7885. return ((parseInt(value.substr(2), 10) + 1 + Math.floor(check / 11)) % 11) === (check % 11);
  7886. }
  7887. },
  7888. /**
  7889. * Validate United Kingdom VAT number
  7890. *
  7891. * @param {String} value VAT number
  7892. * @returns {Boolean}
  7893. */
  7894. _gb: function(value) {
  7895. if (/^GB[0-9]{9}$/.test(value) /* Standard */
  7896. || /^GB[0-9]{12}$/.test(value) /* Branches */
  7897. || /^GBGD[0-9]{3}$/.test(value) /* Government department */
  7898. || /^GBHA[0-9]{3}$/.test(value) /* Health authority */
  7899. || /^GB(GD|HA)8888[0-9]{5}$/.test(value))
  7900. {
  7901. value = value.substr(2);
  7902. }
  7903. if (!/^[0-9]{9}$/.test(value)
  7904. && !/^[0-9]{12}$/.test(value)
  7905. && !/^GD[0-9]{3}$/.test(value)
  7906. && !/^HA[0-9]{3}$/.test(value)
  7907. && !/^(GD|HA)8888[0-9]{5}$/.test(value))
  7908. {
  7909. return false;
  7910. }
  7911. var length = value.length;
  7912. if (length === 5) {
  7913. var firstTwo = value.substr(0, 2),
  7914. lastThree = parseInt(value.substr(2), 10);
  7915. return ('GD' === firstTwo && lastThree < 500) || ('HA' === firstTwo && lastThree >= 500);
  7916. } else if (length === 11 && ('GD8888' === value.substr(0, 6) || 'HA8888' === value.substr(0, 6))) {
  7917. if (('GD' === value.substr(0, 2) && parseInt(value.substr(6, 3), 10) >= 500)
  7918. || ('HA' === value.substr(0, 2) && parseInt(value.substr(6, 3), 10) < 500))
  7919. {
  7920. return false;
  7921. }
  7922. return (parseInt(value.substr(6, 3), 10) % 97 === parseInt(value.substr(9, 2), 10));
  7923. } else if (length === 9 || length === 12) {
  7924. var sum = 0,
  7925. weight = [8, 7, 6, 5, 4, 3, 2, 10, 1];
  7926. for (var i = 0; i < 9; i++) {
  7927. sum += parseInt(value.charAt(i), 10) * weight[i];
  7928. }
  7929. sum = sum % 97;
  7930. if (parseInt(value.substr(0, 3), 10) >= 100) {
  7931. return (sum === 0 || sum === 42 || sum === 55);
  7932. } else {
  7933. return (sum === 0);
  7934. }
  7935. }
  7936. return true;
  7937. },
  7938. /**
  7939. * Validate Greek VAT number
  7940. *
  7941. * @param {String} value VAT number
  7942. * @returns {Boolean}
  7943. */
  7944. _gr: function(value) {
  7945. if (/^(GR|EL)[0-9]{9}$/.test(value)) {
  7946. value = value.substr(2);
  7947. }
  7948. if (!/^[0-9]{9}$/.test(value)) {
  7949. return false;
  7950. }
  7951. if (value.length === 8) {
  7952. value = '0' + value;
  7953. }
  7954. var sum = 0,
  7955. weight = [256, 128, 64, 32, 16, 8, 4, 2];
  7956. for (var i = 0; i < 8; i++) {
  7957. sum += parseInt(value.charAt(i), 10) * weight[i];
  7958. }
  7959. sum = (sum % 11) % 10;
  7960. return (sum + '' === value.substr(8, 1));
  7961. },
  7962. // EL is traditionally prefix of Greek VAT numbers
  7963. _el: function(value) {
  7964. return this._gr(value);
  7965. },
  7966. /**
  7967. * Validate Hungarian VAT number
  7968. *
  7969. * @param {String} value VAT number
  7970. * @returns {Boolean}
  7971. */
  7972. _hu: function(value) {
  7973. if (/^HU[0-9]{8}$/.test(value)) {
  7974. value = value.substr(2);
  7975. }
  7976. if (!/^[0-9]{8}$/.test(value)) {
  7977. return false;
  7978. }
  7979. var sum = 0,
  7980. weight = [9, 7, 3, 1, 9, 7, 3, 1];
  7981. for (var i = 0; i < 8; i++) {
  7982. sum += parseInt(value.charAt(i), 10) * weight[i];
  7983. }
  7984. return (sum % 10 === 0);
  7985. },
  7986. /**
  7987. * Validate Croatian VAT number
  7988. *
  7989. * @param {String} value VAT number
  7990. * @returns {Boolean}
  7991. */
  7992. _hr: function(value) {
  7993. if (/^HR[0-9]{11}$/.test(value)) {
  7994. value = value.substr(2);
  7995. }
  7996. if (!/^[0-9]{11}$/.test(value)) {
  7997. return false;
  7998. }
  7999. return FormValidation.Helper.mod11And10(value);
  8000. },
  8001. /**
  8002. * Validate Irish VAT number
  8003. *
  8004. * @param {String} value VAT number
  8005. * @returns {Boolean}
  8006. */
  8007. _ie: function(value) {
  8008. if (/^IE[0-9]{1}[0-9A-Z\*\+]{1}[0-9]{5}[A-Z]{1,2}$/.test(value)) {
  8009. value = value.substr(2);
  8010. }
  8011. if (!/^[0-9]{1}[0-9A-Z\*\+]{1}[0-9]{5}[A-Z]{1,2}$/.test(value)) {
  8012. return false;
  8013. }
  8014. var getCheckDigit = function(value) {
  8015. while (value.length < 7) {
  8016. value = '0' + value;
  8017. }
  8018. var alphabet = 'WABCDEFGHIJKLMNOPQRSTUV',
  8019. sum = 0;
  8020. for (var i = 0; i < 7; i++) {
  8021. sum += parseInt(value.charAt(i), 10) * (8 - i);
  8022. }
  8023. sum += 9 * alphabet.indexOf(value.substr(7));
  8024. return alphabet[sum % 23];
  8025. };
  8026. // The first 7 characters are digits
  8027. if (/^[0-9]+$/.test(value.substr(0, 7))) {
  8028. // New system
  8029. return value.charAt(7) === getCheckDigit(value.substr(0, 7) + value.substr(8) + '');
  8030. } else if ('ABCDEFGHIJKLMNOPQRSTUVWXYZ+*'.indexOf(value.charAt(1)) !== -1) {
  8031. // Old system
  8032. return value.charAt(7) === getCheckDigit(value.substr(2, 5) + value.substr(0, 1) + '');
  8033. }
  8034. return true;
  8035. },
  8036. /**
  8037. * Validate Icelandic VAT (VSK) number
  8038. *
  8039. * @params {String} value VAT number
  8040. * @returns {Boolean}
  8041. */
  8042. _is: function(value) {
  8043. if (/^IS[0-9]{5,6}$/.test(value)) {
  8044. value = value.substr(2);
  8045. }
  8046. return /^[0-9]{5,6}$/.test(value);
  8047. },
  8048. /**
  8049. * Validate Italian VAT number, which consists of 11 digits.
  8050. * - First 7 digits are a company identifier
  8051. * - Next 3 are the province of residence
  8052. * - The last one is a check digit
  8053. *
  8054. * @param {String} value VAT number
  8055. * @returns {Boolean}
  8056. */
  8057. _it: function(value) {
  8058. if (/^IT[0-9]{11}$/.test(value)) {
  8059. value = value.substr(2);
  8060. }
  8061. if (!/^[0-9]{11}$/.test(value)) {
  8062. return false;
  8063. }
  8064. if (parseInt(value.substr(0, 7), 10) === 0) {
  8065. return false;
  8066. }
  8067. var lastThree = parseInt(value.substr(7, 3), 10);
  8068. if ((lastThree < 1) || (lastThree > 201) && lastThree !== 999 && lastThree !== 888) {
  8069. return false;
  8070. }
  8071. return FormValidation.Helper.luhn(value);
  8072. },
  8073. /**
  8074. * Validate Lithuanian VAT number
  8075. * It can be:
  8076. * - 9 digits, for legal entities
  8077. * - 12 digits, for temporarily registered taxpayers
  8078. *
  8079. * @param {String} value VAT number
  8080. * @returns {Boolean}
  8081. */
  8082. _lt: function(value) {
  8083. if (/^LT([0-9]{7}1[0-9]{1}|[0-9]{10}1[0-9]{1})$/.test(value)) {
  8084. value = value.substr(2);
  8085. }
  8086. if (!/^([0-9]{7}1[0-9]{1}|[0-9]{10}1[0-9]{1})$/.test(value)) {
  8087. return false;
  8088. }
  8089. var length = value.length,
  8090. sum = 0,
  8091. i;
  8092. for (i = 0; i < length - 1; i++) {
  8093. sum += parseInt(value.charAt(i), 10) * (1 + i % 9);
  8094. }
  8095. var check = sum % 11;
  8096. if (check === 10) {
  8097. sum = 0;
  8098. for (i = 0; i < length - 1; i++) {
  8099. sum += parseInt(value.charAt(i), 10) * (1 + (i + 2) % 9);
  8100. }
  8101. }
  8102. check = check % 11 % 10;
  8103. return (check + '' === value.charAt(length - 1));
  8104. },
  8105. /**
  8106. * Validate Luxembourg VAT number
  8107. *
  8108. * @param {String} value VAT number
  8109. * @returns {Boolean}
  8110. */
  8111. _lu: function(value) {
  8112. if (/^LU[0-9]{8}$/.test(value)) {
  8113. value = value.substr(2);
  8114. }
  8115. if (!/^[0-9]{8}$/.test(value)) {
  8116. return false;
  8117. }
  8118. return ((parseInt(value.substr(0, 6), 10) % 89) + '' === value.substr(6, 2));
  8119. },
  8120. /**
  8121. * Validate Latvian VAT number
  8122. *
  8123. * @param {String} value VAT number
  8124. * @returns {Boolean}
  8125. */
  8126. _lv: function(value) {
  8127. if (/^LV[0-9]{11}$/.test(value)) {
  8128. value = value.substr(2);
  8129. }
  8130. if (!/^[0-9]{11}$/.test(value)) {
  8131. return false;
  8132. }
  8133. var first = parseInt(value.charAt(0), 10),
  8134. sum = 0,
  8135. weight = [],
  8136. i,
  8137. length = value.length;
  8138. if (first > 3) {
  8139. // Legal entity
  8140. sum = 0;
  8141. weight = [9, 1, 4, 8, 3, 10, 2, 5, 7, 6, 1];
  8142. for (i = 0; i < length; i++) {
  8143. sum += parseInt(value.charAt(i), 10) * weight[i];
  8144. }
  8145. sum = sum % 11;
  8146. return (sum === 3);
  8147. } else {
  8148. // Check birth date
  8149. var day = parseInt(value.substr(0, 2), 10),
  8150. month = parseInt(value.substr(2, 2), 10),
  8151. year = parseInt(value.substr(4, 2), 10);
  8152. year = year + 1800 + parseInt(value.charAt(6), 10) * 100;
  8153. if (!FormValidation.Helper.date(year, month, day)) {
  8154. return false;
  8155. }
  8156. // Check personal code
  8157. sum = 0;
  8158. weight = [10, 5, 8, 4, 2, 1, 6, 3, 7, 9];
  8159. for (i = 0; i < length - 1; i++) {
  8160. sum += parseInt(value.charAt(i), 10) * weight[i];
  8161. }
  8162. sum = (sum + 1) % 11 % 10;
  8163. return (sum + '' === value.charAt(length - 1));
  8164. }
  8165. },
  8166. /**
  8167. * Validate Maltese VAT number
  8168. *
  8169. * @param {String} value VAT number
  8170. * @returns {Boolean}
  8171. */
  8172. _mt: function(value) {
  8173. if (/^MT[0-9]{8}$/.test(value)) {
  8174. value = value.substr(2);
  8175. }
  8176. if (!/^[0-9]{8}$/.test(value)) {
  8177. return false;
  8178. }
  8179. var sum = 0,
  8180. weight = [3, 4, 6, 7, 8, 9, 10, 1];
  8181. for (var i = 0; i < 8; i++) {
  8182. sum += parseInt(value.charAt(i), 10) * weight[i];
  8183. }
  8184. return (sum % 37 === 0);
  8185. },
  8186. /**
  8187. * Validate Dutch VAT number
  8188. *
  8189. * @param {String} value VAT number
  8190. * @returns {Boolean}
  8191. */
  8192. _nl: function(value) {
  8193. if (/^NL[0-9]{9}B[0-9]{2}$/.test(value)) {
  8194. value = value.substr(2);
  8195. }
  8196. if (!/^[0-9]{9}B[0-9]{2}$/.test(value)) {
  8197. return false;
  8198. }
  8199. var sum = 0,
  8200. weight = [9, 8, 7, 6, 5, 4, 3, 2];
  8201. for (var i = 0; i < 8; i++) {
  8202. sum += parseInt(value.charAt(i), 10) * weight[i];
  8203. }
  8204. sum = sum % 11;
  8205. if (sum > 9) {
  8206. sum = 0;
  8207. }
  8208. return (sum + '' === value.substr(8, 1));
  8209. },
  8210. /**
  8211. * Validate Norwegian VAT number
  8212. *
  8213. * @see http://www.brreg.no/english/coordination/number.html
  8214. * @param {String} value VAT number
  8215. * @returns {Boolean}
  8216. */
  8217. _no: function(value) {
  8218. if (/^NO[0-9]{9}$/.test(value)) {
  8219. value = value.substr(2);
  8220. }
  8221. if (!/^[0-9]{9}$/.test(value)) {
  8222. return false;
  8223. }
  8224. var sum = 0,
  8225. weight = [3, 2, 7, 6, 5, 4, 3, 2];
  8226. for (var i = 0; i < 8; i++) {
  8227. sum += parseInt(value.charAt(i), 10) * weight[i];
  8228. }
  8229. sum = 11 - sum % 11;
  8230. if (sum === 11) {
  8231. sum = 0;
  8232. }
  8233. return (sum + '' === value.substr(8, 1));
  8234. },
  8235. /**
  8236. * Validate Polish VAT number
  8237. *
  8238. * @param {String} value VAT number
  8239. * @returns {Boolean}
  8240. */
  8241. _pl: function(value) {
  8242. if (/^PL[0-9]{10}$/.test(value)) {
  8243. value = value.substr(2);
  8244. }
  8245. if (!/^[0-9]{10}$/.test(value)) {
  8246. return false;
  8247. }
  8248. var sum = 0,
  8249. weight = [6, 5, 7, 2, 3, 4, 5, 6, 7, -1];
  8250. for (var i = 0; i < 10; i++) {
  8251. sum += parseInt(value.charAt(i), 10) * weight[i];
  8252. }
  8253. return (sum % 11 === 0);
  8254. },
  8255. /**
  8256. * Validate Portuguese VAT number
  8257. *
  8258. * @param {String} value VAT number
  8259. * @returns {Boolean}
  8260. */
  8261. _pt: function(value) {
  8262. if (/^PT[0-9]{9}$/.test(value)) {
  8263. value = value.substr(2);
  8264. }
  8265. if (!/^[0-9]{9}$/.test(value)) {
  8266. return false;
  8267. }
  8268. var sum = 0,
  8269. weight = [9, 8, 7, 6, 5, 4, 3, 2];
  8270. for (var i = 0; i < 8; i++) {
  8271. sum += parseInt(value.charAt(i), 10) * weight[i];
  8272. }
  8273. sum = 11 - sum % 11;
  8274. if (sum > 9) {
  8275. sum = 0;
  8276. }
  8277. return (sum + '' === value.substr(8, 1));
  8278. },
  8279. /**
  8280. * Validate Romanian VAT number
  8281. *
  8282. * @param {String} value VAT number
  8283. * @returns {Boolean}
  8284. */
  8285. _ro: function(value) {
  8286. if (/^RO[1-9][0-9]{1,9}$/.test(value)) {
  8287. value = value.substr(2);
  8288. }
  8289. if (!/^[1-9][0-9]{1,9}$/.test(value)) {
  8290. return false;
  8291. }
  8292. var length = value.length,
  8293. weight = [7, 5, 3, 2, 1, 7, 5, 3, 2].slice(10 - length),
  8294. sum = 0;
  8295. for (var i = 0; i < length - 1; i++) {
  8296. sum += parseInt(value.charAt(i), 10) * weight[i];
  8297. }
  8298. sum = (10 * sum) % 11 % 10;
  8299. return (sum + '' === value.substr(length - 1, 1));
  8300. },
  8301. /**
  8302. * Validate Russian VAT number (Taxpayer Identification Number - INN)
  8303. *
  8304. * @param {String} value VAT number
  8305. * @returns {Boolean}
  8306. */
  8307. _ru: function(value) {
  8308. if (/^RU([0-9]{10}|[0-9]{12})$/.test(value)) {
  8309. value = value.substr(2);
  8310. }
  8311. if (!/^([0-9]{10}|[0-9]{12})$/.test(value)) {
  8312. return false;
  8313. }
  8314. var i = 0;
  8315. if (value.length === 10) {
  8316. var sum = 0,
  8317. weight = [2, 4, 10, 3, 5, 9, 4, 6, 8, 0];
  8318. for (i = 0; i < 10; i++) {
  8319. sum += parseInt(value.charAt(i), 10) * weight[i];
  8320. }
  8321. sum = sum % 11;
  8322. if (sum > 9) {
  8323. sum = sum % 10;
  8324. }
  8325. return (sum + '' === value.substr(9, 1));
  8326. } else if (value.length === 12) {
  8327. var sum1 = 0,
  8328. weight1 = [7, 2, 4, 10, 3, 5, 9, 4, 6, 8, 0],
  8329. sum2 = 0,
  8330. weight2 = [3, 7, 2, 4, 10, 3, 5, 9, 4, 6, 8, 0];
  8331. for (i = 0; i < 11; i++) {
  8332. sum1 += parseInt(value.charAt(i), 10) * weight1[i];
  8333. sum2 += parseInt(value.charAt(i), 10) * weight2[i];
  8334. }
  8335. sum1 = sum1 % 11;
  8336. if (sum1 > 9) {
  8337. sum1 = sum1 % 10;
  8338. }
  8339. sum2 = sum2 % 11;
  8340. if (sum2 > 9) {
  8341. sum2 = sum2 % 10;
  8342. }
  8343. return (sum1 + '' === value.substr(10, 1) && sum2 + '' === value.substr(11, 1));
  8344. }
  8345. return false;
  8346. },
  8347. /**
  8348. * Validate Serbian VAT number
  8349. *
  8350. * @param {String} value VAT number
  8351. * @returns {Boolean}
  8352. */
  8353. _rs: function(value) {
  8354. if (/^RS[0-9]{9}$/.test(value)) {
  8355. value = value.substr(2);
  8356. }
  8357. if (!/^[0-9]{9}$/.test(value)) {
  8358. return false;
  8359. }
  8360. var sum = 10,
  8361. temp = 0;
  8362. for (var i = 0; i < 8; i++) {
  8363. temp = (parseInt(value.charAt(i), 10) + sum) % 10;
  8364. if (temp === 0) {
  8365. temp = 10;
  8366. }
  8367. sum = (2 * temp) % 11;
  8368. }
  8369. return ((sum + parseInt(value.substr(8, 1), 10)) % 10 === 1);
  8370. },
  8371. /**
  8372. * Validate Swedish VAT number
  8373. *
  8374. * @param {String} value VAT number
  8375. * @returns {Boolean}
  8376. */
  8377. _se: function(value) {
  8378. if (/^SE[0-9]{10}01$/.test(value)) {
  8379. value = value.substr(2);
  8380. }
  8381. if (!/^[0-9]{10}01$/.test(value)) {
  8382. return false;
  8383. }
  8384. value = value.substr(0, 10);
  8385. return FormValidation.Helper.luhn(value);
  8386. },
  8387. /**
  8388. * Validate Slovenian VAT number
  8389. *
  8390. * @param {String} value VAT number
  8391. * @returns {Boolean}
  8392. */
  8393. _si: function(value) {
  8394. // The Slovenian VAT numbers don't start with zero
  8395. var res = value.match(/^(SI)?([1-9][0-9]{7})$/);
  8396. if (!res) {
  8397. return false;
  8398. }
  8399. if (res[1]) {
  8400. value = value.substr(2);
  8401. }
  8402. var sum = 0,
  8403. weight = [8, 7, 6, 5, 4, 3, 2];
  8404. for (var i = 0; i < 7; i++) {
  8405. sum += parseInt(value.charAt(i), 10) * weight[i];
  8406. }
  8407. sum = 11 - sum % 11;
  8408. if (sum === 10) {
  8409. sum = 0;
  8410. }
  8411. return (sum + '' === value.substr(7, 1));
  8412. },
  8413. /**
  8414. * Validate Slovak VAT number
  8415. *
  8416. * @param {String} value VAT number
  8417. * @returns {Boolean}
  8418. */
  8419. _sk: function(value) {
  8420. if (/^SK[1-9][0-9][(2-4)|(6-9)][0-9]{7}$/.test(value)) {
  8421. value = value.substr(2);
  8422. }
  8423. if (!/^[1-9][0-9][(2-4)|(6-9)][0-9]{7}$/.test(value)) {
  8424. return false;
  8425. }
  8426. return (parseInt(value, 10) % 11 === 0);
  8427. },
  8428. /**
  8429. * Validate Venezuelan VAT number (RIF)
  8430. *
  8431. * @param {String} value VAT number
  8432. * @returns {Boolean}
  8433. */
  8434. _ve: function(value) {
  8435. if (/^VE[VEJPG][0-9]{9}$/.test(value)) {
  8436. value = value.substr(2);
  8437. }
  8438. if (!/^[VEJPG][0-9]{9}$/.test(value)) {
  8439. return false;
  8440. }
  8441. var types = {
  8442. 'V': 4,
  8443. 'E': 8,
  8444. 'J': 12,
  8445. 'P': 16,
  8446. 'G': 20
  8447. },
  8448. sum = types[value.charAt(0)],
  8449. weight = [3, 2, 7, 6, 5, 4, 3, 2];
  8450. for (var i = 0; i < 8; i++) {
  8451. sum += parseInt(value.charAt(i + 1), 10) * weight[i];
  8452. }
  8453. sum = 11 - sum % 11;
  8454. if (sum === 11 || sum === 10) {
  8455. sum = 0;
  8456. }
  8457. return (sum + '' === value.substr(9, 1));
  8458. },
  8459. /**
  8460. * Validate South African VAT number
  8461. *
  8462. * @params {String} value VAT number
  8463. * @returns {Boolean}
  8464. */
  8465. _za: function(value) {
  8466. if (/^ZA4[0-9]{9}$/.test(value)) {
  8467. value = value.substr(2);
  8468. }
  8469. return /^4[0-9]{9}$/.test(value);
  8470. }
  8471. };
  8472. }(jQuery));
  8473. ;(function($) {
  8474. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  8475. 'en_US': {
  8476. vin: {
  8477. 'default': 'Please enter a valid VIN number'
  8478. }
  8479. }
  8480. });
  8481. FormValidation.Validator.vin = {
  8482. /**
  8483. * Validate an US VIN (Vehicle Identification Number)
  8484. *
  8485. * @param {FormValidation.Base} validator The validator plugin instance
  8486. * @param {jQuery} $field Field element
  8487. * @param {Object} options Consist of key:
  8488. * - message: The invalid message
  8489. * @returns {Boolean}
  8490. */
  8491. validate: function(validator, $field, options, validatorName) {
  8492. var value = validator.getFieldValue($field, validatorName);
  8493. if (value === '') {
  8494. return true;
  8495. }
  8496. // Don't accept I, O, Q characters
  8497. if (!/^[a-hj-npr-z0-9]{8}[0-9xX][a-hj-npr-z0-9]{8}$/i.test(value)) {
  8498. return false;
  8499. }
  8500. value = value.toUpperCase();
  8501. var chars = {
  8502. A: 1, B: 2, C: 3, D: 4, E: 5, F: 6, G: 7, H: 8,
  8503. J: 1, K: 2, L: 3, M: 4, N: 5, P: 7, R: 9,
  8504. S: 2, T: 3, U: 4, V: 5, W: 6, X: 7, Y: 8, Z: 9,
  8505. '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '0': 0
  8506. },
  8507. weights = [8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2],
  8508. sum = 0,
  8509. length = value.length;
  8510. for (var i = 0; i < length; i++) {
  8511. sum += chars[value.charAt(i) + ''] * weights[i];
  8512. }
  8513. var reminder = sum % 11;
  8514. if (reminder === 10) {
  8515. reminder = 'X';
  8516. }
  8517. return (reminder + '') === value.charAt(8);
  8518. }
  8519. };
  8520. }(jQuery));
  8521. ;(function($) {
  8522. FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
  8523. 'en_US': {
  8524. zipCode: {
  8525. 'default': 'Please enter a valid postal code',
  8526. country: 'Please enter a valid postal code in %s',
  8527. countries: {
  8528. AT: 'Austria',
  8529. BG: 'Bulgaria',
  8530. BR: 'Brazil',
  8531. CA: 'Canada',
  8532. CH: 'Switzerland',
  8533. CZ: 'Czech Republic',
  8534. DE: 'Germany',
  8535. DK: 'Denmark',
  8536. ES: 'Spain',
  8537. FR: 'France',
  8538. GB: 'United Kingdom',
  8539. IE: 'Ireland',
  8540. IN: 'India',
  8541. IT: 'Italy',
  8542. MA: 'Morocco',
  8543. NL: 'Netherlands',
  8544. PL: 'Poland',
  8545. PT: 'Portugal',
  8546. RO: 'Romania',
  8547. RU: 'Russia',
  8548. SE: 'Sweden',
  8549. SG: 'Singapore',
  8550. SK: 'Slovakia',
  8551. US: 'USA'
  8552. }
  8553. }
  8554. }
  8555. });
  8556. FormValidation.Validator.zipCode = {
  8557. html5Attributes: {
  8558. message: 'message',
  8559. country: 'country'
  8560. },
  8561. COUNTRY_CODES: ['AT', 'BG', 'BR', 'CA', 'CH', 'CZ', 'DE', 'DK', 'ES', 'FR', 'GB', 'IE', 'IN', 'IT', 'MA', 'NL', 'PL', 'PT', 'RO', 'RU', 'SE', 'SG', 'SK', 'US'],
  8562. /**
  8563. * Return true if and only if the input value is a valid country zip code
  8564. *
  8565. * @param {FormValidation.Base} validator The validator plugin instance
  8566. * @param {jQuery} $field Field element
  8567. * @param {Object} options Consist of key:
  8568. * - message: The invalid message
  8569. * - country: The country
  8570. *
  8571. * The country can be defined by:
  8572. * - An ISO 3166 country code
  8573. * - Name of field which its value defines the country code
  8574. * - Name of callback function that returns the country code
  8575. * - A callback function that returns the country code
  8576. *
  8577. * callback: function(value, validator, $field) {
  8578. * // value is the value of field
  8579. * // validator is the FormValidation.Base instance
  8580. * // $field is jQuery element representing the field
  8581. * }
  8582. *
  8583. * @returns {Boolean|Object}
  8584. */
  8585. validate: function(validator, $field, options, validatorName) {
  8586. var value = validator.getFieldValue($field, validatorName);
  8587. if (value === '' || !options.country) {
  8588. return true;
  8589. }
  8590. var locale = validator.getLocale(),
  8591. country = options.country;
  8592. if (typeof country !== 'string' || $.inArray(country, this.COUNTRY_CODES) === -1) {
  8593. // Try to determine the country
  8594. country = validator.getDynamicOption($field, country);
  8595. }
  8596. if (!country || $.inArray(country.toUpperCase(), this.COUNTRY_CODES) === -1) {
  8597. return true;
  8598. }
  8599. var isValid = false;
  8600. country = country.toUpperCase();
  8601. switch (country) {
  8602. // http://en.wikipedia.org/wiki/List_of_postal_codes_in_Austria
  8603. case 'AT':
  8604. isValid = /^([1-9]{1})(\d{3})$/.test(value);
  8605. break;
  8606. case 'BG':
  8607. isValid = /^([1-9]{1}[0-9]{3})$/.test($.trim(value));
  8608. break;
  8609. case 'BR':
  8610. isValid = /^(\d{2})([\.]?)(\d{3})([\-]?)(\d{3})$/.test(value);
  8611. break;
  8612. case 'CA':
  8613. 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);
  8614. break;
  8615. case 'CH':
  8616. isValid = /^([1-9]{1})(\d{3})$/.test(value);
  8617. break;
  8618. case 'CZ':
  8619. // Test: http://regexr.com/39hhr
  8620. isValid = /^(\d{3})([ ]?)(\d{2})$/.test(value);
  8621. break;
  8622. // http://stackoverflow.com/questions/7926687/regular-expression-german-zip-codes
  8623. case 'DE':
  8624. isValid = /^(?!01000|99999)(0[1-9]\d{3}|[1-9]\d{4})$/.test(value);
  8625. break;
  8626. case 'DK':
  8627. isValid = /^(DK(-|\s)?)?\d{4}$/i.test(value);
  8628. break;
  8629. // Zip codes in Spain go from 01XXX to 52XXX.
  8630. // Test: http://refiddle.com/1ufo
  8631. case 'ES':
  8632. isValid = /^(?:0[1-9]|[1-4][0-9]|5[0-2])\d{3}$/.test(value);
  8633. break;
  8634. // http://en.wikipedia.org/wiki/Postal_codes_in_France
  8635. case 'FR':
  8636. isValid = /^[0-9]{5}$/i.test(value);
  8637. break;
  8638. case 'GB':
  8639. isValid = this._gb(value);
  8640. break;
  8641. // Indian PIN (Postal Index Number) validation
  8642. // http://en.wikipedia.org/wiki/Postal_Index_Number
  8643. // Test: http://regex101.com/r/kV0vH3/1
  8644. case 'IN':
  8645. isValid = /^\d{3}\s?\d{3}$/.test(value);
  8646. break;
  8647. // http://www.eircode.ie/docs/default-source/Common/prepare-your-business-for-eircode---published-v2.pdf?sfvrsn=2
  8648. // Test: http://refiddle.com/1kpl
  8649. case 'IE':
  8650. isValid = /^(D6W|[ACDEFHKNPRTVWXY]\d{2})\s[0-9ACDEFHKNPRTVWXY]{4}$/.test(value);
  8651. break;
  8652. // http://en.wikipedia.org/wiki/List_of_postal_codes_in_Italy
  8653. case 'IT':
  8654. isValid = /^(I-|IT-)?\d{5}$/i.test(value);
  8655. break;
  8656. // http://en.wikipedia.org/wiki/List_of_postal_codes_in_Morocco
  8657. case 'MA':
  8658. isValid = /^[1-9][0-9]{4}$/i.test(value);
  8659. break;
  8660. // http://en.wikipedia.org/wiki/Postal_codes_in_the_Netherlands
  8661. case 'NL':
  8662. isValid = /^[1-9][0-9]{3} ?(?!sa|sd|ss)[a-z]{2}$/i.test(value);
  8663. break;
  8664. // http://en.wikipedia.org/wiki/List_of_postal_codes_in_Poland
  8665. case 'PL':
  8666. isValid = /^[0-9]{2}\-[0-9]{3}$/.test(value);
  8667. break;
  8668. // Test: http://refiddle.com/1l2t
  8669. case 'PT':
  8670. isValid = /^[1-9]\d{3}-\d{3}$/.test(value);
  8671. break;
  8672. case 'RO':
  8673. isValid = /^(0[1-8]{1}|[1-9]{1}[0-5]{1})?[0-9]{4}$/i.test(value);
  8674. break;
  8675. case 'RU':
  8676. isValid = /^[0-9]{6}$/i.test(value);
  8677. break;
  8678. case 'SE':
  8679. isValid = /^(S-)?\d{3}\s?\d{2}$/i.test(value);
  8680. break;
  8681. case 'SG':
  8682. isValid = /^([0][1-9]|[1-6][0-9]|[7]([0-3]|[5-9])|[8][0-2])(\d{4})$/i.test(value);
  8683. break;
  8684. case 'SK':
  8685. // Test: http://regexr.com/39hhr
  8686. isValid = /^(\d{3})([ ]?)(\d{2})$/.test(value);
  8687. break;
  8688. case 'US':
  8689. /* falls through */
  8690. default:
  8691. isValid = /^\d{4,5}([\-]?\d{4})?$/.test(value);
  8692. break;
  8693. }
  8694. return {
  8695. valid: isValid,
  8696. message: FormValidation.Helper.format(options.message || FormValidation.I18n[locale].zipCode.country, FormValidation.I18n[locale].zipCode.countries[country])
  8697. };
  8698. },
  8699. /**
  8700. * Validate United Kingdom postcode
  8701. *
  8702. * @see http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
  8703. * @param {String} value The postcode
  8704. * @returns {Boolean}
  8705. */
  8706. _gb: function(value) {
  8707. var firstChar = '[ABCDEFGHIJKLMNOPRSTUWYZ]', // Does not accept QVX
  8708. secondChar = '[ABCDEFGHKLMNOPQRSTUVWXY]', // Does not accept IJZ
  8709. thirdChar = '[ABCDEFGHJKPMNRSTUVWXY]',
  8710. fourthChar = '[ABEHMNPRVWXY]',
  8711. fifthChar = '[ABDEFGHJLNPQRSTUWXYZ]',
  8712. regexps = [
  8713. // AN NAA, ANN NAA, AAN NAA, AANN NAA format
  8714. new RegExp('^(' + firstChar + '{1}' + secondChar + '?[0-9]{1,2})(\\s*)([0-9]{1}' + fifthChar + '{2})$', 'i'),
  8715. // ANA NAA
  8716. new RegExp('^(' + firstChar + '{1}[0-9]{1}' + thirdChar + '{1})(\\s*)([0-9]{1}' + fifthChar + '{2})$', 'i'),
  8717. // AANA NAA
  8718. new RegExp('^(' + firstChar + '{1}' + secondChar + '{1}?[0-9]{1}' + fourthChar + '{1})(\\s*)([0-9]{1}' + fifthChar + '{2})$', 'i'),
  8719. new RegExp('^(BF1)(\\s*)([0-6]{1}[ABDEFGHJLNPQRST]{1}[ABDEFGHJLNPQRSTUWZYZ]{1})$', 'i'), // BFPO postcodes
  8720. /^(GIR)(\s*)(0AA)$/i, // Special postcode GIR 0AA
  8721. /^(BFPO)(\s*)([0-9]{1,4})$/i, // Standard BFPO numbers
  8722. /^(BFPO)(\s*)(c\/o\s*[0-9]{1,3})$/i, // c/o BFPO numbers
  8723. /^([A-Z]{4})(\s*)(1ZZ)$/i, // Overseas Territories
  8724. /^(AI-2640)$/i // Anguilla
  8725. ];
  8726. for (var i = 0; i < regexps.length; i++) {
  8727. if (regexps[i].test(value)) {
  8728. return true;
  8729. }
  8730. }
  8731. return false;
  8732. }
  8733. };
  8734. }(jQuery));