<% option explicit dim nbvisite, cartetype, numerocarte, prefix, prefix1, ok, msg, totalcarte, increment, i, numero, arret arret=false ok=true nbvisite = request("nbvisite")+1 cartetype = request("cartetype") numerocarte = request("numerocarte") prefix=mid(numerocarte,1,2) 'si il doit y avoir un nbre maxi de tentives, le compteur est nbvisite 'if nbvisite>maxi then ' ...action trop de tentatives ' arret=true 'else 'on continue ' ... suite du traitement et ajouter un end if à la fin du code asp 'test si le type de carte a ete indique 'if (nbvisite>1) then 'on ne fait pas le test si c'est le premier affichage ' if cartetype="" then ' msg="

Erreur 0a :
Veuillez indiquer votre type de carte
" ' ok=false ' end if ' if numerocarte="" and ok=true then ' msg="

Erreur 0b :
Veuillez indiquer votre numero de carte
" ' ok=false ' end if 'end if 'Verifie le préfixe en fonction de la carte 'if ok=true then 'on a un numero et un type de carte ' test visa if cartetype=1 then ' test la longueur VISA if len(numerocarte)>16 or len(numerocarte)<13 then msg="

Erreur 1 :
Votre numéro de carte n'est pas valable
" ok=false end if ' test le préfixe VISA if ok= true then prefix=mid(numerocarte,1,1) if prefix<>4 then msg="

Erreur 2 :
Votre numéro de carte n'est pas valable
" ok=false end if end if end if ' fin test visa ' test mastercard if cartetype=2 then ' test de longueur MASTERCARD if len(numerocarte)<>16 then msg="

Erreur 3 :
Votre numéro de carte n'est pas valable
" ok=false end if ' test le préfixe MASTERCARD if ok=true then if prefix<>51 and prefix<>52 and prefix<>53 and prefix<>54 and prefix<>55 then msg="

Erreur 4 :
Votre numéro de carte n'est pas valable
" ok=false end if end if end if ' fin test master card 'test Amex if cartetype=3 then 'test la longueur du numero if len(numerocarte)<>15 then msg="

Erreur 5 :
Votre numéro de carte n'est pas valable
" ok=false end if ' test le préfixe Amex if ok=true then if prefix<>34 and prefix<>37 then msg="

Erreur 6 :
Votre numéro de carte n'est pas valable
" ok=false end if end if end if ' fin test amex 'test Diners Club / Carte Blanche if cartetype=4 then 'test la longueur du numero if len(numerocarte)<>14 then msg="

Erreur 7 :
Votre numéro de carte n'est pas valable
" ok=false end if ' test le préfixe Diners Club / Carte Blanche if ok=true then prefix1=mid(numerocarte,1,3) if prefix<>36 and prefix<>38 then msg="

Erreur 8 :
Votre numéro de carte n'est pas valable
" ok=false else if prefix1>305 or prefix1<300 then msg="

Erreur 2 :
Votre numéro de carte n'est pas valable
" ok=false end if end if end if end if ' fin test Diners Club / Carte Blanche 'test Discover if cartetype=5 then 'test la longueur du numero if len(numerocarte)<>16 then msg="

Erreur 9 :
Votre numéro de carte n'est pas valable
" ok=false end if ' test le préfixe Discover if ok=true then prefix=mid(numerocarte,1,4) if prefix<>6011 then msg="

Erreur 10 :
Votre numéro de carte n'est pas valable
" ok=false end if end if end if ' fin test Discover 'test JCB version1 if cartetype=6 then if len(numerocarte)=16 then ' test le préfixe JCB version1 if ok=true then prefix=mid(numerocarte,1,1) if prefix<>3 then msg="

Erreur 11 :
Votre numéro de carte n'est pas valable
" ok=false end if end if ' fin test JCB version1 else 'test JCB version2 if len(numerocarte)=15 then 'test le préfixe JCB version2 if ok=true then prefix=mid(numerocarte,1,4) if prefix<>2131 and prefix<>1800 then msg="

Erreur 12 :
Votre numéro de carte n'est pas valable
" ok=false end if end if else 'on a un type JCB mais pas le bon nombre de chiffres msg="

Erreur 13 :
Votre numéro de carte n'est pas valable
" ok=false end if 'fin du test JCB Version2 end if 'fin des tests JCB versions 1&2 end if 'end if 'fin on a un numero et un type de carte 'Algorithme de Luhn if isnumeric(numerocarte) and ok=true then ' si c'est bien un numéro if ok=true then ' si les tests précédents ont été effectués avec succès 'debut de l'algo totalcarte=0 increment=0 for i=len(numerocarte) to 1 step -1 numero=cint(mid(numerocarte,i,1)) increment=increment+1 'if increment mod 2 = 0 then ' nombre paire if increment mod 2 <> 0 then totalcarte=totalcarte+numero ' ajout normal dans la somme else ' nombre impaire numero=numero*2 if numero>=10 then totalcarte=totalcarte+1+numero-10 'nombre>10 donc on additionne les 2 chiffres du nombre plus la somme else totalcarte=totalcarte+numero end if end if next ' division du total if (totalcarte mod 10)<>0 then msg="

Erreur 20 :
Votre numéro de carte n'est pas valable
" ok=false end if end if else if msg="" and request("nbvisite")>1 then msg="

Erreur 21 :
Votre numéro de carte n'est pas valable
" ok=false end if end if ' Fin de vérification carte bancaire si ok=true à ce niveau la carte est ok 'end if 'dans le cas d'un nombre limite de tentatives %> Algorithme de Luhn : Javascript <% 'dans le cas d'un nombre limite de tentatives 'if arret=true then %>

Validation de Numéro de carte
Algorithme de Luhn - Version ASP

 

Votre numéro de carte (UNIQUEMENT les chiffres)
checked<%end if%>>VISA
checked<%end if%>>Mastercard
checked<%end if%>>AMEX
checked<%end if%>>Diners Club / Carte Blanche
checked<%end if%>>Discover
checked<%end if%>>JCB


<% if OK<>true AND nbvisite>1 then response.write("
Tentative effectuées : " & nbvisite-1 & "") response.write(msg) end if if ok=true and nbvisite>1 then response.write("
Numero de carte correct") end if %>

 

<% 'dans le cas d'un nombre limite de tentatives 'else ' response.write("

Vous avez fait trop de tentatives infructueuses... Désolé") 'end if %>