无法弄清楚 php 联系表单验证问题

发布于 2024-12-23 00:24:52 字数 3999 浏览 1 评论 0原文

您好,我从互联网脚本中得到了一个联系人,我一直在搞乱,唯一的问题是我试图添加到它的验证不起作用。基本上它的形式有姓名、电子邮件、号码、类型和评论。我的主要验证问题是数字字段,我希望它是空的,它会回显“无数字”,当用户输入字母而不是数字时,它会回显“您需要输入数字”。像哈哈之类的东西。但我被困住了。你们哪位天才可以帮助我吗?提前致谢,下面是完整的代码。 PS 对上一篇文章感到抱歉,我不小心切断了脚本:$

<?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 protected]';
        $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>

Hi I got a contact from script the internet that I have been messing around with, only problem is that the verification that I am trying to add to it just doesn't work. Basically in the form it has name, email, number, type and comment. My main verification woes are with the number field I would like it so that if it is empty it echos "no number" and when the person type in letters instead of numbers it will echo "you need to type with numbers". Something like lol. but I’m stuck. Can any of you geniuses help me? Thanks in advance here is the full code below. p.s. sorry about previous post i accidently cut off the script:$

<?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 protected]';
        $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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

中二柚 2024-12-30 00:24:53

要检查是否已输入数字,您可以使用:

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。

For checking whether a number has been entered you can use:

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

Here you can also specify the amount of numbers that would constitute your valid number with the braces {0,10}, in this case, the number can be up to 11 digits long. If you required it to be only 11 digits long, then use {11}.
If you wish to check if a number has been entered at all you can use:

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

PHP does have a built-in email validation function that you can use

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

which returns true if the entered email is in the correct format.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文