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
Last visit: Tue Apr 26, 2016 9:02 am

Validate a credit card number

Post by hackoo »

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. CardNumber = InputBox("Please type your card number ",Title,"222100")
  3. if IsValidcard(CardNumber) = True Then
  4.     MsgBox CardNumber & " The Card number is valid !",64,Title
  5. else
  6.     MsgBox CardNumber & " The Card number is not valid !",16,Title
  7. End if
  8.  
  9. Function IsValidCard(Num)
  10.     Set RegularExpressionObject = New RegExp
  11.     With RegularExpressionObject
  12.         .Pattern = "^(2221[0-9][0-9])|(2[2-7]20[0-9][0-9])$"
  13.         .IgnoreCase = False
  14.         If .Test(Num)= True then
  15.             IsValidCard = True
  16.         end if
  17.     End With
  18. End Function

jvierra
Posts: 14825
Last visit: Thu Jun 10, 2021 11:20 pm
Answers: 10
Has voted: 3 times
Been upvoted: 10 times

Re: Validate a credit card number

Post by jvierra »

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

Locked