bootstrapValidator.js 131 KB

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