bootstrapValidator.js 136 KB

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