0

Here is my code, which uses an extended phpMailer. This works if I check for firstResult and lastResult, but not if I check for emailResult:

$validator = new FormValidator();
            $firstResult = $validator->checkFirst($_POST['firstname']);
            $lastResult = $validator->checkLast($_POST['lastname']);
            $emailResult = $validator->checkEmail($_POST['emailaddress1']);
            var_dump($emailResult);
            if (is_null($firstResult) && is_null($lastResult) && is_null($emailResult)) {

                $mail = new ULSMail();

                $mail->IsSMTP();  // telling the class to use SMTP
                $mail->AddAddress("[email protected]");

                $mail->Subject  = "test";
                $mail->MsgHTML($messageHTML);

                redirectULS('english/forms/thankyou.php');

                if(!$mail->Send()) {
                    echo 'Message was not sent.';
                    echo 'Mailer error: ' . $mail->ErrorInfo;
                } else {

                    //$bridge->pushLead($lead);
                }
            } else {
                //...
            }

and in my FormValidator class:

function checkEmail($email){

        if(strlen(trim($email)) < 8){
        return 'Please enter a valid email address of more than 8 characters</span>';
    } else {
        return NULL;
    }

}

redirectULS is a simple redirect function for internal redirects on my site. It works as long as I don't check for $emailResult.

4
  • how do you know it does not return NULL? Commented Dec 27, 2010 at 19:58
  • I test for it elsewhere and the test returns some kind of value, but not one I can seem to output. Commented Dec 27, 2010 at 20:05
  • 1
    If using PHP 5.2 or higher, it now has built in email checks - uk.php.net/filter_var Commented Dec 27, 2010 at 20:07
  • 3
    Some small advice..let the function return true or false. This will make it more reusable. Let the script that calls the function determine if to show the errortext or do something else. Commented Dec 27, 2010 at 20:11

3 Answers 3

3

Actually, it does!

if (checkEmail("123456789") === NULL)
  print "Actually, it does!\n";

What you're doing is printing the result directly, which will cast NULL to an empty string. Hence it only appears to return nothing.

Notice that I made use of the triple equals operator, which tests for equality in value AND type. As @Tomcat suggests, you can also use is_null()

Sign up to request clarification or add additional context in comments.

9 Comments

But if I test for whether it is null, it tests as not null if $email is 8 characters or longer.
@shummel How are you testing for NULL? The above code disagrees with you.
@shummel7845 That means your test for null is wrong. Try marcog's example code.
@marcog Just as I was about to edit it ... definitely use ===, not ==, to check for NULL.
@phihag Yes, I just realised PHP had the === operator. My PHP is a little rusty.
|
1

Actually, it returns NULL when your string is lower than 8. Try var_dump instead of echo/print, it should display the "real" value (=NULL) of your string.

Comments

0

How are you testing the return?

echo is_null(checkEmail('[email protected]')) ? 'Pass' : 'Fail';

Also just a note; your validation parameters of 8 characters will fail on an email such as [email protected]. While surely rare, it would be suggested you validate based on form using Regular Expressions. Plenty of examples on the web.


class Validator{

    public function checkEmail($email){
        return preg_match('/^[^@]+@[a-zA-Z0-9._-]+\.[a-zA-Z]+$/', $email);
    }

    public function checkName($name){
        return preg_match('/[a-z]{2,}/i', $name); //valid if $name is 2+ A-Z characters
    }

}

$v = new Validator;
if($v->checkEmail($_POST['email'])
&& $v->checkName($_POST['fname'])
&& $v->checkName($_POST['lname'])){
    //the info is valid
}else{
    //the info is not valid
}

6 Comments

It'll also fail on 123456789 as my answer showed without actually pointing it out. :)
Tomcat--I'm open to using your function above but what does it return? Is it true if it matches and false if it doesn't?
@shummel7845; That's correct. making it easy to use if(validateEmail($user_email)){ ... }, though you may want to explore other regex patterns. I just pulled that one from PHP doc comments. There are far stricter and looser ones, and you'll surely be able to find or create one that fits your needs.
Tomcat, here is what is troubling me: $emailResult = $validator->validateEmail($_POST['emailaddress1']); if (is_null($firstResult) && is_null($lastResult) && $emailResult != FALSE) { ...} The code in the braces never gets executed unless I remove the email part. Thoughts? I've also tried $emailResult = TRUE and $emailResult.
Well, I'm not certain of how $firstResult is determined; I'm guessing you're using a function similar to your email validation of previous. My suggestion is to modify your other validation functions to return boolean values also, therefore allowing your condition to be if($validator->checkFirst($first) && $validator->checkLast($last) && $validator->checkEmail($email)){ stuff_is_valid(); }. Note, I've modified the naming conventions for consistency, something I strongly suggest you do also :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.