无法解决PHP联系表单验证问题


cant figure out php contact form verification woes

嗨,我从互联网上的脚本得到了一个联系人,我一直在搞乱,唯一的问题是,我试图添加到它的验证只是不起作用。基本上在表单中它有名字,电子邮件,号码,类型和评论。我的主要验证问题是数字字段,我希望它是空的,如果它是空的,它会回显"没有数字",当人们输入字母而不是数字时,它会回显"你需要输入数字"。比如lol。但我被卡住了。你们这些天才有谁能帮帮我吗?提前感谢下面是完整的代码。注:对不起,我不小心切断了脚本:$

<?php
$nowDay=date("d.m.Y");
$nowTime=date("H:i:s");
$subject = "E-mail from my site!";  
if (isset($_POST['submit'])) {
    //contactname
    if (trim($_POST['name'] == '')) {
        $hasError = true;
    } else {
        $name = htmlspecialchars(trim($_POST['name']));
    }
    //emailaddress  
    if (trim($_POST['email'] == '')) {
        $hasError = true;
    } else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+'.[a-z]{2,4}$/i",trim($_POST['name']))) {
        $hasError = true;
    } else {
        $email = htmlspecialchars(trim($_POST['email']));
    }            
    //phonenumber    
    if (trim($_POST['number'] == '')) 
    {
       $fake_num = true;
    }
    else if(!is_numeric($_POST['phonenumber']))
    {
        $fake_num = true;
    }
    else 
    {
        $number = htmlspecialchars(trim($_POST['number']));
    }
    //type
    $type = trim($_POST['type']);
    //comment    
    if (trim($_POST['comment'] == '')) {
        $hasError = true;
    } else {
        $comment = htmlspecialchars(trim($_POST['comment']));
    }               
    if (!isset($hasError) && !isset($fake_num)) {
        $emailTo = 'email@hotmail.com';
        $body = "   Name: $name'n'n
                    Email: $email'n'n
                    Phone number: $number'n'n
                    Type: $type'n'n
                    Comment: $comment'n'n
                    'n This message was sent on: $nowDay at $nowTime";
        $headers = 'From: My Site <'.$emailTo.'>'."'r'n" .'Reply-To: '. $email;
        mail($emailTo, $subject, $body, $headers);
        $emailSent = true;
    }
}
?>

<?php
if(isset($hasError))
{
    echo"<p>form has error</p>";
}
?>
<?php           
if(isset($fake_num))
{
    echo"<p>wrong num</p>";
}
?>
<?php
if(isset($emailSent) && $emailSent == true)
{ 
    echo "<p><strong>Email Successfully Sent!</strong></p>";
    echo "<p>Thank you <strong> $name </strong> for using my contact form! Your email was successfully sent and I will be in touch with you soon.</p>";
} 
?>

<div id="stylized" class="myform">
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="bookingform" id="bookingform">
        <h1>Booking form</h1>
        <label>
            Name
            <span class="small"></span>
        </label>
        <input type="text" name="name" id="name" class="required"/>
        <label>
            Your email:
            <span class="small"></span>
        </label>
        <input type="text" name="email" id="email" class="required"/>
        <label>
            Contact Number:
            <span class="small"></span>
        </label>
        <input type="text" name="number" id="number" class="required" />                                                    
        <label>
            Car required:
            <span class="small"></span>
        </label>
        <select name="type" id="type">
            <option selected="selected">Manual</option>
            <option>Automatic</option>
      </select>   
        <label>
            Comment:
            <span class="small"></span>
        </label>
        <textarea cols="" rows="" name="comment" id="comment" class="required"></textarea>                
        <button type="submit" name="submit">Submit</button> 
    </form>
</div>

检查是否输入了数字,可以使用:

if (!preg_match('/^?[0-9]{0,10}$/', $_POST['number'])) {
echo "Please enter a valid number"; //Failed to meet criteria
}

在这里,您还可以使用大括号{0,10}指定组成有效号码的数字数量,在这种情况下,数字最长可以为11位。如果您要求它只有11位长,那么使用{11}。如果要检查是否输入了数字,可以使用:

if (empty($_POST['number'])) {
echo "Please enter a valid number"; //Field was left empty
}

PHP有一个内置的邮件验证函数,你可以使用

filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)

如果输入的电子邮件格式正确,则返回true。