How can I limit the email address extension that can be submitted in an HTML form using PHP? For example, I want to require that all users enter an @gmail.com email address.
EDIT: I have tried using if statements and substr() to return the last ten characters in the submitted email address (which, if it contains the correct email address extension, should be @gmail.com). However, this is probably not the most efficient/secure method. Does anyone know any other, more secure approaches to solving this task.
Example:
if(isset($_POST['submit'])) {
$email = trim(strtolower($_POST['email']));
if(substr($email, -10) != '@gmail.com') {
//show error message here
} else {
//continue script
}
The new html 5 elements should let you do this as they will accept a ‘pattern’ attribute that you can plug a regex into to restrict the end of the string to @gmail.com. You can do the same thing with javacsript/jquery and validate the form against a script on submission. \@gmail.com$ should do it.