bootstrapValidator.js 161 KB

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