-3
<form action="form.php" method="POST">
username:<input type="text" name="username">
<br>
password:<input type="text" name="password">
<input type="submit" value="register">
</form>

I want to alert if @ is not included in email field i.e email should be in proper format otherwise it should throw alert.

sagar
  • 43
  • 2
  • 5

3 Answers3

0

In view use email input <input type="email" name="email"> in html5.

In php validation filter_var

if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
     //Valid email!
}

See more about filter_var: http://www.php.net/filter_var

Juan Caicedo
  • 1,425
  • 18
  • 31
0

HTML5 has come up with many different input types and you can perform the validation at the user interface level. You can do that by

<input type="email" placeholder="Enter your email" required >

In php:

$email=$_POST['email'];
    if (preg_match("/[^a-zA-Z.-_-@0-9]/", $email)) {
                die("Email format is wrong.");
            }

            if (!(strrpos($email,'@')< strripos($email,'.'))) {
                die("Email format is wrong. Email should be like name@example.com");
            }
Lokesh Pandey
  • 1,739
  • 23
  • 50
0
function validate_email($eml){

  $eml_components = explode('@', $eml);

  if(!isset($eml_components[1]) || !strpos($eml_components[1],'.')){
    echo "Invalid email format.";
  }else{
    echo "Valid email format.";
  }
}
coderodour
  • 1,072
  • 8
  • 16