验证 php 中是否输入了电子邮件

当邮箱中没有输入任何内容时,如何提示输入电子邮件?验证用户端输入的电子邮件的最简单方法是什么。


<?php

## CONFIG ##

# LIST EMAIL ADDRESS

$recipient = "test@test.com";

# SUBJECT (Subscribe/Remove)

$subject = "Subscribe";

# RESULT PAGE

$location = "index.html";

## FORM VALUES ##

# SENDER - WE ALSO USE THE RECIPIENT AS SENDER IN THIS SAMPLE

# DON'T INCLUDE UNFILTERED USER INPUT IN THE MAIL HEADER!

$sender = $recipient;

# MAIL BODY

$body .= "Email: ".$_REQUEST['Email']." \n";

# add more fields here if required 

## SEND MESSGAE ##

mail( $recipient, $subject, $body, "From: $sender" ) or die ("Mail could not be sent.");

## SHOW RESULT PAGE ##

header( "Location: $location" );

?>

这是在html上的


<form action="subscribe.php" class="form-inline" method="POST">

<input class="form-control " name="Email" placeholder="Email" type="Email"/>

<button class="btn btn-default" type="submit" >

Request Info

</button>

</form> 


慕斯709654
浏览 131回答 2
2回答

LEATH

您缺少一个required属性:<input class="form-control " name="Email" placeholder="Email" type="Email" required />顺便说一句:Laravel 框架中使用的电子邮件验证是filter_var($email, FILTER_VALIDATE_EMAIL) !== false;希望能帮助到你。UPD:我已经提到了验证电子邮件字段值的两种可能方法,在用户端执行此操作的最简单方法是添加required属性。但是强烈建议在前端和后端添加验证。

慕婉清6462132

我最近必须为我正在开发的网络应用程序创建电子邮件验证...这就是我所做的:$email = $_POST[‘emailTextBox’];if (!strpos($email, ‘@‘) || !strpos($email, ‘.’)) {&nbsp; &nbsp; &nbsp; echo “Email is invalid”;} else {&nbsp; &nbsp; &nbsp; // code}要检查是否输入了电子邮件,您可以执行以下操作:$email = $_POST[‘emailTextBox’];if (empty($email)) {&nbsp; &nbsp; echo “Email is required”;} else {&nbsp;&nbsp; &nbsp; // code}您可以回显 HTML 代码,使其看起来像实际的错误框,而不是回显纯消息。另外,在输入电子邮件的输入标签中添加“必需”。但请注意,用户可以轻松地进入浏览器上的“检查元素”并删除“必需”,因此我将添加我建议的上述代码。
打开App,查看更多内容
随时随地看视频慕课网APP