bootstrapValidator.js 196 KB

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