bootstrapValidator.js 147 KB

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