Forums

ChronoForm Server Side Validation

jake7 30 Sep, 2009
Hope someone can help me with this query.
I want to run a simple credit card validation script on the server side, I've placed my script in the serverside validation box and selected yes to enable. When I submit my form it ignores my script, so I guessing I'm not calling the function properly for the chronoform. The script works on it's own when I run it in wamp server.

I'm using $ccnumber and $cardtype called from the form.
Here's the script:

<?php
function validateCC($cc_num, $type) {

	if($type == "American") {
	$denum = "American Express";
	} elseif($type == "Dinners") {
	$denum = "Diner's Club";
	} elseif($type == "Discover") {
	$denum = "Discover";
	} elseif($type == "Master") {
	$denum = "Master Card";
	} elseif($type == "Visa") {
	$denum = "Visa";
	}

	if($type == "American") {
	$pattern = "/^([34|37]{2})([0-9]{13})$/";//American Express
	if (preg_match($pattern,$cc_num)) {
	$verified = true;
	} else {
	$verified = false;
	}
	
	
	} elseif($type == "Dinners") {
	$pattern = "/^([30|36|38]{2})([0-9]{12})$/";//Diner's Club
	if (preg_match($pattern,$cc_num)) {
	$verified = true;
	} else {
	$verified = false;
	}
	
	
	} elseif($type == "Discover") {
	$pattern = "/^([6011]{4})([0-9]{12})$/";//Discover Card
	if (preg_match($pattern,$cc_num)) {
	$verified = true;
	} else {
	$verified = false;
	}
	
	
	} elseif($type == "Master") {
	$pattern = "/^([51|52|53|54|55]{2})([0-9]{14})$/";//Mastercard
	if (preg_match($pattern,$cc_num)) {
	$verified = true;
	} else {
	$verified = false;
	}
	
	
	} elseif($type == "Visa") {
	$pattern = "/^([4]{1})([0-9]{12,15})$/";//Visa
	if (preg_match($pattern,$cc_num)) {
	$verified = true;
	} else {
	$verified = false;
	}
	
	}
	
	if($verified == false) {
	//Do something here in case the validation fails
	return "Credit card invalid. Please make sure that you entered a valid <em>" . $denum . "</em> credit card ";
	
	} else { //if it will pass...do something
	return "Your <em>" . $denum . "</em> credit card is valid";
	}


}
echo validateCC("$ccnumber", "$cardtype");
?>
GreyHead 30 Sep, 2009
Hi Jake7,

The requirement for the serverside validation is given in the example

<?php
if($_POST['accept_terms'] != 'yes')
return 'Sorry, but you need to accept our terms to proceed';
?>

Your code doens't return anything so will always pass. Try putting
return validateCC("$ccnumber", "$cardtype");
Then it should always return a string and so will always fail; so you'll need to make sure that the function *only* returns a message on 'fail'.

Bob
jake7 02 Oct, 2009
Thanks for that Bob. Code just needed further update to make it work. All vars from the form need to be in $_POST[yourvar] flavour, using $yourvar won't work. Anyhows this is the code that makes it sing.
return validateCC($_POST['ccnumber'], $_POST['cardtype']);
This topic is locked and no more replies can be posted.