validate that an email address contains "@" and "."

14,277

Solution 1

I suspect you're after something like:

if (!address.contains("@") || !address.contains("."))
{
    // Handle bad address
}

EDIT: This is far from a complete validation, of course. It's barely even the start of validation - but hopefully this will get you going with the particular case you wanted to handle.

Solution 2

You can use commons-validator , Specifically EmailValidator.isValid()

Solution 3

From my personal experience, the only was to validate an email address is to send a email with a validation link or code. I tried many of the validator but they are not complete because the email addresses can be very loose ...

Solution 4

int dot = address.indexOf('.');
int at = address.indexOf('@', dot + 1);

if(dot == -1 || at == -1 || address.length() == 2) {
  // handle bad address
}

This is not complete solution. You will have to check for multiple occurances of @ and address with only '.' and '@'.

Share:
14,277

Related videos on Youtube

r.r
Author by

r.r

Updated on June 01, 2022

Comments

  • r.r
    r.r about 2 years

    i need to validate that an inserted email address contains "@" and "." without a regular expression. Can somebody to give me "java code" and "structure chart" examples please?

  • Kaj
    Kaj about 13 years
    Still rather bad. ".@" isn't even close to a valid address :)
  • Jon Skeet
    Jon Skeet about 13 years
    @Kaj: Absolutely. It's far from a complete validation - I'm only answering what was asked.