Validate a credit card number

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
hackoo
Posts: 103
Joined: Sat Jan 22, 2011 12:27 am

Validate a credit card number

Post by hackoo » Tue Apr 26, 2016 5:46 am

I have this script to validate a credit card number with this condition:
The number must be within this range [222100-272099]
I started a little bit but I still dry in the regexp :roll:
Thank you for your help !
  1. Title = "Valid card number or not"
  2.  
  3. CardNumber = InputBox("Please type your card number ",Title,"222100")
  4.  
  5. if IsValidcard(CardNumber) = True Then
  6.  
  7.     MsgBox CardNumber & " The Card number is valid !",64,Title
  8.  
  9. else
  10.  
  11.     MsgBox CardNumber & " The Card number is not valid !",16,Title
  12.  
  13. End if
  14.  
  15.  
  16.  
  17. Function IsValidCard(Num)
  18.  
  19.     Set RegularExpressionObject = New RegExp
  20.  
  21.     With RegularExpressionObject
  22.  
  23.         .Pattern = "^(2221[0-9][0-9])|(2[2-7]20[0-9][0-9])$"
  24.  
  25.         .IgnoreCase = False
  26.  
  27.         If .Test(Num)= True then
  28.  
  29.             IsValidCard = True
  30.  
  31.         end if
  32.  
  33.     End With
  34.  
  35. End Function

User avatar
jvierra
Posts: 12563
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Validate a credit card number

Post by jvierra » Tue Apr 26, 2016 7:55 am

When working with numbers this may be easier:
  1. Function IsValidCard(Num)
  2.  
  3.     IsValidCard = False
  4.  
  5.     Set regEx = New RegExp
  6.  
  7.     regEx.Pattern = "^\d{6}$"
  8.  
  9.  
  10.  
  11.     If regEx.Test(Num) then
  12.  
  13.         x = CDbl(num)
  14.  
  15.         if x >= 222100 AND x <= 272099 Then
  16.  
  17.             IsValidCard = True
  18.  
  19.         Else
  20.  
  21.             'not in range
  22.  
  23.         End If
  24.  
  25.     Else
  26.  
  27.         'not 6 digits
  28.  
  29.     end if
  30.  
  31.  
  32.  
  33. End Function

Locked