Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Credit Card Javascript 1

Status
Not open for further replies.

powerlock2005

Technical User
Feb 23, 2005
42
US
All,
I have the below script working, but I need to modify it a bit, and I will explain what I want to do after the script. The below goes into the <head> portion of a webpage:

<SCRIPT language=javascript>
<!--
//********************************************************************
// Credit Card Number Validator Written By: Lokesh
// Date : 04/08/2004
// Uses Linh mod-10 formula to determine the validity and then
// calls different functions to check what category the number belongs
// to.
//********************************************************************

var invalid = "Invalid";
//********************************************************************
// Check the Linh Mod-10 validity

function CheckCreditCardNum(num)
{
var len=num.length;
var first;
var sum=0;
var mul=1;
var i;
var numAtI;
var tempSum;
var theway;
var txtcard;
theway = "";
for(i=(len-1); i >= 0 ;--i)
{
numAtI = parseInt(num.charAt(i));
tempSum = numAtI * mul;
theway += numAtI +" x " + mul +"=" + tempSum;
if(tempSum > 9)
{
first = tempSum % 10;
theway += " => "+ 1 +" + " + first +"=";
tempSum = 1 + first;
theway += tempSum +" ";
}
theway += "\n";
sum += tempSum;

if (mul == 1)
mul++;
else
mul--;
}
theway += "----------\nSum: "+ sum +"\n";


//*********************************************************
// Comment this line if you want to see the calculation


theway = "calculation not shown\n";
if(sum % 10)
{
if (len == 15)
{
txtcard = CheckFor15(num,0);
if ( txtcard == invalid )
{
alert(theway +"The number: "+ num +" is not valid!! ");
return false;
}
}
else
{
alert(theway +"The number: "+ num +" is not valid!! ");
return false;
}

}

{
switch(len)
{
case 13:
txtcard = CheckFor13(num);
break;
case 14:
txtcard = CheckFor14(num);
break;
case 15:
txtcard = CheckFor15(num,1);
break;
case 16:
txtcard = CheckFor16(num);
break;
default:
txtcard = invalid;
}
if(txtcard == invalid)
{
alert(theway +"The number: "+ num +" is not valid!! ");
return false;
}
else
theway+= "The number: "+ num +" is valid!! \nand it looks like it is: "+ txtcard;
}
alert(theway);
return true;
}

//***********************************************************************************
// Checks for Visa..
function CheckFor13(number)
{
if(parseInt(number.charAt(0)) == 4 )
return "Visa";
return invalid;
}

//************************************************************************************
// Check for Diners Club
function CheckFor14(number)
{
if( parseInt(number.charAt(0)) != 3 )
return invalid;
if( (parseInt(number.charAt(1)) == 6) || (parseInt(number.charAt(1)) == 8) )
return "Diners Club/Carte Blanche"
if( !(parseInt(number.charAt(1)) ))
if( (parseInt(number.charAt(2)) >= 0) && (parseInt(number.charAt(2)) <= 5))
return "Diners Club/Carte Blanche"
return invalid;
}

//************************************************************************************
// Check for American Express, enRoute, JCB
function CheckFor15(number, chec)
{
if ( (number.charAt(0) == 3) && ( (number.charAt(1) == 4)||(number.charAt(1) == 7) ) && chec)
return "American Express";
var FirstFour = parseInt(number.charAt(0))*1000;
FirstFour += parseInt(number.charAt(1))*100;
FirstFour += parseInt(number.charAt(2))*10;
FirstFour += parseInt(number.charAt(3));
//alert(FirstFour);
if( (FirstFour == 2014) || (FirstFour == 2149) )
return "enRoute";

if( ((FirstFour == 2131) || (FirstFour == 1800)) && chec )
return "JCB";

return invalid;
}

//************************************************************************************
// Check for Visa, MasterCard, Discover or JCB
function CheckFor16(number)
{
var a = parseInt(number.charAt(0));
var b = parseInt(number.charAt(1));
//alert (a);
switch (a)
{
case 5:
if((b > 0)&& (b < 6))
return "MasterCard";
else
return invalid;
break;
case 4:
return "Visa";
break;
case 6:
if ( (b == 0) && ( parseInt(number.charAt(2)) == 1) && ( parseInt(number.charAt(3)) == 1))
return "Discover";
else
return invalid;
break;
case 3:
return "JCB";
break;
default:
return invalid;
break;
}
}
--></SCRIPT>

I have placed the following for the credit card field:

<input maxLength="19" size="19" id=txtCCcard OnBlur="CheckCreditCardNum(this.value)" name="creditCardNo">

I have placed the following where the Submit button is:

<input type="submit" onclick=CheckCreditCardNum(this.value) value="Submit Form">

I need to modify the code if the submit button is clicked and an invalid credit card is in the field, it will give the alert and then setfocus in that field, erasing the mistake so the user can input the correct number.

Right now when the submit is clicked and the correct number is in the credit field it gives the following message:

}
else
theway+= "The number: "+ num +" is valid!! \nand it looks like it is: "+ txtcard;
}
alert(theway);
return true;

It gives this message, but I want it to NOT give this message and just submit the form.

Can someone help me with this?

Any help will be appreciated.

Thanks,
 

To stop the alert from happening with valid numbers, simply remove the alert line:

Code:
alert(theway);

To clear and focus the text entry box for invalid numbers, change all occurrences of this:

Code:
alert(theway +"The number: "+ num +" is not valid!! ");
return false;

to this:

Code:
alert(theway +"The number: "+ num +" is not valid!! ");
document.forms['formName'].elements['creditCardNo'].value = '';
setTimeout('document.forms[\'formName\'].elements[\'creditCardNo\'].focus();', 50);
return false;

You'll have to change 'formName' to be whatever name your form has (in both places).

I've put a setTimeout on the focus, as it appears FF and NN have issues focusing with 0 delay (see another post in this forum).

Hope this helps,
Dan



 
Dan,
I modified the script based on your input. See below:

<SCRIPT language=javascript>
<!--
//********************************************************************
// Credit Card Number Validator Written By: Lokesh
// Date : 04/08/2004
// Uses Linh mod-10 formula to determine the validity and then
// calls different functions to check what category the number belongs
// to.
//********************************************************************

var invalid = "Invalid";
//********************************************************************
// Check the Linh Mod-10 validity

function CheckCreditCardNum(num)
{
var len=num.length;
var first;
var sum=0;
var mul=1;
var i;
var numAtI;
var tempSum;
var theway;
var txtcard;
theway = "";
for(i=(len-1); i >= 0 ;--i)
{
numAtI = parseInt(num.charAt(i));
tempSum = numAtI * mul;
theway += numAtI +" x " + mul +"=" + tempSum;
if(tempSum > 9)
{
first = tempSum % 10;
theway += " => "+ 1 +" + " + first +"=";
tempSum = 1 + first;
theway += tempSum +" ";
}
theway += "\n";
sum += tempSum;

if (mul == 1)
mul++;
else
mul--;
}
theway += "----------\nSum: "+ sum +"\n";


//*********************************************************
// Comment this line if you want to see the calculation


theway = "calculation not shown\n";
if(sum % 10)
{
if (len == 15)
{
txtcard = CheckFor15(num,0);
if ( txtcard == invalid )
{
alert(theway +"The number: "+ num +" is not valid!! ");
document.forms['FrontPage_Form1'].elements['creditCardNo'].value = '';
setTimeout('document.forms[\'FrontPage_Form1\'].elements[\'creditCardNo\'].focus();', 50);
return false;
}
}
else
{
alert(theway +"The number: "+ num +" is not valid!! ");
document.forms['FrontPage_Form1'].elements['creditCardNo'].value = '';
setTimeout('document.forms[\'FrontPage_Form1\'].elements[\'creditCardNo\'].focus();', 50);
return false;
}

}

{
switch(len)
{
case 13:
txtcard = CheckFor13(num);
break;
case 14:
txtcard = CheckFor14(num);
break;
case 15:
txtcard = CheckFor15(num,1);
break;
case 16:
txtcard = CheckFor16(num);
break;
default:
txtcard = invalid;
}
if(txtcard == invalid)
{
alert(theway +"The number: "+ num +" is not valid!! ");
document.forms['FrontPage_Form1'].elements['creditCardNo'].value = '';
setTimeout('document.forms[\'FrontPage_Form1\'].elements[\'creditCardNo\'].focus();', 50);
return false;
}
else
theway+= "The number: "+ num +" is valid!! \nand it looks like it is: "+ txtcard;
}
alert(theway);
return true;
}

//***********************************************************************************
// Checks for Visa..
function CheckFor13(number)
{
if(parseInt(number.charAt(0)) == 4 )
return "Visa";
return invalid;
}

//************************************************************************************
// Check for Diners Club
function CheckFor14(number)
{
if( parseInt(number.charAt(0)) != 3 )
return invalid;
if( (parseInt(number.charAt(1)) == 6) || (parseInt(number.charAt(1)) == 8) )
return "Diners Club/Carte Blanche"
if( !(parseInt(number.charAt(1)) ))
if( (parseInt(number.charAt(2)) >= 0) && (parseInt(number.charAt(2)) <= 5))
return "Diners Club/Carte Blanche"
return invalid;
}

//************************************************************************************
// Check for American Express, enRoute, JCB
function CheckFor15(number, chec)
{
if ( (number.charAt(0) == 3) && ( (number.charAt(1) == 4)||(number.charAt(1) == 7) ) && chec)
return "American Express";
var FirstFour = parseInt(number.charAt(0))*1000;
FirstFour += parseInt(number.charAt(1))*100;
FirstFour += parseInt(number.charAt(2))*10;
FirstFour += parseInt(number.charAt(3));
//alert(FirstFour);
if( (FirstFour == 2014) || (FirstFour == 2149) )
return "enRoute";

if( ((FirstFour == 2131) || (FirstFour == 1800)) && chec )
return "JCB";

return invalid;
}

//************************************************************************************
// Check for Visa, MasterCard, Discover or JCB
function CheckFor16(number)
{
var a = parseInt(number.charAt(0));
var b = parseInt(number.charAt(1));
//alert (a);
switch (a)
{
case 5:
if((b > 0)&& (b < 6))
return "MasterCard";
else
return invalid;
break;
case 4:
return "Visa";
break;
case 6:
if ( (b == 0) && ( parseInt(number.charAt(2)) == 1) && ( parseInt(number.charAt(3)) == 1))
return "Discover";
else
return invalid;
break;
case 3:
return "JCB";
break;
default:
return invalid;
break;
}
}
--></SCRIPT>

This is the bottom half of my form:

<!--webbot bot="Validation" b-value-required="TRUE" --><select name="creditCardType">
<option selected>Visa</option>
<option>Master Card</option>
<option>AMEX</option>
<option>Discover</option>
</select> </li>
<li>
<address>
<span style="font-style: normal">Account Number:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>
<!--webbot bot="Validation" b-value-required="TRUE" i-maximum-length="19" --><input maxLength="19" size="19" id=txtCCcard OnBlur="CheckCreditCardNum(this.value)" name="creditCardNo"></address>
</li>
<li>Card Holder's Name:
<!--webbot bot="Validation" b-value-required="TRUE" i-maximum-length="50" --><input maxLength="50" size="50" name="cardHoldersName"> </li>
<li>Expiration Date:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<!--webbot bot="Validation" b-value-required="TRUE" i-maximum-length="10" --><input size="10" name="expDate" maxlength="10"> -- <em>yyyy/mm</em></li>
</dir>
<p>
<input type="submit" onclick=CheckCreditCardNum(this.value) value="Submit Form"></a>
<input type="reset" value="Reset Form"> </p>



</form>

When I click the submit button I get this now:

Calculation not shown
The number: Submit Form is not valid!!

Everytime I click the ok button for that alert it deletes the data out of the credit card field and I have to enter the number again, and then I get that same error message when I try to submit again. An endless loop.

How can I remedy this problem?

The other part you gave me works beautifully!!!!!!

Once that loop problem gets solved then everything should be fine.

 

Are you entering a valid number the second time? If not, that might be why it is still giving you the error.

Dan

 
Dan,
I figured out the problem.

I took this line away from the submit area:

onclick=CheckCreditCardNum(this.value)

That did the trick:)

I REALLY APPRECIATE YOUR HELP ON THIS!!!!

Thank you so much my friend.

Have a great week!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top