Results 1 to 3 of 3

Thread: credit card validation

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2004
    Posts
    47

    Exclamation credit card validation

    Hi,

    I have tried on doing the credit card validation however, it does not work out. What actually went wrong?

    VB Code:
    1. VB Script

    Function ValidateCreditCard(ByVal cardName As String, _
    ByVal cardNo As String, ByVal cardMonth As Integer, _
    ByVal cardYear As Integer, ByVal PaymentAmt As String)

    'check to ensure that the number is numeric
    If Not IsNumeric(cardNo) Then
    Session("errorMsg") = "Please enter a valid number for credit card."
    Return 1 'If return 1, invalid credit card number

    Else
    ValidateCCNumber(cardNo)
    Session("errorMsg") = "Please enter a valid number for credit card."
    End If

    Session("errorMsg") = ""
    Return 0

    End Function

    Function ValidateCCNumber(ByVal cardNo)
    Dim intCCType
    intCCType = GetCCType(cardNo)
    Select Case intCCType
    Case 1 'Visa
    'Validates Length Based on CardType If
    ' true actually validates number
    If (Len(cardNo) = 13 Or Len(cardNo) = 16) And ValidateMod10(cardNo) = True Then
    ValidateCCNumber = intCCType 'Valid
    Else
    ValidateCCNumber = intCCType * -1 'Invalid
    End If
    Case 2, 3 'Mastercard / Discover
    If Len(cardNo) = 16 And ValidateMod10(cardNo) = True Then
    ValidateCCNumber = intCCType 'Valid
    Else
    ValidateCCNumber = intCCType * -1 'Invalid
    End If
    Case 4 'American Express
    If Len(cardNo) = 15 And ValidateMod10(cardNo) = True Then
    ValidateCCNumber = intCCType 'Valid
    Else
    ValidateCCNumber = intCCType * -1 'Invalid
    End If
    Case 5 'Diners Club
    If Len(cardNo) = 14 And ValidateMod10(cardNo) = True Then
    ValidateCCNumber = intCCType 'Valid
    Else
    ValidateCCNumber = intCCType * -1 'Invalid
    End If
    Case Else
    ValidateCCNumber = 0
    Return 3
    End Select
    End Function


    Function GetCCType(ByVal cardNo)
    If Left(cardNo, 1) = 4 Then
    'Visa
    GetCCType = 1
    ElseIf Left(cardNo, 2) >= 51 And Left(cardNo, 2) <= 55 Then
    'Mastercard
    GetCCType = 2
    ElseIf Left(cardNo, 4) = 6011 Then
    'Discover
    GetCCType = 3
    ElseIf Left(cardNo, 2) = 34 Or Left(cardNo, 2) = 37 Then
    'American Express
    GetCCType = 4
    ElseIf (Left(cardNo, 3) >= 300 And Left(cardNo, 3) <= 305) Or _
    Left(cardNo, 2) = 36 Or Left(cardNo, 2) = 38 Then
    'Diners Club -- I don't know if this works coz i dun haf one to check it wif
    GetCCType = 5
    Else
    GetCCType = 0
    End If
    End Function

    'This is the actual Mod10
    Private Function ValidateMod10(ByVal cardNo)
    Dim intCounter, intTotal
    Dim strTotal

    intTotal = 0
    For intCounter = 1 To Len(cardNo)
    If (intCounter Mod 2) <> 0 Then
    strTotal = strTotal & Mid(cardNo, intCounter, 1) * 2
    Else
    strTotal = strTotal & Mid(cardNo, intCounter, 1)
    End If
    Next

    For intCounter = 1 To Len(strTotal)
    intTotal = intTotal + Mid(strTotal, intCounter, 1)
    Next

    If (intTotal Mod 10) Then
    ValidateMod10 = False
    Else
    ValidateMod10 = True
    End If
    End Function

  2. #2
    Lively Member
    Join Date
    Nov 2000
    Location
    Pakistan
    Posts
    80
    Hi Try using these functions for validating credit card.


    <%
    function isNumber(val)
    if isNumeric(val.value) then
    isNumber = true
    else
    msgbox "This value must be numeric."
    val.select
    isNumber = false
    end if
    end function

    function isLength(val)
    if len(val.value)>0 then
    isLength = true
    else
    msgbox "This value can't be empty."
    val.select
    isLength = false
    end if
    end function

    function isProperdate(val)
    if isDate(trim(val.value)) then
    isProperdate = true
    else
    msgbox "Please enter a valid date."
    val.select
    isProperdate = false
    end if
    end function

    function isCCnumber(val)
    if isLength(val) then
    ccnumber = false
    val.value = trim(replace(val.value," ",""))
    if isNumeric(val.value) then
    ccnum = val.value
    for i = len(ccnum) to 2 step -2
    total = total + cint(mid(ccnum,i,1))
    tmp = cstr((mid(ccnum,i-1,1))*2)
    total = total + cint(left(tmp, 1))
    if len(tmp)>1 then
    total = total + cint(right(tmp,1))
    end if
    next
    if len(ccnum) mod 2 = 1 then
    total = total + cint(left(ccnum,1))
    end if
    if total mod 10 = 0 then
    ccnumber = true
    end if
    end if
    if ccnumber then
    isCCnumber = true
    else
    msgbox "Please use a valid credit card number."
    val.select
    isCCnumber = false
    end if
    end if
    end function

    function isCCdate(val)
    if isLength(val) then
    ccdate = false
    if len(val.value)>=3 and len(val.value)<=5 then
    if instr(val.value,"/")>0 then
    tCC = split(val.value,"/")
    if isNumeric(tCC(0)) and isNumeric(tCC(1)) then
    mn = cint(tCC(0))
    yr = cint(tCC(1))
    currYear = cint(right(year(date()),2))
    if mn>0 and mn<13 then
    if yr = currYear then
    if mn >= month(date()) then
    ccdate = true
    end if
    else
    if yr<currYear+4 then
    ccdate = true
    end if
    end if
    end if
    end if
    end if
    end if
    if ccdate then
    isCCdate = true
    else
    msgbox "Please enter a valid expiry date."
    val.select
    isCCdate = false
    end if
    end if
    end function


    function isPositive(val)
    if isNumber(val) then
    if val.value>=0 then
    isPositive = true
    else
    msgbox "Please enter a positive number."
    val.select
    isPositive = false
    end if
    end if
    end function

    function isNegative(val)
    if isNumber(val) then
    if val.value<0 then
    isNegative = true
    else
    msgbox "Please enter a negative number."
    val.select
    isNegative = false
    end if
    end if
    end function

    function isAlpha(val)
    if isLength(val) then
    isNot=" !@#$^*()_+=-'`~\|]}[{;:/?.,<>&%"
    invalid = false
    if instr(val.value,chr(34))>0 then
    invalid=true
    else
    for i = 1 to len(val.value)
    for x = 1 to len(isNotAlpha)
    if mid(val.value,i,1)=mid(isNot,x,1) then
    invalid = true
    end if
    next
    next
    end if
    if not invalid then
    isAlpha = true
    else
    msgbox "Please use alphanumeric characters."
    val.select
    isAlpha = false
    end if
    end if
    end function
    %>



    Adeel Ahmed

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2004
    Posts
    47
    Thank You!! My saviour!

    Can you explain to me how this code below works as i need to confirm how it works. Thank You very much!

    coding:

    function isCCnumber(val)
    if isLength(val) then
    ccnumber = false
    val.value = trim(replace(val.value," ",""))
    if isNumeric(val.value) then
    ccnum = val.value
    for i = len(ccnum) to 2 step -2
    total = total + cint(mid(ccnum,i,1))
    tmp = cstr((mid(ccnum,i-1,1))*2)
    total = total + cint(left(tmp, 1))
    if len(tmp)>1 then
    total = total + cint(right(tmp,1))
    end if
    next
    if len(ccnum) mod 2 = 1 then
    total = total + cint(left(ccnum,1))
    end if
    if total mod 10 = 0 then
    ccnumber = true
    end if
    end if
    if ccnumber then
    isCCnumber = true
    else
    msgbox "Please use a valid credit card number."
    val.select
    isCCnumber = false
    end if
    end if
    end function


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width