bootstrapValidator.js 168 KB

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