bootstrapValidator.js 192 KB

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