Email validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hotmodi
    New Member
    • Apr 2010
    • 9

    Email validation

    Hi

    I am working on getting my email form validated. The user has to write an email containing "@", ".", "dk or com or org or net", but I cant get it to work.

    I am using the strstr function and I understand that writing only "dk" well not work.

    Code:
     
    
    if (strstr($email, "@") && strstr($email, ".") && strstr($email, "dk"))
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    To do something like that, you would be better of using a regular expression. They are kind of tough to learn, but well worth it.

    For example, this should validate *most* email addresses coming from .net, .com or .dk domains.
    [code=php]<?php
    $regexp = '#^[A-Z0-9._%+\-]+@[A-Z0-9.\-]+\.(net|com|dk) $#i';
    $email = $_POST['email'];
    if(preg_match($ regexp, $email)) {
    echo "Valid!";
    }
    else {
    echo "Invalid!";
    }
    ?>[/code]
    That regular expression (excluding the modification that allows only the three domains) comes from the regular-expressions.inf o site, which is one of the better resources on regular expressions out there. (They also have a nice tutorial, if you are interested.)

    Comment

    • hotmodi
      New Member
      • Apr 2010
      • 9

      #3
      Hi Atli

      Thanks for the help;) That work just fine

      Comment

      Working...