bootstrapValidator.js 150 KB

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