0

Possible Duplicate:
E-mail validation in php? please help

I have Ajax sign up form. The code checks if the email exists in mysql, if it does, it says "sorry your email exists" - otherwise it will insert the email into mySQL.

How do I check to see if they have inserted a valid email? Like a rule char@ext

<?php
$con = mysql_connect("localhost","user","pass");
mysql_select_db("mytable", $con);
if(isset($_GET['email'])){
    $e= $_GET['email'];
    $cc= mysql_real_escape_string($e);
    $r = mysql_query("SELECT * FROM `maillist` WHERE `email` = '".$cc."'");
        if (mysql_affected_rows()==0) {
            $r = mysql_query('INSERT INTO maillist (id, email) VALUES(NULL,"'.$cc.'")');
            echo $cc;
        } else {
            echo 'Your email already exists!';
        }   
}
mysql_close($con);
?>
Community
  • 1
  • 1
TheBlackBenzKid
  • 26,324
  • 41
  • 139
  • 209
  • 6
    There a lots of questions concerning email validation via PHP already. For example **[this one](http://stackoverflow.com/questions/2146331/php-email-validation-function)**. – Till Helge Nov 01 '11 at 12:20

2 Answers2

2

You can use PHP filters : http://php.net/manual/en/filter.filters.validate.php

Specifically : FILTER_VALIDATE_EMAIL

if (filter_var('bob@example.com', FILTER_VALIDATE_EMAIL) === false) {
  //fake email
}
JohnP
  • 49,507
  • 13
  • 108
  • 140
  • Sweet. PHP is amazing. I would have to make a seperate function in ASP CLASSIC in VB and then make find replaces and checkers.. I think jQuery validation would be better making sure only emails go the PHP script but this is always good for fall backs for security. Especially for GET request. Thanks. REP ++ – TheBlackBenzKid Nov 01 '11 at 13:57
0

You should consult the docs on validate filters:

if (filter_var($someEmail, FILTER_VALIDATE_EMAIL)) {
    // ...
} 
jensgram
  • 31,109
  • 6
  • 81
  • 98