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@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>
4

1 に答える 1

2

番号が入力されているかどうかを確認するには、次を使用できます。

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 を返します。

于 2011-12-25T23:23:39.393 に答える