bootstrapValidator.js 162 KB

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