使用PHP验证用户输入的电子邮箱是否合法,正则表达式写法如下:
$pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
那么,只需要:
PHP Code复制内容到剪贴板
- $email = strip_tags(trim($_POST["email"]));
- $pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
- if($email==""){
- echo "<div align='center'><font color=red size='2pt'>Please enter the email</font></div>";
- $check = false;
- }else{
- if(!preg_match($pattern,$email)){
- echo "<div align='center'><font color=red size='2pt'>Please make sure your email address is correct,otherwise the recipient will not be able to reply</font></div>";
- $check = false;
- }
- }
即可。