Email Validation in PHP

When you register on at a website, the site normally checks if the e-mail address that you enter is in a valid format. This is done by using what called a Regular Expression. What we need to do is check if a string (eg $email) matches the regular expression:

For this purpose we will make a form for validation

<form name="form1" method="post" action="">
Email:<input name="email" type="text" id="email"><br />
<input name="submit" type="submit" id="submit" value="Submit">
</form>

Now we will create a code that will check email validation

<?php
$email=$_POST['email'];
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) {
echo "Email is in correct";
}
else{
echo "Correct email format";
}
?>

see this example we have used a function eregi that will check pattern, like @ and dot(.).

Bookmark This Page

Link Partners