Adding credit card number validation
Number validation could be enough validation for a credit card number; however, using regular expressions, it is possible to check for number combinations to match credit card numbers from Visa, MasterCard, American Express, and more.
Getting ready
Make sure that you have validation.js
from the previous two recipes in this chapter open and ready for modification.
How to do it…
Use jQuery to provide form input validation for credit card numbers by performing the following step-by-step instructions:
Update
validation.js
to add the credit card validation function and the additional class check on the input fields:$(function(){ $('.submit-btn').click(function(event){ //Prevent form submission event.preventDefault(); var inputs = $('input'); var isError = false; for (var i = 0; i < inputs.length; i++) { // -- JavaScript from previous two recipes hidden if ($(input).hasClass('credit-card') && !validateCreditCard...