Processing math: 100%

JavaScript credit card validation

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

Mastercard credit card validation regex

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/^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}$/
/^5[1-5][0-9]{14}|2(?:2(?:2[19]|[39][09])|[36][09][09]|7(?:[01][09]|20))[09]12/
/^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}$/

American Express credit card validation regex

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/^3[47][0-9]{13}$/
/^3[47][0-9]{13}$/
/^3[47][0-9]{13}$/

Visa credit card validation regex

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/^4[0-9]{12}(?:[0-9]{3})?$/
/^4[0-9]{12}(?:[0-9]{3})?$/
/^4[0-9]{12}(?:[0-9]{3})?$/

Discover credit card validation regex

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/^65[4-9][0-9]{13}|64[4-9][0-9]{13}|6011[0-9]{12}|(622(?:12[6-9]
|1[3-9][0-9]|[2-8][0-9][0-9]|9[01][0-9]|92[0-5])[0-9]{10})$/
/^65[4-9][0-9]{13}|64[4-9][0-9]{13}|6011[0-9]{12}|(622(?:12[6-9] |1[3-9][0-9]|[2-8][0-9][0-9]|9[01][0-9]|92[0-5])[0-9]{10})$/
/^65[4-9][0-9]{13}|64[4-9][0-9]{13}|6011[0-9]{12}|(622(?:12[6-9]
|1[3-9][0-9]|[2-8][0-9][0-9]|9[01][0-9]|92[0-5])[0-9]{10})$/

Maestro credit card validation regex

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/^(5018|5081|5044|5020|5038|603845|6304|6759|676[1-3]|6799|6220|504834|504817|504645)[0-9]{8,15}$/
/^(5018|5081|5044|5020|5038|603845|6304|6759|676[1-3]|6799|6220|504834|504817|504645)[0-9]{8,15}$/
/^(5018|5081|5044|5020|5038|603845|6304|6759|676[1-3]|6799|6220|504834|504817|504645)[0-9]{8,15}$/

JCB credit card validation regex

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/^(?:2131|1800|35[0-9]{3})[0-9]{11}$/
/^(?:2131|1800|35[0-9]{3})[0-9]{11}$/
/^(?:2131|1800|35[0-9]{3})[0-9]{11}$/

Diner’s Club credit card validation regex

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/
/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/
/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html lang="en">
<head>
<script>
function creditCardValidation(creditCradNum)
{
var regEx = /^5[1-5][0-9]{14}/;
if(creditCradNum.value.match(regEx))
{
return true;
}
else
{
alert("Please enter a valid credit card number.");
return false;
}
}
</script>
</head>
<body>
<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"
onclick="creditCardValidation(document.form1.creditCradNum)"/>
</form>
</div>
</body>
</html>
<!DOCTYPE html> <html lang="en"> <head> <script> function creditCardValidation(creditCradNum) { var regEx = /^5[1-5][0-9]{14}/; if(creditCradNum.value.match(regEx)) { return true; } else { alert("Please enter a valid credit card number."); return false; } } </script> </head> <body> <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" onclick="creditCardValidation(document.form1.creditCradNum)"/> </form> </div> </body> </html>
<!DOCTYPE html>
<html lang="en">
<head>
<script>
function creditCardValidation(creditCradNum)
{
var regEx = /^5[1-5][0-9]{14}/;
if(creditCradNum.value.match(regEx))
{
return true;
}
else
{
alert("Please enter a valid credit card number.");
return false;
}
}
</script>
</head>
<body>
<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"
onclick="creditCardValidation(document.form1.creditCradNum)"/>
</form>
</div>
</body>
</html>