User:Valmar2388/sandbox

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