samflex
02-02-2005, 04:21 PM
Hi All,
This is my *first time* on this forum.
I have been trying now, unsuccessfully to validate a credit js script that I have been working on for days.
When I test it, it does nothing.
Any one has any ideas, please?
Here is the entire code, hope it is not too much.
<SCRIPT LANGUAGE=javascript>
<!--
// Client script validates form field entries for credit card
function validate(theForm)
{
if(document.cform.IsHidden.value=="false")
{
if (theForm.cardname.value == "" || theForm.cardname.value.length < 2)
{
alert("Please fill in the name found on your credit card.");
theForm.cardname.focus() ;
return false;
}
if ((theForm.paymentm = "Visa" || theForm.paymentm = "Disc" || theForm.paymentm = "MC" ) && (theForm.cardno.value == "" || theForm.cardno.value == "0000-0000-0000-0000")
{
alert("Please fill in the card number in this format: 0000-0000-0000-0000.");
theForm.cardno.focus();
return false;
}
return true;
}
}
function isValidCreditCard(type, ccnum) {
validLength = true;
if (type == "Visa") {
if (ccnum.length != 19){validLength = false} // I ued 19, 16 digits plus 3 dashes
// Visa: length 16, prefix 4, dashes optional.
var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
} if (type == "MC") {
if (ccnum.length != 19){validLength = false}
// Mastercard: length 16, prefix 51-55, dashes optional.
var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
} if (type == "Disc") {
if (ccnum.length != 19){validLength = false}
// Discover: length 16, prefix 6011, dashes optional.
var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
} if (type == "AmEx") {
if (ccnum.length != 18){validLength = false}
// American Express: length 15, prefix 34 or 37.
var re = /^3[4,7]\d{13}$/;
} if (type == "Diners") {
if (ccnum.length != 17){validLength = false}
// Diners: length 14, prefix 30, 36, or 38.
var re = /^3[0,6,8]\d{12}$/;
}
if (!validLength) return false;
if (!re.test(ccnum)) return false;
// 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)) * 2;
if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }
}
if ((checksum % 10) == 0) return true; else return false;
}
</script>
Then I invoke the script like this:
<td width="350"> <select name="paymentm" onChange="isValidCreditCard(this)"> <option value="Disc">Discover</option> <option selected value="Visa">Visa</option> <option value="AmEx">American Express</option> <option value="MC">Mastercard</option> <option value="Diners">Diner's Club</option> </select> </td>
Many thanks in advance
This is my *first time* on this forum.
I have been trying now, unsuccessfully to validate a credit js script that I have been working on for days.
When I test it, it does nothing.
Any one has any ideas, please?
Here is the entire code, hope it is not too much.
<SCRIPT LANGUAGE=javascript>
<!--
// Client script validates form field entries for credit card
function validate(theForm)
{
if(document.cform.IsHidden.value=="false")
{
if (theForm.cardname.value == "" || theForm.cardname.value.length < 2)
{
alert("Please fill in the name found on your credit card.");
theForm.cardname.focus() ;
return false;
}
if ((theForm.paymentm = "Visa" || theForm.paymentm = "Disc" || theForm.paymentm = "MC" ) && (theForm.cardno.value == "" || theForm.cardno.value == "0000-0000-0000-0000")
{
alert("Please fill in the card number in this format: 0000-0000-0000-0000.");
theForm.cardno.focus();
return false;
}
return true;
}
}
function isValidCreditCard(type, ccnum) {
validLength = true;
if (type == "Visa") {
if (ccnum.length != 19){validLength = false} // I ued 19, 16 digits plus 3 dashes
// Visa: length 16, prefix 4, dashes optional.
var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
} if (type == "MC") {
if (ccnum.length != 19){validLength = false}
// Mastercard: length 16, prefix 51-55, dashes optional.
var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
} if (type == "Disc") {
if (ccnum.length != 19){validLength = false}
// Discover: length 16, prefix 6011, dashes optional.
var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
} if (type == "AmEx") {
if (ccnum.length != 18){validLength = false}
// American Express: length 15, prefix 34 or 37.
var re = /^3[4,7]\d{13}$/;
} if (type == "Diners") {
if (ccnum.length != 17){validLength = false}
// Diners: length 14, prefix 30, 36, or 38.
var re = /^3[0,6,8]\d{12}$/;
}
if (!validLength) return false;
if (!re.test(ccnum)) return false;
// 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)) * 2;
if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }
}
if ((checksum % 10) == 0) return true; else return false;
}
</script>
Then I invoke the script like this:
<td width="350"> <select name="paymentm" onChange="isValidCreditCard(this)"> <option value="Disc">Discover</option> <option selected value="Visa">Visa</option> <option value="AmEx">American Express</option> <option value="MC">Mastercard</option> <option value="Diners">Diner's Club</option> </select> </td>
Many thanks in advance