一 代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>验证电子邮箱格式</title>
</head>
<body>
<form name="form" method="post" action="#">
<label>请输入邮箱:</label>
<input type="text" name="mail" id="mail" value="<?php echo $_POST['mail'];?>">
<input type="submit" name="sub" value="测试"><p>
</form>
<?php
$checkmail="/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/";//定义正则表达式
if(isset($_POST['mail']) && $_POST['mail']!=""){ //判断文本框中是否有值
$mail=$_POST['mail']; //将传过来的值赋给变量$mail
if(preg_match($checkmail,$mail)){ //用正则表达式函数进行判断
echo "电子邮箱格式正确";
}else{
echo "电子邮箱格式不正确";
}
}
?>
</body>
</html>
二 运行结果