Add validation in Sign Up using PHP

reddensoft

Banned-Spam
Joined
Oct 7, 2022
Messages
84
Reaction score
9
Can anyone help me how to add validation in phone number and email id using PHP while creating a website.
 
Code:
<?php
$email = $_POST['email']; // Assuming the email is being sent via a POST request

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "Valid email address.";
} else {
    echo "Invalid email address.";
}
?>

Code:
<?php
$phone = $_POST['phone']; // Assuming the phone number is being sent via a POST request

// Basic validation for a 10-digit phone number (common in the US, but adjust regex for other countries)
if (preg_match('/^[0-9]{10}$/', $phone)) {
    echo "Valid phone number.";
} else {
    echo "Invalid phone number.";
}
?>
 
Can anyone help me how to add validation in phone number and email id using PHP while creating a website.
You can use PHP’s filter_var() function to validate email and a regular expression (preg_match()) to validate phone numbers.
 
Back
Top