bootstrapValidator.js 202 KB

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