我想使用pregmatch验证电子邮件地址域。我在电子邮件列表数组中插入了有效的edu域名,因此当用户输入该条目首先检查电子邮件列表数组时的电子邮件地址。如果它可用,那么它是验证。我正在服务器端做验证部分..任何帮助都是熟悉的。谢谢先进...
<?php
$email = $_POST['email']; // get the email value
$email_exp = explode("@",$email); // split email
$email_name = $email_exp[1]; // get the domain of email address
$email_list = array("berkely.edu","ucfs.edu","udef.edu","ucms.edu","ucef.edu"); // valid edu domain
for($i=0;$i<sizeof($email_list);$i++)
{
if(in_array($email_name,$email_list))
{
if (preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $email_name))
{
// validate email
}
}
}
答案 0 :(得分:1)
使用filter_var
,并用preg_match
来代替if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) == TRUE) {
// email is valid
}
来电。
<?php
$email = $_POST['email']; // get the email value
$email_exp = explode("@",$email); // split email
$email_name = $email_exp[1]; // get the domain of email address
$email_list = array("berkely.edu","ucfs.edu","udef.edu","ucms.edu","ucef.edu");
$email_is_valid = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) == TRUE;
if($email_is_valid && in_array($email_name,$email_list) ) {
// email is valid for your purposes
}
因此,更新的代码将是:
{{1}}