PHP form validation: number, email and URL validity verification

王林
Release: 2023-08-07 20:02:01
Original
975 people have browsed it

PHP Form Validation: Number, Email and URL Legality Verification

In web development, forms are one of the important components for interacting with users. After the user submits the form data, in order to ensure the legality and security of the data, we need to verify the data entered by the user. This article will introduce how to use PHP to verify the validity of numbers, emails, and URLs, and provide relevant code examples.

1. Numeric verification

  1. Use the is_numeric() function to verify whether the user input is a number. The is_numeric() function is used to detect whether a variable is a number or a numeric string. Returns true if it is a number or numeric string, false otherwise.

The following is a sample code:

<?php
$num = $_POST['num'];

if (is_numeric($num)) {
    echo "数字验证通过!";
} else {
    echo "请输入有效的数字!";
}
?>
Copy after login

In the above code, we first get the numeric data submitted by the user and then validate it using the is_numeric() function. If the verification passes, output "Digital verification passed!"; otherwise, output "Please enter a valid number!".

2. Email verification

  1. Use the filter_var() function to verify whether the email address entered by the user is legal. The filter_var() function is used to verify and filter variables. It can filter and verify variables according to the specified filter. We can use the FILTER_VALIDATE_EMAIL filter to verify email addresses.

The following is a sample code:

<?php
$email = $_POST['email'];

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "邮箱验证通过!";
} else {
    echo "请输入有效的邮箱地址!";
}
?>
Copy after login

In the above code, we first get the email address submitted by the user, and then verify it using the filter_var() function. If the verification is passed, "Email verification passed!" is output; otherwise, "Please enter a valid email address!" is output.

3. URL verification

  1. Use the filter_var() function to verify whether the URL entered by the user is legal. We can use the FILTER_VALIDATE_URL filter to validate URL addresses.

The following is a sample code:

<?php
$url = $_POST['url'];

if (filter_var($url, FILTER_VALIDATE_URL)) {
    echo "URL验证通过!";
} else {
    echo "请输入有效的URL地址!";
}
?>
Copy after login

In the above code, we first get the URL address submitted by the user and then verify it using the filter_var() function. If the verification passes, "URL verification passed!" is output; otherwise, "Please enter a valid URL address!" is output.

To sum up, this article introduces how to use PHP to verify the validity of numbers, emails and URLs. Through the above sample code, we can easily verify the data entered by the user to ensure the legality and security of the data. Of course, in addition to the above verification methods, we can also use other methods such as regular expressions for more stringent verification according to actual needs.

The above is the detailed content of PHP form validation: number, email and URL validity verification. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template