bootstrapValidator.js 192 KB

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