bootstrapValidator.js 195 KB

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