PHP Form - Verification Email and URL

When it comes to verification, we need to know the regular expression:

Regular expression

Regular expression is a method of describing a text rule. It is not an exact match, but a fuzzy match through some specific symbols

In PHP, we use the preg_match function to Perform regular expression matching. One parameter is our regular expression rule, and the second parameter is the text to be checked

preg_match (string $regular, string $character String[, array &$result] )

Function: Match $string variable based on $regular variable. If it exists, return the number of matches and put the matched results into the $result variable. If no result is found, 0 is returned.

^indicates the beginning;$indicates the end

Let’s take a look at the code:

The above code matches numbers starting with date. The matching results are as follows:

Matching successful


preg_matchedeThe third parameter is matched Content, usually we will pass an empty array in, because it is a call by address. After the matching is completed, the specific matching content will be obtained in the array

Example

Program running result:

Array ( [0] => date )


In regular expressions, letters are represented by \w and numbers are represented by \d (\D represents non-digits)

+means one or more

*means 0 or more

?means there or not

{n}represents the specific number

{m, n}represents more than m and less than n

Just like the following :

Program running result:

Array ( [0] => an )


Using or conditions can be used to match strings. If it is just a single letter or character, you can use a range representation

Use [] Indicates the value range of a character

'/[a0\.]/' can match any string containing a or 0 or .

In addition, regular expressions can also be used - To represent a set of ranges

• [a-z] represents any one of the 26 lowercase letters

• [A-Z] represents an uppercase letter

• [0-9 ] represents a decimal number

Now that we know so much, let’s look at using regular expressions to match the content of the form.


PHP - Validation Name

The following code will detect whether the name field contains letters and spaces in a simple way. If the name field value is illegal, an error message will be output:

$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z]*$/",$name)) {
$nameErr = "Only letters and spaces allowed";
}


##PHP - Verification Email

Rules: The email name can be any character consisting of letters, numbers, underscores and dots; the email must contain the @ symbol, and the following text is processed according to domain name rules

The following code will check whether the e-mail address is legal in a simple way. If the e-mail address is illegal, an error message will be output:

$email = test_input($_POST["email"]);

if (!preg_match("/^ [a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/",$email)) {
$emailErr = "Invalid email format!";
}


PHP - Verification URL

The following code will check whether the URL address is legal (the following regular expression operation URL contains dashes: "-"). If the URL address is illegal, an error message will be output:

$website = test_input($_POST["website"]);

if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0- 9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%
=~_|]/i",$website)) {
$websiteErr = "Invalid URL";
}

We will now combine the knowledge we learned above to verify the data in our form.

    PHP中文网 

PHP 验证实例

* 必需的字段

"> 姓名: *

邮箱: *

网址:

评论:

性别: 女性 男性 *

您的输入:"; echo $name; echo "
"; echo $email; echo "
"; echo $website; echo "
"; echo $comment; echo "
"; echo $gender; ?>

If we don’t fill in the rules according to the rules we wrote above, the following prompt will appear:

0.png

If the information you enter is correct, it will be verified.

You can also use regular expressions to match mobile phone numbers. Think about the rules of mobile phone numbers.


Continuing Learning
||
PHP中文网

PHP 验证实例

* 必需的字段

"> 姓名: *

邮箱: *

网址:

评论:

性别: 女性 男性 *

您的输入:"; echo $name; echo "
"; echo $email; echo "
"; echo $website; echo "
"; echo $comment; echo "
"; echo $gender; ?>
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!