bootstrapValidator.js 194 KB

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