简单JS和PHP正则验证

 <?php
if(isset($_POST['action']) && $_POST['action']=='submitted')
{
    $email=$_POST['email'];
    if(!preg_match("/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/",$email))
     {
        echo "电子邮件检测失败";
     }
    else
     {
        echo "电子邮件检测成功";
     }
}
else
{
?>
<html>
<head><title>EMAIL检测</title>
<script type="text/javascript">
function checkEmail(sText)
     {
        var reg=/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
        var email=document.getElementById(sText).value;
        if(!reg.test(email))
         {
             alert("电子邮件检测失败");
         }
        else
         {
             alert("电子邮件格式正确");
         }
     }
</script>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
电子邮件:<input type="text" id="email" name="email" /><br />
<input type="hidden" name="action" value="submitted" />
<input type="button" name="button" value="客户端检测" onclick="checkEmail('email')" />
<input type="submit" name="submit" value="服务器端检测" />
</form>
</body>
</html>
<?php
}
?>

你可能感兴趣的:(简单JS和PHP正则验证)