bootstrapValidator.js 168 KB

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