Verification email and URL

1. Match name

"/^[a-zA-Z]*$/"
Only spaces and letters are allowed, "^" means the beginning, "$" means the end, [a -zA-Z ] represents a character from a-z or A-Z or a space.

Example:

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

2. Match E-mail

“/([\w-]+\@[\w-] +.[\w-]+)/”
“\w” matches any word character including an underscore. Equivalent to '[A-Za-z0-9_]';
+ Matches the previous subexpression one or more times;
"-" matches "-".

3. Match URL

“/\b(?:(?:https?|ftp):\/\/|www.)[-a-z0-9+&@ #\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i”

The red lines are all marked Regular expression.

Regular expression, also known as regular expression. (English: Regular Expression, often abbreviated as regex, regexp or RE in code), a concept in computer science. Regular tables are usually used to retrieve and replace text that matches a certain pattern (rule).

Regular expressions will be learned in the following advanced PHP tutorials. You can use them first when learning forms, just for understanding.


##

Continuing Learning
||
<?php // 定义变量并默认设置为空值 $nameErr = $emailErr = $genderErr = $websiteErr = ""; $name = $email = $gender = $comment = $website = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["name"])) { $nameErr = "Name is required"; } else { $name = test_input($_POST["name"]); // 检测名字是否只包含字母跟空格 if (!preg_match("/^[a-zA-Z ]*$/",$name)) { $nameErr = "只允许字母和空格"; } }
submitReset Code