bootstrapValidator.js 165 KB

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