bootstrapValidator.js 165 KB

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