bootstrapValidator.js 173 KB

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