bootstrapValidator.js 195 KB

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