如何从表单帖子中检查电子邮件地址中的@

时间:2015-06-04 16:33:27

标签: php forms email login verification

我有一张登录和注册的表单。

我的表格

<form method="POST" action="login.php">
    <input type="text" name="mail" value="Input your email"/>
    <input type="submit" value="Check"/>
</form>

如果有人输入了他们的电子邮件,我想检查地址中是否有@。我尝试过使用数组,但这不起作用。

1 个答案:

答案 0 :(得分:1)

您可以使用php函数strpos http://php.net/strpos

if(strpos($myEmailPostVariable, '@') === FALSE) {
    // do things here to say it failed
}

如果您已修复使用数组,则可以使用explode http://php.net/explode

$parts = explode('@', $myEmailPostVariable);
if(count($parts) != 2) {
    // do things here to say it failed
}

请记住,数组方式不是很好,因为搜索字符串更容易,更快,更易读。

正如@jeroen建议您要验证电子邮件,然后使用filter_input()是最好的...

if(filter_input(INPUT_POST, 'mail', FILTER_VALIDATE_EMAIL) === FALSE) {
    // do things here to say it failed
}