好的,我想要实现的是一个非常简单的表单验证,如下所示。
- 名称:[必填,最小长度:2,最大长度:255]
- 电子邮件:[必填,最小长度:3,最大长度:255,有效的电子邮件格式]
- 出生日期:[可选,格式:dd/mm/yyyy]
但是,一旦我单击提交(如果字段为空或已填充),我会在空白页上显示所有回显的错误。
“名称必须至少 2 个字符名称是必需的电子邮件必须至少 3 个字符无效电子邮件电子邮件不能留空”
到目前为止我的代码:
索引.php
<form method="post" action="confirm.php">
Name:<input type="text" name="name" />
email:<input type="text" name="email" />
DOB:<input type="date" name="dob" />
<input type="submit" value="submit" />
</form>
和
确认.php
<?php
$name = $_POST['$name'];
$email = $_POST['$email'];
$dob = $_POST['$dob'];
$namelen = strlen($email);
$emaillen = strlen($email);
$max = 255;
$minname = 2;
$minemail = 3;
if($namelen<$minname){
echo"name must be at least 2 characters";
}
elseif($namelen>$max){
echo"name must be less than 255 characters";
}
if(empty($name)){
echo"name is required";
}
else{
continue;
}
if($emaillen<$minemail){
echo"email must be at least 3 characters";
}
elseif($emaillen>$max){
echo"email must be less than 255 characters";
}
if(filter_var($email, FILTER_VALIDATE_EMAIL)){
continue;
}
else{
echo"invalid email";
}
if(empty($email)){
echo"email cannot be left empty";
}
else{
continue;
}
?>
非常感谢您的帮助,谢谢。