bootstrapValidator.js 136 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617
  1. /**
  2. * BootstrapValidator (http://bootstrapvalidator.com)
  3. *
  4. * A jQuery plugin to validate form fields. Use with Bootstrap 3
  5. *
  6. * @version v0.5.0-dev
  7. * @author https://twitter.com/nghuuphuoc
  8. * @copyright (c) 2013 - 2014 Nguyen Huu Phuoc
  9. * @license MIT
  10. */
  11. (function($) {
  12. var BootstrapValidator = function(form, options) {
  13. this.$form = $(form);
  14. this.options = $.extend({}, BootstrapValidator.DEFAULT_OPTIONS, options);
  15. this.$invalidField = null; // First invalid field
  16. this.$submitButton = null; // The submit button which is clicked to submit form
  17. // Validating status
  18. this.STATUS_NOT_VALIDATED = 'NOT_VALIDATED';
  19. this.STATUS_VALIDATING = 'VALIDATING';
  20. this.STATUS_INVALID = 'INVALID';
  21. this.STATUS_VALID = 'VALID';
  22. // Determine the event that is fired when user change the field value
  23. // Most modern browsers supports input event except IE 7, 8.
  24. // IE 9 supports input event but the event is still not fired if I press the backspace key.
  25. // In that case I will use the keydown event
  26. var el = document.createElement('div');
  27. this._changeEvent = ('oninput' in el) ? 'input' : 'keydown';
  28. // The flag to indicate that the form is ready to submit when a remote/callback validator returns
  29. this._submitIfValid = null;
  30. 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 $.trim(item);
  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 = $field.is('select')
  818. ? validator.getFieldElements($field.attr('data-bv-field')).find('option').filter(':selected').length
  819. : validator.getFieldElements($field.attr('data-bv-field')).filter(':checked').length;
  820. if ((options.min && numChoices < options.min) || (options.max && numChoices > options.max)) {
  821. return false;
  822. }
  823. return true;
  824. }
  825. };
  826. }(window.jQuery));
  827. ;(function($) {
  828. $.fn.bootstrapValidator.validators.creditCard = {
  829. /**
  830. * Return true if the input value is valid credit card number
  831. * Based on https://gist.github.com/DiegoSalazar/4075533
  832. *
  833. * @param {BootstrapValidator} validator The validator plugin instance
  834. * @param {jQuery} $field Field element
  835. * @param {Object} options Can consist of the following key:
  836. * - message: The invalid message
  837. * @returns {Boolean}
  838. */
  839. validate: function(validator, $field, options) {
  840. var value = $field.val();
  841. if (value == '') {
  842. return true;
  843. }
  844. // Accept only digits, dashes or spaces
  845. if (/[^0-9-\s]+/.test(value)) {
  846. return false;
  847. }
  848. value = value.replace(/\D/g, '');
  849. if (!$.fn.bootstrapValidator.helpers.luhn(value)) {
  850. return false;
  851. }
  852. // Validate the card number based on prefix (IIN ranges) and length
  853. var cards = {
  854. AMERICAN_EXPRESS: {
  855. length: [15],
  856. prefix: ['34', '37']
  857. },
  858. DINERS_CLUB: {
  859. length: [14],
  860. prefix: ['300', '301', '302', '303', '304', '305', '36']
  861. },
  862. DINERS_CLUB_US: {
  863. length: [16],
  864. prefix: ['54', '55']
  865. },
  866. DISCOVER: {
  867. length: [16],
  868. prefix: ['6011', '622126', '622127', '622128', '622129', '62213',
  869. '62214', '62215', '62216', '62217', '62218', '62219',
  870. '6222', '6223', '6224', '6225', '6226', '6227', '6228',
  871. '62290', '62291', '622920', '622921', '622922', '622923',
  872. '622924', '622925', '644', '645', '646', '647', '648',
  873. '649', '65']
  874. },
  875. JCB: {
  876. length: [16],
  877. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  878. },
  879. LASER: {
  880. length: [16, 17, 18, 19],
  881. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  882. },
  883. MAESTRO: {
  884. length: [12, 13, 14, 15, 16, 17, 18, 19],
  885. prefix: ['5018', '5020', '5038', '6304', '6759', '6761', '6762', '6763', '6764', '6765', '6766']
  886. },
  887. MASTERCARD: {
  888. length: [16],
  889. prefix: ['51', '52', '53', '54', '55']
  890. },
  891. SOLO: {
  892. length: [16, 18, 19],
  893. prefix: ['6334', '6767']
  894. },
  895. UNIONPAY: {
  896. length: [16, 17, 18, 19],
  897. prefix: ['622126', '622127', '622128', '622129', '62213', '62214',
  898. '62215', '62216', '62217', '62218', '62219', '6222', '6223',
  899. '6224', '6225', '6226', '6227', '6228', '62290', '62291',
  900. '622920', '622921', '622922', '622923', '622924', '622925']
  901. },
  902. VISA: {
  903. length: [16],
  904. prefix: ['4']
  905. }
  906. };
  907. var type, i;
  908. for (type in cards) {
  909. for (i in cards[type]['prefix']) {
  910. if (value.substr(0, cards[type]['prefix'][i].length) == cards[type]['prefix'][i] // Check the prefix
  911. && cards[type]['length'].indexOf(value.length) != -1) // and length
  912. {
  913. return true;
  914. }
  915. }
  916. }
  917. return false;
  918. }
  919. };
  920. }(window.jQuery));
  921. ;(function($) {
  922. $.fn.bootstrapValidator.validators.cvv = {
  923. html5Attributes: {
  924. message: 'message',
  925. ccfield: 'creditCardField'
  926. },
  927. /**
  928. * Return true if the input value is a valid CVV number.
  929. *
  930. * @param {BootstrapValidator} validator The validator plugin instance
  931. * @param {jQuery} $field Field element
  932. * @param {Object} options Can consist of the following keys:
  933. * - creditCardField: The credit card number field. It can be null
  934. * - message: The invalid message
  935. * @returns {Boolean}
  936. */
  937. validate: function(validator, $field, options) {
  938. var value = $field.val();
  939. if (value == '') {
  940. return true;
  941. }
  942. if (!/^[0-9]{3,4}$/.test(value)) {
  943. return false;
  944. }
  945. if (!options.creditCardField) {
  946. return true;
  947. }
  948. // Get the credit card number
  949. var creditCard = validator.getFieldElements(options.creditCardField).val();
  950. if (creditCard == '') {
  951. return true;
  952. }
  953. // Supported credit card types
  954. var cards = {
  955. AMERICAN_EXPRESS: {
  956. length: [15],
  957. prefix: ['34', '37']
  958. },
  959. DINERS_CLUB: {
  960. length: [14],
  961. prefix: ['300', '301', '302', '303', '304', '305', '36']
  962. },
  963. DINERS_CLUB_US: {
  964. length: [16],
  965. prefix: ['54', '55']
  966. },
  967. DISCOVER: {
  968. length: [16],
  969. prefix: ['6011', '622126', '622127', '622128', '622129', '62213',
  970. '62214', '62215', '62216', '62217', '62218', '62219',
  971. '6222', '6223', '6224', '6225', '6226', '6227', '6228',
  972. '62290', '62291', '622920', '622921', '622922', '622923',
  973. '622924', '622925', '644', '645', '646', '647', '648',
  974. '649', '65']
  975. },
  976. JCB: {
  977. length: [16],
  978. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  979. },
  980. LASER: {
  981. length: [16, 17, 18, 19],
  982. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  983. },
  984. MAESTRO: {
  985. length: [12, 13, 14, 15, 16, 17, 18, 19],
  986. prefix: ['5018', '5020', '5038', '6304', '6759', '6761', '6762', '6763', '6764', '6765', '6766']
  987. },
  988. MASTERCARD: {
  989. length: [16],
  990. prefix: ['51', '52', '53', '54', '55']
  991. },
  992. SOLO: {
  993. length: [16, 18, 19],
  994. prefix: ['6334', '6767']
  995. },
  996. UNIONPAY: {
  997. length: [16, 17, 18, 19],
  998. prefix: ['622126', '622127', '622128', '622129', '62213', '62214',
  999. '62215', '62216', '62217', '62218', '62219', '6222', '6223',
  1000. '6224', '6225', '6226', '6227', '6228', '62290', '62291',
  1001. '622920', '622921', '622922', '622923', '622924', '622925']
  1002. },
  1003. VISA: {
  1004. length: [16],
  1005. prefix: ['4']
  1006. }
  1007. };
  1008. var type, i, creditCardType = null;
  1009. for (type in cards) {
  1010. for (i in cards[type]['prefix']) {
  1011. if (creditCard.substr(0, cards[type]['prefix'][i].length) == cards[type]['prefix'][i] // Check the prefix
  1012. && cards[type]['length'].indexOf(creditCard.length) != -1) // and length
  1013. {
  1014. creditCardType = type;
  1015. break;
  1016. }
  1017. }
  1018. }
  1019. return (creditCardType == null)
  1020. ? false
  1021. : (('AMERICAN_EXPRESS' == creditCardType) ? (value.length == 4) : (value.length == 3));
  1022. }
  1023. };
  1024. }(window.jQuery));
  1025. ;(function($) {
  1026. $.fn.bootstrapValidator.validators.date = {
  1027. html5Attributes: {
  1028. message: 'message',
  1029. format: 'format'
  1030. },
  1031. /**
  1032. * Return true if the input value is valid date
  1033. *
  1034. * @param {BootstrapValidator} validator The validator plugin instance
  1035. * @param {jQuery} $field Field element
  1036. * @param {Object} options Can consist of the following keys:
  1037. * - format: The date format. Default is MM/DD/YYYY
  1038. * Support the following formats:
  1039. * YYYY/DD/MM
  1040. * YYYY/DD/MM h:m A
  1041. * YYYY/MM/DD
  1042. * YYYY/MM/DD h:m A
  1043. *
  1044. * YYYY-DD-MM
  1045. * YYYY-DD-MM h:m A
  1046. * YYYY-MM-DD
  1047. * YYYY-MM-DD h:m A
  1048. *
  1049. * MM/DD/YYYY
  1050. * MM/DD/YYYY h:m A
  1051. * DD/MM/YYYY
  1052. * DD/MM/YYYY h:m A
  1053. *
  1054. * MM-DD-YYYY
  1055. * MM-DD-YYYY h:m A
  1056. * DD-MM-YYYY
  1057. * DD-MM-YYYY h:m A
  1058. * - message: The invalid message
  1059. * @returns {Boolean}
  1060. */
  1061. validate: function(validator, $field, options) {
  1062. var value = $field.val();
  1063. if (value == '') {
  1064. return true;
  1065. }
  1066. // Determine the separator
  1067. options.format = options.format || 'MM/DD/YYYY';
  1068. var separator = (options.format.indexOf('/') != -1)
  1069. ? '/'
  1070. : ((options.format.indexOf('-') != -1) ? '-' : null);
  1071. if (separator == null) {
  1072. return false;
  1073. }
  1074. var month, day, year, minutes = null, hours = null, matches;
  1075. switch (true) {
  1076. case (separator == '/' && (matches = value.match(/^(\d{4})\/(\d{1,2})\/(\d{1,2})$/i)) && options.format == 'YYYY/DD/MM'):
  1077. case (separator == '-' && (matches = value.match(/^(\d{4})-(\d{1,2})-(\d{1,2})$/i)) && options.format == 'YYYY-DD-MM'):
  1078. year = matches[1]; day = matches[2]; month = matches[3];
  1079. break;
  1080. case (separator == '/' && (matches = value.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/i)) && options.format == 'DD/MM/YYYY'):
  1081. case (separator == '-' && (matches = value.match(/^(\d{1,2})-(\d{1,2})-(\d{4})$/i)) && options.format == 'DD-MM-YYYY'):
  1082. day = matches[1]; month = matches[2]; year = matches[3];
  1083. break;
  1084. case (separator == '/' && (matches = value.match(/^(\d{4})\/(\d{1,2})\/(\d{1,2})$/i)) && options.format == 'YYYY/MM/DD'):
  1085. case (separator == '-' && (matches = value.match(/^(\d{4})-(\d{1,2})-(\d{1,2})$/i)) && options.format == 'YYYY-MM-DD'):
  1086. year = matches[1]; month = matches[2]; day = matches[3];
  1087. break;
  1088. case (separator == '/' && (matches = value.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/i)) && options.format == 'MM/DD/YYYY'):
  1089. case (separator == '-' && (matches = value.match(/^(\d{1,2})-(\d{1,2})-(\d{4})$/i)) && options.format == 'MM-DD-YYYY'):
  1090. month = matches[1]; day = matches[2]; year = matches[3];
  1091. break;
  1092. 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'):
  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. year = matches[1]; day = matches[2]; month = matches[3]; hours = matches[4]; minutes = matches[5];
  1095. break;
  1096. 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'):
  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. day = matches[1]; month = matches[2]; year = matches[3]; hours = matches[4]; minutes = matches[5];
  1099. break;
  1100. 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'):
  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. year = matches[1]; month = matches[2]; day = matches[3]; hours = matches[4]; minutes = matches[5];
  1103. break;
  1104. 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'):
  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. month = matches[1]; day = matches[2]; year = matches[3]; hours = matches[4]; minutes = matches[5];
  1107. break;
  1108. default:
  1109. return false;
  1110. }
  1111. // Validate hours and minutes
  1112. if (hours && minutes) {
  1113. hours = parseInt(hours, 10);
  1114. minutes = parseInt(minutes, 10);
  1115. if (hours < 1 || hours > 12 || minutes < 0 || minutes > 59) {
  1116. return false;
  1117. }
  1118. }
  1119. // Validate day, month, and year
  1120. day = parseInt(day, 10);
  1121. month = parseInt(month, 10);
  1122. year = parseInt(year, 10);
  1123. if (year < 1000 || year > 9999 || month == 0 || month > 12) {
  1124. return false;
  1125. }
  1126. var numDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  1127. // Update the number of days in Feb of leap year
  1128. if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
  1129. numDays[1] = 29;
  1130. }
  1131. // Check the day
  1132. return (day > 0 && day <= numDays[month - 1]);
  1133. }
  1134. };
  1135. }(window.jQuery));
  1136. ;(function($) {
  1137. $.fn.bootstrapValidator.validators.different = {
  1138. html5Attributes: {
  1139. message: 'message',
  1140. field: 'field'
  1141. },
  1142. /**
  1143. * Return true if the input value is different with given field's value
  1144. *
  1145. * @param {BootstrapValidator} validator The validator plugin instance
  1146. * @param {jQuery} $field Field element
  1147. * @param {Object} options Consists of the following key:
  1148. * - field: The name of field that will be used to compare with current one
  1149. * - message: The invalid message
  1150. * @returns {Boolean}
  1151. */
  1152. validate: function(validator, $field, options) {
  1153. var value = $field.val();
  1154. if (value == '') {
  1155. return true;
  1156. }
  1157. var compareWith = validator.getFieldElements(options.field);
  1158. if (compareWith == null) {
  1159. return true;
  1160. }
  1161. if (value != compareWith.val()) {
  1162. validator.updateStatus(options.field, validator.STATUS_VALID, 'different');
  1163. return true;
  1164. } else {
  1165. return false;
  1166. }
  1167. }
  1168. };
  1169. }(window.jQuery));
  1170. ;(function($) {
  1171. $.fn.bootstrapValidator.validators.digits = {
  1172. /**
  1173. * Return true if the input value contains digits only
  1174. *
  1175. * @param {BootstrapValidator} validator Validate plugin instance
  1176. * @param {jQuery} $field Field element
  1177. * @param {Object} options
  1178. * @returns {Boolean}
  1179. */
  1180. validate: function(validator, $field, options) {
  1181. var value = $field.val();
  1182. if (value == '') {
  1183. return true;
  1184. }
  1185. return /^\d+$/.test(value);
  1186. }
  1187. }
  1188. }(window.jQuery));
  1189. ;(function($) {
  1190. $.fn.bootstrapValidator.validators.emailAddress = {
  1191. enableByHtml5: function($field) {
  1192. return ('email' == $field.attr('type'));
  1193. },
  1194. /**
  1195. * Return true if and only if the input value is a valid email address
  1196. *
  1197. * @param {BootstrapValidator} validator Validate plugin instance
  1198. * @param {jQuery} $field Field element
  1199. * @param {Object} options
  1200. * @returns {Boolean}
  1201. */
  1202. validate: function(validator, $field, options) {
  1203. var value = $field.val();
  1204. if (value == '') {
  1205. return true;
  1206. }
  1207. // Email address regular expression
  1208. // http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
  1209. 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,}))$/;
  1210. return emailRegExp.test(value);
  1211. }
  1212. }
  1213. }(window.jQuery));
  1214. ;(function($) {
  1215. $.fn.bootstrapValidator.validators.file = {
  1216. html5Attributes: {
  1217. extension: 'extension',
  1218. maxsize: 'maxSize',
  1219. message: 'message',
  1220. type: 'type'
  1221. },
  1222. /**
  1223. * Validate upload file. Use HTML 5 API if the browser supports
  1224. *
  1225. * @param {BootstrapValidator} validator The validator plugin instance
  1226. * @param {jQuery} $field Field element
  1227. * @param {Object} options Can consist of the following keys:
  1228. * - extension: The allowed extensions, separated by a comma
  1229. * - maxSize: The maximum size in bytes
  1230. * - message: The invalid message
  1231. * - type: The allowed MIME type, separated by a comma
  1232. * @returns {Boolean}
  1233. */
  1234. validate: function(validator, $field, options) {
  1235. var value = $field.val();
  1236. if (value == '') {
  1237. return true;
  1238. }
  1239. var ext,
  1240. extensions = options.extension ? options.extension.split(',') : null,
  1241. types = options.type ? options.type.split(',') : null,
  1242. html5 = (window.File && window.FileList && window.FileReader);
  1243. if (html5) {
  1244. // Get FileList instance
  1245. var files = $field.get(0).files,
  1246. total = files.length;
  1247. for (var i = 0; i < total; i++) {
  1248. // Check file size
  1249. if (options.maxSize && files[i].size > parseInt(options.maxSize)) {
  1250. return false;
  1251. }
  1252. // Check file extension
  1253. ext = files[i].name.substr(files[i].name.lastIndexOf('.') + 1);
  1254. if (extensions && extensions.indexOf(ext) == -1) {
  1255. return false;
  1256. }
  1257. // Check file type
  1258. if (types && types.indexOf(files[i].type) == -1) {
  1259. return false;
  1260. }
  1261. }
  1262. } else {
  1263. // Check file extension
  1264. ext = value.substr(value.lastIndexOf('.') + 1);
  1265. if (extensions && extensions.indexOf(ext) == -1) {
  1266. return false;
  1267. }
  1268. }
  1269. return true;
  1270. }
  1271. };
  1272. }(window.jQuery));
  1273. ;(function($) {
  1274. $.fn.bootstrapValidator.validators.greaterThan = {
  1275. html5Attributes: {
  1276. message: 'message',
  1277. value: 'value',
  1278. inclusive: 'inclusive'
  1279. },
  1280. enableByHtml5: function($field) {
  1281. var min = $field.attr('min');
  1282. if (min) {
  1283. return {
  1284. value: min
  1285. };
  1286. }
  1287. return false;
  1288. },
  1289. /**
  1290. * Return true if the input value is greater than or equals to given number
  1291. *
  1292. * @param {BootstrapValidator} validator Validate plugin instance
  1293. * @param {jQuery} $field Field element
  1294. * @param {Object} options Can consist of the following keys:
  1295. * - value: The number used to compare to
  1296. * - inclusive [optional]: Can be true or false. Default is true
  1297. * - message: The invalid message
  1298. * @returns {Boolean}
  1299. */
  1300. validate: function(validator, $field, options) {
  1301. var value = $field.val();
  1302. if (value == '') {
  1303. return true;
  1304. }
  1305. value = parseFloat(value);
  1306. return (options.inclusive === true) ? (value > options.value) : (value >= options.value);
  1307. }
  1308. }
  1309. }(window.jQuery));
  1310. ;(function($) {
  1311. $.fn.bootstrapValidator.validators.hex = {
  1312. /**
  1313. * Return true if and only if the input value is a valid hexadecimal number
  1314. *
  1315. * @param {BootstrapValidator} validator The validator plugin instance
  1316. * @param {jQuery} $field Field element
  1317. * @param {Object} options Consist of key:
  1318. * - message: The invalid message
  1319. * @returns {Boolean}
  1320. */
  1321. validate: function(validator, $field, options) {
  1322. var value = $field.val();
  1323. if (value == '') {
  1324. return true;
  1325. }
  1326. return /^[0-9a-fA-F]+$/.test(value);
  1327. }
  1328. };
  1329. }(window.jQuery));
  1330. ;(function($) {
  1331. $.fn.bootstrapValidator.validators.hexColor = {
  1332. enableByHtml5: function($field) {
  1333. return ('color' == $field.attr('type'));
  1334. },
  1335. /**
  1336. * Return true if the input value is a valid hex color
  1337. *
  1338. * @param {BootstrapValidator} validator The validator plugin instance
  1339. * @param {jQuery} $field Field element
  1340. * @param {Object} options Can consist of the following keys:
  1341. * - message: The invalid message
  1342. * @returns {Boolean}
  1343. */
  1344. validate: function(validator, $field, options) {
  1345. var value = $field.val();
  1346. if (value == '') {
  1347. return true;
  1348. }
  1349. return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(value);
  1350. }
  1351. };
  1352. }(window.jQuery));
  1353. ;(function($) {
  1354. $.fn.bootstrapValidator.validators.iban = {
  1355. html5Attributes: {
  1356. message: 'message',
  1357. country: 'country'
  1358. },
  1359. /**
  1360. * Validate an International Bank Account Number (IBAN)
  1361. * To test it, take the sample IBAN from
  1362. * http://www.nordea.com/Our+services/International+products+and+services/Cash+Management/IBAN+countries/908462.html
  1363. *
  1364. * @param {BootstrapValidator} validator The validator plugin instance
  1365. * @param {jQuery} $field Field element
  1366. * @param {Object} options Can consist of the following keys:
  1367. * - message: The invalid message
  1368. * - country: The ISO 3166-1 country code
  1369. * @returns {Boolean}
  1370. */
  1371. validate: function(validator, $field, options) {
  1372. var value = $field.val();
  1373. if (value == '') {
  1374. return true;
  1375. }
  1376. // See
  1377. // http://www.swift.com/dsp/resources/documents/IBAN_Registry.pdf
  1378. // http://en.wikipedia.org/wiki/International_Bank_Account_Number#IBAN_formats_by_country
  1379. var ibanRegex = {
  1380. 'AD': 'AD[0-9]{2}[0-9]{4}[0-9]{4}[A-Z0-9]{12}', // Andorra
  1381. 'AE': 'AE[0-9]{2}[0-9]{3}[0-9]{16}', // United Arab Emirates
  1382. 'AL': 'AL[0-9]{2}[0-9]{8}[A-Z0-9]{16}', // Albania
  1383. 'AO': 'AO[0-9]{2}[0-9]{21}', // Angola
  1384. 'AT': 'AT[0-9]{2}[0-9]{5}[0-9]{11}', // Austria
  1385. 'AZ': 'AZ[0-9]{2}[A-Z]{4}[A-Z0-9]{20}', // Azerbaijan
  1386. 'BA': 'BA[0-9]{2}[0-9]{3}[0-9]{3}[0-9]{8}[0-9]{2}', // Bosnia and Herzegovina
  1387. 'BE': 'BE[0-9]{2}[0-9]{3}[0-9]{7}[0-9]{2}', // Belgium
  1388. 'BF': 'BF[0-9]{2}[0-9]{23}', // Burkina Faso
  1389. 'BG': 'BG[0-9]{2}[A-Z]{4}[0-9]{4}[0-9]{2}[A-Z0-9]{8}', // Bulgaria
  1390. 'BH': 'BH[0-9]{2}[A-Z]{4}[A-Z0-9]{14}', // Bahrain
  1391. 'BI': 'BI[0-9]{2}[0-9]{12}', // Burundi
  1392. 'BJ': 'BJ[0-9]{2}[A-Z]{1}[0-9]{23}', // Benin
  1393. 'BR': 'BR[0-9]{2}[0-9]{8}[0-9]{5}[0-9]{10}[A-Z][A-Z0-9]', // Brazil
  1394. 'CH': 'CH[0-9]{2}[0-9]{5}[A-Z0-9]{12}', // Switzerland
  1395. 'CI': 'CI[0-9]{2}[A-Z]{1}[0-9]{23}', // Ivory Coast
  1396. 'CM': 'CM[0-9]{2}[0-9]{23}', // Cameroon
  1397. 'CR': 'CR[0-9]{2}[0-9]{3}[0-9]{14}', // Costa Rica
  1398. 'CV': 'CV[0-9]{2}[0-9]{21}', // Cape Verde
  1399. 'CY': 'CY[0-9]{2}[0-9]{3}[0-9]{5}[A-Z0-9]{16}', // Cyprus
  1400. 'CZ': 'CZ[0-9]{2}[0-9]{20}', // Czech Republic
  1401. 'DE': 'DE[0-9]{2}[0-9]{8}[0-9]{10}', // Germany
  1402. 'DK': 'DK[0-9]{2}[0-9]{14}', // Denmark
  1403. 'DO': 'DO[0-9]{2}[A-Z0-9]{4}[0-9]{20}', // Dominican Republic
  1404. 'DZ': 'DZ[0-9]{2}[0-9]{20}', // Algeria
  1405. 'EE': 'EE[0-9]{2}[0-9]{2}[0-9]{2}[0-9]{11}[0-9]{1}', // Estonia
  1406. 'ES': 'ES[0-9]{2}[0-9]{4}[0-9]{4}[0-9]{1}[0-9]{1}[0-9]{10}', // Spain
  1407. 'FI': 'FI[0-9]{2}[0-9]{6}[0-9]{7}[0-9]{1}', // Finland
  1408. 'FO': 'FO[0-9]{2}[0-9]{4}[0-9]{9}[0-9]{1}', // Faroe Islands
  1409. 'FR': 'FR[0-9]{2}[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}', // France
  1410. 'GB': 'GB[0-9]{2}[A-Z]{4}[0-9]{6}[0-9]{8}', // United Kingdom
  1411. 'GE': 'GE[0-9]{2}[A-Z]{2}[0-9]{16}', // Georgia
  1412. 'GI': 'GI[0-9]{2}[A-Z]{4}[A-Z0-9]{15}', // Gibraltar
  1413. 'GL': 'GL[0-9]{2}[0-9]{4}[0-9]{9}[0-9]{1}', // Greenland[
  1414. 'GR': 'GR[0-9]{2}[0-9]{3}[0-9]{4}[A-Z0-9]{16}', // Greece
  1415. 'GT': 'GT[0-9]{2}[A-Z0-9]{4}[A-Z0-9]{20}', // Guatemala
  1416. 'HR': 'HR[0-9]{2}[0-9]{7}[0-9]{10}', // Croatia
  1417. 'HU': 'HU[0-9]{2}[0-9]{3}[0-9]{4}[0-9]{1}[0-9]{15}[0-9]{1}', // Hungary
  1418. 'IE': 'IE[0-9]{2}[A-Z]{4}[0-9]{6}[0-9]{8}', // Ireland
  1419. 'IL': 'IL[0-9]{2}[0-9]{3}[0-9]{3}[0-9]{13}', // Israel
  1420. 'IR': 'IR[0-9]{2}[0-9]{22}', // Iran
  1421. 'IS': 'IS[0-9]{2}[0-9]{4}[0-9]{2}[0-9]{6}[0-9]{10}', // Iceland
  1422. 'IT': 'IT[0-9]{2}[A-Z]{1}[0-9]{5}[0-9]{5}[A-Z0-9]{12}', // Italy
  1423. 'JO': 'JO[0-9]{2}[A-Z]{4}[0-9]{4}[0]{8}[A-Z0-9]{10}', // Jordan
  1424. 'KW': 'KW[0-9]{2}[A-Z]{4}[0-9]{22}', // Kuwait
  1425. 'KZ': 'KZ[0-9]{2}[0-9]{3}[A-Z0-9]{13}', // Kazakhstan
  1426. 'LB': 'LB[0-9]{2}[0-9]{4}[A-Z0-9]{20}', // Lebanon
  1427. 'LI': 'LI[0-9]{2}[0-9]{5}[A-Z0-9]{12}', // Liechtenstein
  1428. 'LT': 'LT[0-9]{2}[0-9]{5}[0-9]{11}', // Lithuania
  1429. 'LU': 'LU[0-9]{2}[0-9]{3}[A-Z0-9]{13}', // Luxembourg
  1430. 'LV': 'LV[0-9]{2}[A-Z]{4}[A-Z0-9]{13}', // Latvia
  1431. 'MC': 'MC[0-9]{2}[0-9]{5}[0-9]{5}[A-Z0-9]{11}[0-9]{2}', // Monaco
  1432. 'MD': 'MD[0-9]{2}[A-Z0-9]{20}', // Moldova
  1433. 'ME': 'ME[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}', // Montenegro
  1434. 'MG': 'MG[0-9]{2}[0-9]{23}', // Madagascar
  1435. 'MK': 'MK[0-9]{2}[0-9]{3}[A-Z0-9]{10}[0-9]{2}', // Macedonia
  1436. 'ML': 'ML[0-9]{2}[A-Z]{1}[0-9]{23}', // Mali
  1437. 'MR': 'MR13[0-9]{5}[0-9]{5}[0-9]{11}[0-9]{2}', // Mauritania
  1438. 'MT': 'MT[0-9]{2}[A-Z]{4}[0-9]{5}[A-Z0-9]{18}', // Malta
  1439. 'MU': 'MU[0-9]{2}[A-Z]{4}[0-9]{2}[0-9]{2}[0-9]{12}[0-9]{3}[A-Z]{3}',// Mauritius
  1440. 'MZ': 'MZ[0-9]{2}[0-9]{21}', // Mozambique
  1441. 'NL': 'NL[0-9]{2}[A-Z]{4}[0-9]{10}', // Netherlands
  1442. 'NO': 'NO[0-9]{2}[0-9]{4}[0-9]{6}[0-9]{1}', // Norway
  1443. 'PK': 'PK[0-9]{2}[A-Z]{4}[A-Z0-9]{16}', // Pakistan
  1444. 'PL': 'PL[0-9]{2}[0-9]{8}[0-9]{16}', // Poland
  1445. 'PS': 'PS[0-9]{2}[A-Z]{4}[A-Z0-9]{21}', // Palestinian
  1446. 'PT': 'PT[0-9]{2}[0-9]{4}[0-9]{4}[0-9]{11}[0-9]{2}', // Portugal
  1447. 'QA': 'QA[0-9]{2}[A-Z]{4}[A-Z0-9]{21}', // Qatar
  1448. 'RO': 'RO[0-9]{2}[A-Z]{4}[A-Z0-9]{16}', // Romania
  1449. 'RS': 'RS[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}', // Serbia
  1450. 'SA': 'SA[0-9]{2}[0-9]{2}[A-Z0-9]{18}', // Saudi Arabia
  1451. 'SE': 'SE[0-9]{2}[0-9]{3}[0-9]{16}[0-9]{1}', // Sweden
  1452. 'SI': 'SI[0-9]{2}[0-9]{5}[0-9]{8}[0-9]{2}', // Slovenia
  1453. 'SK': 'SK[0-9]{2}[0-9]{4}[0-9]{6}[0-9]{10}', // Slovakia
  1454. 'SM': 'SM[0-9]{2}[A-Z]{1}[0-9]{5}[0-9]{5}[A-Z0-9]{12}', // San Marino
  1455. 'SN': 'SN[0-9]{2}[A-Z]{1}[0-9]{23}', // Senegal
  1456. 'TN': 'TN59[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}', // Tunisia
  1457. 'TR': 'TR[0-9]{2}[0-9]{5}[A-Z0-9]{1}[A-Z0-9]{16}', // Turkey
  1458. 'VG': 'VG[0-9]{2}[A-Z]{4}[0-9]{16}' // Virgin Islands, British
  1459. };
  1460. value = value.replace(/[^a-zA-Z0-9]/g, '').toUpperCase();
  1461. var country = options.country || value.substr(0, 2);
  1462. if (!ibanRegex[country]) {
  1463. return false;
  1464. }
  1465. if (!(new RegExp('^' + ibanRegex[country] + '$')).test(value)) {
  1466. return false;
  1467. }
  1468. value = value.substr(4) + value.substr(0, 4);
  1469. value = value.split('').map(function(n) {
  1470. var code = n.charCodeAt(0);
  1471. return (code >= 'A'.charCodeAt(0) && code <= 'Z'.charCodeAt(0))
  1472. // Replace A, B, C, ..., Z with 10, 11, ..., 35
  1473. ? (code - 'A'.charCodeAt(0) + 10)
  1474. : n;
  1475. }).join('');
  1476. var temp = parseInt(value.substr(0, 1), 10),
  1477. length = value.length;
  1478. for (var i = 1; i < length; ++i) {
  1479. temp = (temp * 10 + parseInt(value.substr(i, 1), 10)) % 97;
  1480. }
  1481. return (temp == 1);
  1482. }
  1483. };
  1484. }(window.jQuery));
  1485. ;(function($) {
  1486. $.fn.bootstrapValidator.validators.identical = {
  1487. html5Attributes: {
  1488. message: 'message',
  1489. field: 'field'
  1490. },
  1491. /**
  1492. * Check if input value equals to value of particular one
  1493. *
  1494. * @param {BootstrapValidator} validator The validator plugin instance
  1495. * @param {jQuery} $field Field element
  1496. * @param {Object} options Consists of the following key:
  1497. * - field: The name of field that will be used to compare with current one
  1498. * @returns {Boolean}
  1499. */
  1500. validate: function(validator, $field, options) {
  1501. var value = $field.val();
  1502. if (value == '') {
  1503. return true;
  1504. }
  1505. var compareWith = validator.getFieldElements(options.field);
  1506. if (compareWith == null) {
  1507. return true;
  1508. }
  1509. if (value == compareWith.val()) {
  1510. validator.updateStatus(options.field, validator.STATUS_VALID, 'identical');
  1511. return true;
  1512. } else {
  1513. return false;
  1514. }
  1515. }
  1516. };
  1517. }(window.jQuery));
  1518. ;(function($) {
  1519. $.fn.bootstrapValidator.validators.integer = {
  1520. enableByHtml5: function($field) {
  1521. return ('number' == $field.attr('type'));
  1522. },
  1523. /**
  1524. * Return true if the input value is an integer
  1525. *
  1526. * @param {BootstrapValidator} validator The validator plugin instance
  1527. * @param {jQuery} $field Field element
  1528. * @param {Object} options Can consist of the following key:
  1529. * - message: The invalid message
  1530. * @returns {Boolean}
  1531. */
  1532. validate: function(validator, $field, options) {
  1533. var value = $field.val();
  1534. if (value == '') {
  1535. return true;
  1536. }
  1537. return /^(?:-?(?:0|[1-9][0-9]*))$/.test(value);
  1538. }
  1539. };
  1540. }(window.jQuery));
  1541. ;(function($) {
  1542. $.fn.bootstrapValidator.validators.ip = {
  1543. html5Attributes: {
  1544. message: 'message',
  1545. ipv4: 'ipv4',
  1546. ipv6: 'ipv6'
  1547. },
  1548. /**
  1549. * Return true if the input value is a IP address.
  1550. *
  1551. * @param {BootstrapValidator} validator The validator plugin instance
  1552. * @param {jQuery} $field Field element
  1553. * @param {Object} options Can consist of the following keys:
  1554. * - ipv4: Enable IPv4 validator, default to true
  1555. * - ipv6: Enable IPv6 validator, default to true
  1556. * - message: The invalid message
  1557. * @returns {Boolean}
  1558. */
  1559. validate: function(validator, $field, options) {
  1560. var value = $field.val();
  1561. if (value == '') {
  1562. return true;
  1563. }
  1564. options = $.extend({}, { ipv4: true, ipv6: true }, options);
  1565. if (options.ipv4) {
  1566. 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);
  1567. } else if (options.ipv6) {
  1568. 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);
  1569. }
  1570. return false;
  1571. }
  1572. };
  1573. }(window.jQuery));
  1574. ;(function($) {
  1575. $.fn.bootstrapValidator.validators.isbn = {
  1576. /**
  1577. * Return true if the input value is a valid ISBN 10 or ISBN 13 number
  1578. * Examples:
  1579. * - Valid:
  1580. * ISBN 10: 99921-58-10-7, 9971-5-0210-0, 960-425-059-0, 80-902734-1-6, 85-359-0277-5, 1-84356-028-3, 0-684-84328-5, 0-8044-2957-X, 0-85131-041-9, 0-943396-04-2, 0-9752298-0-X
  1581. * ISBN 13: 978-0-306-40615-7
  1582. * - Invalid:
  1583. * ISBN 10: 99921-58-10-6
  1584. * ISBN 13: 978-0-306-40615-6
  1585. *
  1586. * @param {BootstrapValidator} validator The validator plugin instance
  1587. * @param {jQuery} $field Field element
  1588. * @param {Object} options Can consist of the following keys:
  1589. * - message: The invalid message
  1590. * @returns {Boolean}
  1591. */
  1592. validate: function(validator, $field, options) {
  1593. var value = $field.val();
  1594. if (value == '') {
  1595. return true;
  1596. }
  1597. // http://en.wikipedia.org/wiki/International_Standard_Book_Number#Overview
  1598. // Groups are separated by a hyphen or a space
  1599. var type;
  1600. switch (true) {
  1601. case /^\d{9}[\dX]$/.test(value):
  1602. case (value.length == 13 && /^(\d+)-(\d+)-(\d+)-([\dX])$/.test(value)):
  1603. case (value.length == 13 && /^(\d+)\s(\d+)\s(\d+)\s([\dX])$/.test(value)):
  1604. type = 'ISBN10';
  1605. break;
  1606. case /^(978|979)\d{9}[\dX]$/.test(value):
  1607. case (value.length == 17 && /^(978|979)-(\d+)-(\d+)-(\d+)-([\dX])$/.test(value)):
  1608. case (value.length == 17 && /^(978|979)\s(\d+)\s(\d+)\s(\d+)\s([\dX])$/.test(value)):
  1609. type = 'ISBN13';
  1610. break;
  1611. default:
  1612. return false;
  1613. }
  1614. // Replace all special characters except digits and X
  1615. value = value.replace(/[^0-9X]/gi, '');
  1616. var chars = value.split(''),
  1617. length = chars.length,
  1618. sum = 0,
  1619. checksum;
  1620. switch (type) {
  1621. case 'ISBN10':
  1622. sum = 0;
  1623. for (var i = 0; i < length - 1; i++) {
  1624. sum += ((10 - i) * parseInt(chars[i]));
  1625. }
  1626. checksum = 11 - (sum % 11);
  1627. if (checksum == 11) {
  1628. checksum = 0;
  1629. } else if (checksum == 10) {
  1630. checksum = 'X';
  1631. }
  1632. return (checksum + '' == chars[length - 1]);
  1633. case 'ISBN13':
  1634. sum = 0;
  1635. for (var i = 0; i < length - 1; i++) {
  1636. sum += ((i % 2 == 0) ? parseInt(chars[i]) : (parseInt(chars[i]) * 3));
  1637. }
  1638. checksum = 10 - (sum % 10);
  1639. if (checksum == 10) {
  1640. checksum = '0';
  1641. }
  1642. return (checksum + '' == chars[length - 1]);
  1643. default:
  1644. return false;
  1645. }
  1646. }
  1647. };
  1648. }(window.jQuery));
  1649. ;(function($) {
  1650. $.fn.bootstrapValidator.validators.lessThan = {
  1651. html5Attributes: {
  1652. message: 'message',
  1653. value: 'value',
  1654. inclusive: 'inclusive'
  1655. },
  1656. enableByHtml5: function($field) {
  1657. var max = $field.attr('max');
  1658. if (max) {
  1659. return {
  1660. value: max
  1661. };
  1662. }
  1663. return false;
  1664. },
  1665. /**
  1666. * Return true if the input value is less than or equal to given number
  1667. *
  1668. * @param {BootstrapValidator} validator The validator plugin instance
  1669. * @param {jQuery} $field Field element
  1670. * @param {Object} options Can consist of the following keys:
  1671. * - value: The number used to compare to
  1672. * - inclusive [optional]: Can be true or false. Default is true
  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. value = parseFloat(value);
  1682. return (options.inclusive === false) ? (value <= options.value) : (value < options.value);
  1683. }
  1684. };
  1685. }(window.jQuery));
  1686. ;(function($) {
  1687. $.fn.bootstrapValidator.validators.mac = {
  1688. /**
  1689. * Return true if the input value is a MAC address.
  1690. *
  1691. * @param {BootstrapValidator} validator The validator plugin instance
  1692. * @param {jQuery} $field Field element
  1693. * @param {Object} options Can consist of the following keys:
  1694. * - message: The invalid message
  1695. * @returns {Boolean}
  1696. */
  1697. validate: function(validator, $field, options) {
  1698. var value = $field.val();
  1699. if (value == '') {
  1700. return true;
  1701. }
  1702. return /^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$/.test(value);
  1703. }
  1704. };
  1705. }(window.jQuery));
  1706. ;(function($) {
  1707. $.fn.bootstrapValidator.validators.notEmpty = {
  1708. enableByHtml5: function($field) {
  1709. var required = $field.attr('required') + '';
  1710. return ('required' == required || 'true' == required);
  1711. },
  1712. /**
  1713. * Check if input value is empty or not
  1714. *
  1715. * @param {BootstrapValidator} validator The validator plugin instance
  1716. * @param {jQuery} $field Field element
  1717. * @param {Object} options
  1718. * @returns {Boolean}
  1719. */
  1720. validate: function(validator, $field, options) {
  1721. var type = $field.attr('type');
  1722. if ('radio' == type || 'checkbox' == type) {
  1723. return validator
  1724. .getFieldElements($field.attr('data-bv-field'))
  1725. .filter(':checked')
  1726. .length > 0;
  1727. }
  1728. return $.trim($field.val()) != '';
  1729. }
  1730. };
  1731. }(window.jQuery));
  1732. ;(function($) {
  1733. $.fn.bootstrapValidator.validators.numeric = {
  1734. /**
  1735. * Validate decimal number
  1736. *
  1737. * @param {BootstrapValidator} validator The validator plugin instance
  1738. * @param {jQuery} $field Field element
  1739. * @param {Object} options Consist of key:
  1740. * - message: The invalid message
  1741. * @returns {Boolean}
  1742. */
  1743. validate: function(validator, $field, options) {
  1744. var value = $field.val();
  1745. if (value == '') {
  1746. return true;
  1747. }
  1748. return !isNaN(parseFloat(value)) && isFinite(value);
  1749. }
  1750. };
  1751. }(window.jQuery));
  1752. ;(function($) {
  1753. $.fn.bootstrapValidator.validators.phone = {
  1754. html5Attributes: {
  1755. message: 'message',
  1756. country: 'country'
  1757. },
  1758. /**
  1759. * Return true if the input value contains a valid US phone number only
  1760. *
  1761. * @param {BootstrapValidator} validator Validate plugin instance
  1762. * @param {jQuery} $field Field element
  1763. * @param {Object} options Consist of key:
  1764. * - message: The invalid message
  1765. * - country: The ISO 3166 country code
  1766. * Currently it only supports United State (US) country
  1767. * @returns {Boolean}
  1768. */
  1769. validate: function(validator, $field, options) {
  1770. var value = $field.val();
  1771. if (value == '') {
  1772. return true;
  1773. }
  1774. var country = (options.country || 'US').toUpperCase();
  1775. switch (country) {
  1776. case 'US':
  1777. default:
  1778. // Make sure US phone numbers have 10 digits
  1779. // May start with 1, +1, or 1-; should discard
  1780. // Area code may be delimited with (), & sections may be delimited with . or -
  1781. // Test: http://regexr.com/38mqi
  1782. value = value.replace(/\D/g, '');
  1783. return (/^(?:(1\-?)|(\+1 ?))?\(?(\d{3})[\)\-\.]?(\d{3})[\-\.]?(\d{4})$/).test(value) && (value.length == 10);
  1784. }
  1785. }
  1786. }
  1787. }(window.jQuery));
  1788. ;(function($) {
  1789. $.fn.bootstrapValidator.validators.regexp = {
  1790. html5Attributes: {
  1791. message: 'message',
  1792. regexp: 'regexp'
  1793. },
  1794. enableByHtml5: function($field) {
  1795. var pattern = $field.attr('pattern');
  1796. if (pattern) {
  1797. return {
  1798. regexp: pattern
  1799. };
  1800. }
  1801. return false;
  1802. },
  1803. /**
  1804. * Check if the element value matches given regular expression
  1805. *
  1806. * @param {BootstrapValidator} validator The validator plugin instance
  1807. * @param {jQuery} $field Field element
  1808. * @param {Object} options Consists of the following key:
  1809. * - regexp: The regular expression you need to check
  1810. * @returns {Boolean}
  1811. */
  1812. validate: function(validator, $field, options) {
  1813. var value = $field.val();
  1814. if (value == '') {
  1815. return true;
  1816. }
  1817. var regexp = ('string' == typeof options.regexp) ? new RegExp(options.regexp) : options.regexp;
  1818. return regexp.test(value);
  1819. }
  1820. };
  1821. }(window.jQuery));
  1822. ;(function ($) {
  1823. $.fn.bootstrapValidator.validators.remote = {
  1824. html5Attributes: {
  1825. message: 'message',
  1826. url: 'url',
  1827. name: 'name'
  1828. },
  1829. /**
  1830. * Request a remote server to check the input value
  1831. *
  1832. * @param {BootstrapValidator} validator Plugin instance
  1833. * @param {jQuery} $field Field element
  1834. * @param {Object} options Can consist of the following keys:
  1835. * - url
  1836. * - data [optional]: By default, it will take the value
  1837. * {
  1838. * <fieldName>: <fieldValue>
  1839. * }
  1840. * - name [optional]: Override the field name for the request.
  1841. * - message: The invalid message
  1842. * @returns {Boolean|Deferred}
  1843. */
  1844. validate: function(validator, $field, options) {
  1845. var value = $field.val();
  1846. if (value == '') {
  1847. return true;
  1848. }
  1849. var name = $field.attr('data-bv-field'), data = options.data;
  1850. if (data == null) {
  1851. data = {};
  1852. }
  1853. // Support dynamic data
  1854. if ('function' == typeof data) {
  1855. data = data.call(this, validator);
  1856. }
  1857. data[options.name || name] = value;
  1858. var dfd = new $.Deferred();
  1859. var xhr = $.ajax({
  1860. type: 'POST',
  1861. url: options.url,
  1862. dataType: 'json',
  1863. data: data
  1864. });
  1865. xhr.then(function(response) {
  1866. dfd.resolve($field, 'remote', response.valid === true || response.valid === 'true');
  1867. });
  1868. dfd.fail(function() {
  1869. xhr.abort();
  1870. });
  1871. return dfd;
  1872. }
  1873. };
  1874. }(window.jQuery));
  1875. ;(function($) {
  1876. $.fn.bootstrapValidator.validators.siren = {
  1877. /**
  1878. * Check if a string is a siren number
  1879. *
  1880. * @param {BootstrapValidator} validator The validator plugin instance
  1881. * @param {jQuery} $field Field element
  1882. * @param {Object} options Consist of key:
  1883. * - message: The invalid message
  1884. * @returns {Boolean}
  1885. */
  1886. validate: function(validator, $field, options) {
  1887. var value = $field.val();
  1888. if (value == '') {
  1889. return true;
  1890. }
  1891. if (!/^\d{9}$/.test(value)) {
  1892. return false;
  1893. }
  1894. return $.fn.bootstrapValidator.helpers.luhn(value);
  1895. }
  1896. };
  1897. }(window.jQuery));
  1898. ;(function($) {
  1899. $.fn.bootstrapValidator.validators.siret = {
  1900. /**
  1901. * Check if a string is a siret number
  1902. *
  1903. * @param {BootstrapValidator} validator The validator plugin instance
  1904. * @param {jQuery} $field Field element
  1905. * @param {Object} options Consist of key:
  1906. * - message: The invalid message
  1907. * @returns {Boolean}
  1908. */
  1909. validate: function(validator, $field, options) {
  1910. var value = $field.val();
  1911. if (value == '') {
  1912. return true;
  1913. }
  1914. var sum = 0,
  1915. length = value.length,
  1916. tmp;
  1917. for (var i = 0; i < length; i++) {
  1918. tmp = parseInt(value.charAt(i), 10);
  1919. if ((i % 2) == 0) {
  1920. tmp = tmp * 2;
  1921. if (tmp > 9) {
  1922. tmp -= 9;
  1923. }
  1924. }
  1925. sum += tmp;
  1926. }
  1927. return (sum % 10 == 0);
  1928. }
  1929. };
  1930. }(window.jQuery));
  1931. ;(function($) {
  1932. $.fn.bootstrapValidator.validators.step = {
  1933. html5Attributes: {
  1934. message: 'message',
  1935. base: 'baseValue',
  1936. step: 'step'
  1937. },
  1938. /**
  1939. * Return true if the input value is valid step one
  1940. *
  1941. * @param {BootstrapValidator} validator The validator plugin instance
  1942. * @param {jQuery} $field Field element
  1943. * @param {Object} options Can consist of the following keys:
  1944. * - baseValue: The base value
  1945. * - step: The step
  1946. * - message: The invalid message
  1947. * @returns {Boolean}
  1948. */
  1949. validate: function(validator, $field, options) {
  1950. var value = $field.val();
  1951. if (value == '') {
  1952. return true;
  1953. }
  1954. options = $.extend({}, { baseValue: 0, step: 1 }, options);
  1955. value = parseFloat(value);
  1956. if (isNaN(value) || !isFinite(value)) {
  1957. return false;
  1958. }
  1959. var round = function(x, precision) {
  1960. var m = Math.pow(10, precision);
  1961. x = x * m;
  1962. var sign = (x > 0) | -(x < 0),
  1963. isHalf = (x % 1 === 0.5 * sign);
  1964. if (isHalf) {
  1965. return (Math.floor(x) + (sign > 0)) / m;
  1966. } else {
  1967. return Math.round(x) / m;
  1968. }
  1969. },
  1970. floatMod = function(x, y) {
  1971. if (y == 0.0) {
  1972. return 1.0;
  1973. }
  1974. var dotX = (x + '').split('.'),
  1975. dotY = (y + '').split('.'),
  1976. precision = ((dotX.length == 1) ? 0 : dotX[1].length) + ((dotY.length == 1) ? 0 : dotY[1].length);
  1977. return round(x - y * Math.floor(x / y), precision);
  1978. };
  1979. var mod = floatMod(value - options.baseValue, options.step);
  1980. return (mod == 0.0 || mod == options.step);
  1981. }
  1982. };
  1983. }(window.jQuery));
  1984. ;(function($) {
  1985. $.fn.bootstrapValidator.validators.stringCase = {
  1986. html5Attributes: {
  1987. message: 'message',
  1988. 'case': 'case'
  1989. },
  1990. /**
  1991. * Check if a string is a lower or upper case one
  1992. *
  1993. * @param {BootstrapValidator} validator The validator plugin instance
  1994. * @param {jQuery} $field Field element
  1995. * @param {Object} options Consist of key:
  1996. * - message: The invalid message
  1997. * - case: Can be 'lower' (default) or 'upper'
  1998. * @returns {Boolean}
  1999. */
  2000. validate: function(validator, $field, options) {
  2001. var value = $field.val();
  2002. if (value == '') {
  2003. return true;
  2004. }
  2005. var stringCase = (options['case'] || 'lower').toLowerCase();
  2006. switch (stringCase) {
  2007. case 'upper':
  2008. return value === value.toUpperCase();
  2009. case 'lower':
  2010. default:
  2011. return value === value.toLowerCase();
  2012. }
  2013. }
  2014. };
  2015. }(window.jQuery));
  2016. ;(function($) {
  2017. $.fn.bootstrapValidator.validators.stringLength = {
  2018. html5Attributes: {
  2019. message: 'message',
  2020. min: 'min',
  2021. max: 'max'
  2022. },
  2023. enableByHtml5: function($field) {
  2024. var maxLength = $field.attr('maxlength');
  2025. if (maxLength) {
  2026. return {
  2027. max: parseInt(maxLength, 10)
  2028. };
  2029. }
  2030. return false;
  2031. },
  2032. /**
  2033. * Check if the length of element value is less or more than given number
  2034. *
  2035. * @param {BootstrapValidator} validator The validator plugin instance
  2036. * @param {jQuery} $field Field element
  2037. * @param {Object} options Consists of following keys:
  2038. * - min
  2039. * - max
  2040. * At least one of two keys is required
  2041. * - message: The invalid message
  2042. * @returns {Boolean}
  2043. */
  2044. validate: function(validator, $field, options) {
  2045. var value = $field.val();
  2046. if (value == '') {
  2047. return true;
  2048. }
  2049. var length = $.trim(value).length;
  2050. if ((options.min && length < options.min) || (options.max && length > options.max)) {
  2051. return false;
  2052. }
  2053. return true;
  2054. }
  2055. };
  2056. }(window.jQuery));
  2057. ;(function($) {
  2058. $.fn.bootstrapValidator.validators.uri = {
  2059. enableByHtml5: function($field) {
  2060. return ('url' == $field.attr('type'));
  2061. },
  2062. /**
  2063. * Return true if the input value is a valid URL
  2064. *
  2065. * @param {BootstrapValidator} validator The validator plugin instance
  2066. * @param {jQuery} $field Field element
  2067. * @param {Object} options
  2068. * @returns {Boolean}
  2069. */
  2070. validate: function(validator, $field, options) {
  2071. var value = $field.val();
  2072. if (value == '') {
  2073. return true;
  2074. }
  2075. // Credit to https://gist.github.com/dperini/729294
  2076. //
  2077. // Regular Expression for URL validation
  2078. //
  2079. // Author: Diego Perini
  2080. // Updated: 2010/12/05
  2081. //
  2082. // the regular expression composed & commented
  2083. // could be easily tweaked for RFC compliance,
  2084. // it was expressly modified to fit & satisfy
  2085. // these test for an URL shortener:
  2086. //
  2087. // http://mathiasbynens.be/demo/url-regex
  2088. //
  2089. // Notes on possible differences from a standard/generic validation:
  2090. //
  2091. // - utf-8 char class take in consideration the full Unicode range
  2092. // - TLDs have been made mandatory so single names like "localhost" fails
  2093. // - protocols have been restricted to ftp, http and https only as requested
  2094. //
  2095. // Changes:
  2096. //
  2097. // - IP address dotted notation validation, range: 1.0.0.0 - 223.255.255.255
  2098. // first and last IP address of each class is considered invalid
  2099. // (since they are broadcast/network addresses)
  2100. //
  2101. // - Added exclusion of private, reserved and/or local networks ranges
  2102. //
  2103. // Compressed one-line versions:
  2104. //
  2105. // Javascript version
  2106. //
  2107. // /^(?:(?: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
  2108. //
  2109. // PHP version
  2110. //
  2111. // _^(?:(?: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
  2112. var urlExp = new RegExp(
  2113. "^" +
  2114. // protocol identifier
  2115. "(?:(?:https?|ftp)://)" +
  2116. // user:pass authentication
  2117. "(?:\\S+(?::\\S*)?@)?" +
  2118. "(?:" +
  2119. // IP address exclusion
  2120. // private & local networks
  2121. "(?!10(?:\\.\\d{1,3}){3})" +
  2122. "(?!127(?:\\.\\d{1,3}){3})" +
  2123. "(?!169\\.254(?:\\.\\d{1,3}){2})" +
  2124. "(?!192\\.168(?:\\.\\d{1,3}){2})" +
  2125. "(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})" +
  2126. // IP address dotted notation octets
  2127. // excludes loopback network 0.0.0.0
  2128. // excludes reserved space >= 224.0.0.0
  2129. // excludes network & broacast addresses
  2130. // (first & last IP address of each class)
  2131. "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
  2132. "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
  2133. "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
  2134. "|" +
  2135. // host name
  2136. "(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)" +
  2137. // domain name
  2138. "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*" +
  2139. // TLD identifier
  2140. "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" +
  2141. ")" +
  2142. // port number
  2143. "(?::\\d{2,5})?" +
  2144. // resource path
  2145. "(?:/[^\\s]*)?" +
  2146. "$", "i"
  2147. );
  2148. return urlExp.test(value);
  2149. }
  2150. };
  2151. }(window.jQuery));
  2152. ;(function($) {
  2153. $.fn.bootstrapValidator.validators.uuid = {
  2154. html5Attributes: {
  2155. message: 'message',
  2156. version: 'version'
  2157. },
  2158. /**
  2159. * Return true if and only if the input value is a valid UUID string
  2160. *
  2161. * @see http://en.wikipedia.org/wiki/Universally_unique_identifier
  2162. * @param {BootstrapValidator} validator The validator plugin instance
  2163. * @param {jQuery} $field Field element
  2164. * @param {Object} options Consist of key:
  2165. * - message: The invalid message
  2166. * - version: Can be 3, 4, 5, null
  2167. * @returns {Boolean}
  2168. */
  2169. validate: function(validator, $field, options) {
  2170. var value = $field.val();
  2171. if (value == '') {
  2172. return true;
  2173. }
  2174. // See the format at http://en.wikipedia.org/wiki/Universally_unique_identifier#Variants_and_versions
  2175. var patterns = {
  2176. '3': /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
  2177. '4': /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
  2178. '5': /^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
  2179. all: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i
  2180. },
  2181. version = options.version ? (options.version + '') : 'all';
  2182. return (null == patterns[version]) ? true : patterns[version].test(value);
  2183. }
  2184. };
  2185. }(window.jQuery));
  2186. ;(function($) {
  2187. $.fn.bootstrapValidator.validators.vat = {
  2188. html5Attributes: {
  2189. message: 'message',
  2190. country: 'country'
  2191. },
  2192. /**
  2193. * Validate an European VAT number
  2194. *
  2195. * @param {BootstrapValidator} validator The validator plugin instance
  2196. * @param {jQuery} $field Field element
  2197. * @param {Object} options Consist of key:
  2198. * - message: The invalid message
  2199. * - country: The ISO 3166-1 country code
  2200. * @returns {Boolean}
  2201. */
  2202. validate: function(validator, $field, options) {
  2203. var value = $field.val();
  2204. if (value == '' || !options.country) {
  2205. return true;
  2206. }
  2207. var country = options.country || value.substr(0, 2),
  2208. method = ['_', country.toLowerCase()].join('');
  2209. if (this[method] && 'function' == typeof this[method]) {
  2210. return this[method](value);
  2211. }
  2212. return true;
  2213. },
  2214. // VAT validators
  2215. /**
  2216. * Validate Austrian VAT number
  2217. * Example:
  2218. * - Valid: ATU13585627
  2219. * - Invalid: ATU13585626
  2220. *
  2221. * @param {String} value VAT number
  2222. * @returns {Boolean}
  2223. */
  2224. _at: function(value) {
  2225. if (!/^ATU[0-9]{8}$/.test(value)) {
  2226. return false;
  2227. }
  2228. value = value.substr(3);
  2229. var sum = 0,
  2230. weight = [1, 2, 1, 2, 1, 2, 1],
  2231. temp = 0;
  2232. for (var i = 0; i < 7; i++) {
  2233. temp = parseInt(value.charAt(i)) * weight[i];
  2234. if (temp > 9) {
  2235. temp = Math.floor(temp / 10) + temp % 10;
  2236. }
  2237. sum += temp;
  2238. }
  2239. sum = 10 - (sum + 4) % 10;
  2240. if (sum == 10) {
  2241. sum = 0;
  2242. }
  2243. return (sum == value.substr(7, 1));
  2244. },
  2245. /**
  2246. * Validate Belgian VAT number
  2247. * Example:
  2248. * - Valid: BE0428759497
  2249. * - Invalid: BE431150351
  2250. *
  2251. * @param {String} value VAT number
  2252. * @returns {Boolean}
  2253. */
  2254. _be: function(value) {
  2255. if (!/^BE[0]{0,1}[0-9]{9}$/.test(value)) {
  2256. return false;
  2257. }
  2258. value = value.substr(2);
  2259. if (value.length == 9) {
  2260. value = '0' + value;
  2261. }
  2262. if (value.substr(1, 1) == 0) {
  2263. return false;
  2264. }
  2265. var sum = parseInt(value.substr(0, 8), 10) + parseInt(value.substr(8, 2), 10);
  2266. return (sum % 97 == 0);
  2267. },
  2268. /**
  2269. * Validate Bulgarian VAT number
  2270. * Example:
  2271. * - Valid: BG175074752,
  2272. * BG7523169263, BG8032056031,
  2273. * BG7542011030,
  2274. * BG7111042925
  2275. * - Invalid: BG175074753, BG7552A10004, BG7111042922
  2276. *
  2277. * @param {String} value VAT number
  2278. * @returns {Boolean}
  2279. */
  2280. _bg: function(value) {
  2281. if (!/^BG[0-9]{9,10}$/.test(value)) {
  2282. return false;
  2283. }
  2284. value = value.substr(2);
  2285. var total = 0, sum = 0, weight = [], i = 0;
  2286. // Legal entities
  2287. if (value.length == 9) {
  2288. for (i = 0; i < 8; i++) {
  2289. sum += parseInt(value.charAt(i)) * (i + 1);
  2290. }
  2291. sum = sum % 11;
  2292. if (sum == 10) {
  2293. sum = 0;
  2294. for (i = 0; i < 8; i++) {
  2295. sum += parseInt(value.charAt(i)) * (i + 3);
  2296. }
  2297. }
  2298. sum = sum % 10;
  2299. return (sum == value.substr(8));
  2300. }
  2301. // Physical persons, foreigners and others
  2302. else if (value.length == 10) {
  2303. // Validate Bulgarian national identification numbers
  2304. var egn = function(value) {
  2305. // Check the birth date
  2306. var year = parseInt(value.substr(0, 2), 10) + 1900,
  2307. month = parseInt(value.substr(2, 2), 10),
  2308. day = parseInt(value.substr(4, 2), 10);
  2309. if (month > 40) {
  2310. year += 100;
  2311. month -= 40;
  2312. } else if (month > 20) {
  2313. year -= 100;
  2314. month -= 20;
  2315. }
  2316. try {
  2317. var d = new Date(year, month, day);
  2318. } catch (ex) {
  2319. return false;
  2320. }
  2321. var sum = 0,
  2322. weight = [2, 4, 8, 5, 10, 9, 7, 3, 6];
  2323. for (var i = 0; i < 9; i++) {
  2324. sum += parseInt(value.charAt(i)) * weight[i];
  2325. }
  2326. sum = (sum % 11) % 10;
  2327. return (sum == value.substr(9, 1));
  2328. },
  2329. // Validate Bulgarian personal number of a foreigner
  2330. pnf = function(value) {
  2331. var sum = 0,
  2332. weight = [21, 19, 17, 13, 11, 9, 7, 3, 1];
  2333. for (var i = 0; i < 9; i++) {
  2334. sum += parseInt(value.charAt(i)) * weight[i];
  2335. }
  2336. sum = sum % 10;
  2337. return (sum == value.substr(9, 1));
  2338. },
  2339. // Finally, consider it as a VAT number
  2340. vat = function(value) {
  2341. var sum = 0,
  2342. weight = [4, 3, 2, 7, 6, 5, 4, 3, 2];
  2343. for (var i = 0; i < 9; i++) {
  2344. sum += parseInt(value.charAt(i)) * weight[i];
  2345. }
  2346. sum = 11 - sum % 11;
  2347. if (sum == 10) {
  2348. return false;
  2349. }
  2350. if (sum == 11) {
  2351. sum = 0;
  2352. }
  2353. return (sum == value.substr(9, 1));
  2354. };
  2355. return (egn(value) || pnf(value) || vat(value));
  2356. }
  2357. return false;
  2358. },
  2359. /**
  2360. * Validate Swiss VAT number
  2361. *
  2362. * @param {String} value VAT number
  2363. * @returns {Boolean}
  2364. */
  2365. _ch: function(value) {
  2366. if (!/^CHE[0-9]{9}(MWST)?$/.test(value)) {
  2367. return false;
  2368. }
  2369. value = value.substr(3);
  2370. var sum = 0,
  2371. weight = [5, 4, 3, 2, 7, 6, 5, 4];
  2372. for (var i = 0; i < 8; i++) {
  2373. sum += parseInt(value.charAt(i), 10) * weight[i];
  2374. }
  2375. sum = 11 - sum % 11;
  2376. if (sum == 10) {
  2377. return false;
  2378. }
  2379. if (sum == 11) {
  2380. sum = 0;
  2381. }
  2382. return (sum == value.substr(8, 1));
  2383. },
  2384. /**
  2385. * Validate Cypriot VAT number
  2386. * Examples:
  2387. * - Valid: CY10259033P
  2388. * - Invalid: CY10259033Z
  2389. *
  2390. * @param {String} value VAT number
  2391. * @returns {Boolean}
  2392. */
  2393. _cy: function(value) {
  2394. if (!/^CY[0-5|9]{1}[0-9]{7}[A-Z]{1}$/.test(value)) {
  2395. return false;
  2396. }
  2397. value = value.substr(2);
  2398. // Do not allow to start with "12"
  2399. if (value.substr(0, 2) == '12') {
  2400. return false;
  2401. }
  2402. // Extract the next digit and multiply by the counter.
  2403. var sum = 0,
  2404. translation = {
  2405. '0': 1, '1': 0, '2': 5, '3': 7, '4': 9,
  2406. '5': 13, '6': 15, '7': 17, '8': 19, '9': 21
  2407. };
  2408. for (var i = 0; i < 8; i++) {
  2409. var temp = parseInt(value.charAt(i), 10);
  2410. if (i % 2 == 0) {
  2411. temp = translation[temp + ''];
  2412. }
  2413. sum += temp;
  2414. }
  2415. sum = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[sum % 26];
  2416. return (sum == value.substr(8, 1));
  2417. },
  2418. /**
  2419. * Validate Czech Republic VAT number
  2420. * Can be:
  2421. * i) Legal entities (8 digit numbers)
  2422. * ii) Individuals with a RC (the 9 or 10 digit Czech birth number)
  2423. * iii) Individuals without a RC (9 digit numbers beginning with 6)
  2424. *
  2425. * Examples:
  2426. * - Valid: i) CZ25123891; ii) CZ7103192745, CZ991231123; iii) CZ640903926
  2427. * - Invalid: i) CZ25123890; ii) CZ1103492745, CZ590312123
  2428. *
  2429. * @param {String} value VAT number
  2430. * @returns {Boolean}
  2431. */
  2432. _cz: function(value) {
  2433. if (!/^CZ[0-9]{8,10}$/.test(value)) {
  2434. return false;
  2435. }
  2436. value = value.substr(2);
  2437. var sum = 0, weight = [], i = 0;
  2438. if (value.length == 8) {
  2439. // Do not allow to start with '9'
  2440. if (value.charAt(0) + '' == '9') {
  2441. return false;
  2442. }
  2443. sum = 0;
  2444. for (var i = 0; i < 7; i++) {
  2445. sum += parseInt(value.charAt(i), 10) * (8 - i);
  2446. }
  2447. sum = 11 - sum % 11;
  2448. if (sum == 10) {
  2449. sum = 0;
  2450. }
  2451. if (sum == 11) {
  2452. sum = 1;
  2453. }
  2454. return (sum == value.substr(7, 1));
  2455. } else if (value.length == 9 && (value.charAt(0) + '' == '6')) {
  2456. sum = 0;
  2457. // Skip the first (which is 6)
  2458. for (var i = 0; i < 7; i++) {
  2459. sum += parseInt(value.charAt(i + 1), 10) * (8 - i);
  2460. }
  2461. sum = 11 - sum % 11;
  2462. if (sum == 10) {
  2463. sum = 0;
  2464. }
  2465. if (sum == 11) {
  2466. sum = 1;
  2467. }
  2468. sum = [8, 7, 6, 5, 4, 3, 2, 1, 0, 9, 10][sum - 1];
  2469. return (sum == value.substr(8, 1));
  2470. } else if (value.length == 9 || value.length == 10) {
  2471. // Validate Czech birth number (Rodné číslo), which is also national identifier
  2472. var rc = function(value) {
  2473. var year = 1900 + parseInt(value.substr(0, 2)),
  2474. month = parseInt(value.substr(2, 2)) % 50 % 20,
  2475. day = parseInt(value.substr(4, 2));
  2476. if (value.length == 9) {
  2477. if (year >= 1980) {
  2478. year -= 100;
  2479. }
  2480. if (year > 1953) {
  2481. return false;
  2482. }
  2483. } else if (year < 1954) {
  2484. year += 100;
  2485. }
  2486. try {
  2487. var d = new Date(year, month, day);
  2488. } catch (ex) {
  2489. return false;
  2490. }
  2491. // Check that the birth date is not in the future
  2492. if (value.length == 10) {
  2493. var check = parseInt(value.substr(0, 9), 10) % 11;
  2494. if (year < 1985) {
  2495. check = check % 10;
  2496. }
  2497. return (check == value.substr(9, 1));
  2498. }
  2499. return true;
  2500. };
  2501. }
  2502. return false;
  2503. },
  2504. /**
  2505. * Validate German VAT number
  2506. * Examples:
  2507. * - Valid: DE136695976
  2508. * - Invalid: DE136695978
  2509. *
  2510. * @param {String} value VAT number
  2511. * @returns {Boolean}
  2512. */
  2513. _de: function(value) {
  2514. if (!/^DE[0-9]{9}$/.test(value)) {
  2515. return false;
  2516. }
  2517. value = value.substr(2);
  2518. var product = 10,
  2519. sum = 0;
  2520. for (var i = 0; i < 8; i++) {
  2521. sum = (parseInt(value.charAt(i), 10) + product) % 10;
  2522. if (sum == 0) {
  2523. sum = 10;
  2524. }
  2525. product = (sum * 2) % 11;
  2526. }
  2527. var checkDigit = (11 - product == 10) ? 0 : (11 - product);
  2528. return (checkDigit == value.substr(8, 1));
  2529. },
  2530. /**
  2531. * Validate Danish VAT number
  2532. * Example:
  2533. * - Valid: DK13585628
  2534. * - Invalid: DK13585627
  2535. *
  2536. * @param {String} value VAT number
  2537. * @returns {Boolean}
  2538. */
  2539. _dk: function(value) {
  2540. if (!/^DK[0-9]{8}$/.test(value)) {
  2541. return false;
  2542. }
  2543. value = value.substr(2);
  2544. var sum = 0,
  2545. weight = [2, 7, 6, 5, 4, 3, 2, 1];
  2546. for (var i = 0; i < 8; i++) {
  2547. sum += parseInt(value.charAt(i), 10) * weight[i];
  2548. }
  2549. return (sum % 11 == 0);
  2550. },
  2551. /**
  2552. * Validate Estonian VAT number
  2553. * Examples:
  2554. * - Valid: EE100931558, EE100594102
  2555. * - Invalid: EE100594103
  2556. *
  2557. * @param {String} value VAT number
  2558. * @returns {Boolean}
  2559. */
  2560. _ee: function(value) {
  2561. if (!/^EE[0-9]{9}$/.test(value)) {
  2562. return false;
  2563. }
  2564. value = value.substr(2);
  2565. var sum = 0,
  2566. weight = [3, 7, 1, 3, 7, 1, 3, 7, 1];
  2567. for (var i = 0; i < 9; i++) {
  2568. sum += parseInt(value.charAt(i)) * weight[i];
  2569. }
  2570. return (sum % 10 == 0);
  2571. },
  2572. /**
  2573. * Validate Spanish VAT number (NIF - Número de Identificación Fiscal)
  2574. * Can be:
  2575. * i) DNI (Documento nacional de identidad), for Spaniards
  2576. * ii) NIE (Número de Identificación de Extranjeros), for foreigners
  2577. * iii) CIF (Certificado de Identificación Fiscal), for legal entities and others
  2578. *
  2579. * Examples:
  2580. * - Valid: i) ES54362315K; ii) ESX2482300W, ESX5253868R; iii) ESM1234567L, ESJ99216582, ESB58378431, ESB64717838
  2581. * - Invalid: i) ES54362315Z; ii) ESX2482300A; iii) ESJ99216583
  2582. *
  2583. * @param {String} value VAT number
  2584. * @returns {Boolean}
  2585. */
  2586. _es: function(value) {
  2587. if (!/^ES[0-9A-Z][0-9]{7}[0-9A-Z]$/.test(value)) {
  2588. return false;
  2589. }
  2590. value = value.substr(2);
  2591. var dni = function(value) {
  2592. var check = parseInt(value.substr(0, 8), 10);
  2593. check = 'TRWAGMYFPDXBNJZSQVHLCKE'[check % 23];
  2594. return (check == value.substr(8, 1));
  2595. },
  2596. nie = function(value) {
  2597. var check = ['XYZ'.indexOf(value.charAt(0)), value.substr(1)].join('');
  2598. check = parseInt(check, 10);
  2599. check = 'TRWAGMYFPDXBNJZSQVHLCKE'[check % 23];
  2600. return (check == value.substr(8, 1));
  2601. },
  2602. cif = function(value) {
  2603. var first = value.charAt(0), check;
  2604. if ('KLM'.indexOf(first) != -1) {
  2605. // K: Spanish younger than 14 year old
  2606. // L: Spanish living outside Spain without DNI
  2607. // M: Granted the tax to foreigners who have no NIE
  2608. check = parseInt(value.substr(1, 8), 10);
  2609. check = 'TRWAGMYFPDXBNJZSQVHLCKE'[check % 23];
  2610. return (check == value.substr(8, 1));
  2611. } else if ('ABCDEFGHJNPQRSUVW'.indexOf(first) != -1) {
  2612. var sum = 0,
  2613. weight = [2, 1, 2, 1, 2, 1, 2],
  2614. temp = 0;
  2615. for (var i = 0; i < 7; i++) {
  2616. temp = parseInt(value.charAt(i + 1)) * weight[i];
  2617. if (temp > 9) {
  2618. temp = Math.floor(temp / 10) + temp % 10;
  2619. }
  2620. sum += temp;
  2621. }
  2622. sum = 10 - sum % 10;
  2623. return (sum == value.substr(8, 1) || 'JABCDEFGHI'[sum] == value.substr(8, 1));
  2624. }
  2625. return false;
  2626. };
  2627. var first = value.charAt(0);
  2628. if (/^[0-9]$/.test(first)) {
  2629. return dni(value);
  2630. } else if (/^[XYZ]$/.test(first)) {
  2631. return nie(value);
  2632. } else {
  2633. return cif(value);
  2634. }
  2635. },
  2636. /**
  2637. * Validate Finnish VAT number
  2638. * Examples:
  2639. * - Valid: FI20774740
  2640. * - Invalid: FI20774741
  2641. *
  2642. * @param {String} value VAT number
  2643. * @returns {Boolean}
  2644. */
  2645. _fi: function(value) {
  2646. if (!/^FI[0-9]{8}$/.test(value)) {
  2647. return false;
  2648. }
  2649. value = value.substr(2);
  2650. var sum = 0,
  2651. weight = [7, 9, 10, 5, 8, 4, 2, 1];
  2652. for (var i = 0; i < 8; i++) {
  2653. sum += parseInt(value.charAt(i)) * weight[i];
  2654. }
  2655. return (sum % 11 == 0);
  2656. },
  2657. /**
  2658. * Validate French VAT number (TVA - taxe sur la valeur ajoutée)
  2659. * It's constructed by a SIREN number, prefixed by two characters.
  2660. *
  2661. * Examples:
  2662. * - Valid: FR40303265045, FR23334175221, FRK7399859412, FR4Z123456782
  2663. * - Invalid: FR84323140391
  2664. *
  2665. * @param {String} value VAT number
  2666. * @returns {Boolean}
  2667. */
  2668. _fr: function(value) {
  2669. if (!/^FR[0-9A-Z]{2}[0-9]{9}$/.test(value)) {
  2670. return false;
  2671. }
  2672. value = value.substr(2);
  2673. if (!$.fn.bootstrapValidator.helpers.luhn(value.substr(2))) {
  2674. return false;
  2675. }
  2676. if (/^[0-9]{2}$/.test(value.substr(0, 2))) {
  2677. // First two characters are digits
  2678. return value.substr(0, 2) == (parseInt(value.substr(2) + '12', 10) % 97);
  2679. } else {
  2680. // The first characters cann't be O and I
  2681. var alphabet = '0123456789ABCDEFGHJKLMNPQRSTUVWXYZ',
  2682. check;
  2683. // First one is digit
  2684. if (/^[0-9]{1}$/.test(value.charAt(0))) {
  2685. check = alphabet.indexOf(value.charAt(0)) * 24 + alphabet.indexOf(value.charAt(1)) - 10;
  2686. } else {
  2687. check = alphabet.indexOf(value.charAt(0)) * 34 + alphabet.indexOf(value.charAt(1)) - 100;
  2688. }
  2689. return ((parseInt(value.substr(2), 10) + 1 + Math.floor(check / 11)) % 11) == (check % 11);
  2690. }
  2691. },
  2692. /**
  2693. * Validate United Kingdom VAT number
  2694. * Example:
  2695. * - Valid: GB980780684
  2696. * - Invalid: GB802311781
  2697. *
  2698. * @param {String} value VAT number
  2699. * @returns {Boolean}
  2700. */
  2701. _gb: function(value) {
  2702. if (!/^GB[0-9]{9}$/.test(value) // Standard
  2703. && !/^GB[0-9]{12}$/.test(value) // Branches
  2704. && !/^GBGD[0-9]{3}$/.test(value) // Government department
  2705. && !/^GBHA[0-9]{3}$/.test(value) // Health authority
  2706. && !/^GB(GD|HA)8888[0-9]{5}$/.test(value))
  2707. {
  2708. return false;
  2709. }
  2710. value = value.substr(2);
  2711. var length = value.length;
  2712. if (length == 5) {
  2713. var firstTwo = value.substr(0, 2),
  2714. lastThree = parseInt(value.substr(2));
  2715. return ('GD' == firstTwo && lastThree < 500) || ('HA' == firstTwo && lastThree >= 500);
  2716. } else if (length == 11 && ('GD8888' == value.substr(0, 6) || 'HA8888' == value.substr(0, 6))) {
  2717. if (('GD' == value.substr(0, 2) && parseInt(value.substr(6, 3)) >= 500)
  2718. || ('HA' == value.substr(0, 2) && parseInt(value.substr(6, 3)) < 500))
  2719. {
  2720. return false;
  2721. }
  2722. return (parseInt(value.substr(6, 3)) % 97 == parseInt(value.substr(9, 2)));
  2723. } else if (length == 9 || length == 12) {
  2724. var sum = 0,
  2725. weight = [8, 7, 6, 5, 4, 3, 2, 10, 1];
  2726. for (var i = 0; i < 9; i++) {
  2727. sum += parseInt(value.charAt(i)) * weight[i];
  2728. }
  2729. sum = sum % 97;
  2730. if (parseInt(value.substr(0, 3)) >= 100) {
  2731. return (sum == 0 || sum == 42 || sum == 55);
  2732. } else {
  2733. return (sum == 0);
  2734. }
  2735. }
  2736. return true;
  2737. },
  2738. /**
  2739. * Validate Greek VAT number
  2740. * Examples:
  2741. * - Valid: GR023456780, EL094259216
  2742. * - Invalid: EL123456781
  2743. *
  2744. * @param {String} value VAT number
  2745. * @returns {Boolean}
  2746. */
  2747. _gr: function(value) {
  2748. if (!/^GR[0-9]{9}$/.test(value)) {
  2749. return false;
  2750. }
  2751. value = value.substr(2);
  2752. if (value.length == 8) {
  2753. value = '0' + value;
  2754. }
  2755. var sum = 0,
  2756. weight = [256, 128, 64, 32, 16, 8, 4, 2];
  2757. for (var i = 0; i < 8; i++) {
  2758. sum += parseInt(value.charAt(i)) * weight[i];
  2759. }
  2760. sum = (sum % 11) % 10;
  2761. return (sum == value.substr(8, 1));
  2762. },
  2763. // EL is traditionally prefix of Greek VAT numbers
  2764. _el: function(value) {
  2765. if (!/^EL[0-9]{9}$/.test(value)) {
  2766. return false;
  2767. }
  2768. value = 'GR' + value.substr(2);
  2769. return this._gr(value);
  2770. },
  2771. /**
  2772. * Validate Hungarian VAT number
  2773. * Examples:
  2774. * - Valid: HU12892312
  2775. * - Invalid: HU12892313
  2776. *
  2777. * @param {String} value VAT number
  2778. * @returns {Boolean}
  2779. */
  2780. _hu: function(value) {
  2781. if (!/^HU[0-9]{8}$/.test(value)) {
  2782. return false;
  2783. }
  2784. value = value.substr(2);
  2785. var sum = 0,
  2786. weight = [9, 7, 3, 1, 9, 7, 3, 1];
  2787. for (var i = 0; i < 8; i++) {
  2788. sum += parseInt(value.charAt(i)) * weight[i];
  2789. }
  2790. return (sum % 10 == 0);
  2791. },
  2792. /**
  2793. * Validate Croatian VAT number
  2794. * Examples:
  2795. * - Valid: HR33392005961
  2796. * - Invalid: HR33392005962
  2797. *
  2798. * @param {String} value VAT number
  2799. * @returns {Boolean}
  2800. */
  2801. _hr: function(value) {
  2802. if (!/^HR[0-9]{11}$/.test(value)) {
  2803. return false;
  2804. }
  2805. value = value.substr(2);
  2806. var sum = 10,
  2807. temp = 0;
  2808. for (var i = 0; i < 10; i++) {
  2809. temp = (parseInt(value.charAt(i), 10) + sum) % 10;
  2810. if (temp == 0) {
  2811. temp = 10;
  2812. }
  2813. sum = (temp * 2) % 11;
  2814. }
  2815. sum += parseInt(value.substr(10, 1), 10);
  2816. return (sum % 10 == 1);
  2817. },
  2818. /**
  2819. * Validate Irish VAT number
  2820. * Examples:
  2821. * - Valid: IE6433435F, IE6433435OA, IE8D79739I
  2822. * - Invalid: IE8D79738J
  2823. *
  2824. * @param {String} value VAT number
  2825. * @returns {Boolean}
  2826. */
  2827. _ie: function(value) {
  2828. if (!/^IE[0-9]{1}[0-9A-Z\*\+]{1}[0-9]{5}[A-Z]{1,2}$/.test(value)) {
  2829. return false;
  2830. }
  2831. value = value.substr(2);
  2832. var getCheckDigit = function(value) {
  2833. while (value.length < 7) {
  2834. value = '0' + value;
  2835. }
  2836. var alphabet = 'WABCDEFGHIJKLMNOPQRSTUV',
  2837. sum = 0;
  2838. for (var i = 0; i < 7; i++) {
  2839. sum += parseInt(value.charAt(i)) * (8 - i);
  2840. }
  2841. sum += 9 * alphabet.indexOf(value.substr(7));
  2842. return alphabet[sum % 23];
  2843. };
  2844. // The first 7 characters are digits
  2845. if (/^[0-9]+$/.test(value.substr(0, 7))) {
  2846. // New system
  2847. return value.charAt(7) == getCheckDigit(value.substr(0, 7) + value.substr(8) + '');
  2848. } else if ('ABCDEFGHIJKLMNOPQRSTUVWXYZ+*'.indexOf(value.charAt(1)) != -1) {
  2849. // Old system
  2850. return value.charAt(7) == getCheckDigit(value.substr(2, 5) + value.substr(0, 1) + '');
  2851. }
  2852. return true;
  2853. },
  2854. /**
  2855. * Validate Italian VAT number, which consists of 11 digits.
  2856. * - First 7 digits are a company identifier
  2857. * - Next 3 are the province of residence
  2858. * - The last one is a check digit
  2859. *
  2860. * Examples:
  2861. * - Valid: IT00743110157
  2862. * - Invalid: IT00743110158
  2863. *
  2864. * @param {String} value VAT number
  2865. * @returns {Boolean}
  2866. */
  2867. _it: function(value) {
  2868. if (!/^IT[0-9]{11}$/.test(value)) {
  2869. return false;
  2870. }
  2871. value = value.substr(2);
  2872. if (parseInt(value.substr(0, 7)) == 0) {
  2873. return false;
  2874. }
  2875. var lastThree = parseInt(value.substr(7, 3));
  2876. if ((lastThree < 1) || (lastThree > 201) && lastThree != 999 && lastThree != 888) {
  2877. return false;
  2878. }
  2879. return $.fn.bootstrapValidator.helpers.luhn(value);
  2880. },
  2881. /**
  2882. * Validate Lithuanian VAT number
  2883. * It can be:
  2884. * - 9 digits, for legal entities
  2885. * - 12 digits, for temporarily registered taxpayers
  2886. *
  2887. * Examples:
  2888. * - Valid: LT119511515, LT100001919017, LT100004801610
  2889. * - Invalid: LT100001919018
  2890. *
  2891. * @param {String} value VAT number
  2892. * @returns {Boolean}
  2893. */
  2894. _lt: function(value) {
  2895. if (!/^LT([0-9]{7}1[0-9]{1}|[0-9]{10}1[0-9]{1})$/.test(value)) {
  2896. return false;
  2897. }
  2898. value = value.substr(2);
  2899. var length = value.length,
  2900. sum = 0;
  2901. for (var i = 0; i < length - 1; i++) {
  2902. sum += parseInt(value.charAt(i)) * (1 + i % 9);
  2903. }
  2904. var check = sum % 11;
  2905. if (check == 10) {
  2906. sum = 0;
  2907. for (var i = 0; i < length - 1; i++) {
  2908. sum += parseInt(value.charAt(i)) * (1 + (i + 2) % 9);
  2909. }
  2910. }
  2911. check = check % 11 % 10;
  2912. return (check == value.charAt(length - 1));
  2913. },
  2914. /**
  2915. * Validate Luxembourg VAT number
  2916. * Examples:
  2917. * - Valid: LU15027442
  2918. * - Invalid: LU15027443
  2919. *
  2920. * @param {String} value VAT number
  2921. * @returns {Boolean}
  2922. */
  2923. _lu: function(value) {
  2924. if (!/^LU[0-9]{8}$/.test(value)) {
  2925. return false;
  2926. }
  2927. value = value.substr(2);
  2928. return (value.substr(0, 6) % 89 == value.substr(6, 2));
  2929. },
  2930. /**
  2931. * Validate Latvian VAT number
  2932. * Examples:
  2933. * - Valid: LV40003521600, LV16117519997
  2934. * - Invalid: LV40003521601, LV16137519997
  2935. *
  2936. * @param {String} value VAT number
  2937. * @returns {Boolean}
  2938. */
  2939. _lv: function(value) {
  2940. if (!/^LV[0-9]{11}$/.test(value)) {
  2941. return false;
  2942. }
  2943. value = value.substr(2);
  2944. var first = parseInt(value.charAt(0)),
  2945. sum = 0,
  2946. weight = [],
  2947. i = 0,
  2948. length = value.length;
  2949. if (first > 3) {
  2950. // Legal entity
  2951. sum = 0;
  2952. weight = [9, 1, 4, 8, 3, 10, 2, 5, 7, 6, 1];
  2953. for (i = 0; i < length; i++) {
  2954. sum += parseInt(value.charAt(i)) * weight[i];
  2955. }
  2956. sum = sum % 11;
  2957. return (sum == 3);
  2958. } else {
  2959. // Check birth date
  2960. var day = parseInt(value.substr(0, 2)),
  2961. month = parseInt(value.substr(2, 2)),
  2962. year = parseInt(value.substr(4, 2));
  2963. year = year + 1800 + parseInt(value.charAt(6)) * 100;
  2964. try {
  2965. var d = new Date(year, month, day);
  2966. } catch (ex) {
  2967. return false;
  2968. }
  2969. // Check personal code
  2970. sum = 0;
  2971. weight = [10, 5, 8, 4, 2, 1, 6, 3, 7, 9];
  2972. for (i = 0; i < length - 1; i++) {
  2973. sum += parseInt(value.charAt(i)) * weight[i];
  2974. }
  2975. sum = (sum + 1) % 11 % 10;
  2976. return (sum == value.charAt(length - 1));
  2977. }
  2978. return true;
  2979. },
  2980. /**
  2981. * Validate Maltese VAT number
  2982. * Examples:
  2983. * - Valid: MT11679112
  2984. * - Invalid: MT11679113
  2985. *
  2986. * @param {String} value VAT number
  2987. * @returns {Boolean}
  2988. */
  2989. _mt: function(value) {
  2990. if (!/^MT[0-9]{8}$/.test(value)) {
  2991. return false;
  2992. }
  2993. value = value.substr(2);
  2994. var sum = 0,
  2995. weight = [3, 4, 6, 7, 8, 9, 10, 1];
  2996. for (var i = 0; i < 8; i++) {
  2997. sum += parseInt(value.charAt(i)) * weight[i];
  2998. }
  2999. return (sum % 37 == 0);
  3000. },
  3001. /**
  3002. * Validate Dutch VAT number
  3003. * Examples:
  3004. * - Valid: NL004495445B01
  3005. * - Invalid: NL123456789B90
  3006. *
  3007. * @param {String} value VAT number
  3008. * @returns {Boolean}
  3009. */
  3010. _nl: function(value) {
  3011. if (!/^NL[0-9]{9}B[0-9]{2}$/.test(value)) {
  3012. return false;
  3013. }
  3014. value = value.substr(2);
  3015. var sum = 0,
  3016. weight = [9, 8, 7, 6, 5, 4, 3, 2];
  3017. for (var i = 0; i < 8; i++) {
  3018. sum += parseInt(value.charAt(i)) * weight[i];
  3019. }
  3020. sum = sum % 11;
  3021. if (sum > 9) {
  3022. sum = 0;
  3023. }
  3024. return (sum == value.substr(8, 1));
  3025. },
  3026. /**
  3027. * Validate Norwegian VAT number
  3028. *
  3029. * @see http://www.brreg.no/english/coordination/number.html
  3030. * @param {String} value VAT number
  3031. * @returns {Boolean}
  3032. */
  3033. _no: function(value) {
  3034. if (!/^NO[0-9]{9}$/.test(value)) {
  3035. return false;
  3036. }
  3037. value = value.substr(2);
  3038. var sum = 0,
  3039. weight = [3, 2, 7, 6, 5, 4, 3, 2];
  3040. for (var i = 0; i < 8; i++) {
  3041. sum += parseInt(value.charAt(i)) * weight[i];
  3042. }
  3043. sum = 11 - sum % 11;
  3044. if (sum == 11) {
  3045. sum = 0;
  3046. }
  3047. return (sum == value.substr(8, 1));
  3048. },
  3049. /**
  3050. * Validate Polish VAT number
  3051. * Examples:
  3052. * - Valid: PL8567346215
  3053. * - Invalid: PL8567346216
  3054. *
  3055. * @param {String} value VAT number
  3056. * @returns {Boolean}
  3057. */
  3058. _pl: function(value) {
  3059. if (!/^PL[0-9]{10}$/.test(value)) {
  3060. return false;
  3061. }
  3062. value = value.substr(2);
  3063. var sum = 0,
  3064. weight = [6, 5, 7, 2, 3, 4, 5, 6, 7, -1];
  3065. for (var i = 0; i < 10; i++) {
  3066. sum += parseInt(value.charAt(i)) * weight[i];
  3067. }
  3068. return (sum % 11 == 0);
  3069. },
  3070. /**
  3071. * Validate Portuguese VAT number
  3072. * Examples:
  3073. * - Valid: PT501964843
  3074. * - Invalid: PT501964842
  3075. *
  3076. * @param {String} value VAT number
  3077. * @returns {Boolean}
  3078. */
  3079. _pt: function(value) {
  3080. if (!/^PT[0-9]{9}$/.test(value)) {
  3081. return false;
  3082. }
  3083. value = value.substr(2);
  3084. var sum = 0,
  3085. weight = [9, 8, 7, 6, 5, 4, 3, 2];
  3086. for (var i = 0; i < 8; i++) {
  3087. sum += parseInt(value.charAt(i)) * weight[i];
  3088. }
  3089. sum = 11 - sum % 11;
  3090. if (sum > 9) {
  3091. sum = 0;
  3092. }
  3093. return (sum == value.substr(8, 1));
  3094. },
  3095. /**
  3096. * Validate Romanian VAT number
  3097. * Examples:
  3098. * - Valid: RO18547290
  3099. * - Invalid: RO18547291
  3100. *
  3101. * @param {String} value VAT number
  3102. * @returns {Boolean}
  3103. */
  3104. _ro: function(value) {
  3105. if (!/^RO[1-9][0-9]{1,9}$/.test(value)) {
  3106. return false;
  3107. }
  3108. value = value.substr(2);
  3109. var length = value.length,
  3110. weight = [7, 5, 3, 2, 1, 7, 5, 3, 2].slice(10 - length),
  3111. sum = 0;
  3112. for (var i = 0; i < length - 1; i++) {
  3113. sum += parseInt(value.charAt(i)) * weight[i];
  3114. }
  3115. sum = (10 * sum) % 11 % 10;
  3116. return (sum == value.substr(length - 1, 1));
  3117. },
  3118. /**
  3119. * Validate Serbian VAT number
  3120. *
  3121. * @param {String} value VAT number
  3122. * @returns {Boolean}
  3123. */
  3124. _rs: function(value) {
  3125. if (!/^RS[0-9]{9}$/.test(value)) {
  3126. return false;
  3127. }
  3128. value = value.substr(2);
  3129. var sum = 10,
  3130. temp = 0;
  3131. for (var i = 0; i < 8; i++) {
  3132. temp = (parseInt(value.charAt(i)) + sum) % 10;
  3133. if (temp == 0) {
  3134. temp = 10;
  3135. }
  3136. sum = (2 * temp) % 11;
  3137. }
  3138. return ((sum + parseInt(value.substr(8, 1))) % 10 == 1);
  3139. },
  3140. /**
  3141. * Validate Swedish VAT number
  3142. * Examples:
  3143. * - Valid: SE123456789701
  3144. * - Invalid: SE123456789101
  3145. *
  3146. * @param {String} value VAT number
  3147. * @returns {Boolean}
  3148. */
  3149. _se: function(value) {
  3150. if (!/^SE[0-9]{10}01$/.test(value)) {
  3151. return false;
  3152. }
  3153. value = value.substr(2, 10);
  3154. return $.fn.bootstrapValidator.helpers.luhn(value);
  3155. },
  3156. /**
  3157. * Validate Slovenian VAT number
  3158. * Examples:
  3159. * - Valid: SI50223054
  3160. * - Invalid: SI50223055
  3161. *
  3162. * @param {String} value VAT number
  3163. * @returns {Boolean}
  3164. */
  3165. _si: function(value) {
  3166. if (!/^SI[0-9]{8}$/.test(value)) {
  3167. return false;
  3168. }
  3169. value = value.substr(2);
  3170. var sum = 0,
  3171. weight = [8, 7, 6, 5, 4, 3, 2];
  3172. for (var i = 0; i < 7; i++) {
  3173. sum += parseInt(value.charAt(i)) * weight[i];
  3174. }
  3175. sum = 11 - sum % 11;
  3176. if (sum == 10) {
  3177. sum = 0;
  3178. }
  3179. return (sum == value.substr(7, 1));
  3180. },
  3181. /**
  3182. * Validate Slovak VAT number
  3183. * Examples:
  3184. * - Valid: SK2022749619
  3185. * - Invalid: SK2022749618
  3186. *
  3187. * @param {String} value VAT number
  3188. * @returns {Boolean}
  3189. */
  3190. _sk: function(value) {
  3191. if (!/^SK[1-9][0-9][(2-4)|(6-9)][0-9]{7}$/.test(value)) {
  3192. return false;
  3193. }
  3194. value = value.substr(2);
  3195. return (value % 11 == 0);
  3196. }
  3197. };
  3198. }(window.jQuery));
  3199. ;(function($) {
  3200. $.fn.bootstrapValidator.validators.vin = {
  3201. /**
  3202. * Validate an US VIN (Vehicle Identification Number)
  3203. *
  3204. * @param {BootstrapValidator} validator The validator plugin instance
  3205. * @param {jQuery} $field Field element
  3206. * @param {Object} options Consist of key:
  3207. * - message: The invalid message
  3208. * @returns {Boolean}
  3209. */
  3210. validate: function(validator, $field, options) {
  3211. var value = $field.val();
  3212. if (value == '') {
  3213. return true;
  3214. }
  3215. // Don't accept I, O, Q characters
  3216. if (!/^[a-hj-npr-z0-9]{8}[0-9xX][a-hj-npr-z0-9]{8}$/i.test(value)) {
  3217. return false;
  3218. }
  3219. value = value.toUpperCase();
  3220. var chars = {
  3221. A: 1, B: 2, C: 3, D: 4, E: 5, F: 6, G: 7, H: 8,
  3222. J: 1, K: 2, L: 3, M: 4, N: 5, P: 7, R: 9,
  3223. S: 2, T: 3, U: 4, V: 5, W: 6, X: 7, Y: 8, Z: 9,
  3224. '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '0': 0
  3225. },
  3226. weights = [8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2],
  3227. sum = 0,
  3228. length = value.length;
  3229. for (var i = 0; i < length; i++) {
  3230. sum += chars[value.charAt(i) + ''] * weights[i];
  3231. }
  3232. var reminder = sum % 11;
  3233. if (reminder == 10) {
  3234. reminder = 'X';
  3235. }
  3236. return reminder == value.charAt(8);
  3237. }
  3238. };
  3239. }(window.jQuery));
  3240. ;(function($) {
  3241. $.fn.bootstrapValidator.validators.zipCode = {
  3242. html5Attributes: {
  3243. message: 'message',
  3244. country: 'country'
  3245. },
  3246. /**
  3247. * Return true if and only if the input value is a valid country zip code
  3248. *
  3249. * @param {BootstrapValidator} validator The validator plugin instance
  3250. * @param {jQuery} $field Field element
  3251. * @param {Object} options Consist of key:
  3252. * - message: The invalid message
  3253. * - country: The ISO 3166 country code
  3254. *
  3255. * Currently it supports the following countries:
  3256. * - US (United State)
  3257. * - CA (Canada)
  3258. * - DK (Denmark)
  3259. * - SE (Sweden)
  3260. * @returns {Boolean}
  3261. */
  3262. validate: function(validator, $field, options) {
  3263. var value = $field.val();
  3264. if (value == '' || !options.country) {
  3265. return true;
  3266. }
  3267. var country = (options.country || 'US').toUpperCase();
  3268. switch (country) {
  3269. case 'CA':
  3270. 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);
  3271. case 'DK':
  3272. return /^(DK(-|\s)?)?\d{4}$/i.test(value);
  3273. case 'SE':
  3274. return /^(S-)?\d{3}\s?\d{2}$/i.test(value);
  3275. case 'US':
  3276. default:
  3277. return /^\d{4,5}([\-]\d{4})?$/.test(value);
  3278. }
  3279. }
  3280. };
  3281. }(window.jQuery));