Check a Credit card number : String « Data Type « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » Data Type » String 
2.26.19.Check a Credit card number
' Quote from
'Visual Basic 2005 Cookbook Solutions for VB 2005 Programmers
'by Tim Patrick (Author), John Craig (Author)
'# Publisher: O'Reilly Media, Inc. (September 212006)
'# Language: English
'# ISBN-100596101775
'# ISBN-13978-0596101770


Public Class Tester
    Public Shared Sub Main
    
Console.WriteLine(VerifyCreditCard("123123123123123123"))
    End Sub
    Private Shared Function VerifyCreditCard(ByVal cardNumber As StringAs Boolean
        ' ----- Given a card number, make sure it is valid. This method
        '       uses the Luhn algorithm to verify the number. This routine
        '       assumes that cardNumber contains only digits.
        Dim counter As Integer
        Dim digitTotal As Integer
        Dim holdValue As Integer
        Dim checkDigit As Integer
        Dim calcDigit As Integer
        Dim useCard As String

        ' ----- Perform some initial checks.
        useCard = Trim(cardNumber)
        If (IsNumeric(useCard= FalseThen Return False

        ' ----- Separate out the last digit, the check digit. For cards with
        '       an odd number of digits, prepend with a zero.
        If ((Len(useCardMod 2<> 0Then useCard = "0" & useCard
        checkDigit = useCard.Substring(Len(useCard11)
        useCard = useCard.Substring(0, Len(useCard1)

        ' ----- Process each digit.
        digitTotal = 0
        For counter = To Len(useCard)
            If ((counter Mod 21Then
                ' ----- This is an odd digit position. Double the number.
                holdValue = CInt(Mid(useCard, counter, 1)) 2
                If (holdValue > 9Then
                    ' ----- Break the digits (e.g., 19 becomes 1+9).
                    digitTotal += (holdValue \ 10(holdValue - 10)
                Else
                    digitTotal += holdValue
                End If
            Else
                ' ----- This is an even digit position. Simply add it.
                digitTotal += CInt(Mid(useCard, counter, 1))
            End If
        Next counter

        ' ----- Calculate the 10's complement of both values.
        calcDigit = 10 (digitTotal Mod 10)
        If (calcDigit = 10Then calcDigit = 0
        If (checkDigit = calcDigitThen Return True Else Return False
    End Function

End Class
False
2.26.String
2.26.1.Declare String Variable and assign value
2.26.2.String.Empty
2.26.3.Demonstrating String class constructors
2.26.4.Join string
2.26.5.Copy characters from string1 into character Array
2.26.6.String Length property
2.26.7.Demonstrating method GetHashCode of class String
2.26.8.Insert sub string by index
2.26.9.Insert method returns the resulting string
2.26.10.SubString
2.26.11.Get string morse code
2.26.12.String operation timing
2.26.13.Change tab to space
2.26.14.ToString
2.26.15.Catch Exception for String.Substring
2.26.16.Count Vowels
2.26.17.Reverse a string
2.26.18.Make a reference copy
2.26.19.Check a Credit card number
2.26.20.Read String value from Keyboard
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.