bootstrapValidator.js 161 KB

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