The following code will use a simple way to detect whether the name field contains letters and spaces. If the name field value is illegal, error message will be output:
$name = test_input($_POST["name"]); if (!preg_match("/^[a-zA-Z ]*$/",$name)) { $nameErr = "只允许字母及空格"; }
PS:
preg_match — perform regular expression matching.
Syntax:int preg_match ( string $pattern , string $subject [, array $matches [, int $flags ]] )
Search the subject string for the regular expression given by pattern content that matches the formula. If matches is provided, it will be populated with the results of the search. $matches[0] will contain text that matches the entire pattern, $matches[1] will contain text that matches the first captured subpattern in parentheses, and so on.The following code will check whether the e-mail address is legitimate 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("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) { $emailErr = "非法邮件地址"; }
The following code will check whether the URL address is legal (the following regular expression runs in the URL containing 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 = "不合法的 URL"; }
The code is as follows:
Example
##
The above is the detailed content of PHP development form verification email and URL. For more information, please follow other related articles on the PHP Chinese website!