Credit Card CVV Code Validation in ASP.NET
What is CVV?
The Card Verification Value (CVV) is an extra code printed on your debit or credit card. It sometimes called Card Verification Value (CVV or CV2), Card Verification Value Code (CVVC), Card Verification Code (CVC), Verification Code (V-Code or V Code), or Card Code Verification (CCV).
CVV is a new authentication procedure established by credit card companies to further efforts towards reducing fraud for internet transactions. It consists of requiring a card holder to enter the CVV number in at transaction time to verify that the card is on hand.
CVV Code in Credit Cards
CVV for Visa, MasterCard, BankCard and Diners is the final three digits of the number printed on the signature strip on the back of your card. CVV for American Express appears as a separate 4-digit code printed on the front of your card.
CVV Code Validation
We will see how to validate a CVV number using JavaScript as well as C# in this article. I have written methods/functions in C# and JavasScript respectively for this validation.
The JavaScript function validateCvvCode() is used to validate a CVV code at client side. The ValidateCVVCode() written in C# is used to check the CVV code at server side. These functions determines number of digits required for the given card type and then checks if the CVV Code have the required count of digits and have only numeric digits 0 to 9 using a regular expression. The regular expression is formed dynamically based on the card type.
Validation Using JavaScript
The following code contains the javascript function to validation CVV code with HTML markup of the demo application.
&l;%@ Page Language="C#" AutoEventWireup="true" CodeFile="CVVValidationDemo.aspx.cs"
Inherits="CVVValidationDemo" %&g;
&l;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&g;
&l;html xmlns="http://www.w3.org/1999/xhtml"&g;
&l;head runat="server"&g;
&l;title&g;CVV Credit Card Number Validation &l;/title&g;
&l;script type="text/javascript" language="javascript"&g;
function validateCvvCode() {
//Get the text of the selected card type
var cardType = document.getElementById('ddlCardType').options[document.getElementById('ddlCardType').selectedIndex].text;
// Get the value of the CVV code
var cvvCode = document.getElementById('txtCVVCode').value;
var digits = 0;
switch (cardType.toUpperCase()) {
case 'MASTERCARD':
case 'EUROCARD':
case 'EUROCARD/MASTERCARD':
case 'VISA':
case 'DISCOVER':
digits = 3;
break;
case 'AMEX':
case 'AMERICANEXPRESS':
case 'AMERICAN EXPRESS':
digits = 4;
break;
default:
return false;
}
var regExp = new RegExp('[0-9]{' + digits + '}');
return (cvvCode.length == digits && regExp.test(cvvCode))
}
function checkCvvCode() {
var result = validateCvvCode();
if (result)
alert('Valid CVV Code');
else
alert('Invalid CVV Code');
}
&l;/script&g;
&l;style type="text/css"&g;
.style1
{
font-size: large;
font-weight: bold;
font-family: Arial;
}
&l;/style&g;
&l;/head&g;
&l;body&g;
&l;form id="form1" runat="server"&g;
&l;div&g;
&l;table width="100%" cellspacing="0" cellpadding="0"&g;
&l;tr&g;
&l;td width="200px"&g;
&l;/td&g;
&l;td class="style1"&g;
CVV Code Validation
&l;/td&g;
&l;/tr&g;
&l;tr&g;
&l;td width="200px"&g;
&l;/td&g;
&l;td&g;
&l;asp:ValidationSummary ID="vsCVVValidationSummary" runat="server" /&g;
&l;/td&g;
&l;/tr&g;
&l;tr&g;
&l;td width="200px"&g;
Card Type
&l;/td&g;
&l;td&g;
&l;asp:DropDownList ID="ddlCardType" runat="server" Width="250px"&g;
&l;/asp:DropDownList&g;
&l;/td&g;
&l;/tr&g;
&l;tr&g;
&l;td width="200px"&g;
&l;/td&g;
&l;td&g;
&l;/td&g;
&l;/tr&g;
&l;tr&g;
&l;td width="200px"&g;
CVV Code
&l;/td&g;
&l;td&g;
&l;asp:TextBox ID="txtCVVCode" runat="server"&g;&l;/asp:TextBox&g;
&l;asp:CustomValidator ID="custValidCVV" runat="server" ErrorMessage="Invalid CVV Code"&g;*&l;/asp:CustomValidator&g;
&l;/td&g;
&l;/tr&g;
&l;tr&g;
&l;td width="200px"&g;
&l;/td&g;
&l;td&g;
&l;/td&g;
&l;/tr&g;
&l;tr&g;
&l;td width="200px"&g;
&l;/td&g;
&l;td&g;
&l;asp:Button ID="btnCvvServerValidate" runat="server" Text="Server Side Validation"
OnClick="btnCvvServerValidate_Click" /&g;
&l;input id="hbtnCvvClientValidate" type="button" value="Client Side Validation" onclick="checkCvvCode()" /&g;
&l;/td&g;
&l;/tr&g;
&l;/table&g;
&l;/div&g;
&l;/form&g;
&l;/body&g;
&l;/html&g;
Validation Using C#
The following code snippet has server side written in C# to demonstrate the CVV code validation. I have loaded and checked the following card types in a dropdown list for demonstration purpose: MASTERCARD, EUROCARD, EUROCARD/MASTERCARD, VISA, DISCOVER and AMERICAN EXPRESS.
using System;
using System.Data;
using System.Text.RegularExpressions;
public partial class CVVValidationDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
BindCardType();
}
private void BindCardType()
{
System.Data.DataTable dtCardType = new System.Data.DataTable();
dtCardType.Columns.Add(new DataColumn("Card_Type_Id", typeof(int)));
dtCardType.Columns.Add(new DataColumn("Card_Type_Name", typeof(string)));
dtCardType.Rows.Add(new object[] { 1, "MASTERCARD" });
dtCardType.Rows.Add(new object[] { 2, "EUROCARD" });
dtCardType.Rows.Add(new object[] { 3, "EUROCARD/MASTERCARD" });
dtCardType.Rows.Add(new object[] { 4, "VISA" });
dtCardType.Rows.Add(new object[] { 5, "DISCOVER" });
dtCardType.Rows.Add(new object[] { 6, "AMEX" });
dtCardType.Rows.Add(new object[] { 7, "AMERICANEXPRESS" });
dtCardType.Rows.Add(new object[] { 8, "AMERICAN EXPRESS" });
ddlCardType.DataSource = dtCardType;
ddlCardType.DataValueField = "Card_Type_Id";
ddlCardType.DataTextField = "Card_Type_Name";
ddlCardType.DataBind();
}
protected void btnCvvServerValidate_Click(object sender, EventArgs e)
{
custValidCVV.ErrorMessage = "Invalid CVV Code";
custValidCVV.IsValid = ValidateCVVCode();
}
private bool ValidateCVVCode()
{
var cardType = ddlCardType.SelectedItem.Text;
var cvvCode = txtCVVCode.Text;
var digits = 0;
switch (cardType.ToUpper())
{
case "MASTERCARD":
case "EUROCARD":
case "EUROCARD/MASTERCARD":
case "VISA":
case "DISCOVER":
digits = 3;
break;
case "AMEX":
case "AMERICANEXPRESS":
case "AMERICAN EXPRESS":
digits = 4;
break;
default:
return false;
}
Regex regEx = new Regex("[0-9]{" + digits + "}");
return (cvvCode.Length == digits && regEx.Match(cvvCode).Success);
}
}
Regard
Prateek
http://jm-anik.blogspot.com/
ReplyDeletehttp://www.testcreditcard.org
ReplyDeleteThis site allows you to check if your credit card (Master and Visa Card) is valid and has money or not
Hi guys. this site change my life and they are making it bigger and bigger. friendly and very reliable. I did some few business with them and all was great. make your dreams come true. I'm not selfish to share the site.
ReplyDeletehttps://buytoolsz.ga/
Want to buy CVV/Fullz/Dumps/PayPal accounts? We at Legit CC are here for you!
ReplyDeleteWe have a large database of our services which is updated regularly.
Our customers are very happy with us as our services are 100% guaranteed.
If for any reason you got a bad card we will replace it, no questions asked!
For more details and to place your order, visit our website: https://legitcc.website