bootstrapValidator.js 142 KB

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