PHP Forms - Validate E-mail and URL
To validate E-mail and URL inputs in PHP forms, you can use the filter_var()
function, which provides an easy way to validate data formats. Here's a simple example for both:
PHP Form Example with Email and URL Validation
Explanation:
-
filter_var($email, FILTER_VALIDATE_EMAIL)
: Checks if the input is a valid email address. -
filter_var($website, FILTER_VALIDATE_URL)
: Checks if the input is a valid URL. -
htmlspecialchars($_SERVER["PHP_SELF"])
: Prevents XSS by escaping special characters in the form action.