如何使用JavaScript检查空字段以及在服务器端使用PHP验证电子邮件,用户名,以便进行客户端验证。我是初学者。
到目前为止,我已在客户端完成
<script type="text/javascript">
function checkme()
{
if(document.myform.uname.value == "")
{
alert("Please Enter your User Name")
document.myform.uname.focus();
return false;
}
if(document.myform.mail.value == "")
{
alert("Please Enter Your E-mail Address");
document.myform.mail.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<form name="myform" method="POST" action="index.php" onsubmit="return checkme();">
User Name : <input type="text" name="uname" /><label> * </label>
<br />
E-Mail : <input type="text" name="mail" /><label> * </label>
<br />
<input type="submit" name="register" value="Register" />
</form>
</body>
到目前为止,我已在服务器端完成
<?php
$uname = $_POST["uname"]; $unameerr = $mailerr = "" ;
$mail = $_POST["mail"] ;
if (! isset($_POST['register']))
{
echo "You need to submit the form " ;
}
if (empty($_POST["uname"]) || empty($_POST["mail"]))
{
echo "All fields are required" ;
exit ;
}
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if(strlen($_POST["uname"])< 2 || (strlen($_POST["uname"])>12))
{
$unameerr = "Please Enter your USER NAME with in 2-12 characters" ;
}
else
{
$uname = $_POST["uname"] ;
}
if(!preg_match("/^[A-Za-z0-9_.]+\@+[A-Za-z]+\.+[a-z{2,4}]$/", $mail))
{
$mailerr = "Please Enter your email address in correct format[abc@abc.com]" ;
}
else
{
$mail = $_POST["mail"] ;
}
}
echo "Your inputs are :" ;
echo "<br />" ;
echo "Your User name is : " . $uname ;
echo "<br />" ;
echo "Your Registered E-mail is : " . $mail ;
?>
<form> <input type="button" value="Print A Copy" onsubmit="window.print()" /> </form>
任何建议???