My validate email check always turns up as true

Hello,

My name is Andres and I am new to php and to this forum as well.

My problem is that I can not figure out, why my validate email check always shows error no matter what (field is empty, random letters on field or a real valid email)

Here is my code:
[php]
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = “E-mail address not valid!”;
}
[/php]
Before that I also tried this:
[php]
if (!preg_match ("/([\w-]+@[\w-]+.[\w-]+)/", $email)) {
$errors[] = “E-mail address not valid!”;
}
[/php]
This is how I call an error.
[php]

<? if (!empty($errors)) foreach ($errors as $error): echo $error; echo ''; endforeach; ?>

[/php]

I figured that with the first code I used, it will always add something to array, either it is true or false and because I check if $errors is empty it will show error even if it is not true.

I am not sure if the filter_var function does the same thing but I suspect it does.
Is there a way I can make something different to keep array empty in case email is valid and make the code work correctly?

If you need to see my whole code, you can check from
github.com/andressoop/Pood-2.0

Files that include bits from my contact form:
Pood-2.0 / controllers / contact.php
Pood-2.0 / views / contact_index_view.php
Pood-2.0 / functions.php

[php]$errors = array_filter($errors);[/php]

Welcome Moonfly to PHPHELP.

Your code is fine, I’ve checked it on my side and it work perfectly fine; displays error only when email is not a valid email address.

Try clearing out your browser’s cache or change the browser to see if it works or not.

Thank you for your replies,

It is really interesting that it works for you.
I Did a cache clear and tried 3 different browser but still gives me error every time I enter a valid email address.
Maybe I should try to run my code in a different computer.

I tried using it but the problem still exists.
Maybe I am using it in a wrong way?
[php]
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = “E-mail address not valid!”;
$errors = array_filter($errors);
}[/php]

How are you getting the value of $email???

Here is the full code
[php]
if ($_SERVER[“REQUEST_METHOD”] == “POST”) {
$firstname = check_input($_POST[‘firstname’], “Eesnimi on puudu!”);
$lastname = check_input($_POST[‘lastname’], “Perekonnanimi on puudu!”);
$email = check_input($_POST[‘email’], “Email on puudu!”);
$message = check_input($_POST[‘message’], “Tekst on puudu!”);
/* If e-mail is not valid show error message */
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = “E-mail address not valid!”;
}
if (empty($errors)) {
$formcontent = “From: $firstname $lastname\n\nMessage: \n$message”;
$recipient = “[email protected]”;
$subject = "ilukaubad.ee - " . $_POST[‘subject’];
$mailheader = “From: $email \r\n”;
mail($recipient, $subject, $formcontent, $mailheader) or $errors[] = (“Kirja saatmisega tekkis probleem”);
$info[] = (“Thank You!”);
}
}[/php]

Also the check_input function from functions.php
[php]
function check_input($data, $problem = ‘’)
{
global $errors;
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0) {
$errors[] = $problem;
}
}[/php]

I think the check_input function might be creating problems.

Do an echo on $email right before:

[php]if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {[/php]

to see what information you are getting back in the $email variable.

echo wont show anything on $email and var_dump shows “null”

Made an echo ‘Hello’ check on the same place, to see if it functions and it seems to be fine.

Well, If you are getting nothing (NULL) in the $email, then there must some problem in the check_input function. Do one more thing, echo $email before:

[php]if ($_SERVER[“REQUEST_METHOD”] == “POST”) {[/php]

to be 100% sure. If you are still getting nothing then you might have misspelled the form input field.

I think i figured it out

[php]
function check_input($data, $problem = ‘’)
{
global $errors;
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0) {
$errors[] = $problem;
}
else {
return $data;
}
}[/php]
I added return $data; in the end and now it functions correctly.

Thank you for pointing me into right direction! :slight_smile:

That’s nice your problem was solved. But now I’ve one question. I couldn’t understand your logic in this condition

[php]strlen($data) == 0[/php]

I think It will always evaluate to false ???

It is supposed to check if string length is equal to 0 (field is empty)

It seems to work as it is supposed to every field I have.
If I leave the field empty, it prints error but if I add at least 1 character, it works.

Ok. Thanks for explaining though I understood your logic right after hitting the submit button on the previous post. :smiley:

Sponsor our Newsletter | Privacy Policy | Terms of Service