Hi guys, I have written the following javascript code that validates the credit card information entered by a user. I now want to rewrite the same validation code in PHP. But since I am still very much new to PHP, can anyone give me pointers or help me rewrite it?

    if (cardName=="") {
    errMsg += "Please enter the name on the credit card.\n";
}
if ((!cardType==="MASTERCARD") || (!cardType==="VISA") || (!cardType==="AMERICANEXPRESS")) {
    errMsg+="Please choose the credit card type.\n";
}
else {
    if (cardType==="VISA") {
        if (!cardNumber.match(/^4\d+/)) {
            errMsg+="Please enter a valid card number.\n";
            result = false;
        }
    }
    if (cardType==="MASTERCARD") {
        if  (!cardNumber.match(/^5[1-5][0-9]{14}$/)){
            errMsg+="Please enter a valid card number.\n";
            result = false;
            }
    }
    if  (cardType==="AMERICANEXPRESS") {
        if  (!cardNumber.match(/^3[4-7][0-9]{13}$/)) {
            errMsg+="Please enter a valid card number.\n";
            result = false;
        }
    }
}
if ((cardType==="MASTERCARD") || (cardType==="VISA")) {
    if (!cvv.match(/^[1-9]{3}$/)) {
        errMsg+="Please enter a valid cvv.\n";
        result = false;
    }
}
else if (cardType==="AMERICANEXPRESS") {
        if (!cvv.match(/^[1-9]{4}$/)) {
            errMsg+="Please enter a valid cvv.\n";
            result = false;
        }
    }

if (cardNumber=="") {
    errMsg += "Please enter the credit card number. \n";
}
if (cardDate=="") {
    errMsg+="Please enter the credit card expiration date. \n";

}   
else {
    if (!cardDate.match(/^(0[1-9]|1[0-2])[-][0-9]{2}/)) {
       errMsg += "The expire date format is not correct!\n";
       result = false;
    }
    else {
        // get current year and month reference: https://stackoverflow.com/questions/32931526/how-to-use-javascript-to-validate-credit-card-expire-datemm-yy-must-not-expire
        var d = new Date();
        var currentYear = d.getFullYear();
        var currentMonth = d.getMonth() + 1;
        // get parts of the expiration date
        var parts = cardDate.split('-');
        var year = parseInt(parts[1], 10) + 2000;
        var month = parseInt(parts[0], 10);
        // compare the dates
        if (year < currentYear || (year == currentYear && month < currentMonth)) {
            errMsg += "The expiry date has passed.\n";
            result = false;
        }
}

if (cvv=="") {
    errMsg+="Please enter the CCV code. \n";
//  more validation here 
}

if (errMsg!="") {
    alert (errMsg);

}
}

return result;

Curious: why did you use a triple equals when comparing the cardType string?

PHP syntax is just a little different (variables have dollar signs, method names are different, etc.) but the logic is mostly going to be the same.

I see you’re using regular expressions so it looks like pritaeas pointed you towards how to do a regex match in PHP.

However, I want to encourage you to keep the javascript-based check in place, even if you add it on the php-side as well.

When users are filling out forms, any quick browser-based form validation is great so they see they had a typo or whatever more quickly instead of submitting the firm and waiting for the round trip to the server to return back an error (takes much longer).

Also, why are you doing this at all? What credit card processor are you using? The big ones, Braintree and Stripe, both have sophisticated APIs that do this for you.

I agree with the last reply. It really makes no sense doing this yourself, and taking on all the possible problems you could cause for your users if your security is in any way compromised and their card numbers are harvested. You could end up in real financial trouble yourself. Get one of the Big Boys to do it for you. And Good Luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.