Solved

Mod Overflow

Posted on 2001-09-12
11
1,340 Views
Last Modified: 2012-06-27
Hi

I am trying to do a Mod 11 check on a 16 digit number to validate credit/debit cards but I get an overflow error. I guess this is because the number is too big.  How can I get round this problem?

Thanks

Dave
0
Comment
Question by:daveamour
  • 3
  • 2
  • 2
  • +3
11 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 25 total points
ID: 6476410
http://radu70.multimania.com/bestVB.htm#How%20to%20check%20if%20the%20credit%20card%20is%20valid?

Gives an example of how to validate a credit card. This may be useful to you.
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6476417
Hi daveamour,

Just for information:

Mod function will only return a value as treat as a Long data type. The Long data type is (2^31)-1 , 2147483648 - 1.

So any result as bigger than 2147483648 will cause an error overflow.

<listening..>
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 6476722
Ths is code I use to validate credit cards and may help you:

Function CheckCreditCard(CCNum As String, CCType As String) As Boolean
Dim ValidCC As Boolean
Dim CCNumLen As Integer
Dim CheckDigit As Integer
Dim Pos As Integer
Dim Digit As Integer
Dim CCSum As Integer

ValidCC = False
CCNumLen = Len(CCNum)
Select Case CCType
   Case "American Express"
      ValidCC = (CCNum Like "37*" Or CCNum Like "34*") And CCNumLen = 15
   
   Case "CarteBlanche"
      ValidCC = (CCNum Like "94*" Or CCNum Like "95*" Or CCNum Like "389") And CCNumLen = 14
   
   Case "CarteBlanche International"
      ValidCC = (CCNum Like "9*") And CCNumLen = 10

   Case "Diners Club"
      ValidCC = (CCNum Like "30*" Or CCNum Like "36*" Or CCNum Like "38[1-8]*") And CCNumLen = 14
     
   Case "Discover (Novus)"
      ValidCC = (CCNum Like "60110*" Or CCNum Like "6011[2-4]*" Or CCNum Like "60119*") And CCNumLen = 16
     
   Case "Encryption"
      ValidCC = (CCNumLen >= 16 And CCNumLen <= 19)
   
   Case "JCB"
      ValidCC = (Left$(CCNum, 4) >= "3528" And Left$(CCNum, 4) <= "3589") And CCNumLen = 16
   
   Case "MasterCard"
      ValidCC = (CCNum Like "5[1-5]*") And CCNumLen = 16
     
   Case "Optima"
      ValidCC = (CCNum Like "37?7*") And CCNumLen = 15

   Case "Switch"
      ValidCC = (CCNum Like "49*" Or CCNum Like "56*" Or CCNum Like "6*") And (CCNumLen = 16 Or CCNumLen = 18 Or CCNumLen = 19)
   
   Case "Visa"
      ValidCC = (CCNum Like "4*") And (CCNumLen = 13 Or CCNumLen = 16)

   Case Else
      ValidCC = False

End Select

If ValidCC Then
   For Pos = 1 To CCNumLen - 1
      Digit = CInt(Mid$(CCNum, CCNumLen - Pos, 1)) * 2 ^ (Pos Mod 2)
      CCSum = CCSum + Digit \ 10 + Digit Mod 10
   Next
   ValidCC = (10 - (CCSum Mod 10)) = CInt(Right$(CCNum, 1))
End If
CheckCreditCard = ValidCC

End Function


0
 
LVL 19

Author Comment

by:daveamour
ID: 6476744
Ok this is all great guys and I am evaluating these ideas now.  One problem with this though is that of testing my code.  I have found some test numbers at http://www.cpromptguru.com/JSOFUserManFrame.htm?http://www.cpromptguru.com/JSOFUserMan_AutomatedCCProcessing.htm but I also need other test numbers particularly for switch, solo and delta.  Do any of you guys know of any test numbers for these?

Cheers

Dave

0
 
LVL 8

Expert Comment

by:DennisBorg
ID: 6477073
This happens when the expressions used with the MOD operator are assumed to be a certain numeric type, but the results are of a different numeric type which induces an overflow error.

For example, if the expressions used are Integers (implied or explicit), but the result is of type Long, this can generate an Overflow Error.

Try the following in the Debug Window:

   Print 32767 * 2

You get an Overflow Error because both numbers are Integers, but the result is Long. But, if you do the following:

   Print CLng(32767) * 2

You do not get an error since one of the terms is a Long, therefore the result is assumed to be a Long.


The same thing can occur with the MOD operator. You may need to convert one or more terms of your expression into a Long Integer.


Barring that, perhaps you could give us an example which causes this overflow error, and the current values and data type of each of the involved variables.


-Dennis Borg
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 6477139
Dennis,

Actually in this case and as ryancys previously pointed out MOD simply does not handle any numeric expression larger than 2,147,483,647.

In any case if all that is required is a check on the credit card digits than there is no need to do a MOD on the whole number.  The algorithm (I believe it is called the Luhn algorithm) that I posted is quite simple.

But your point is well made and I know what you are referring to; we have had this issue since the days MS Basic.

Anthony
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6477247
The old way to do modulus, before mod was readily available, was:

replace:

x= a mod 11

with:

x = a - int(a/11)*11

Make sure that a is defined as a long to handle the 16 digit numbers.

0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6477255
...or if you can't use 'long', try 'double' which will (hopefully) not lose anything significant in the division.
0
 
LVL 8

Expert Comment

by:DennisBorg
ID: 6477717
Anthony:

>Actually in this case and as ryancys previously pointed
>out MOD simply does not handle any numeric expression
>larger than 2,147,483,647.

Somehow I had missed ryancys' comments; I *knew* there was something else, for there was another post sometime back which had asked this same question, and I could not quite recall exactly what the problem was. But it's all coming back to me now.

The manual method of arriving with the MOD (such as rspahitz suggested) was the solution to that problem.


Thank you for the clarification, Anthony.

-Dennis Borg
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6477756
Yes.  There are known limitations of each of the mathematical functions used in VB.  It used to be that things like Sine, Cosine, etc. only offered single precision (or maybe that was the pre-VB days of QuickBASIC).  Eventually they were upgraded since the processors actually encouraged such improvements.

With the advent of .net, and it's 64-bit processing, there's a good chance that the mod function will support the larger numbers that you are requesting...but I suppose you don't want to wait around for that.

One solution is to create an "anticipatory" function so you can more easily upgrade in the future.  To do this, instead of using mod, use a user-defined function such as this:

CreditCardType = MyOwnModulus(CreditCardNumber, 11)

...
Private Function MyOwnModulus(BaseNumber as double, Modulus as integer) as long
   MyOwnModulus = BaseNumber - int(BaseNumber / Modulus) * Modulus)
'   'The following is for future use when the 'mod' function supports larger numbers
'   MyOwnModulus = BaseNumber mod Modulus
end Function

In the future, when implementing .net, change this routine to:

Private Function MyOwnModulus(BaseNumber as double, Modulus as integer) as long
   MyOwnModulus = BaseNumber mod Modulus
end Function

--
If you find a lot of these "workarounds" you may want to drop them into a separate module or even your own class module.
0
 
LVL 19

Author Comment

by:daveamour
ID: 6478857
Thanks for all your input guys.  All very useful but Tim's was the first that I used which solved my problem.

Thanks again

Dave
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now