Ilian Zapryanov
Ilian Zapryanov

Reputation: 1158

Email validation, Am I doing the right thing?

I am writing some PHP code where I end it with a email validation checking. Can someone review it and confirm if it's a good solution?

<?php
function isTrue($var) {
    return (isset($var)) ? TRUE : FALSE;
}

function len($str) {
    $i;
    for($i=0; isTrue($str[$i]); $i++) {
        /* count me */
    }
    return $i;
}

function parsMe($str) {
    $at; $dot; $isvalid=0; $isnotvalid=0;
    for ( $at=0; isTrue($str[$at]); $at++) {
        if ( $str[$at] == "@" ) {
            for ( $dot=$at+1; isTrue($str[$dot]); $dot++ ) {
                if ( $str[$dot] == "." ) $isvalid += 1;
                if ( $str[$dot] =="@" || $str[len($str)-1] == "@" || $str[len($str)-1] == "." ) {
                    die("Error email entered");
                }
            }
        }
        if ( $str[$at] != "@" ) {
            $isnotvalid+=1;
        }
    }

    /* exit cond */
    if ( $isnotvalid == len($str)  ) die("Error mail usage");
    else if ( $isvalid == 0 ) die("Error mail");
    else echo "Mail is OK ";
}

$eml = "dotdot@com.";

parsMe($eml);
?>

parsMe() should produce error.

Upvotes: 1

Views: 218

Answers (1)

Lawrence Cherone
Lawrence Cherone

Reputation: 46602

There are built in functions for checking validity of emails:

<?php 
$email ='dotdot@com';
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
 echo "E-mail is not valid";
}else{
 echo "E-mail is valid";
}
?>

Upvotes: 12

Related Questions