bootstrapValidator.js 196 KB

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