bootstrapValidator.js 138 KB

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