kamalpreet kamalpreet - 1 month ago 10
C# Question

Transforming PHP code with Regular Expressions into C#

I am trying to implement Authorize.NET Payment Gateway.
While trying to validate Credit Card I found the following code on Authorize.NET but it is only in PHP.

Can someone please help to get it converted into C#.

// This function will take a credit card number and check to make sure it
// contains the right amount of digits and uses the Luhn Algorithm to
// weed out made up numbers
function validateCreditcard_number($credit_card_number)
{
// Get the first digit
$firstnumber = substr($credit_card_number, 0, 1);
// Make sure it is the correct amount of digits. Account for dashes being present.
switch ($firstnumber)
{
case 3:
if (!preg_match('/^3\d{3}[ \-]?\d{6}[ \-]?\d{5}$/', $credit_card_number))
{
return 'This is not a valid American Express card number';
}
break;
case 4:
if (!preg_match('/^4\d{3}[ \-]?\d{4}[ \-]?\d{4}[ \-]?\d{4}$/', $credit_card_number))
{
return 'This is not a valid Visa card number';
}
break;
case 5:
if (!preg_match('/^5\d{3}[ \-]?\d{4}[ \-]?\d{4}[ \-]?\d{4}$/', $credit_card_number))
{
return 'This is not a valid MasterCard card number';
}
break;
case 6:
if (!preg_match('/^6011[ \-]?\d{4}[ \-]?\d{4}[ \-]?\d{4}$/', $credit_card_number))
{
return 'This is not a valid Discover card number';
}
break;
default:
return 'This is not a valid credit card number';
}
// Here's where we use the Luhn Algorithm
$credit_card_number = str_replace('-', '', $credit_card_number);
$map = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 2, 4, 6, 8, 1, 3, 5, 7, 9);
$sum = 0;
$last = strlen($credit_card_number) - 1;
for ($i = 0; $i <= $last; $i++)
{
$sum += $map[$credit_card_number[$last - $i] + ($i & 1) * 10];
}
if ($sum % 10 != 0)
{
return 'This is not a valid credit card number';
}
// If we made it this far the credit card number is in a valid format
return 'This is a valid credit card number';
}
echo validateCreditcard_number('4111-1111-1111-1111'); // This is a valid credit card number
echo validateCreditcard_number('4111-1111-1111-1112'); // This is not a valid credit card number
echo validateCreditcard_number('5558-545f-1234'); // This is not a valid MasterCard card number
echo validateCreditcard_number('9876-5432-1012-3456'); // This is not a valid credit card number


Here is the Source of this Code.

So far I'm able to convert the most of it. But I'm getting error in Regular Expressions-

private Request_Response Validate_Credit_Card_Number(string credit_card_number)
{
Request_Response result = new Request_Response();

// Get the first digit
var firstnumber = credit_card_number.Substring(0, 1);
// Make sure it is the correct amount of digits. Account for dashes being present.
switch (firstnumber)
{
case "3":
if (!Regex.IsMatch("/^3\d{3}[ \-]?\d{6}[ \-]?\d{5}$/", credit_card_number))
{
result.response_Status = false;
return result;
//return "This is not a valid American Express card number";
}
break;
case "4":
if (!Regex.IsMatch("/^4\d{3}[ \-]?\d{4}[ \-]?\d{4}[ \-]?\d{4}$/", credit_card_number))
{
result.response_Status = false;
return result;
//return "This is not a valid Visa card number";
}
break;
case "5":
if (!Regex.IsMatch("/^5\d{3}[ \-]?\d{4}[ \-]?\d{4}[ \-]?\d{4}$/", credit_card_number))
{
result.response_Status = false;
return result;
//return 'This is not a valid MasterCard card number';
}
break;
case "6":
if (!Regex.IsMatch("/^6011[ \-]?\d{4}[ \-]?\d{4}[ \-]?\d{4}$/", credit_card_number))
{
result.response_Status = false;
return result;
//return 'This is not a valid Discover card number';
}
break;
default:
result.response_Status = false;
break;
//return null;
//return "This is not a valid credit card number";
}

// Here's where we use the Luhn Algorithm
credit_card_number = credit_card_number.Replace("-", "");
int[] map = new int[]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 2, 4, 6, 8, 1, 3, 5, 7, 9};

int sum = 0;
int last = credit_card_number.Length - 1;
for (int i = 0; i <= last; i++)
{
sum += map[credit_card_number[last - i] + (i & 1) * 10];
}
if (sum % 10 != 0)
{
result.response_Status = false;
//return null;
//return 'This is not a valid credit card number';
}

// If we made it this far the credit card number is in a valid format
result.response_Status = true;
//return null;
//return 'This is a valid credit card number';
}

Answer

Anyway, I will try to help you. Try this code.

using System;
using System.Text.RegularExpressions;
class Program
{
    public static string Validate(string ccNumber)
    {
        // Get the first digit
        var firstnumber = ccNumber[0];
        // Make sure it is the correct amount of digits. Account for dashes being present.
        switch (firstnumber)
        {
            case '3':
                if (!Regex.IsMatch(ccNumber, @"^3\d{3}[ \-]?\d{6}[ \-]?\d{5}$"))
                {
                    return "This is not a valid American Express card number";
                }
                break;
            case '4':
                if (!Regex.IsMatch(ccNumber, @"^4\d{3}[ \-]?\d{4}[ \-]?\d{4}[ \-]?\d{4}$"))
                {
                    return "This is not a valid Visa card number";
                }
                break;
            case '5':
                if (!Regex.IsMatch(ccNumber, @"^5\d{3}[ \-]?\d{4}[ \-]?\d{4}[ \-]?\d{4}$"))
                {
                    return "This is not a valid MasterCard card number";
                }
                break;
            case '6':
                if (!Regex.IsMatch(ccNumber, @"^6011[ \-]?\d{4}[ \-]?\d{4}[ \-]?\d{4}$"))
                {
                    return "This is not a valid Discover card number";
                }
                break;
            default:
                return "This is not a valid credit card number";
        }
        // Here's where we use the Luhn Algorithm
        ccNumber = ccNumber.Replace("-", "");
        var map = new[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
            0, 2, 4, 6, 8, 1, 3, 5, 7, 9};
        var sum = 0;
        var last = ccNumber.Length - 1;
        for (var i = 0; i <= last; i++)
        {
            sum += map[ccNumber[last - i] - '0' + (i & 1) * 10];
        }
        if (sum % 10 != 0)
        {
            return "This is not a valid credit card number";
        }
        // If we made it this far the credit card number is in a valid format
        return "This is a valid credit card number";
    }



    static void Main(string[] args)
    {
        Console.WriteLine(Validate("4111-1111-1111-1111")); // This is a valid credit card number
        Console.WriteLine(Validate("4111-1111-1111-1112")); // This is not a valid credit card number
        Console.WriteLine(Validate("5558-545f-1234"));      // This is not a valid MasterCard card number
        Console.WriteLine(Validate("9876-5432-1012-3456")); // This is not a valid credit card number

        Console.ReadLine();

    }
}
Comments