Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Thursday, April 23, 2009

Credit Card Validation

Hi Friends,

Some times in e-commerce website, we need to validate the credit card information.
So that we can do at both places "Client" side using JavaScript and at "Server" side using C# or VB.Net.

Here i have created two methods one in JavaScript and one in C#.

Credit card validation requires use Regular Expressions, so try to get someoverview of Regular Expressions before reading this article..
Background

first Client side validation using JavaScript...
you have to put to controls one is drop down list (for different credit
cards names)and other is textbox (for card number).
just pass two parameter to this function one is id of dropdownlist and
textbox.

function ValidateCC(CCType, CCNum)
{
var cctype= document.getElementById(CCType);
var ccnum= document.getElementById(CCNum);
var validCCNum=false;
var validCC=false;
if(ccnum.value == "")
{
return false;
}
validCC= isValidCreditCard
(cctype.options[cctype.selectedIndex].value,ccnum.value);
if( validCC)
{
return true;
}
return false;
}


this function is calling another function isValidCreditCard
for number validation and it is here...

function isValidCreditCard(type, ccnum)
{
if (type == "Visa")
var re = /^[4]([0-9]{15}$|[0-9]{12}$)/;
else if (type == "MasterCard")
var re = /^[5][1-5][0-9]{14}$/;
else if (type == "Discover")
var re = /^6011-?d{4}-?d{4}-?d{4}$/;
else if (type == "Diners Club")
var re = /(^30[0-5][0-9]{11}$)|(^(36|38)[0-9]{12}$)/;
else if (type == "American Express")
var re = /^[34|37][0-9]{14}$/;
else if (type == "enRoute")
var re = /^(2014|2149)[0-9]{11}$/;
else if (type == "JCB")
var re = /(^3[0-9]{15}$)|(^(2131|1800)[0-9]{11}$)/;
if (!re.test(ccnum))
return false;
ccnum = ccnum.split("-").join("");
var checksum = 0;
for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2)
{
checksum += parseInt(ccnum.charAt(i-1));
}
for (var i=(ccnum.length % 2) + 1; i
var digit = parseInt(ccnum.charAt(i-1)) * 2;
if (digit < 10)
{ checksum += digit; }
else
{ checksum += (digit-9);
}
}
if ((checksum % 10) == 0)
{
return true;
}
else
return false;
}



now at the server side in asp.net with c#...


private bool checkCCValidation()
{
bool validCC = false;
if(txtCCNumber.Text == "")
{
return false;
}
validCC= isValidCreditCard(selectCCType.Value,txtCCNumber.Text.Trim());
if( validCC)
{
return true;
}
return false;
}


this method is also calling another method and it is here..

private bool isValidCreditCard(string type, string ccnum)
{
string regExp = "";
if (type == "Visa")
regExp = "^[4]([0-9]{15}$|[0-9]{12}$)";
else if (type == "MasterCard")
regExp = "^[5][1-5][0-9]{14}$";
else if (type == "Discover")
regExp = "^6011-?d{4}-?d{4}-?d{4}$";
else if (type == "Diners Club")
regExp = "(^30[0-5][0-9]{11}$)|(^(36|38)[0-9]{12}$)";
else if (type == "American Express")
regExp = "^[34|37][0-9]{14}$";
else if (type == "enRoute")
regExp = "^(2014|2149)[0-9]{11}$";
else if (type == "JCB")
regExp = "(^3[0-9]{15}$)|(^(2131|1800)[0-9]{11}$)";
if (!Regex.IsMatch(ccnum,regExp))
return false;
string[] tempNo = ccnum.Split('-');
ccnum = String.Join("", tempNo);
int checksum = 0;
for (int i = (2-(ccnum.Length % 2)); i <= ccnum.Length; i += 2)
{
checksum += Convert.ToInt32(ccnum[i-1].ToString());
}
int digit = 0;
for (int i = (ccnum.Length % 2) + 1; i < ccnum.Length; i += 2)
{
digit = 0;
digit = Convert.ToInt32(ccnum[i-1].ToString()) * 2;
if (digit < 10)
{ checksum += digit; }
else
{ checksum += (digit - 9); }
}
if ((checksum % 10) == 0)
return true;
else
return false;
}



so that is the end of simple way of validating credit card, without using
any third party controls..so enjoy... :)

Tuesday, April 21, 2009

IP Address to Long and Log to IP Address in C#

Hi Friends,

sometimes in Socket programming we need to supply the "Long" data type version of IPAddress which is form "127.0.0.1".

So we need to convert this IP address to the Long format. Here two functions i have created which will convert IP address to Long and vice-verse.



public static uint IPAddressToLong(string IPAddr)
{
IPAddress oIP = IPAddress.Parse(IPAddr);
byte[] byteIP = oIP.GetAddressBytes();


uint ip = (uint)byteIP[0] << 24;
ip += (uint)byteIP[1] << 16;
ip += (uint)byteIP[2] << 8;
ip += (uint)byteIP[3];

return ip;
}

public static string LongToIPAddress(uint ipLong)
{
//string ipAddress = string.Empty;
byte[] addByte = new byte[4];

addByte[0] = (byte)((ipLong >> 24) & 0xFF);
addByte[1] = (byte)((ipLong >> 16) & 0xFF);
addByte[2] = (byte)((ipLong >> 8) & 0xFF);
addByte[3] = (byte)((ipLong >> 0) & 0xFF);

return addByte[0].ToString() + "." + addByte[1].ToString() + "." + addByte[2].ToString() + "." + addByte[3].ToString();
}

Monday, April 13, 2009

Use of "Using" Keyword

Hi Friends,

We often face some problems with disposing connection, adapter and command objects.
But we can avoid such kind of situations by modifying some process logic. We can use "Using" keywords which will dispose the object which it contains.

It will work just like method block, means when method goes out of scope all object inside it will be prompted to Garbage Collector, here also those objects will be disposed after "Using" statement.

Introduction


This example demonstrates use of "Using" keyword which disposes all resources used inside it and in Data Access base classes we don't have to worried about connection and adapter dispose, they will be disposed automatically immediatly after use..

internal DataTable GetNetworkSettingsByNetworkID(int networkID)
{
try
{
using (Connection = new MySqlConnection(connectionString))
{
using (MySqlCommand command = new MySqlCommand())
{
command.CommandText = SQL_GET_NETWORKSETTINGS_BY_NETWORKID;
command.CommandType = CommandType.StoredProcedure;
command.Connection = Connection;
command.Parameters.Add(new MySqlParameter("?_NetworkID", MySqlDbType.Int32));
command.Parameters[0].Value = networkID;
using (MySqlDataAdapter dataAdapter = new MySqlDataAdapter(command))
{
using (DataTable dtNetworkSettings = new DataTable())
{
dataAdapter.Fill(dtNetworkSettings);
return dtNetworkSettings;
}
}
}
}
}
catch (Exception ex)
{
PacketUtils.WriteLogError(ex, "NetworkDA::GetNetworkSettingsByNetworkID");
throw ex;
}
finally
{
if (Connection != null)
{
if (Connection.State == ConnectionState.Open)
Connection.Close();
Connection.Dispose();
}
}
}


So always use the practice of using "Using" keywords for those objects which implements IDisposable interface.

Followers

My Google Reader