bootstrapValidator.js 196 KB

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