bootstrapValidator.js 188 KB

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