Jump to content

If Else Logic Something Wrong


lovephp

Recommended Posts

guys what is it im doing wrong here that the else if($dataError['ctype'] == "MasterCard"){ portion which should not let the number 4 to be entered is letting it to enter without showing error?

 

 

if(empty($dataError['cnumber'])) {
				   $errormsg['cnumber'] = 'Credit Card Number is required!';
				   }else if(!is_numeric($dataError['cnumber'])) {
				   $errormsg['cnumber'] = 'Card Number must be numeric!';
				   }else if(strlen($dataError['cnumber']) < 15) {
				   $errormsg['cnumber'] = 'Insert a valid Credit Card Number!';
				   }else if(strlen($dataError['cnumber']) > 16) {
				   $errormsg['cnumber'] = 'Credit Card Number cannot be More than 16 Digits!';	   
				   }else if($dataError['ctype'] == "AmericanExpress"){
				   $pattern = "/^([34|37]{2})([0-9]{13})$/";
				   if(!preg_match($pattern,$dataError['cnumber'])){
				   $errormsg['cnumber'] = 'Not a valid American Express Card Number!';
				   }
				   }else if($dataError['ctype'] == "Discover"){
				   $pattern = "/^([6011]{4})([0-9]{12})$/";
				   if(!preg_match($pattern,$dataError['cnumber'])){
				   $errormsg['cnumber'] = 'Not a valid Discover Card Number!';
				   }
				   }else if($dataError['ctype'] == "MasterCard"){
				   $pattern = "/^([51|52|53|54|55]{2})([0-9]{14})$/";
				   if(!preg_match($pattern,$dataError['cnumber'])){
				   $errormsg['cnumber'] = 'Not a valid MasterCard Card Number!';
				   }
				   }else if($dataError['ctype'] == "Visa"){
				   $pattern = "/^([4]{1})([0-9]{12,15})$/";
				   if(!preg_match($pattern,$dataError['cnumber'])){
				   $errormsg['cnumber'] = 'Not a valid Visa Card Number!';
				   }else{
}
}

Link to comment
https://forums.phpfreaks.com/topic/269536-if-else-logic-something-wrong/
Share on other sites

Argh! Way, way too many elseif's there. But, I don't understand your question since I don't see anything in the conditions that would exclude the number 4. For matercard this is what you have

else if($dataError['ctype'] == "MasterCard")
{
$pattern = "/^([51|52|53|54|55]{2})([0-9]{14})$/";
if(!preg_match($pattern,$dataError['cnumber']))
{
 $errormsg['cnumber'] = 'Not a valid MasterCard Card Number!';
}
}

 

The way I read that is that the regex is looking for a number that begins with two pairs from 51-55, (e.g. 5151, 5154, 5453, etc.) and is then followed by 14 additional digits. If that match is true then you would get the error condition. Maybe I am missing it, what in that code makes you think the number 4 should be excluded?

That's not how regular exp<b></b>ressions work. The [] construct means "any of the single characters inside this." [abc] matches a, b, or c. [51|52|53|54|55] means 5,1,2,3,4, or |.

 

You (might) want:

$pattern = "/^((51)|(52)|(53)|(54)|(55))([0-9]{14})$/";

 

That matches one of your 5 starting pairs, then 14 more digits

Looking at my MaterCard, that RegEx is wrong anyway (based upon how I think you wanted it to work)

$pattern = "/^([51|52|53|54|55]{2})([0-9]{14})$/"; 

 

The {2} is looking for two matches. Since you had 51, 52, 53, 54, or 55 followed by 14 other digits.I think you were trying to match any of those pairs twice followed by 14 digits. But, you should only match one of those pairs (as ManiacDan has above).

 

EDIT: Also, since you are already doing validations that the input is only numbers, do you really need to verify that the characters at the end are numbers? Or maybe, you don't need to do those general validations up front since your regex's should capture those problems anyway.

 

This is MUCH more efficient

$ccNumber = trim($dataError['cnumber']);
$ccType = trim($dataError['ctype']);

if(empty($ccNumber))
{
   $errormsg['cnumber'] = 'Credit Card Number is required!';
}
else if(!ctype_digit($ccNumber))  //Don't use is_numeric because it allows decimals!
{
   $errormsg['cnumber'] = 'Card Number must be numeric!';
}
else
{
   //Number passed general validations, perform specific validation based on type
   switch($ccType)
   {
    case "AmericanExpress":
	    $pattern = "/^((34)|(37))\d{13}$/";
	    $ccType = 'American Express';
	    break;

    case "Discover":
	    $pattern = "/^6011\d{12}$/";
	    break;

    case "MasterCard":
	    $pattern = "/^((51)|(52)|(53)|(54)|(55))\d{14}$/";
	    break;

    case "Visa":
	    $pattern = "/^4\d{12,15}$/";
	    break;
   }

   if(!isset($pattern))
   {
    $errormsg['cnumber'] = 'Select a valid Credit Card type!';
   }
   elseif(!preg_match($pattern, $ccNumber))
   {
    $errormsg['cnumber'] = "Not a valid {$ccType} Card Number!";
   }
}

That's not how regular expressions work. The [] construct means "any of the single characters inside this." [abc] matches a, b, or c. [51|52|53|54|55] means 5,1,2,3,4, or |.

 

You (might) want:

$pattern = "/^((51)|(52)|(53)|(54)|(55))([0-9]{14})$/";

 

That matches one of your 5 starting pairs, then 14 more digits

cheers mate it worked yayyyyy

still not working mate when i choose mastercard still no error message when i enter 4587452145695478 tho with Discover and American Express it does show its not valid but only for Mastercard its still the same??

 

So, what was the problem? I tested the code I provided - so I know it worked. When I test using the value you state I get the appropriate message. Perhaps you were passing "Mastercard" rather than "MasterCard" in the post data? If you noticed I had added logic in the code I provided in case the card type was not valid as well. You should definitely be verifying that the card type is a valid one as well. As it was, your original code would pass any values that passed the initial, non-card specific checks if the card type was not one of the ones you coded for.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.