Rene
Rene

Reputation: 59

CodeIgniter email validation

I want to validate an input field with an email address.

If I use

$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[users.email]');

If I enter same examples into the field to test the validation the result is...

"test" = failed

"test@" = failed

"test@test" = not failed

In this example I have a value which is not formatted correctly with email-syntax, but the validation accepts it. An email address of test@test should not pass validation.

What do I have to do so that the form-validation of codeigniter will check for a correctly formatted email syntax?

Upvotes: 1

Views: 465

Answers (1)

Leena Patel
Leena Patel

Reputation: 2453

Yes This was also the case with me. Eventually i created callback function to validate email and that worked like charm.

function validateEmail($email) {
    if (preg_match("/^[_a-z0-9-+]+(\.[_a-z0-9-+]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/", $email)) {
    return true;  
    }
    return false;
}

Add callback function in your validation rules

$this->form_validation->set_rules('email', 'Email', 'required|is_unique[users.email]|callback_validateEmail');

Hope it helps!

Upvotes: 1

Related Questions