Solved

Mod Overflow

Posted on 2001-09-12
11
1,348 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 52

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month9 days, 15 hours left to enroll

623 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