bootstrapValidator.js 148 KB

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