After ensuring that fields are not empty, the next step is to verify that the data submitted is in the correct format. PHP provides powerful tools like filter_var() and Regular Expressions (RegEx) for this purpose.
The easiest and safest way to check if an e-mail address is well-formed is to use the filter_var() function with the FILTER_VALIDATE_EMAIL filter.
To validate a website URL, we use a Regular Expression. The pattern below checks if the URL syntax is valid and allows for protocols like http, https, and ftp.
For a name field, you usually want to restrict input to only letters and white spaces. We use the preg_match() function to enforce this rule.
Proper validation prevents "bad data" from entering your system. This includes:
user@example.com format.filter_var() is great for emails, complex strings like URLs often require Regular Expressions to handle different protocols and domain extensions correctly.