CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    [RESOLVED] REGEX to validate email inputs

    i need a regex to validate the email input in the textbox of the form that end users enter.

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: REGEX to validate email inputs

    You need to learn to start using Google. It can find plenty of solutions so that you don't have to wait upon us to reply.

    http://www.google.com/search?hl=en&q...hp&btnG=Search
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Resolved Re: REGEX to validate email inputs

    thanks; i googled it!
    i just paste the code here for other lazy developers like me

    Code:
    <?php
    
    $email = "[email protected]";
    
    if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
      echo "Valid email address.";
    }
    else {
      echo "Invalid email address.";
    }
    
    ?>

  4. #4
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Lightbulb Re: [RESOLVED] REGEX to validate email inputs

    the previous post that i did uses POSIX function i investigated more and found that PCRE (Perl Compatible Regular Expression) functions are faster and more popular that POSIX functions, so i post the PCRE code here also as a better solution for whom care to this thread:

    Code:
    <?php 
      if ($submit) { 
        $okay = preg_match(
          '/^[A-z0-9_\-]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{2,4}$/', 
          $emailfield
        ); 
        if ($okay) { 
          echo "E-mail is validated"; 
        } else { 
          echo "E-mail is incorrect"; 
        } 
      }else { 
    ?> 
    <form method="POST" action="email.php"> 
    E-mail address: <input type="text" name="emailfield"> 
    <br><input type="submit" name="submit" value="Validate"> 
    </form> 
    <?php  
    }  
    ?>
    Touraj Ebrahimi [toraj_e] [at] [yahoo] [dot] [com]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured