Click here to Skip to main content
Click here to Skip to main content
Go to top

Credit card validation on client as well as on server side...(My simple way series article)

, 21 Mar 2007
Rate this:
Please Sign up or sign in to vote.
easy client and server side credit card validation

Introduction

Credit card validation using Regular Expressions.

Background

you have some knowledge of regular expression, because it is the core part of this functionality.

Using the code

Don't worry it is simple code you will understand just by seeing it...
//
// 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<ccnum.length; i+=2) {
                   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...

Remember to set the Language of your code snippet using the Language drop down.

Use the "var" button to to wrap Variable or class names in <code> tags like this.

Points of Interest

I like to Share my knowledge with guys like you, because i am also one of you, that if i dont know anything then i come to you...so keep exchange of knowledge...

History

keep attached with my simple way series articles....

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

JustChiragPatel
Software Developer
India India
Chirag Patel, a Programmer Analyst in a well known IT company working on .NET Technologies since last 2 years. He is interested in Pure business logic and fuzzy logic. his area of interest is in C#.NET, VB.NET and MSSQL 2005.
 
catch me on: http://groups.google.com/group/codewar

Comments and Discussions

 
GeneralNice Attempt* Pinmember[hassan]21-Mar-07 20:31 
GeneralJust a remark... Pinmemberszukuro21-Mar-07 1:36 
GeneralRe: Just a remark... PinmemberJustChiragPatel21-Mar-07 1:40 
Thanks,
yes i will update my code with switch statements,
bye have a nice day.
 
Chirag Patel

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 21 Mar 2007
Article Copyright 2007 by JustChiragPatel
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid