bootstrapValidator.js 197 KB

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