Validate email address

Discussion in 'PHP' started by askscript, Aug 26, 2010.

  1. #1
    Hi, i am doing an exercise on email validation and i met some problem.

    i am trying to validate an email address if there is an @ symbol in it.

    here's the code:

    <?php 
    if(isset($_POST['name'])){
    $name = ucwords($_POST['name']);
    echo "Hello ".$name."<br />";
    }
    if(isset($_POST['email']{
    [B][COLOR="blue"]if(ereg("@",$_POST['email'])){[/COLOR][/B]
    echo "Your email submission is ". $_POST['email']."<br />";
    }else{
    echo "Please enter an @ sign to your email"; 
    }
    }
    ?>
    Code (markup):
    The error is at the bold blue part ( line 7). I wonder if i did anything wrong with the ereg.
    Is ereg function being deprecated in php? if i cannot use ereg, what else can be done?

    pls advise, thanks.
     
    askscript, Aug 26, 2010 IP
  2. HuggyEssex

    HuggyEssex Member

    Messages:
    297
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    45
    #2
    I just wrote this works fine,

    
    <?php
    
    	$string = "glen.email.com";
    	
    	$look = "/@/";
    	
    	if(preg_match($look,$string))
    	
    		{
    		
    		echo "Found it";	
    			
    		}
    		else
    		{
    			
    		echo "It's not there!";	
    			
    		}
    
    ?>
    
    PHP:
     
    HuggyEssex, Aug 26, 2010 IP
  3. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #3
    Yes, ereg function is being deprecated and should not be used, ever. You can use preg_match instead. I think the problem with your code is this:
    if(isset($_POST['email']{
    Code (markup):
    You made a typo, you forgot to add "))"
     
    Rainulf, Aug 26, 2010 IP
  4. askscript

    askscript Member

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    i already got the "))" in the code. i will check preg_match. I just dunno why my script can't run and got an error at that part.
     
    askscript, Aug 26, 2010 IP
  5. askscript

    askscript Member

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    Any more help? I just do not understand why that is an error.. the codes are all fine and ok.
     
    askscript, Aug 26, 2010 IP
  6. askscript

    askscript Member

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #6
    oh yeah i forgot the "))". i spotted it in the post but forgot to correct it in my real script.. Oo"
     
    askscript, Aug 26, 2010 IP
  7. HuggyEssex

    HuggyEssex Member

    Messages:
    297
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    45
    #7
    The script I wrote works fine please read my post.

    You shouldn't use that function anymore it's depreceated.

    Glen
     
    HuggyEssex, Aug 26, 2010 IP
  8. askscript

    askscript Member

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #8
    For preg_match, i need to use "/@/", ( include the backslash?)

    but for ereg there is not a need for the backslash?
     
    askscript, Aug 26, 2010 IP
  9. askscript

    askscript Member

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #9
    yes huggy.. noted. Now i got to learn preg_match.
     
    askscript, Aug 26, 2010 IP
  10. sunnyverma1984

    sunnyverma1984 Well-Known Member

    Messages:
    342
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    120
    #10
    regular expression for email

    ^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$
     
    sunnyverma1984, Aug 26, 2010 IP
  11. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #11
    If your just checking if theirs the @ (at) character within the email string, you can just do (theirs no need for a regex):

    if (substr_count($email, '@') == 1) {
    //proceed...
    }
    PHP:
    However if your trying to validate it to ensure its in a valid email format, you can use a regex, but I don't recommend using sunnyverma1984's one as that has a few flaws (doesn't allow lowercase letters, doesn't accept subdomains, doesn't allow tld's such as co.uk, hasn't escaped/backslashed special characters, doesn't validate the whole string and so on.)
     
    Last edited: Aug 26, 2010
    danx10, Aug 26, 2010 IP
  12. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #12
    The regular expression library really helps when you don't have time to create your own regular expressions:

    http://regexlib.com

    Here is a function I use to check email address:


    
    function checkEmail($string) {
        return preg_match('/^[.\w-]+@([\w-]+\.)+[a-zA-Z]{2,6}$/', $string);
    } 
    
    var_dump(checkEmail('alex.JOHNSON.Department-Outsource@gmail.co.uk.COM'));
    
    PHP:
     
    ThePHPMaster, Aug 26, 2010 IP