Page 1 of 1

Validate a credit card number

Posted: Tue Apr 26, 2016 5:46 am
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

Re: Validate a credit card number

Posted: Tue Apr 26, 2016 7:55 am
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