Email validation in php application

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • curi444
    New Member
    • Jan 2010
    • 18

    Email validation in php application

    How can i improve email validation in the code below?

    Code:
    <?php
    $con = mysql_connect("localhost", "root", "");		
    		if(isset($_POST['Submit']))
    {
    $name=$_POST['textfield'];
    $address=$_POST['textarea'];
    $phno=$_POST['textfield2'];
    $emailid=$_POST['textfield3'];
    $comments=$_POST['textarea2'];
    
    if(!$con)
    {
    die('could not connect'.mysql_error());
    }
    else
    {
    mysql_select_db("tender");
    
    $sql="INSERT INTO feedback values('$name','$address','$phno','$emailid','$comments')";
    if(!mysql_query("$sql",$con))
    {
    die("error");
    }
    echo "added";
    
    mysql_close($con);
    }
    }
    ?>
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Ok. You can use preg_match to check if the email is valid and print an appropriate response based on that.

    See http://www.regular-expressions.inf o/ for info on how to construct regular expressions. There are also a LOT of pre-made regular expressions available on the web specifically to verify email addresses (it's a common task), so you may want to look for some of them. (Check out the top comment on the preg_match page, for example.)

    Comment

    Working...