How to Validate Credit/Debit Card Number Using Regular Expression in Javascript Full Project For Beginners

 

Welcome folks today in this blog post we will be validating credit/debit card number using regular expression in javascript.All the full source code of the application is given below.

 

 

 

Get Started

 

 

 

In order to get started you need to make an index.html file and copy paste the following code

 

 

index.html

 

 

 

<script>
      function checkcard()
        {   
            var type=document.getElementById("type").value;
              type=parseInt(type);
            var cardno=document.getElementById("card").value;
            if(type=="1")
                {
                    
                    
                    var regex=/^5[1-5]{1}[0-9]{2}[\s]?[0-9]{4}[\s]?[0-9]{4}[\s]?[0-9]{4}$/;
                     if(regex.test(cardno)){ alert("It is Valid Master-Card");}
                    else{alert("Invalid Card");}
                }
              else if(type=="2")
                {
                    
                    
                    var regex=/^4{1}[0-9]{3}[\s]?[0-9]{4}[\s]?[0-9]{4}[\s]?[0-9]{1}$/;
                    var regex1=/^4{1}[0-9]{3}[\s]?[0-9]{4}[\s]?[0-9]{4}[\s]?[0-9]{4}$/;
                     if(regex.test(cardno)||regex1.test(cardno)){ alert("It is Valid Visa Card");}
                    else{alert("Invalid Card");}
                }
            else
                {
                    var regex=/^3[4|7]{1}[0-9]{2}[\s]?[0-9]{4}[\s]?[0-9]{4}[\s]?[0-9]{3}$/;
                     if(regex.test(cardno)){ alert("It is Valid American Express Card");}
                    else{alert("Invalid Card");}
                    
                }
           
           
        }
       
    
    </script>

Leave a Reply