bootstrapValidator.js 135 KB

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