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 !

Code: Select all

Title = "Valid card number or not"
CardNumber = InputBox("Please type your card number ",Title,"222100")
if IsValidcard(CardNumber) = True Then
MsgBox CardNumber & " The Card number is valid !",64,Title
else
MsgBox CardNumber & " The Card number is not valid !",16,Title
End if

Function IsValidCard(Num)
Set RegularExpressionObject = New RegExp
With RegularExpressionObject
.Pattern = "^(2221[0-9][0-9])|(2[2-7]20[0-9][0-9])$"
.IgnoreCase = False
If .Test(Num)= True then
IsValidCard = True
end if
End With
End Function

User avatar
jvierra
Posts: 13387
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:

Code: Select all

Function IsValidCard(Num)
IsValidCard = False
Set regEx = New RegExp
regEx.Pattern = "^\d{6}$"

If regEx.Test(Num) then
x = CDbl(num)
if x >= 222100 AND x <= 272099 Then
IsValidCard = True
Else
'not in range
End If
Else
'not 6 digits
end if

End Function

Locked