bootstrapValidator.js 195 KB

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