Home >Backend Development >PHP Tutorial >The correct way to verify EMAIL address with PHP (1)_PHP tutorial
The Internet Task Force Engineering Group (IETF) document RFC 3696 "Application Technical Review and Naming Transformation" written by John Klensin gives multiple real and valid EMAIL addresses, but unfortunately these addresses have been Rejected by most PHP validation programs, the addresses Abc@def@example.com, customer/department=shipping@example.com and !def!xyz%abc@example.com are all valid, as shown below in the relevant literature A very popular regular expression is given, but it rejects all EMAIL addresses given earlier:
^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$ |
This regular expression only allows underscores (_) and hyphens (-), numbers and Uppercase and lowercase letters. Addresses containing slashes (/), equal signs (=), exclamation points (!), and percent signs (%) will be rejected even if the letters have been converted to uppercase and lowercase before this. This expression also requires that the top-level domain component be at least 2 or 3 characters, so valid domains such as .museum will also be rejected.
Another favorite regex is:
^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$ |
This regex will also reject the valid example address given in the previous paragraph, and it elegantly solves the capital letter problem , and no error will be reported when the top-level domain name has only 2 or 3 characters, but it also allows invalid domain names, such as example..com.
Listing 1 shows an example from the PHP Dev Shed (