简体   繁体   English

php电子邮件和电话号码验证

[英]php email and phone number validation

I currently have the following working code but I need to build on to this some email and phone number validation to make sure it is an actual email and phone number not other data.我目前有以下工作代码,但我需要在此基础上进行一些电子邮件和电话号码验证,以确保它是实际的电子邮件和电话号码,而不是其他数据。 I have found code to use我找到了要使用的代码

function valid_email($email) {
    return !!filter_var($email, FILTER_VALIDATE_EMAIL);
}

but I'm unsure how to incorporate this into my current code.但我不确定如何将其合并到我当前的代码中。 I would love some help with this.我希望得到一些帮助。

This is my current php code这是我当前的 php 代码

//load form validate
$firstName= $_POST['firstName'];
$lastName= $_POST['lastName']; 
$address = $_POST['address'];
$phoneNumber = $_POST['phoneNumber'];
$email= $_POST['email'];
$username= $_POST['username'];
$password = sha1 ($_POST['password']);
$role = 1;


//prepare input data in an array
$userdata = array($firstName, $lastName, $address, $phoneNumber, $email, $username, $password,$role);


//prepare error list
$errors = array ();

//Validation tests and store list
if ($firstName == "" || $lastName == "" || 
    $address == "" || $phoneNumber == "" || 
    $email == "" || $username == "" || 
    $password == "" || $role == "" ) {
    array_push($errors, "All form fields must be filled out before submitting.");
}

//if errors redirect back to form page and save attempted data.
if (count($errors) > 0) {
    $_SESSION['userdata'] = $userdata;
    $_SESSION['errors'] = $errors;
    header("Location: ../register.php");
}else{
    unset($_SESSION['userdata']);
    unset($_SESSION['errors']);
    $query = "INSERT INTO customers (firstName, lastName, address, phoneNumber, email, username, password) VALUES ('$firstName','$lastName', '$address', '$phoneNumber', '$email', '$username', '$password')";

    //die (print_r($query));
    if (mysqli_query($mysqli, $query)) {
        $last_id = $mysqli->insert_id;
        $query2 = "INSERT INTO users_in_roles (userID, roleID) VALUES ('$last_id', 1);";

        if ($mysqli->query($query2)) {
            header("Location: ../Login.php");
        }else {
            die(print_r($mysqli->error));
        }
    }
}

This is one example.这是一个例子。
I made a foreach with an array just to show you both outputs.我用一个数组做了一个 foreach 只是为了向你展示两个输出。

$email = ["abc@gmail.com","abc@gmailcom"];
foreach ($email as $e){
    if (filter_var($e, FILTER_VALIDATE_EMAIL)) {
        echo "valid";
    } else {
        echo "not valid";
    }
}

https://3v4l.org/GcKRp https://3v4l.org/GcKRp

In your case you should probably have it after you check if any of the fields are empty:在您的情况下,您可能应该在检查任何字段为空后拥有它:

if ($firstName == "" || $lastName == "" || 
        $address == "" || $phoneNumber == "" || 
        $email == "" || $username == "" || 
        $password == "" || $role == "" ) {
    array_push($errors, "All form fields must be filled out before submitting.");
}

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    array_push($errors, "Email is not valid.");
}

$phone = "11-331-11111";
$phone = preg_replace("/[^\d]/", "", $phone);
if (strlen($phone) <= 10) { 
    array_push($errors, "Phone not valid.");
}

Removes non digits and checks if length is less than or equal to 10.删除非数字并检查长度是否小于或等于 10。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM