[thelist] PHP Form Validation

Hans-Fr=?ISO-8859-1?B?6WTp?=ric Fraser hfraser at videotron.ca
Mon Jan 6 10:55:01 CST 2003


On 1/6/03 11:17 AM, "Andrew Maynes" <andrew at humanbehaviour.co.uk> wrote:
> The tell a friend form needs to validate two email addresses, the
> sender's and the sender's friend.  At the moment I only have this:
>
> /*e-mail´s validity is check with this */
> if (!eregi ("^[_\\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\\.)+[a-z]{2,4}$", $email))
> $error_message = "A valid e-mail address is required.";
>
> Another part of this problem is (which is prossibly part of the answer) the
> same
> error message will apply to both missing fields and I realy want two
> different
> messages.

to start you can create a function out of this snipet!

function validateEmail($email){
    if (!eregi ("^[_\\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\\.)+[a-z]{2,4}$",
$email)){
        return False;
    }
    else {
    return true;
    }
}

then you can call it like this

if(!validateEmail($sendermail)){
    $error_messageSender = 'the sender needs a valid e-mail address'
}

if(!validateEmail($freindEmail)){
    $error_messageFreind = 'the freind needs a valid e-mail address'
}

then check if the error variables are set if so return the page with the
errors if not well ... go on with the script!

on top of that you could add a javascript to your form to check the validity
of the emails before submiting and give them error messages!

that way  you save server and user time and your user does not have to
reload a page to check if what he sent is valid!

hans!




More information about the thelist mailing list