demoshop

demo, trying to be the best_

加強版?其實就只是加了驗證他是什麼類型的卡而已,不過資訊還不足,demo只知道Visa和MasterCard的規則而已,希望廣大的網友可以幫我提供更多的資訊來源

C#

string creditCardNumber = "1234567891234563"; //這裡請自行帶入你要驗證的號碼
string creditCardType="";
if (creditCardNumber.Length < 16)
{
    Page.ClientScript.RegisterStartupScript(this.GetType(), "dd", "alert('錯誤數字只有" + creditCardNumber.Length + "碼');", true);
}
else
{
    int[] Int = new int[16];
    int x = 0;
    int num = 0;
    int sun = 0;
    #region 就是加這段而已
     if (Convert.ToInt32(creditCardNumber.Substring(0, 2)) > 50 && Convert.ToInt32(creditCardNumber.Substring(0, 2)) < 57)
        creditCardType = "MasterCard";
    else if(Convert.ToInt32(creditCardNumber.Substring(0, 1)) == 4)
        creditCardType = "Visa";
    #endregion
    
    for (x = 0; x <= 15; x++)
    {
        num = Convert.ToInt16(creditCardNumber.Substring(x, 1));
        //偶數乘1奇數乘2
        if ((x + 1) % 2 != 0)
        {
            Int[x] = num * 2;
        }
        else
        {
            Int[x] = num;
        }
    }
    for (x = 0; x <= 15; x++)
    {
        if (Int[x] > 9)
        {
            Int[x] = (Int[x] % 10) + 1;
        }
        sun += Int[x];
    }
    if (sun % 10 == 0)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "ddd", "alert('正確的" + creditCardType + "信用卡');", true);
    }
    else
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "dd", "alert('錯誤');", true);
    }
} 

VB.NET

Dim creditCardNumber As String = "1234567891234563" '這裡請自行帶入你要驗證的號碼
Dim creditCardType As String = ""
If creditCardNumber.Length < 16 Then
    Page.ClientScript.RegisterStartupScript(Me.[GetType](), "dd", "alert('錯誤數字只有" & creditCardNumber.Length & "碼');", True)
Else
    Dim Int As Integer() = New Integer(15) {}
    Dim x As Integer = 0
    Dim num As Integer = 0
    Dim sun As Integer = 0
 
    If Val(creditCardNumber.Substring(0, 2)) > 50 AndAlso Val(creditCardNumber.Substring(0, 2)) < 57 Then
        creditCardType = "MasterCard"
    ElseIf Convert.ToInt32(creditCardNumber.Substring(0, 1)) = 4 Then
        creditCardType = "Visa"
    End If
 
 
    For x = 0 To 15
        num = Convert.ToInt16(creditCardNumber.Substring(x, 1))
        '偶數乘1奇數乘2
        If (x + 1) Mod 2 <> 0 Then
            Int(x) = num * 2
        Else
            Int(x) = num
        End If
    Next
    For x = 0 To 15
        If Int(x) > 9 Then
            Int(x) = (Int(x) Mod 10) + 1
        End If
        sun += Int(x)
    Next
    If sun Mod 10 = 0 Then
        Page.ClientScript.RegisterStartupScript(Me.[GetType](), "ddd", "alert('正確的" & creditCardType & "信用卡');", True)
    Else
        Page.ClientScript.RegisterStartupScript(Me.[GetType](), "dd", "alert('錯誤');", True)
    End If
End If 

回應討論