bootstrapValidator.js 196 KB

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