Bootstrap 验证邮件是否有效并拨打电话

f4t66c6m  于 2022-12-08  发布在  Bootstrap
关注(0)|答案(2)|浏览(213)

我遇到了一个问题。当我们在联系表格中填写了一个无效的电子邮件或电话号码时,我甚至还没有收到电子邮件,就会出现 Bootstrap 警报,显示为警报-成功
contact.php

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

if(isset($_POST["send"])){

    $body = $_POST['message'];
    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];
    

    $mail = new PHPMailer(true);
    
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = '73.auto.73@gmail.com';
    $mail->Password = 'rlylecrtuvztqosz';
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
    $mail->Port = 465;      
    $mail->setFrom('73.auto.73@gmail.com');

    $mail->addAddress($_POST["email"]);

    $mail->isHTML(true);

    $mail->Subject = 'Projet web';
    $mail->Body = "Message:" . $body . "<br>Phone number: " . $phone . "<br>Name: " . $name . "<br>Mail: " . $email;

    $mail->send();

    echo "success";
    die;
}

index.php

<div class="col-md-4 mb-3 mb-md-0" data-aos="fade-left">
                    <form id="contactForm" action="contact.php" method="POST">
                    <input type="hidden" name="send">
                        <div class="alert alert-success" id="success-message" role="alert">
                            A simple success alert—check it out!
                        </div>

                        <div class="alert alert-danger" id="error-message" role="alert">
                            A simple success alert—check it out!
                        </div>

                        <div class="input-group mb-3">
                            <span class="input-group-text" id="basic-addon1"><i class="bi bi-person-fill"></i></span>
                            <input type="text" id="name" name="name" class="form-control" placeholder="Nom Prénom"
                            aria-label="Nom Prénom" aria-describedby="basic-addon2" required>
                        </div>
                        <!-- Email address input -->
                        <div class="input-group mb-3">
                            <span class="input-group-text" id="basic-addon1">@</span>
                            <input type="text" id="email" name="email" class="form-control" placeholder="Email"
                            aria-label="email" aria-describedby="basic-addon1" required>
                        </div>
                        <div class="input-group mb-3">
                            <span class="input-group-text" id="basic-addon1"><i class="bi bi-telephone-fill"></i></span>
                            <input type="text" id="phone" name="phone" class="form-control" placeholder="Téléphone"
                            aria-describedby="basic-addon2" required>
                        </div>
                        <!-- Message input -->
                        <div class="mb-3">
                            <textarea class="form-control" id="exampleFormControlTextarea1" rows="3"
                            placeholder="Decrivez le plus possible votre projet" name="message" required></textarea>
                        </div>
                        <!-- Form submit button -->
                        <div class="d-grid">
                            <button class="btn btn-primary btn-lg" name="send" type="submit" id="send-form">Submit</button>
                        </div>
                    </form>

                </div>

    <script>
        AOS.init();
        $(document).ready(function(){
            $('#contactForm').submit(function(e){
                e.preventDefault();
                const $this = $(this);

                // disable submit button
                const $button = $(this).find('button[type="submit"]').text('Submit...').attr('disabled', 'disabled');

                // send message
                $.ajax({
                    type: 'POST',
                    url: 'contact.php',
                    data: $this.serialize(),
                    success: function(data){
                        $('#success-message').css('display', 'block');
                         $this[0].reset(); // reset form
                    },
                    error: function(jqXHR, textStatus, errorThrown){
                        $('#error-message').css('display', 'block');
                    },
                    complete: function(jqXHR, textStatus){
                        // enable submit button
                        $button.text('Submit').removeAttr('disabled');
                    }
                })
            })
        })
    </script>

上面你可以看到的代码有jquery AJAX ,php.我想知道我怎么做,使它验证电子邮件,如果它是一个书面电子邮件(也就是说在这种格式:mymail@mail.com我收到的电子邮件时,我联系,如果电子邮件是无效的(@mail.com)有引导警报,出现写电子邮件不发送,因为电子邮件是无效的我想这样做的电子邮件和电话,以便只有数字可以输入。我该怎么做?

gfttwv5a

gfttwv5a1#

首先,始终使用type=“email”作为电子邮件,其次,永远不要仅在客户端站点进行验证,您必须在两端进行验证,即:HTML和PHP,在PHP端验证此为:

$email_val = $_POST['email'];
if(filter($email_val, FILTER_VALIDATE_EMAIL)){
    // enter code here
}

同样,您可以通过应用regex验证电话号码。

7nbnzgx9

7nbnzgx92#

if ($mail->send()) {
    echo "success";
} else {
    echo "unnable to sent mail id error!!!";
}

相关问题