0
如何建立客戶端驗證,僅使用JavaScript檢查空字段並在服務器端使用PHP驗證電子郵件和用戶名。我是一名初學者。客戶端和服務器端(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[[email protected]]" ;
}
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>
任何建議所做的一切???
所以......有什麼用,這個問題?有用?有什麼錯誤?什麼? – arieljuod
請讓我們知道您在代碼中面臨的困難。清楚地向我們解釋你需要什麼。這將幫助我們回答你的問題,你非常歡迎StackOverflow btw :) – Kirk