Php 我使用的是一个;如果..elseif..else“;陈述电子邮件验证
嗨:)这是我第一次在这里发帖,但我想不出来,应该很简单。我想我只是看了太久了。所以我有一个表单,我正在为它执行表单验证,所有的验证工作都在进行,它会发送到数据库 我遇到的一个小问题是,当涉及到电子邮件和确认电子邮件验证时,第一个if语句检查文本框是否为空,如果为空,我应该得到“email is required”消息。但是由于第二条if语句,我认为$emailErr变量会被第二条错误消息覆盖,只有在电子邮件语法无效时才会出现 因此,如果我将文本框留空,我仍然会收到“syntax invalid”消息,而不是“email is required”消息 我的困惑来自这样一个事实,例如,我的“firstname”验证(和所有其他验证)几乎是相同的想法,但它们不会被第二条错误消息覆盖,第二条错误消息也是通过使用第二条if语句呈现的 我将复制我的名字验证代码和电子邮件验证代码,以便您了解我在说什么。任何帮助都将不胜感激。如果没有,我相信我最终会解决的:)谢谢 名字验证-如果我将文本框留空,我会收到错误消息“需要名字”-这是正确的Php 我使用的是一个;如果..elseif..else“;陈述电子邮件验证,php,Php,嗨:)这是我第一次在这里发帖,但我想不出来,应该很简单。我想我只是看了太久了。所以我有一个表单,我正在为它执行表单验证,所有的验证工作都在进行,它会发送到数据库 我遇到的一个小问题是,当涉及到电子邮件和确认电子邮件验证时,第一个if语句检查文本框是否为空,如果为空,我应该得到“email is required”消息。但是由于第二条if语句,我认为$emailErr变量会被第二条错误消息覆盖,只有在电子邮件语法无效时才会出现 因此,如果我将文本框留空,我仍然会收到“syntax invalid”
//Check if the firstname textbox is empty
if (empty($_POST['fname']))
//Show error message
{
$fnameErr = "First name is required";
}
//Check if fname is set
elseif (isset($_POST['fname']))
//Check the text using the test_input function and assign it to $fname
{$fname = test_input($_POST['fname']);}
//Check if first name contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$fname))
//Show error message & unset the fname variable
{
$fnameErr = "Only letters and white space allowed";
unset($_POST['fname']);
}
else
//Check the text using the test_input function and assign it to $fname
{$fname = test_input($_POST['fname']);}
电子邮件验证-如果我将文本框留空,我会收到错误消息“无效电子邮件格式”-应该是“需要电子邮件”-为什么
//Check if the email textbox is empty
if (empty($_POST['email']))
//Show error message
{
$emailErr = "Email is required";
}
//Check if email is set
elseif (isset($_POST['email']))
//Check the text using the test_input function and assign it to $email
{$email = test_input($_POST['email']);}
//Check if e-mail syntax is valid
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
//Show error message & unset the email variable
{
$emailErr = "Invalid email format";
unset($_POST['email']);
}
else
//Check the text using the test_input function
{$email = test_input($_POST['email']);}
验证电子邮件的正确方法是使用
filter\u var
$email = filter_var(filter_var($_POST['email'],FILTER_SANITIZE_EMAIL),FILTER_VALIDATE_EMAIL)
if(!$email)
$invalidemailMessage = 'You have entered an invalid email address!';
故事结束了
如果您真的、真的、真的需要输出“需要电子邮件”:
验证电子邮件的正确方法是使用
filter\u var
$email = filter_var(filter_var($_POST['email'],FILTER_SANITIZE_EMAIL),FILTER_VALIDATE_EMAIL)
if(!$email)
$invalidemailMessage = 'You have entered an invalid email address!';
故事结束了
如果您真的、真的、真的需要输出“需要电子邮件”:
验证电子邮件的正确方法是使用
filter\u var
$email = filter_var(filter_var($_POST['email'],FILTER_SANITIZE_EMAIL),FILTER_VALIDATE_EMAIL)
if(!$email)
$invalidemailMessage = 'You have entered an invalid email address!';
故事结束了
如果您真的、真的、真的需要输出“需要电子邮件”:
验证电子邮件的正确方法是使用
filter\u var
$email = filter_var(filter_var($_POST['email'],FILTER_SANITIZE_EMAIL),FILTER_VALIDATE_EMAIL)
if(!$email)
$invalidemailMessage = 'You have entered an invalid email address!';
故事结束了
如果您真的、真的、真的需要输出“需要电子邮件”:
通过对当前代码进行一些调整,您可以保留它,尽管@tftd所说的关于消毒和验证是绝对正确的
$error = array();
if (empty($_POST['email'])) {
$error[__LINE__] = "Email is required";
} elseif (isset($_POST['email'])) {
$email = test_input($_POST['email']);
}
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) {
$error[__LINE__] = "Invalid email format";
unset($_POST['email']);
} else {
$email = test_input($_POST['email']);
}
if ($error){
print_r($error);
}
通过对当前代码进行一些调整,您可以保留它,尽管@tftd所说的关于消毒和验证是绝对正确的
$error = array();
if (empty($_POST['email'])) {
$error[__LINE__] = "Email is required";
} elseif (isset($_POST['email'])) {
$email = test_input($_POST['email']);
}
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) {
$error[__LINE__] = "Invalid email format";
unset($_POST['email']);
} else {
$email = test_input($_POST['email']);
}
if ($error){
print_r($error);
}
通过对当前代码进行一些调整,您可以保留它,尽管@tftd所说的关于消毒和验证是绝对正确的
$error = array();
if (empty($_POST['email'])) {
$error[__LINE__] = "Email is required";
} elseif (isset($_POST['email'])) {
$email = test_input($_POST['email']);
}
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) {
$error[__LINE__] = "Invalid email format";
unset($_POST['email']);
} else {
$email = test_input($_POST['email']);
}
if ($error){
print_r($error);
}
通过对当前代码进行一些调整,您可以保留它,尽管@tftd所说的关于消毒和验证是绝对正确的
$error = array();
if (empty($_POST['email'])) {
$error[__LINE__] = "Email is required";
} elseif (isset($_POST['email'])) {
$email = test_input($_POST['email']);
}
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) {
$error[__LINE__] = "Invalid email format";
unset($_POST['email']);
} else {
$email = test_input($_POST['email']);
}
if ($error){
print_r($error);
}
代码的部分问题是最后一个if仍在运行,因此如果email字段为空,您将始终收到错误 改变这个
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
对此
if (isset($email) && !preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
代码的部分问题是最后一个if仍在运行,因此如果email字段为空,您将始终收到错误 改变这个
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
对此
if (isset($email) && !preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
代码的部分问题是最后一个if仍在运行,因此如果email字段为空,您将始终收到错误 改变这个
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
对此
if (isset($email) && !preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
代码的部分问题是最后一个if仍在运行,因此如果email字段为空,您将始终收到错误 改变这个
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
对此
if (isset($email) && !preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
说真的,减少代码,直截了当地说,其他人会跑掉。事实上,电子邮件格式是不正确的,因为它是空白的,所以这是一个问题吗?2件事,1。为什么不检查<代码>字符串长度> 0代码/代码>(这将消除可能的空白字符串。2。您可能需要考虑用<代码> \p{L}验证FieldNeX。而不是
a-zA-z
cuz的名字,比如Björn,Paweł等,@Yisera OP仍然需要验证服务器端,前端不可信,因为javascript可以关闭。真的,减少代码,直截了当地说,其他人就会跑掉。事实上,电子邮件格式是不正确的,因为它是空白的,所以这是一个问题吗2件事,1。为什么不检查<代码>字符串长度> 0 < /代码>(这将消除可能的空白字符串。2),您可能想考虑用<代码> \p{L}验证FrestNordNo.而不是a-zA-z
cuz的名字,比如Björn,Paweł等,@Yisera OP仍然需要验证服务器端,前端不可信,因为javascript可以关闭。真的,减少代码,直截了当地说,其他人就会跑掉。事实上,电子邮件格式是不正确的,因为它是空白的,所以这是一个问题吗2件事,1。为什么不检查<代码>字符串长度> 0 < /代码>(这将消除可能的空白字符串。2),您可能想考虑用<代码> \p{L}验证FrestNordNo.而不是a-zA-z
cuz的名字,比如Björn,Paweł等,@Yisera OP仍然需要验证服务器端,前端不可信,因为javascript可以关闭。真的,减少代码,直截了当地说,其他人就会跑掉。事实上,电子邮件格式是不正确的,因为它是空白的,所以这是一个问题吗2件事,1。为什么不检查<代码>字符串长度> 0 < /代码>(这将消除可能的空白字符串。2),您可能想考虑用<代码> \p{L}验证FrestNordNo.而不是a-zA-z
cuz的名字,比如Björn、Paweł等,@Yisera OP仍然需要验证服务器端,前端不可信,因为javascript可以关闭。