-5

I'm trying to validate email address entered in a textbox, I want error message to be displayed when the user leave email address field with invalid email address "The email address is NOT valid."

suganya
  • 1
  • 3
  • 3
    There are lot of posts already having this, did you try searching? – AyB Feb 20 '14 at 05:27
  • 1
    possible duplicate of [How to validate an Email in PHP?](http://stackoverflow.com/questions/5855811/how-to-validate-an-email-in-php) – Marty Feb 20 '14 at 05:32
  • It really doesn't, with the new TLD's that are out now the answer you thought was best will invalidate almost all of them. See http://newgtlds.icann.org/en/program-status/delegated-strings – Shannon Feb 20 '14 at 06:21

6 Answers6

5

you can use filter_var ()

<?php
$email_address = "me@example.com";
if (filter_var($email_address, FILTER_VALIDATE_EMAIL)) {
  // The email address is valid
} else {
  // The email address is not valid
}
?>
Nagaraj S
  • 13,316
  • 6
  • 32
  • 53
3

You can go throgh this link

<?php
$email = "someone@example.com";

if(!filter_var($email, FILTER_VALIDATE_EMAIL))
  {
  echo "E-mail is not valid";
  }
else
  {
  echo "E-mail is valid";
  }
?> 
Sherin Jose
  • 2,508
  • 3
  • 18
  • 39
2

Email validation

<?php
$email_a = 'joe@example.com';


if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {
    echo "This ($email_a) email address is considered valid.";
}
else
{
    echo "This ($email_a) email address is not considered valid.";
}

Reference

Zeeshan
  • 1,659
  • 13
  • 17
0

from http://jqueryvalidation.org/email-method

<body>
<form id="myform">
    <label for="field">Required, email: </label>
    <input class="left" id="field" name="field">
    <br/>
    <input type="submit" value="Validate!">
</form>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://jquery.bassistance.de/validate/jquery.validate.js"></script>
<script src="http://jquery.bassistance.de/validate/additional-methods.js"></script>
<script>
// just for the demos, avoids form submit
jQuery.validator.setDefaults({
    debug: true,
    success: "valid"
});
$( "#myform" ).validate({
    rules: {
        field: {
            required: true,
            email: true
        }
    }
});
</script>
</body>
ihsan
  • 2,279
  • 20
  • 36
0

You can use ,

if (filter_var($email_address, FILTER_VALIDATE_EMAIL)) 
{
  #valid email
} 
else
{
  #inavalid email
}

OR

$pattern = '/^(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}@)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-+[a-z0-9]+)*\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-+[a-z0-9]+)*)|(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))$/iD';



if (preg_match($pattern, $$email_address) === 1) {
    // emailaddress is valid
}

OR using jQuery

var email = $('#email').val();
var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return regex.test(email);
Dimag Kharab
  • 4,439
  • 1
  • 24
  • 45
-1

below are the very fast and efficient way to valid email address

   <?php 
$email = "abc123@lolhaha"; // Invalid email address 
//$email = \"somebody@somesite.com\"; // Valid email address 
// Set up regular expression strings to evaluate the value of email variable against
$regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/'; 
// Run the preg_match() function on regex against the email address
if (preg_match($regex, $email)) {
     echo $email . " is a valid email. We can accept it.";
} else { 
     echo $email . " is an invalid email. Please try again.";
} 
?>
liyakat
  • 11,825
  • 2
  • 40
  • 46