Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I've 2 questions:
1. For the code below, how would I add a validation for if the field subject doesn't equal 'Level 1' 'Level 2' or 'Level 3' it outputs the Error message.
elseif ($subject != "Level 1")
{ echo "Error"; }Question 2. I use the following code to stop certain submissions via my email forms,
function validate_fields($s) {
$forbidden = array('\r', '\n', 'banned words', 'more banned words', 'etc', 'etc');
foreach ($forbidden as $f)
if (strpos($s, $f) !== false) return false;
return true;
}
BUT how could I shorten the email file to verify the fields as I have nearly 100 fields which are numbered field1 TO field87.code:
elseif (!validate_fields($_POST['field2']))
{ echo "Error"; }
elseif (!validate_fields($_POST['field3']))
{ echo "Error"; }
elseif (!validate_fields($_POST['field4']))
{ echo "Error"; }
elseif (!validate_fields($_POST['field5']))
{ echo "Error"; }AND SO ON.
Thanks for any help.
Ian

Ok I've answered Question 1 myself by using the && instead of the || (not sure why OR doesn't work and AND does!)
Any tips on Question 2???
Thanks Ian

"(not sure why OR doesn't work and AND does!)"
When you use OR, then if any of the conditions is true then it resolves to true. With AND all the conditions must be true for it to resolve to true. So,
if ($subject != "Level 1" || $subject != "Level 1")
Would ALWAYS resolve to true, because at least one of thos statements would be true.
As to Q2, you could do something like this (assuming all the fields are sequential):
$error = false;
for ($i=1; $i<=87; $i++) {
if (validate_fields($_POST['field'.$i])) {
$error = true;
}
}if ($error) {
//display error msg
} else {
//send the email
}Michael J

Hi Micheal
Thanks for that, me to confused with the AND / OR.
There are 3 forms all processed in one mail send file and by using the && it works the || doesn't....Each form has it OWN individual subject... Yes strange.
I'll do a test with your suggestion for Q2.
Thanks again.
Ian

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |