What fields must be validated by the form in PHP
This article mainly introduces the PHP form must verify which fields, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian with you to understand.
PHP form-required fields
Field validation rules
Name is required. + can only contain letters and spaces
E-mail is required. + must contain a valid email address (including "@" and ".")
The web address is optional. If it exists, it must contain a valid URL
Remarks are optional. Multiline fields (text fields).
Gender is required. One must be selected.
:
PHP forms-validate messages and URL
Now let's learn how to verify names (name), e-mails (email), and URLs.
.
PHP-verify name
The following code detects whether the name field contains letters and spaces in a simple way, and outputs an error message if the name field value is illegal:
$name = test_input ($_ POST ["name"])
If (! preg_match ("/ ^ [a-zA-Z] * $/", $name)) {
$nameErr = "only letters and spaces are allowed"
}
.
Preg_match-regular expression matching is performed.
Http://www.iis7.com/a/lm/gjcpmcx/
Syntax: int preg_match (string $pattern, string $subject [, array $matches [, int $flags]])
Search the subject string for something that matches the regular expression given by pattern. If matches is provided, it is populated with the search results. $matches [0] will contain text that matches the entire pattern, $matches [1] will contain text that matches the subpattern in parentheses that was captured first, and so on.
.
PHP-validate messages
The following code will detect whether the e-mail address is valid in a simple way. If the e-mail address is invalid, an error message will be output:
$email = test_input ($_ POST ["email"])
If (! preg_match ("/ ([\ w\ -] +\ @ [\ w\ -] +\. [\ w\ -] +) /", $email)) {
$emailErr = "illegal mailbox format"
}
.
PHP-verify URL
The following code detects whether the URL address is valid (the following regular expression runs URL with a dash: "-"), and 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 thanks thanks #\ /%? = ~ _ |!:,.;] * [- a-z0-9 thanks / /% = ~ _ | / I", $website)) {
$websiteErr = "illegal URL address"
}
Thank you for reading this article carefully. I hope the article "which fields must be verified in the form in PHP" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!