电子邮件验证,使用 if else 语句将不允许它继续检查第一个 if 是否有错误?

发布于 2024-10-07 17:11:05 字数 1063 浏览 8 评论 0原文

我有:

if(isset($_POST['submit'])) {
    if (empty($name)) {
        echo'<span class="error">ERROR: Missing Name </span><br/>';
    } else if(empty($phone) || empty($email)) {
        echo'<span class="error">ERROR: You must insert a phone number or email</span><br/>';
    } else if(!preg_match('/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/', $email)) {
        echo'<span class="error">ERROR: Please Insert a valid Email</span><br/>';
    }  else {
        mail( "[email protected]", "Monthly Specials Email",
            "Name: $name 
            Email: $email 
            Phone Number: $phone
            Comment: $comment", "From: $email" );
        echo'<span id="valid">Message has been sent</span><br/>';
    }
}

在不使用 else if 的情况下,我还能如何检查所有这些问题? 当我使用 else if 时,它会检查第一个 if 语句,如果存在问题,它将不会继续检查该语句后面的其他 if 语句。

有什么想法吗?谢谢

I have:

if(isset($_POST['submit'])) {
    if (empty($name)) {
        echo'<span class="error">ERROR: Missing Name </span><br/>';
    } else if(empty($phone) || empty($email)) {
        echo'<span class="error">ERROR: You must insert a phone number or email</span><br/>';
    } else if(!preg_match('/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/', $email)) {
        echo'<span class="error">ERROR: Please Insert a valid Email</span><br/>';
    }  else {
        mail( "[email protected]", "Monthly Specials Email",
            "Name: $name 
            Email: $email 
            Phone Number: $phone
            Comment: $comment", "From: $email" );
        echo'<span id="valid">Message has been sent</span><br/>';
    }
}

How else could I check for all of those issues without using else if?
When I use else if, it checks through the first if statement, if there is an issue with it it will not continue going through the other if statements following that one.

Any ideas? Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

隐诗 2024-10-14 17:11:05

您可以将所有错误收集在一个数组中,如下所示:

if (isset($_POST['submit'])) {
    $errors = array();
    if (empty($name)) {
        $errors[] = '<span class="error">ERROR: Missing Name </span><br/>';
    }
    if (empty($phone) || empty($email)) {
        $errors[] = '<span class="error">ERROR: You must insert a phone number or email</span><br/>';
    }
    if (!preg_match('/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/', $email)) {
        $errors[] = '<span class="error">ERROR: Please Insert a valid Email</span><br/>';
    }
    if ($errors) {
        echo 'There were some errors: ';
        echo '<ul><li>', implode('</li><li>', $errors), '</li></ul>';
    } else {
        mail( "[email protected]", "Monthly Specials Email",
            "Name: $name\n".
            "Email: $email\n".
            "Phone Number: $phone\n".
            "Comment: $comment", "From: $email");
        echo'<span id="valid">Message has been sent</span><br/>';
    }
}

通过此,您可以检查所有要求并报告所有错误,而不仅仅是第一个错误。

You could collect all errors in an array like this:

if (isset($_POST['submit'])) {
    $errors = array();
    if (empty($name)) {
        $errors[] = '<span class="error">ERROR: Missing Name </span><br/>';
    }
    if (empty($phone) || empty($email)) {
        $errors[] = '<span class="error">ERROR: You must insert a phone number or email</span><br/>';
    }
    if (!preg_match('/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/', $email)) {
        $errors[] = '<span class="error">ERROR: Please Insert a valid Email</span><br/>';
    }
    if ($errors) {
        echo 'There were some errors: ';
        echo '<ul><li>', implode('</li><li>', $errors), '</li></ul>';
    } else {
        mail( "[email protected]", "Monthly Specials Email",
            "Name: $name\n".
            "Email: $email\n".
            "Phone Number: $phone\n".
            "Comment: $comment", "From: $email");
        echo'<span id="valid">Message has been sent</span><br/>';
    }
}

With this you can check all requirements and report all errors and not just the first one.

许久 2024-10-14 17:11:05

使用:

$error = 0;
if(empty($var1)){ $error = 1; }
if(empty($var2)){ $error = 1; }
if(empty($var3)){ $error = 1; }
if(empty($var4)){ $error = 1; }
if(empty($var5)){ $error = 1; }

if($error > 0)
{
   // Do actions for your errors
}
else
{
   // Send Email
}

use:

$error = 0;
if(empty($var1)){ $error = 1; }
if(empty($var2)){ $error = 1; }
if(empty($var3)){ $error = 1; }
if(empty($var4)){ $error = 1; }
if(empty($var5)){ $error = 1; }

if($error > 0)
{
   // Do actions for your errors
}
else
{
   // Send Email
}
夏末染殇 2024-10-14 17:11:05

您可以使用 try...catch 语句进行错误检查,如下所示。
每当遇到应该生成错误的情况时,都可以使用抛出新的异常子句。

you can use try...catch statements for error checking like this.
whenever you encounter a condition where an error should be generated, you can use throw new Exception clause.

冷了相思 2024-10-14 17:11:05

使用脏标志。检查所有这些并将消息附加到脏变量中。

Use a dirty flag. Check them all and append the message to the dirty variable.

许一世地老天荒 2024-10-14 17:11:05

试试这个:

if(isset($_POST['submit'])) {
    $errors = array();

    if (empty($name)) {
        $errors[] = 'Missing Name';
    }
    if(empty($phone) || empty($email)) {
        $errors[] = 'You must insert a phone number or email';
    }
    if(!preg_match('/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/', $email)) {
        $errors[] = 'Please Insert a valid Email';
    }

    if (!empty($errors)) {
        for ($i = 0; i < count($errors); $i++) {
            echo sprintf('<span class="error">ERROR: %s</span><br/>', $errors[$i]);
        }
    }  else {
        mail( "[email protected]", "Monthly Specials Email",
                                            "Name: $name 
                                             Email: $email 
                                             Phone Number: $phone
                                             Comment: $comment", "From: $email" );
        echo'<span id="valid">Message has been sent</span><br/>';
     }
}

Try this:

if(isset($_POST['submit'])) {
    $errors = array();

    if (empty($name)) {
        $errors[] = 'Missing Name';
    }
    if(empty($phone) || empty($email)) {
        $errors[] = 'You must insert a phone number or email';
    }
    if(!preg_match('/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/', $email)) {
        $errors[] = 'Please Insert a valid Email';
    }

    if (!empty($errors)) {
        for ($i = 0; i < count($errors); $i++) {
            echo sprintf('<span class="error">ERROR: %s</span><br/>', $errors[$i]);
        }
    }  else {
        mail( "[email protected]", "Monthly Specials Email",
                                            "Name: $name 
                                             Email: $email 
                                             Phone Number: $phone
                                             Comment: $comment", "From: $email" );
        echo'<span id="valid">Message has been sent</span><br/>';
     }
}
独享拥抱 2024-10-14 17:11:05
if(isset($_POST['submit'])) {
    $valid = true;
    if (empty($name)) {
        echo'<span class="error">ERROR: Missing Name </span><br/>';
        $valid = false;
    }  

    if(empty($phone) || empty($email)) {
        echo'<span class="error">ERROR: You must insert a phone number or email</span><br/>';
        $valid=false;
    } 

    if(!preg_match('/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/', $email)) {
        echo'<span class="error">ERROR: Please Insert a valid Email</span><br/>';
        $valid = FALSE;
    }  

    if($valid) {
        mail( "[email protected]", "Monthly Specials Email",
            "Name: $name 
            Email: $email 
            Phone Number: $phone
            Comment: $comment", "From: $email" );
        echo'<span id="valid">Message has been sent</span><br/>';
    }
}
if(isset($_POST['submit'])) {
    $valid = true;
    if (empty($name)) {
        echo'<span class="error">ERROR: Missing Name </span><br/>';
        $valid = false;
    }  

    if(empty($phone) || empty($email)) {
        echo'<span class="error">ERROR: You must insert a phone number or email</span><br/>';
        $valid=false;
    } 

    if(!preg_match('/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/', $email)) {
        echo'<span class="error">ERROR: Please Insert a valid Email</span><br/>';
        $valid = FALSE;
    }  

    if($valid) {
        mail( "[email protected]", "Monthly Specials Email",
            "Name: $name 
            Email: $email 
            Phone Number: $phone
            Comment: $comment", "From: $email" );
        echo'<span id="valid">Message has been sent</span><br/>';
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文