<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Validazione carte di credito in asp - Luhn formula]]></title><description><![CDATA[<p dir="auto">I numeri di carta di credito vengono generati in maniera tale da soddisfare la Luhn formula.</p>
<p dir="auto">La Luhn formula (o formula di Luhn) si applica moltiplicando per 2 tutte le cifre in posizioni dispari e successivamente sommando singolarmente tutte le cife così ottenute. Se la loro somma è multiplo di 10 allora il numero è valido.</p>
<p dir="auto">È di conseguenza utile utilizzare questo tipo di controllo per una prima validazione della carta di credito utilizzata dall'utente. Questo tipo di controllo non è ovviamente completamente esaustivo. Ci sono altre variabili da prendere in considerazione, ad esempio la lunghezza del numero, che varia da tipo a tipo, la scadenza, la lunghezza del codice di controllo.</p>
<p dir="auto">Il controllo sulla liquidità e reale esistenza della carta non è possibile effettuarlo da codice senza il supporto di una banca.</p>
<p dir="auto">Ci sono numeri che soddisferebbero la luhn formula pur essendo palesemente inadeguati, tipo 4111111111111111 per Visa.</p>
<p dir="auto">Ad esempio per il numero 5632198745456126 la luhn formula si applica così:<br />
1+0+6+6+2+2+9+1+6+7+8+5+8+5+1+2+1+4+6 = 80</p>
<p dir="auto">(PS: vi giuro che ho scritto questo numero a caso eppure soddisfa la luhn formula... le probabilità erano quasi nulle, che mago! Provate voi <img src="https://dev.connect.gt/assets/plugins/nodebb-plugin-emoji/emoji/customizations/73c86915-581f-433d-82e4-a63f221ee3e6-smile.gif?v=12b9555a9a1" class="not-responsive emoji emoji-customizations emoji--1" style="height:1.4em;width:auto;vertical-align:middle" title=":))" alt="" /></p>
<p dir="auto">Ecco una funzione che applica la luhn formula:</p>
<pre><code>&lt;%
Function ValidCcNumber(ccnumber)
    ccnumber = cleanccnum(ccnumber)
    If ccnumber = "" then
        validccnumber = false
    Else
        iseven = false
        digits = ""
        For i = len(ccnumber) to 1 step -1
            If iseven then
                digits = digits &amp; cint(mid(ccnumber, i, 1))*2
            Else
                digits = digits &amp; cint(mid(ccnumber,i,1))
            End if
            iseven= (not iseven)
        Next
        checksum = 0
        For i = 1 to len(digits) Step 1
            checksum = checksum + cint(mid(digits, i, 1))
        Next
        validccnumber = ((checksum mod 10)=0)
    End if
End function
%&gt;
</code></pre>
<p dir="auto">A questo controllo ne vanno affiancati altri che ora non ho tempo di commentare approfonditamente ma credo che si commentino da soli:</p>
<p dir="auto">Controllo delle prime cifre del numero:</p>
<pre><code>Function ValidCcType(ccnumber,cctype)
    ValidCcType = true
    If cctype = "Visa" then
        If CInt(cleanccnum(Left(ccnumber,1))) &lt;&gt; 4 then ValidCcType = false
    Elseif cctype = "MasterCard" then
        If not (CInt(cleanccnum(Left(ccnumber,2))) &gt; 50 and CInt(cleanccnum(Left(ccnumber,2))) &lt; 56) then ValidCcType = false
    
    Elseif cctype = "AmericanExpress" then
        If not CInt(cleanccnum(Left(ccnumber,2))) = 34 and not CInt(cleanccnum(Left(ccnumber,2))) = 37 then ValidCcType = false
    Elseif cctype = "DinersClubCarteBlanche" then
        If not (CInt(cleanccnum(Left(ccnumber,3))) &gt; 299 and CInt(cleanccnum(Left(ccnumber,3))) &lt; 306) and not CInt(cleanccnum(Left(ccnumber,2))) = 37 and not CInt(cleanccnum(Left(ccnumber,2))) = 36 and not CInt(cleanccnum(Left(ccnumber,2))) = 38 then ValidCcType = false
    Elseif cctype = "Discover" then
        If not CInt(cleanccnum(Left(ccnumber,2))) = 6011 then ValidCcType = false
    End if
End function
</code></pre>
<p dir="auto">Controllo sulla lunghezza del numero:</p>
<pre><code>Function ValidCcLength(ccnumber,cctype)
    ValidCcLength = true
    If cctype = "Visa" then
        If Len(ccnumber) &lt;&gt; 16 and Len(ccnumber) &lt;&gt; 13 then ValidCcLength = false
    Elseif cctype = "MasterCard" then
        If Len(ccnumber) &lt;&gt; 16 then ValidCcLength = false
    Elseif cctype = "AmericanExpress" then
        If Len(ccnumber) &lt;&gt; 15 then ValidCcLength = false
    Elseif cctype = "DinersClubCarteBlanche" then
        If Len(ccnumber) &lt;&gt; 14 then ValidCcLength = false
    Elseif cctype = "Discover" then
        If Len(ccnumber) &lt;&gt; 16 then ValidCcLength = false
    End if
End function
</code></pre>
<p dir="auto">Controllo sulla lunghezza del codice di controllo:</p>
<pre><code>Function ValidCvcLength(cvc_code,cctype)
    ValidCvcLength = true
    If cctype = "Visa" then
        If Len(cvc_code) &lt;&gt; 3 then ValidCvcLength = false
    Elseif cctype = "MasterCard" then
        If Len(cvc_code) &lt;&gt; 3 then ValidCvcLength = false
    Elseif cctype = "AmericanExpress" then
        If Len(cvc_code) &lt;&gt; 4 then ValidCvcLength = false
    Elseif cctype = "DinersClubCarteBlanche" then
        If Len(cvc_code) &lt;&gt; 3 then ValidCvcLength = false
    Elseif cctype = "Discover" then
        If Len(cvc_code) &lt;&gt; 3 then ValidCvcLength = false
    End if
End function
</code></pre>
<p dir="auto">Quest'ultima funzione viene richiamata dalle altre per ripulire il numero da caratteri non numerici tipo spazi e trattini:</p>
<pre><code>Function cleanccnum(ccnumber)
    For i = 1 to len(ccnumber)
        If isnumeric (mid(ccnumber, i, 1)) then
            cleanccnum = cleanccnum &amp; mid(ccnumber, i,1)
        End if
    Next
End function
</code></pre>
<p dir="auto">Spero che questa pillola risulti utile... postate tranquillamente le vostre eventuali domande.</p>
<p dir="auto">Ciao <img src="https://dev.connect.gt/assets/plugins/nodebb-plugin-emoji/emoji/android/1f642.png?v=12b9555a9a1" class="not-responsive emoji emoji-android emoji--slightly_smiling_face" style="height:1.4em;width:auto;vertical-align:middle" title=":)" alt="🙂" /></p>
]]></description><link>https://dev.connect.gt/topic/16084/validazione-carte-di-credito-in-asp-luhn-formula</link><generator>RSS for Node</generator><lastBuildDate>Thu, 26 Nov 2026 12:34:06 GMT</lastBuildDate><atom:link href="https://dev.connect.gt/topic/16084.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 05 Sep 2006 10:36:54 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Validazione carte di credito in asp - Luhn formula on Thu, 07 Mar 2013 20:03:45 GMT]]></title><description><![CDATA[<p dir="auto">ciao madai,</p>
<p dir="auto">scusa la mia ignoranza.... come faccio ad applicare queste funzioni?</p>
]]></description><link>https://dev.connect.gt/post/364762</link><guid isPermaLink="true">https://dev.connect.gt/post/364762</guid><dc:creator><![CDATA[guidoc]]></dc:creator><pubDate>Thu, 07 Mar 2013 20:03:45 GMT</pubDate></item></channel></rss>