bootstrapValidator.js 163 KB

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