bootstrapValidator.js 197 KB

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