Pronunciation / adjective
Bringing something good and unforseen.
    Skip Navigation Links > Code Repository
   

Credit Card Validation Function



 
Return

Credit Card Validation Function

Adding to the javascript credit card validation for client side here is a VB.Net function for credit card validation server side (or in a windows application).

  1. Private Function CardIsValid() As Boolean
  2. Try
  3. '---Create a regular expression object to check the creditcard number against.
  4. Dim RegEx As System.Text.RegularExpressions.Regex
  5. Select Case CreditCardTypeDropDownList.SelectedValue
  6. Case "American Express"
  7. RegEx = New System.Text.RegularExpressions.Regex("3[4,7]\d{13}")
  8. Case "Discover"
  9. RegEx = New System.Text.RegularExpressions.Regex("6011-?\d{4}-?\d{4}-?\d{4}")
  10. Case "Master Card"
  11. RegEx = New System.Text.RegularExpressions.Regex("5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}")
  12. Case "Visa"
  13. RegEx = New System.Text.RegularExpressions.Regex("4\d{3}-?\d{4}-?\d{4}-?\d{4}")
  14. End Select
  15. '---Get the credit card number
  16. Dim CreditCardNumber As String = CreditCardNumberTextBox.Text.Trim.Replace("-", "")
  17. '---If the regular expression matches then also validate with a checksum.
  18. ' If it doesn't then just don't submit the page.
  19. If RegEx.Match(CreditCardNumberTextBox.Text).Success Then
  20. '---Initialize the CheckSum
  21. Dim CheckSum As Int32 = 0
  22. '---Get the character index to start with
  23. Dim CharacterCount As Int32 = 2 - (CreditCardNumber.Length Mod 2)
  24. '---Check if the credit card number submitted passes a checksum routine.
  25. ' Checksum ("Mod 10")
  26. ' Add even digits in even length strings or odd digits in odd length string.
  27. Do While CharacterCount <= CreditCardNumber.Length
  28. '---Add the number to the checksum
  29. CheckSum += CType(CreditCardNumber.Chars(CharacterCount - 1).ToString, Int32)
  30. '---Move to the next character
  31. CharacterCount += 2
  32. Loop
  33. '---Reset the character count
  34. CharacterCount = (CreditCardNumber.Length Mod 2) + 1
  35. '---Initialize the Digit variable
  36. Dim Digit As Int32
  37. '---Analyze odd digits in even length strings or even digits in odd length strings.
  38. Do While CharacterCount < CreditCardNumber.Length
  39. '---Get the digit
  40. Digit = (CType(CreditCardNumber.Chars(CharacterCount - 1).ToString, Int32) * 2)
  41. '---Add to checksum
  42. If Digit < 10 Then
  43. CheckSum += Digit
  44. Else
  45. CheckSum += Digit - 9
  46. End If
  47. '---Move to the next character
  48. CharacterCount += 2
  49. Loop
  50. '---The checksum should 0 out. If it doesn't don't submit the page.
  51. If Not ((CheckSum Mod 10) = 0) Then
  52. CreditCardNumberRequiredFieldValidator.Text = "The card number entered is not valid."
  53. CreditCardNumberRequiredFieldValidator.IsValid = False
  54. Return False
  55. Else
  56. '---If the card number is valid then check if the expiration date is also valid.
  57. '---Get the year entered
  58. Dim ExpirationYear As Int32 = CType(CreditCardExpirationYearDropDownList.SelectedValue, Int32)
  59. '---If the expiration year is equal to today's year then check that the month is later
  60. If ExpirationYear = CType(Today.Year.ToString, Int32) Then
  61. Dim ExpirationMonth As Int32 = CType(CreditCardExpirationMonthDropDownList.SelectedValue, Int32)
  62. If ExpirationMonth < Today.Month Then
  63. CreditCardExpirationMonthRequiredFieldValidator.Text = "The expiration date selected is in the past."
  64. CreditCardExpirationMonthRequiredFieldValidator.IsValid = False
  65. Return False
  66. End If
  67. End If
  68. End If
  69. Else
  70. CreditCardNumberRequiredFieldValidator.Text = "The card number entered is not valid."
  71. CreditCardNumberRequiredFieldValidator.IsValid = False
  72. Return False
  73. End If
  74. Return True
  75. Catch ex As Exception
  76. Throw ex
  77. End Try
  78. End Function


Return
Microsoft Certified Professional   © 2018 Fortunate.  All rights reserved.
contact: justin@aboutfortunate.com