bootstrapValidator.js 135 KB

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