Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Become a Premium Member and unlock a new, free course in leading technologies each month.

Solved

Posted on 2006-10-19

I wanted to check the credit card number on a form before submiting. What function can I run on the input before submission so that the input has something like onblur or onchange or something. Also any way to check the CVV number too or is there no algorithm for that? Thanks in advance for all the help.

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

- Help others & share knowledge
- Earn cash & points
- Learn & ask questions

6 Comments

I would use the onblur event for the input field or have it part of script that is run with the form's onsubmit event.

I have a regular expression you can use to test numbers for major credit cards. You could use it in JavaScript with Match(). In the example below replace object with the name of your object.

if (object.match(/^(?:4[0-9]{

} else {

}

Credit for the regular expression goes to RegEx Buddy's library. There are some cases where I don't mind code made by another. :) Let me know if you have any questions or need more information.

b0lsc0tt

http://www.bilkent.edu.tr/~berkant/security/hack/luhn_check.html

<html>

<head>

<title>Zvonko *</title>

<script>

function checkCardNum(theField){

var theNum = theField.value;

var check = 0;

for(var i=0;i<theNum.length;i++){

var num = theNum.substr(i,1);

num *= ((i+theNum.length)%2)?1:2;

check += (num>9)?num%10+1:num;

}

if(check%10){

alert("Wrong CreditCard number.");

theField.select();

theField.focus();

return false;

}

return true;

}

</script>

</head>

<body>

<form onSubmit="return checkCardNum(this.cardNumb

<input type=text name="cardNumber" size="16" maxLength="16" value="446-667-651">

<input type=submit>

</form>

</body>

</html>

The code handles the different cases for cards of even length and cards for odd length (American Express) with the same code. That part is probably the most confusing part of the code when looking at it.

function isValidCreditCard(type, ccnum) {

if (type == "Visa") {

// Visa: length 16, prefix 4, dashes optional.

var re = /^4\d{3}-?\d{4}-?\d{4}-?\d

} else if (type == "MC") {

// Mastercard: length 16, prefix 51-55, dashes optional.

var re = /^5[1-5]\d{2}-?\d{4}-?\d{4

} else if (type == "Disc") {

// Discover: length 16, prefix 6011, dashes optional.

var re = /^6011-?\d{4}-?\d{4}-?\d{4

} else if (type == "AmEx") {

// American Express: length 15, prefix 34 or 37.

var re = /^3[4,7]\d{13}$/;

} else if (type == "Diners") {

// Diners: length 14, prefix 30, 36, or 38.

var re = /^3[0,6,8]\d{12}$/;

}

if (!re.test(ccnum)) return false;

// Remove all dashes for the checksum checks to eliminate negative numbers

ccnum = ccnum.split("-").join("");

// Checksum ("Mod 10")

// Add even digits in even length strings or odd digits in odd length strings.

var checksum = 0;

for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) {

checksum += parseInt(ccnum.charAt(i-1)

}

// Analyze odd digits in even length strings or even digits in odd length strings.

for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+=2) {

var digit = parseInt(ccnum.charAt(i-1)

if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }

}

if ((checksum % 10) == 0) return true; else return false;

}

Note that the first character in a string is charAt(0), so that is why 1 is subtracted from the value of i all the time. For the first pass, if the length is even, then the length mod 2 ("mod" is a mathematical term that says to return the remainder of the first number divided by the second number, so 5 mod 2 is 1 and 6 mod 2 is 0. % is the mod operator in JavaScript) results in 0. So 2 minus 0 is 2, so the value of i will walk through the even numbered digits. In the first pass for strings of odd length, the length mod 2 is 1, so 2 minus 1 is 1, and the value of i will walk through the odd numbered digits.

R.K

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Course of the Month6 days, left to enroll

Join the community of 500,000 technology professionals and ask your questions.