bootstrapValidator.js 196 KB

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