Hi! I'm asking for e-mail address of my user so I can send him an e-mail to validate his registration. I'm using the function mail to accomplish this. The function triggers an error if the address is not sintacticaly correct and I can trapp the error but it's too late. By that moment I already update my databases. I would to e-mail address syntax earlier, maybe with the rest of the fields in the form.
Does any body knows a function to do that ?...

quijotemx

    I'll give you the function I use because it's pretty complicated.

    
    function validateEmail($fEmail, $fBannedHosts)
    {
    	// use regex to see if email follows pattern: <name>@<domain>.<suffix>
    	if(!eregi("^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $fEmail))
    	{
    		// if not die w/ error
    		die("<b>Error: </b> You entered an invalid email address!");
    	}
    
    // if $fBannedHosts is set to free use these hosts
    if($fBannedHosts == "free")
    {
    	$fBannedHosts = array("hotmail.com", 
    				    "yahoo.com", 
    				    "gmail.com"
    				    );
    }
    
    // split up the email addy into two variables
    list($fUser, $fDomain) = split("@", $fEmail);
    
    // go through all of the banned hosts
    foreach($fBannedHosts as $fBannedHosts)
    {
    	// check to see if users host is a banned host
    	if($fDomain == $fBannedHosts)
    	{
    		// if it is die w/ error
    		die("<b>Error: </b> Your email host(". $fDomain .") is not allowed by this site!");
    	}
    }
    }
    
    

    You can call it like this:

    $banned = array("banned.com","aol.com");
    validateEmail("email@email.com", $banned);
    

    or like this:

    validateEmail("email@email.com", "free");
    

    if you use free, it automaticlly uses the free hosts listed.

      Thank you ShawnK.

      Think it's very userful, specially because of banned hosts...

      Thanks again ...

      el_quijote ...

        Write a Reply...