Home>Article>Backend Development> How to verify the email and URL in the form in PHP form learning?

How to verify the email and URL in the form in PHP form learning?

WBOY
WBOY Original
2021-10-29 14:56:31 2019browse

In the previous article, I brought you "How to verify the required fields in the form when learning PHP forms?"", which introduces in detail the knowledge about how to verify the required fields in the form in PHP. In this article, we will continue to look at how to verify the email and URL in the form. I hope everyone has to help!

How to verify the email and URL in the form in PHP form learning?

In the previous article we have learned how to verify the required information, that is, the required fields. We need to know that when we fill in the information, although some data are not Required, but this information has format requirements, such as mobile phone number or website address, etc. Such information cannot be filled in casually. How can we complete the verification of this information at this time?

At this time we will use regular expressions to verify the right-click and URL information in the form. If you are not sure about regular expressions, you can click on "Regular Expression Video Tutorial" to learn. Let me briefly introduce to you some knowledge of regular expressions that we will use when validating form data.

Regular expression

Regular expression is a method of describing the rules of a piece of text. It is not an exact match, but through some Fuzzy matching of specific symbols. In PHP, we use thepreg_matchfunction to perform regular expression matching. One parameter is our regular expression rule, and the second parameter is the text that needs to be checked.

preg_matchThe syntax format of the function is as follows:

preg_match ( string $正则 , string $字符串 [, array &$结果] )

What we need to note is: According to the$regular variable, match$ String 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.^means the beginning;$means the end.

Next, let’s look at the application of regular expressions through examples:

In the above example, we are matching numbers starting with date. Output result:

How to verify the email and URL in the form in PHP form learning?

Next let’s take a look atpreg_matchedeThe third parameter is the matching 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.

Let’s take a look at an example. The example is as follows:

Output result:

How to verify the email and URL in the form in PHP form learning?

In the regular expression Use\wfor letters and\dfor numbers (\Drepresents non-digits) to represent

represents one Or multiple,*means 0 or more,?means yes or no,{n}means specific number,{m, n}means greater than m and less than n.

Examples are as follows:

Output results:

How to verify the email and URL in the form in PHP form learning?

Using or conditions can be used to match strings, if only A single letter or character can be represented by a range. Use[]to represent the value range of a character'/[a0\.]/'can match a or 0 or any string of..

In addition, regular expressions can also use - 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.

Knowing so much about regular expressions, next we have to verify whether the format of the form is correct.

PHP Validation Name, Email and URL

Validation rules for names are required and can only contain letters and spaces. The E-mail validation rule is required, which must be a valid email address (containing '@' and '.'). The URL validation rule is optional, and if present, it must contain a valid URL. Validation rules for notes are optional, multi-line input fields. A gender validation rule is required and one must be selected.

Then we can write the following code through the above policy expression, which 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. , an example is as follows:

$name = test_input($_POST["name"]); if (!preg_match("/^[a-zA-Z]*$/",$name)) { $nameErr = "只允许字母和空格"; }

The above code can prompt the user to fill in the name format, and then we can use the same method to verify the email and URL.

邮箱名可以是字母、数字、下划线和点组成的任意字符;邮箱要包含@符号,后面的文字按域名规则处理,以下代码将通过简单的方式来检测 e-mail 地址是否合法。如果 e-mail 地址不合法,将输出错误信息,示例如下:

$email = test_input($_POST["email"]); if (!preg_match("/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/",$email)) { $emailErr = "无效的 email 格式!"; }

以下代码将检测URL地址是否合法 (以下正则表达式运行URL中含有破折号:"-"), 如果 URL 地址不合法,将输出错误信息,示例如下:

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

将上述的验证方法添加到整体的格式中,示例如下:

    PHP中文网 

PHP 验证实例

* 必需的字段

"> 姓名: *

邮箱: *

网址:

评论:

性别: 女性 男性 *

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

通过上述代码,当我们没有按照我们设置的规则填写的话,输出结果如下:

How to verify the email and URL in the form in PHP form learning?

当然作为扩展知识,也可以使用弹窗,只需要在上述示例中的代码做些改动,将输出的字符变成弹窗的格式,示例如下:

    PHP中文网 
{ alert(\"姓名是必填的\"); //弹出对话框 } "; } else { $name = test_input($_POST["name"]); if (!preg_match("/^[a-zA-Z ]*$/",$name)) { $nameErr = ""; } } if (empty($_POST["email"])) { $emailErr = ""; } else { $email = test_input($_POST["email"]); // 检查电子邮件地址语法是否有效 if (!preg_match("/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/",$email)) { $emailErr = ""; } } if (empty($_POST["website"])) { $website = ""; } else { $website = test_input($_POST["website"]); // 检查 URL 地址语法是否有效(正则表达式也允许 URL 中的斜杠) if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) { $websiteErr = ""; } } if (empty($_POST["comment"])) { $comment = ""; } else { $comment = test_input($_POST["comment"]); } if (empty($_POST["gender"])) { $genderErr = ""; } else { $gender = test_input($_POST["gender"]); } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?>

PHP 验证实例

* 必需的字段

"> 姓名: *

邮箱: *

网址:

评论:

性别: 女性 男性 *

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

当填写内容不按规定规则是,点击提交,输出结果如下:

How to verify the email and URL in the form in PHP form learning?

How to verify the email and URL in the form in PHP form learning?

How to verify the email and URL in the form in PHP form learning?

如此我们便完成了PHP验证名称、邮件和URL。

大家如果感兴趣的话,可以点击《PHP视频教程》进行更多关于PHP知识的学习。

The above is the detailed content of How to verify the email and URL in the form in PHP form learning?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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