Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Help ! abt the creditcard validation

rinkarinka Member Posts: 9
hey all, is there anyone can help me regarding my creditcard validation codes ? When i entered the creditcard number, the error message "pls enter valid card number" pop up. But once i click OK, the page redirect to next page. i thought the page should come back to the current page instead of next page ? let's say, if i entered a wrong card number, the msg pop up, what i need to do is re-enter again,rite ? but, the thing straight away go to next page.
the current page is :G:FYPWEBSITEcreditcard.html
the next page is :G:FYPWEBSITEconfirmation.php (only go to next page when there is no error)




K-star KTV Pte Ltd











function mod10( cardNumber ) { // LUHN Formula for validation of credit card numbers.
var ar = new Array( cardNumber.length );
var i = 0,sum = 0;


for( i = 0; i < cardNumber.length; ++i ) {
ar[i] = parseInt(cardNumber.charAt(i));
}
for( i = ar.length -2; i >= 0; i-=2 ) { // you have to start from the right, and work back.
ar[i] *= 2; // every second digit starting with the right most (check digit)
if( ar[i] > 9 ) ar[i]-=9; // will be doubled, and summed with the skipped digits.
} // if the double digit is > 9, ADD those individual digits together


for( i = 0; i < ar.length; ++i ) {
sum += ar[i]; // if the sum is divisible by 10 mod10 succeeds
}
return (((sum%10)==0)?true:false);
}


function expired( month, year ) {
var now = new Date(); // this function is designed to be Y2K compliant.
var expiresIn = new Date(year,month,0,0,0); // create an expired on date object with valid thru expiration date
expiresIn.setMonth(expiresIn.getMonth()+1); // adjust the month, to first day, hour, minute & second of expired month
if( now.getTime() < expiresIn.getTime() ) return false;
return true; // then we get the miliseconds, and do a long integer comparison
}


function validateCard(cardNumber,cardType,cardMonth,cardYear) {
if( cardNumber.length == 0 ) { //most of these checks are self explanitory
alert("Please enter a valid card number.");
return false;
}
for( var i = 0; i < cardNumber.length; ++i ) { // make sure the number is all digits.. (by design)
var c = cardNumber.charAt(i);


if( c < '0' || c > '9' ) {
alert("Please enter a valid card number. Use only digits. do not use spaces or hyphens.");
return false;
}
}
var length = cardNumber.length; //perform card specific length and prefix tests


switch( cardType ) {
case 'a':


if( length != 15 ) {
alert("Please enter a valid American Express Card number.");
return;
}
var prefix = parseInt( cardNumber.substring(0,2));


if( prefix != 34 && prefix != 37 ) {
alert("Please enter a valid American Express Card number.");
return;
}
break;
case 'd':


if( length != 16 ) {
alert("Please enter a valid Discover Card number.");
return;
}
var prefix = parseInt( cardNumber.substring(0,4));


if( prefix != 6011 ) {
alert("Please enter a valid Discover Card number.");
return;
}
break;
case 'm':


if( length != 16 ) {
alert("Please enter a valid MasterCard number.");
return;
}
var prefix = parseInt( cardNumber.substring(0,2));


if( prefix < 51 || prefix > 55) {
alert("Please enter a valid MasterCard number.");
return;
}
break;
case 'v':


if( length != 16 && length != 13 ) {
alert("Please enter a valid Visa Card number.");
return;
}
var prefix = parseInt( cardNumber.substring(0,1));


if( prefix != 4 ) {
alert("Please enter a valid Visa Card number.");
return;
}
break;
}

if( !mod10( cardNumber ) ) { // run the check digit algorithm
alert("Sorry! this is not a valid credit card number.");
return false;
}

if( expired( cardMonth, cardYear ) ) { // check if entered date is already expired.
alert("Sorry! The expiration date you have entered would make this card invalid.");
return false;
}

return true; // at this point card has not been proven to be invalid
}



       

Select Card Type:



-Please Select-
American Express
Discover
MasterCard
Visa

Enter Card Number:

example: ( 1234567890123456 )

Select Expiration Date:



01
02
03
04
05
06
07
08
09
10
11
12


05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20


example: ( MM YY )

Issuing Bank:



-Please Select-
POSB
UOB
CITIBANK
DBS
OCBC












Can tell me where's my mistakes ?? Thankx ^^

Comments

  • CyGuyCyGuy Member Posts: 312
    [b][red]This message was edited by CyGuy at 2005-12-4 4:55:21[/red][/b][hr]
    hi, validation looks good. just need to put page in focus, before you return with error. do document.ccform.[red]name of error field.focus();[/red] before return;

    : hey all, is there anyone can help me regarding my creditcard validation codes ? When i entered the creditcard number, the error message "pls enter valid card number" pop up. But once i click OK, the page redirect to next page. i thought the page should come back to the current page instead of next page ? let's say, if i entered a wrong card number, the msg pop up, what i need to do is re-enter again,rite ? but, the thing straight away go to next page.
    : the current page is :G:FYPWEBSITEcreditcard.html
    : the next page is :G:FYPWEBSITEconfirmation.php (only go to next page when there is no error)
    :
    :
    :
    :
    : K-star KTV Pte Ltd
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    : function mod10( cardNumber ) { // LUHN Formula for validation of credit card numbers.
    : var ar = new Array( cardNumber.length );
    : var i = 0,sum = 0;
    :
    :
    : for( i = 0; i < cardNumber.length; ++i ) {
    : ar[i] = parseInt(cardNumber.charAt(i));
    : }
    : for( i = ar.length -2; i >= 0; i-=2 ) { // you have to start from the right, and work back.
    : ar[i] *= 2; // every second digit starting with the right most (check digit)
    : if( ar[i] > 9 ) ar[i]-=9; // will be doubled, and summed with the skipped digits.
    : } // if the double digit is > 9, ADD those individual digits together
    :
    :
    : for( i = 0; i < ar.length; ++i ) {
    : sum += ar[i]; // if the sum is divisible by 10 mod10 succeeds
    : }
    : return (((sum%10)==0)?true:false);
    : }
    :
    :
    : function expired( month, year ) {
    : var now = new Date(); // this function is designed to be Y2K compliant.
    : var expiresIn = new Date(year,month,0,0,0); // create an expired on date object with valid thru expiration date
    : expiresIn.setMonth(expiresIn.getMonth()+1); // adjust the month, to first day, hour, minute & second of expired month
    : if( now.getTime() < expiresIn.getTime() ) return false;
    : return true; // then we get the miliseconds, and do a long integer comparison
    : }
    :
    :
    : function validateCard(cardNumber,cardType,cardMonth,cardYear) {
    : if( cardNumber.length == 0 ) { //most of these checks are self explanitory
    : alert("Please enter a valid card number.");
    : return false;
    : }
    : for( var i = 0; i < cardNumber.length; ++i ) { // make sure the number is all digits.. (by design)
    : var c = cardNumber.charAt(i);
    :
    :
    : if( c < '0' || c > '9' ) {
    : alert("Please enter a valid card number. Use only digits. do not use spaces or hyphens.");
    : return false;
    : }
    : }
    : var length = cardNumber.length; //perform card specific length and prefix tests
    :
    :
    : switch( cardType ) {
    : case 'a':
    :
    :
    : if( length != 15 ) {
    : alert("Please enter a valid American Express Card number.");
    : return;
    : }
    : var prefix = parseInt( cardNumber.substring(0,2));
    :
    :
    : if( prefix != 34 && prefix != 37 ) {
    : alert("Please enter a valid American Express Card number.");
    : return;
    : }
    : break;
    : case 'd':
    :
    :
    : if( length != 16 ) {
    : alert("Please enter a valid Discover Card number.");
    : return;
    : }
    : var prefix = parseInt( cardNumber.substring(0,4));
    :
    :
    : if( prefix != 6011 ) {
    : alert("Please enter a valid Discover Card number.");
    : return;
    : }
    : break;
    : case 'm':
    :
    :
    : if( length != 16 ) {
    : alert("Please enter a valid MasterCard number.");
    : return;
    : }
    : var prefix = parseInt( cardNumber.substring(0,2));
    :
    :
    : if( prefix < 51 || prefix > 55) {
    : alert("Please enter a valid MasterCard number.");
    : return;
    : }
    : break;
    : case 'v':
    :
    :
    : if( length != 16 && length != 13 ) {
    : alert("Please enter a valid Visa Card number.");
    : return;
    : }
    : var prefix = parseInt( cardNumber.substring(0,1));
    :
    :
    : if( prefix != 4 ) {
    : alert("Please enter a valid Visa Card number.");
    : return;
    : }
    : break;
    : }
    :
    : if( !mod10( cardNumber ) ) { // run the check digit algorithm
    : alert("Sorry! this is not a valid credit card number.");
    : return false;
    : }
    :
    : if( expired( cardMonth, cardYear ) ) { // check if entered date is already expired.
    : alert("Sorry! The expiration date you have entered would make this card invalid.");
    : return false;
    : }
    :
    : return true; // at this point card has not been proven to be invalid
    : }
    :
    :
    :
    :
    :
    :
    : : : :  
    :
    : : : :  
    :
    : : : :  
    :
    : : : :  
    :
    : : :

    Select Card Type:


    :
    : -Please Select-
    : American Express
    : Discover
    : MasterCard
    : Visa
    :
    :

    Enter Card Number:


    :

    example: ( 1234567890123456 )

    :

    Select Expiration Date:


    :


    :
    : 01
    : 02
    : 03
    : 04
    : 05
    : 06
    : 07
    : 08
    : 09
    : 10
    : 11
    : 12
    :
    :
    : 05
    : 06
    : 07
    : 08
    : 09
    : 10
    : 11
    : 12
    : 13
    : 14
    : 15
    : 16
    : 17
    : 18
    : 19
    : 20
    :
    :
    : example: ( MM YY )
    :

    :

    Issuing Bank:


    :


    :
    : -Please Select-
    : POSB
    : UOB
    : CITIBANK
    : DBS
    : OCBC
    :
    :


    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    : Can tell me where's my mistakes ?? Thankx ^^
    :
    [green]also the confirm and clear buttons should be in the ccform, or your values will not pass.[/green] That could prove frustrating for mouse happy customers. [blue]good luck[/blue]


  • rinkarinka Member Posts: 9
    hey there. i already tried to put in the ccform thingy, but it still proceed to next page eventhough there are errors. can u help me check my coding pls... thanx u so much..



    K-star KTV Pte Ltd











    function validateForm()
    {
    var cardNumber = trimBetweenSpaces(trimBegEndSpaces(document.ccform.UMnumber.value));
    var expire = trimBetweenSpaces(trimBegEndSpaces(stripOffNonDigit(document.ccform.UMexpirM.value + document.ccform.UMexpirY.value)));
    }



    function mod10( cardNumber ) { // LUHN Formula for validation of credit card numbers.
    var ar = new Array( cardNumber.length );
    var i = 0,sum = 0;


    for( i = 0; i < cardNumber.length; ++i ) {
    ar[i] = parseInt(cardNumber.charAt(i));
    }
    for( i = ar.length -2; i >= 0; i-=2 ) { // you have to start from the right, and work back.
    ar[i] *= 2; // every second digit starting with the right most (check digit)
    if( ar[i] > 9 ) ar[i]-=9; // will be doubled, and summed with the skipped digits.
    } // if the double digit is > 9, ADD those individual digits together


    for( i = 0; i < ar.length; ++i ) {
    sum += ar[i]; // if the sum is divisible by 10 mod10 succeeds
    }
    return (((sum%10)==0)?true:false);
    }


    function expired( month, year ) {
    var now = new Date(); // this function is designed to be Y2K compliant.
    var expiresIn = new Date(year,month,0,0,0); // create an expired on date object with valid thru expiration date
    expiresIn.setMonth(expiresIn.getMonth()+1); // adjust the month, to first day, hour, minute & second of expired month
    if( now.getTime() < expiresIn.getTime() ) return false;
    return true; // then we get the miliseconds, and do a long integer comparison
    }


    function validateCard(cardNumber,cardType,cardMonth,cardYear) {
    if( cardNumber.length == 0 ) { //most of these checks are self explanitory
    alert("Please enter a valid card number.");
    document.ccform.UMnumber.focus == true;
    return false;
    }
    for( var i = 0; i < cardNumber.length; ++i ) { // make sure the number is all digits.. (by design)
    var c = cardNumber.charAt(i);


    if( c < '0' || c > '9' ) {
    alert("Please enter a valid card number. Use only digits. do not use spaces or hyphens.");
    document.ccform.UMnumber.focus == true;
    return false;
    }
    }
    var length = cardNumber.length; //perform card specific length and prefix tests


    switch( cardType ) {
    case 'a':


    if( length != 15 ) {
    alert("Please enter a valid American Express Card number.");
    return;
    }
    var prefix = parseInt( cardNumber.substring(0,2));


    if( prefix != 34 && prefix != 37 ) {
    alert("Please enter a valid American Express Card number.");
    return;
    }
    break;
    case 'd':


    if( length != 16 ) {
    alert("Please enter a valid Discover Card number.");
    return;
    }
    var prefix = parseInt( cardNumber.substring(0,4));


    if( prefix != 6011 ) {
    alert("Please enter a valid Discover Card number.");
    return;
    }
    break;
    case 'm':


    if( length != 16 ) {
    alert("Please enter a valid MasterCard number.");
    return;
    }
    var prefix = parseInt( cardNumber.substring(0,2));


    if( prefix < 51 || prefix > 55) {
    alert("Please enter a valid MasterCard number.");
    return;
    }
    break;
    case 'v':


    if( length != 16 && length != 13 ) {
    alert("Please enter a valid Visa Card number.");
    return;
    }
    var prefix = parseInt( cardNumber.substring(0,1));


    if( prefix != 4 ) {
    alert("Please enter a valid Visa Card number.");
    return;
    }
    break;
    }

    if( !mod10( cardNumber ) ) { // run the check digit algorithm
    alert("Sorry! this is not a valid credit card number.");
    document.ccform.UMnumber.focus == true;
    return false;
    }

    if( expired( cardMonth, cardYear ) ) { // check if entered date is already expired.
    alert("Sorry! The expiration date you have entered would make this card invalid.");
    document.ccform.UMnumber.focus == true;
    return false;
    }

    return true; // at this point card has not been proven to be invalid
    }




           

    Select Card Type:



    -Please Select-
    American Express
    Discover
    MasterCard
    Visa

    Enter Card Number:

    example: ( 1234567890123456 )

    Select Expiration Date:



    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12


    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20


    example: ( MM YY )

    Issuing Bank:



    -Please Select-
    POSB
    UOB
    CITIBANK
    DBS
    OCBC













  • CyGuyCyGuy Member Posts: 312
    : hey there. i already tried to put in the ccform thingy, but it still proceed to next page [green]i see, try making a function that calls all the other field check functions, and use that for the OnSubmit event in the Submit button. [/green]eventhough there are errors.[green] you can use this any time the form proceeds to the next page when a field is blank or in error.[/green] can u help me check my coding pls... [green] i will look over your script and get back to you [/green] thanx u so much..

  • CyGuyCyGuy Member Posts: 312
    [green]I am new too, thank you for having patience. there are two errors in the code that are repeated.[red]
    1. The name in the focus() is not cardName, it's UMname that is not in the form
    2. Any return that is not followed by false, is assumed true[/red]
    that is why the form keeps submitting. The focus() only highlight the field you specify, but the function must return false to stay on the current page. I will show you where, if you are still having problems, email me, and I will get you the code.[/green]


    :
    :
    :
    : K-star KTV Pte Ltd
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    : function validateForm()
    : {
    : var cardNumber = trimBetweenSpaces(trimBegEndSpaces(document.ccform.UMnumber.value));
    : var expire = trimBetweenSpaces(trimBegEndSpaces(stripOffNonDigit(document.ccform.UMexpirM.value + document.ccform.UMexpirY.value)));
    : }
    :
    :
    :
    : function mod10( cardNumber ) { // LUHN Formula for validation of credit card numbers.
    : var ar = new Array( cardNumber.length );
    : var i = 0,sum = 0;
    :
    :
    : for( i = 0; i < cardNumber.length; ++i ) {
    : ar[i] = parseInt(cardNumber.charAt(i));
    : }
    : for( i = ar.length -2; i >= 0; i-=2 ) { // you have to start from the right, and work back.
    : ar[i] *= 2; // every second digit starting with the right most (check digit)
    : if( ar[i] > 9 ) ar[i]-=9; // will be doubled, and summed with the skipped digits.
    : } // if the double digit is > 9, ADD those individual digits together
    :
    :
    : for( i = 0; i < ar.length; ++i ) {
    : sum += ar[i]; // if the sum is divisible by 10 mod10 succeeds
    : }
    : return (((sum%10)==0)?true:false);
    : }
    :
    :
    : function expired( month, year ) {
    : var now = new Date(); // this function is designed to be Y2K compliant.
    : var expiresIn = new Date(year,month,0,0,0); // create an expired on date object with valid thru expiration date
    : expiresIn.setMonth(expiresIn.getMonth()+1); // adjust the month, to first day, hour, minute & second of expired month
    : if( now.getTime() < expiresIn.getTime() ) return false;
    : return true; // then we get the miliseconds, and do a long integer comparison
    : }
    :
    :
    : function validateCard(cardNumber,cardType,cardMonth,cardYear) {
    : if( cardNumber.length == 0 ) { //most of these checks are self explanitory
    : alert("Please enter a valid card number.");
    : document.ccform.UMnumber.focus == true;
    : return false;
    : }
    : for( var i = 0; i < cardNumber.length; ++i ) { // make sure the number is all digits.. (by design)
    : var c = cardNumber.charAt(i);
    :
    :
    : if( c < '0' || c > '9' ) {
    : alert("Please enter a valid card number. Use only digits. do not use spaces or hyphens.");
    : document.ccform.UMnumber.focus == true;[red]there is nothing in the document with this name
    UMnumber should be cardName instead[/red]
    : return false;
    : }
    : }
    : var length = cardNumber.length; //perform card specific length and prefix tests
    :
    :
    : switch( cardType ) {
    : case 'a':
    :
    :
    : if( length != 15 ) {
    : alert("Please enter a valid American Express Card number.");
    : return;[red]<--needs false here[/red]
    : }
    : var prefix = parseInt( cardNumber.substring(0,2));
    :
    :
    : if( prefix != 34 && prefix != 37 ) {
    : alert("Please enter a valid American Express Card number.");
    : return;[red]<--needs false here[/red]
    : }
    : break;
    : case 'd':
    :
    :
    : if( length != 16 ) {
    : alert("Please enter a valid Discover Card number.");
    : return;[red]<--needs false here[/red]
    : }
    : var prefix = parseInt( cardNumber.substring(0,4));
    :
    :
    : if( prefix != 6011 ) {
    : alert("Please enter a valid Discover Card number.");
    : return;[red]<--needs false here[/red]
    : }
    : break;
    : case 'm':
    :
    :
    : if( length != 16 ) {
    : alert("Please enter a valid MasterCard number.");
    : return;[red]<--needs false here[/red]
    : }
    : var prefix = parseInt( cardNumber.substring(0,2));
    :
    :
    : if( prefix < 51 || prefix > 55) {
    : alert("Please enter a valid MasterCard number.");
    : return;[red]<--needs false here[/red]
    : }
    : break;
    : case 'v':
    :
    :
    : if( length != 16 && length != 13 ) {
    : alert("Please enter a valid Visa Card number.");
    : return;[red]<--needs false here[/red]
    : }
    : var prefix = parseInt( cardNumber.substring(0,1));
    :
    :
    : if( prefix != 4 ) {
    : alert("Please enter a valid Visa Card number.");
    : return;[red]<--needs false here[/red]
    : }
    : break;
    : }
    :
    : if( !mod10( cardNumber ) ) { // run the check digit algorithm
    : alert("Sorry! this is not a valid credit card number.");
    : document.ccform.UMnumber.focus == true;
    : return false;
    : }
    :
    : if( expired( cardMonth, cardYear ) ) { // check if entered date is already expired.
    : alert("Sorry! The expiration date you have entered would make this card invalid.");
    : document.ccform.UMnumber.focus == true;
    : return false;
    : }
    :
    : return true; // at this point card has not been proven to be invalid
    : }
    :
    : </SCRIPT>
    :
    :
    :
    :
    :
    :
    : : : :  
    :
    : : : :  
    :
    : : : :  
    :
    : : : :  
    :
    : : :

    Select Card Type:


    :
    : -Please Select-
    : American Express
    : Discover
    : MasterCard
    : Visa
    :
    :

    Enter Card Number:


    :

    example: ( 1234567890123456 )

    :

    Select Expiration Date:


    :


    :
    : 01
    : 02
    : 03
    : 04
    : 05
    : 06
    : 07
    : 08
    : 09
    : 10
    : 11
    : 12
    :
    :
    : 05
    : 06
    : 07
    : 08
    : 09
    : 10
    : 11
    : 12
    : 13
    : 14
    : 15
    : 16
    : 17
    : 18
    : 19
    : 20
    :
    :
    : example: ( MM YY )
    :

    :

    Issuing Bank:


    :


    :
    : -Please Select-
    : POSB
    : UOB
    : CITIBANK
    : DBS
    : OCBC
    :
    :


    : [red]this line may give problems... delete it and it should work fine[/red]
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :

  • rinkarinka Member Posts: 9
    hey ccguy !! thank you so much...hahah...finally i get it done...Muakz...thanx
  • CyGuyCyGuy Member Posts: 312
    : hey ccguy !! thank you so much...hahah...finally i get it done...Muakz...thanx
    :
    it's cyguy
    [green]WHAT you;see_is_what=you.get;[/green]
    -Russ aka DangeRuss

  • rinkarinka Member Posts: 9
    : : hey ccguy !! thank you so much...hahah...finally i get it done...Muakz...thanx
    : :
    : it's cyguy
    : [green]WHAT you;see_is_what=you.get;[/green]
    : -Russ aka DangeRuss
    :
    :
    oh yea...cyguy...sorry...i typed wrongly ^^
  • rinkarinka Member Posts: 9
    hey there. now i facing a serious problem. i forgot a very important thing. i forgot to link the thing to my database. At the beginning of code, i start with php. the query is
    "INSERT INTO creditcard(creditcardno,issuing_bank,card_type,expiry_date) VALUES ($creditcardno,$issuing_bank,$card_type,$expiry_date"
    but it doesnt work. is there any other ways to link my thing to mysql database ? can u pls advice ?


    : : : hey ccguy !! thank you so much...hahah...finally i get it done...Muakz...thanx
    : : :
    : : it's cyguy
    : : [green]WHAT you;see_is_what=you.get;[/green]
    : : -Russ aka DangeRuss
    : :
    : :
    : oh yea...cyguy...sorry...i typed wrongly ^^
    :

  • CyGuyCyGuy Member Posts: 312
    chech the other boards, right now I am focussing on the script of markup. Yes, Iy will get there eventually, sorry, I cant't help you anymore in this regard. best of wishes to you.

    : hey there. now i facing a serious problem. i forgot a very important thing. i forgot to link the thing to my database. At the beginning of code, i start with php. the query is
    : "INSERT INTO creditcard(creditcardno,issuing_bank,card_type,expiry_date) VALUES ($creditcardno,$issuing_bank,$card_type,$expiry_date"
    : but it doesnt work. is there any other ways to link my thing to mysql database ? can u pls advice ?
    :
    :
    : : : : hey ccguy !! thank you so much...hahah...finally i get it done...Muakz...thanx
    : : : :
    : : : it's cyguy
    : : : [green]WHAT you;see_is_what=you.get;[/green]
    : : : -Russ aka DangeRuss
    : : :
    : : :
    : : oh yea...cyguy...sorry...i typed wrongly ^^
    : :
    :
    :

Sign In or Register to comment.