Easy learning with example program codes

JavaScript credit card validation

Credit card validation in JavaScript is used to make sure that all the number entered in the specified credit card number field should be a valid card number. As we know that there are number of companies which provides the credit card and all these have different format of credit card numbers. So we have to create different regular expression for different provider according to card number format. Some of the examples are as given below.

Mastercard credit card validation regex


American Express credit card validation regex


Visa credit card validation regex


Discover credit card validation regex


Maestro credit card validation regex


JCB credit card validation regex


Diner’s Club credit card validation regex



<!DOCTYPE html>
<html lang="en">
function creditCardValidation(creditCradNum)
var regEx = /^5[1-5][0-9]{14}$|^2(?:2(?:2[1-9]|[3-9][0-9])|[3-6][0-9][0-9]|7(?:[01][0-9]|20))[0-9]{12}$/;
      return true;
     alert("Please enter a valid credit card number.");
     return false;
<div class="mail">
<h2>JavaScript Credit Card Number Validation</h2>
<form name="form1" action="#"> 
Credit Number: <input type='text' name='creditCradNum'/></br></br>
<input type="submit" name="submit" value="Submit" 
Please follow and like us:

Copyright © 2020 CodesJava Protection Status