PHP Learner
PHP Learner

Reputation: 51

Confirm email field

I am creating a subscribe to us newsletter page, everything else works perfectly fine, but i'm trying to get the user to insert their email twice, the 2nd email field should work as a "confirm email" field. I'm have a little trouble doing so. Could someone lend a land?

If the 2nd email field does not match the 1st email field, it should display an error message

Here is a snippet of the code:

    if (trim($fv->txt_Email) == '') {
        $e->defineError("email_required", "Please provide your email address.", "txt_Email");
    } elseif (!cmsValidation::isValidEmail($fv->txt_Email)) {
        $e->defineError("invalid_email", "Please enter a valid email address.", "txt_Email");
    }
    if (trim($fv->txt_Email2) == '') {
        $e->defineError("email_required2", "Please confirm your email Address", "txt_Email2");
    } elseif (!cmsValidation::isValidEmail($fv->txt_Email2)) {
        $e->defineError("invalid_email", "Please enter a valid email address.", "txt_Email2");
    }

Upvotes: 1

Views: 190

Answers (2)

Billy
Billy

Reputation: 2448

if (trim($fv->txt_Email2) != $fv->txt_Email) {
        $e->defineError("invalid_email", "Your Email address do not match", "txt_Email2");

Upvotes: 1

David
David

Reputation: 218867

You can simply compare the two fields like you do with any other validation check in this code...

if (trim($fv->txt_Email) != trim($fv->txt_Email2)) {
    $e->defineError("email_mismatch", "Please ensure your email addresses match", "txt_Email2");
}

Upvotes: 1

Related Questions