Jump to content

[SOLVED] Switch not switching


rucia

Recommended Posts

I am validating a UK fax number.  There are a few points where the user could go wrong, so i figure it would be best to inform the user exactly what they did wrong.  But i don't want to overwhelm the user - if they make more than 1 mistake i just want the first one sorted before attempting the next.

 

The following code for some reason gets snarled up at the third case, even if you actually enter a valid number.  Any ideas?

 

switch ($fax)
  {
    case (strlen($fax) < 1):
echo "<div class='error'><b>Please fix the following errors:</b>\n<ul>" 
      . "<li>Please enter a fax number</li></ul></div>";
break;
    case (substr($fax,0,1) <> 0):
echo "<div class='error'><b>Please fix the following errors:</b>\n<ul>"
     . "<li>Please start your fax number with a zero</li></ul></div>";
break;
    case (preg_match("/[a-zA-Z]*$/",$string)):
echo "<div class='error'><b>Please fix the following errors:</b>\n<ul>"
     . "<li>Please enter numbers only with no spaces</li></ul></div>";
break;
    case (preg_match("/[[:space]]*/",$fax)):
echo "<div class='error'><b>Please fix the following errors:</b>\n<ul>"
     . "<li>Please enter numbers only with no spaces</li></ul></div>";
break;							
    case (preg_match("/\D/",$fax)):
if (strlen($fax) < 11 || strlen($fax) > 11)
  {
    echo "<div class='error'><b>Please fix the following errors:</b>\n<ul>"
         . "<li>Please enter a valid fax number</li></ul></div>";
    break;
  }
else
  {
    echo "<div class='success'><h3>$success_message</h3></div>";
  }
break;
  }

Link to comment
Share on other sites

Third case:

case (preg_match("/[a-zA-Z]$/",$string)):

Fourth case:

case (preg_match("/[[:space]]*$/",$fax)):

Your last check... < 11 || > 11, is that not easier to use != 11, however what if you do or don't use an international dialing code? (also applies to starting with 0, e.g. +44)

Link to comment
Share on other sites

I made those changes and it still doesn't work.

 

If i make case 3 as

case (!preg_match("/[a-zA-Z]*$/",$string)):

.  Then that bit is fine.

 

Using

case (!preg_match("/[[:space]]*$/",$fax)):

stops at this case, any of the cases above work as expected, but entering a valid number still returns the error.

 

Here is the full code as it stands:

switch ($fax)
	{

		case (strlen($fax) ==0):
			echo "<div class='error'><b>Please fix the following errors:</b>\n<ul>"; 
			echo "<li>Please enter a fax number</li></ul></div>";
			break;		
		case (substr($fax,0,1) <> 0):
			echo "<div class='error'><b>Please fix the following errors:</b>\n<ul>"; 
			echo "<li>Please start your fax number with a zero</li></ul></div>";
			break;
		case (!preg_match("/[a-zA-Z]$/",$fax)):
				echo "<div class='error'><b>Please fix the following errors:</b>\n<ul>"; 
				echo "<li>Please enter numbers only</li></ul></div>";
				break;
		case (!preg_match("/[[:space]]*$/",$fax)):
				echo "<div class='error'><b>Please fix the following errors:</b>\n<ul>"; 
				echo "<li>Please enter the number without spaces</li></ul></div>";
				break;							
		case (!preg_match("/\D/",$fax)):
			if (strlen($fax) <11 || strlen($fax) >11)
			{
				echo "<div class='error'><b>Please fix the following errors:</b>\n<ul>"; 
				echo "<li>Please enter a valid fax number</li></ul></div>";
				break;
			}
			else
			{
				echo "<div class='success'><h3>$success_message</h3></div>";
			}
			break;


	}

Link to comment
Share on other sites

Sorry, I also changed last bit like this...

    case (preg_match("/[[:space]]*$/",$fax)):
	echo "<div class='error'><b>Please fix the following errors:</b>\n<ul>"
		. "<li>Please enter numbers only with no spaces</li></ul></div>";
	break;

    case (preg_match("/\D/",$fax)):
    	echo "<div class='error'><b>Please fix the following errors:</b>\n<ul>"
		. "<li>xyz</li></ul></div>";
	break;

    default:
	//if (strlen($fax) < 11 || strlen($fax) > 11)
	if (strlen($fax) != 11)
	{
		echo "<div class='error'><b>Please fix the following errors:</b>\n<ul>"
			. "<li>Please enter a valid fax number</li></ul></div>";
		break;
	}
	else
	{
		echo "<div class='success'><h3>well done</h3></div>";
	}
	break;
  }

Link to comment
Share on other sites

Ah thats useful for debugging.  Here's a few results i get

 

When entering 012

1: 0

2: 1

3: 0

PLEASE ENTER A VALID FAX NUMBER

 

When entering 012a

1: 1

2: 1

3: 1

PLEASE ENTER A VALID FAX NUMBER

 

When entering 012 3

1: 0

2: 1

3: 1

PLEASE ENTER A VALID FAX NUMBER

 

When entering 012 3a

1: 1

2: 1

3: 1

PLEASE ENTER A VALID FAX NUMBER

 

When entering 01234567891

1: 0

2: 1

3: 0

SUCCESS MESSAGE

 

When entering aaaaaaaaaaa

1: 1

2: 1

3: 1

SUCCESS MESSAGE

 

Link to comment
Share on other sites

Not sure what you mean.

 

All tests returned 1 for condition 2, which makes no sense to me because when you enter just numbers 012, or 01234567891 there are no chars.

 

Entering 01234 567891 returns a message PLEASE ENTER A VALID FAX NUMBER.  But it should be informing the user that they entered a space.  Same thing happens if a char is entered.

 

Link to comment
Share on other sites

This logic table should help!

function test($fax)
{
$s = "<tr>";
$s .= "<td>".$fax."</td>";
$s .= "<td>".preg_match("/[a-zA-Z]*$/",$fax)."</td>";
$s .= "<td>".preg_match("/[[:space]]*$/",$fax)."</td>";
$s .= "<td>".preg_match("/\D/",$fax)."</td>";
$s .= "<td>";
$s .= (strlen($fax) != 11) ? 1 : 0;
$s .= "</td></tr>";
return $s;
}

$f = array("012", "012a", "012 3", "012 3a", "01234567891", "aaaaaaaaaaa");
$s = "<table cellpadding='3'>";
foreach($f as $fax)
{
$s .= test($fax);
}
$s .= "</table>";
echo $s;

The result 1.0.0.0 is success...

Link to comment
Share on other sites

try

<?php
$fax = '02345678 901';
$success_message = 'OK';

switch ($fax)
{
case (strlen($fax) ==0):
echo "<div class='error'><b>Please fix the following errors:</b>\n<ul>"; 
echo "<li>Please enter a fax number</li></ul></div>";
break;

case (preg_match('/^0/',$fax)==0):
echo "<div class='error'><b>Please fix the following errors:</b>\n<ul>"; 
echo "<li>Please start your fax number with a zero</li></ul></div>";
break;

case (preg_match("/ /", $fax)==1): 
echo "<div class='error'><b>Please fix the following errors:</b>\n<ul>"; 
echo "<li>Please enter the number without spaces</li></ul></div>";
break;

case (preg_match("/[^0-9]/",$fax)==1):
echo "<div class='error'><b>Please fix the following errors:</b>\n<ul>"; 
echo "<li>Please enter numbers only</li></ul></div>";
break;

case (preg_match("/ /", $fax)==1): 
echo "<div class='error'><b>Please fix the following errors:</b>\n<ul>"; 
echo "<li>Please enter the number without spaces</li></ul></div>";
break;

case (strlen($fax) != 11):
echo "<div class='error'><b>Please fix the following errors:</b>\n<ul>"; 
echo "<li>Please enter a valid fax number (11 numbers long)</li></ul></div>";
break;

default:
echo "<div class='success'><h3>$success_message</h3></div>";
break;
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.