验证6种信用卡卡号的代码

验证6种信用卡卡号的代码

可以验证的信用卡种类有:

V - Visa
M -
MasterCard          
A -
American Express          
D -
Diners Club
Discover - Discover
JCB - JCB

以下为ASP代码:

Function ValidateCreditCard(ByRef pStrNumber, ByRef pStrType)
 ValidateCreditCard = FALSE ' Initialize negative results
 
 ' Clean Credit Card Number (Removes dashes and spaces)
 pStrNumber = ParseDigits(pStrNumber)  
 
 ' Validate number with LUHN Formula
 If Not LUHN(pStrNumber) Then Exit Function
 
 ' Apply rules based on type of card
 Select Case pStrType
  Case "A"
   Select Case Left(pStrNumber, 2)
    Case "34", "37"
     ' Do Nothing
    Case Else
     Exit Function
   End Select
   If Not Len(pStrNumber) = 15 Then Exit Function
  Case "D"
   Select Case Left(pStrNumber, 2)
    Case "36" , "38"
     ' Do Nothing
    Case "30"
     Select Case Left(pStrNumber, 3)
      Case "300", "301", "302", "303", "304", "305"
       ' Do Nothing
      Case Else
       Exit Function
     End Select
    Case Else
     Exit Function
   End Select
   If Not Len(pStrNumber) = 14 Then Exit Function
  Case "Discover"
   If Not Left(pStrNumber, 4) = "6011" Then Exit Function
   If Not Len(pStrNumber) = 16 Then Exit Function
  Case "JCB"
   If Left(pStrNumber, 1) = "3" And Len(pStrNumber) = 16 Then
    ' Do Nothing
   ElseIf Left(pStrNumber, 14) = "2131" And Len(pStrNumber) = 15 Then
    ' Do Nothing
   ElseIf Left(pStrNumber, 14) = "1800" And Len(pStrNumber) = 15 Then
    ' Do Nothing
   Else
    Exit Function
   End If
  Case "M"
   Select Case Left(pStrNumber, 2)
    Case "51", "52", "53", "54", "55"
     ' Do Nothing
    Case Else
     Exit Function
   End Select
   If Not Len(pStrNumber) = 16 Then Exit Function
  Case "V"
   If Not Left(pStrNumber, 1) = "4" Then Exit Function
   If Not (Len(pStrNumber) = 13 or Len(pStrNumber) = 16) Then Exit Function
  Case Else
   ' Unknown Card Type
   Exit Function
 End Select
 
 ' We got this far so the number passed all the rules!
 ValidateCreditCard = True
 
End Function
' ------------------------------------------------------------------------------
Function LUHN(ByRef pStrDigits)
 
 Dim lLngMaxPosition
 Dim lLngPosition
 Dim lLngSum    ' Sum of all positions
 Dim lLngDigit   ' Current digit in specified position
 
 ' Initialize
 lLngMaxPosition = Len(pStrDigits)
 lLngSum = 0
 
 ' Read from right to left
 For lLngPosition = lLngMaxPosition To 1 Step -1
  
  ' If we are working with an even digit (from the right)
  If (lLngMaxPosition - lLngPosition) Mod 2 = 0 Then
  
   lLngSum = lLngSum + CInt(Mid(pStrDigits, lLngPosition, 1))
  
  Else
  
   ' Double the digit
   lLngDigit = CInt(Mid(pStrDigits, lLngPosition, 1)) * 2
   
   ' shortcut adding sum of digits
   If lLngDigit > 9 Then lLngDigit = lLngDigit - 9
   
   lLngSum = lLngSum + lLngDigit
   
  End If
 Next

 ' A mod 10 check must not return any remainders
 LUHN = lLngSum Mod 10 = 0

End Function
' ------------------------------------------------------------------------------
Function ParseDigits(ByRef pStrData)
 
 ' Strip all the numbers from a string
 ' (cleans up dashes and spaces)
 
 Dim lLngMaxPosition
 Dim lLngPosition
 
 lLngMaxPosition = Len(pStrData)
 
 For lLngPosition = 1 To lLngMaxPosition
  If IsNumeric(Mid(pStrData, lLngPosition, 1)) Then
   ParseDigits = ParseDigits & Mid(pStrData, lLngPosition, 1)
  End If
 Next

End Function


 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值