Credit Card Validation-client side as well as server side in ASP.NET

No.of Views1986
Bookmarked0 times
Downloads 
Votes0
By  justchiragpatel   On  16 Feb 2010 00:02:03
Tag : ASP.NET , Security
Credit Card Validation-client side as well as server side in ASP.NET
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

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.

{codecitation class="brush: javascript; gutter: true;" width="650px"}

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

{/codecitation}

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

{codecitation class="brush: javascript; gutter: true;" width="650px"}

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

{/codecitation}


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

{codecitation class="brush: csharp; gutter: true;" width="650px"}

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

{/codecitation}

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

{codecitation class="brush: csharp; gutter: true;" width="650px"}

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

{/codecitation}


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

Thank you



About the Author


Chirag Patel(MCTS - Web Application Framework 2.0)

Description :I have 4 years of experience in .NET technologies. My main focus is on C#, ASP.Net and SharePoint. I have given my consultancy services to many Pharmaceutical, Medicine and DotCom clients.

Occupation :Senior Software Consultant
Company : AK Systems Inc.
Location :U.S
 
Sign Up to vote for this article
 
About Author
 
justchiragpatel
Occupation-Not Provided
Company-Not Provided
Member Type-Fresh
Location-Not Provided
Joined date-18 Jul 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
By:1111Date Of Posted:12/2/2010 4:42:34 AM
111
var checksum = 0; for (j = 0; j < ccnum.length; j ) { positionNum = parseInt(ccnum.charAt(ccnum.length - 1 - j)); checksum = checksum ((positionNum * (j % 2 1) - 1) % 9 1); }
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top