bootstrapValidator.js 195 KB

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