rucia Posted September 16, 2007 Share Posted September 16, 2007 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; } Quote Link to comment https://forums.phpfreaks.com/topic/69572-solved-switch-not-switching/ Share on other sites More sharing options...
rarebit Posted September 16, 2007 Share Posted September 16, 2007 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) Quote Link to comment https://forums.phpfreaks.com/topic/69572-solved-switch-not-switching/#findComment-349592 Share on other sites More sharing options...
rucia Posted September 16, 2007 Author Share Posted September 16, 2007 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; } Quote Link to comment https://forums.phpfreaks.com/topic/69572-solved-switch-not-switching/#findComment-349619 Share on other sites More sharing options...
rarebit Posted September 16, 2007 Share Posted September 16, 2007 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; } Quote Link to comment https://forums.phpfreaks.com/topic/69572-solved-switch-not-switching/#findComment-349621 Share on other sites More sharing options...
rucia Posted September 16, 2007 Author Share Posted September 16, 2007 Now only the default case is selected, and entering (aaaaaaaaaaa) is also validated as a success! Thanks for your patience - i hope this can be sorted, because i feel its much better for users to be told exactly where they went wrong. Quote Link to comment https://forums.phpfreaks.com/topic/69572-solved-switch-not-switching/#findComment-349630 Share on other sites More sharing options...
rarebit Posted September 16, 2007 Share Posted September 16, 2007 These work for me: echo "1: ".preg_match("/[[:space]]*$/",$fax)."<br>"; echo "2: ".preg_match("/[a-zA-Z]*$/",$fax)."<br>"; echo "3: ".preg_match("/\D/",$fax)."<br>"; Quote Link to comment https://forums.phpfreaks.com/topic/69572-solved-switch-not-switching/#findComment-349633 Share on other sites More sharing options...
rucia Posted September 16, 2007 Author Share Posted September 16, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/69572-solved-switch-not-switching/#findComment-349649 Share on other sites More sharing options...
rarebit Posted September 16, 2007 Share Posted September 16, 2007 So it's (0,1,0) with a length check? Quote Link to comment https://forums.phpfreaks.com/topic/69572-solved-switch-not-switching/#findComment-349663 Share on other sites More sharing options...
rucia Posted September 16, 2007 Author Share Posted September 16, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/69572-solved-switch-not-switching/#findComment-349672 Share on other sites More sharing options...
rarebit Posted September 16, 2007 Share Posted September 16, 2007 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... Quote Link to comment https://forums.phpfreaks.com/topic/69572-solved-switch-not-switching/#findComment-349684 Share on other sites More sharing options...
sasa Posted September 16, 2007 Share Posted September 16, 2007 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/69572-solved-switch-not-switching/#findComment-349696 Share on other sites More sharing options...
rarebit Posted September 16, 2007 Share Posted September 16, 2007 Yep, that's good for me! Well done! Quote Link to comment https://forums.phpfreaks.com/topic/69572-solved-switch-not-switching/#findComment-349699 Share on other sites More sharing options...
rucia Posted September 16, 2007 Author Share Posted September 16, 2007 Thats fantastic sasa. It's working perfectly! Thanks for your perseverance rarebit. Just 1 thing though, whats with the ==0 and ==1 at the end of the pregmatch? Quote Link to comment https://forums.phpfreaks.com/topic/69572-solved-switch-not-switching/#findComment-349717 Share on other sites More sharing options...
rarebit Posted September 16, 2007 Share Posted September 16, 2007 true or false Quote Link to comment https://forums.phpfreaks.com/topic/69572-solved-switch-not-switching/#findComment-349719 Share on other sites More sharing options...
rucia Posted September 16, 2007 Author Share Posted September 16, 2007 Excellent, thanks for that Quote Link to comment https://forums.phpfreaks.com/topic/69572-solved-switch-not-switching/#findComment-349721 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.