bootstrapValidator.js 203 KB

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