bootstrapValidator.js 147 KB

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