0

I am new to Java, and I have built a Person class (Code below) with the email as a username. Now, I want to validate or verify that the username used is an email address. How do I validate within the class itself? I am looking for the simplest way possible.

public class Person {
    private String email;
    private String password;

    public Customer(String username, String password) {
        this.email = email;
        this.password = password;
    }

    public void setUsername(String email){
        this.email = email;
    }

    public void setPassword(String password){
        this.password = password;
    }
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 4
    You will, most likely, need to use some form of regular expression, for example [Java regex email](https://stackoverflow.com/questions/8204680/java-regex-email) - this is only going to validate the "format" of the string, not if it's a real email - that's much, much more compelx – MadProgrammer Nov 04 '18 at 23:16
  • If you read the related RFCs (https://tools.ietf.org/html/rfc3696, https://tools.ietf.org/html/rfc5321, https://tools.ietf.org/html/rfc5322, https://tools.ietf.org/html/rfc6530) you'll see that to build a full-fledged email address validator can be a bit tricky. You'll probably end up making some assumptions to simplify the formats you want to support and just validate to those rules. The simplest validation is that it contains an '@' that is not at the start or end of the address. – Jason Nov 05 '18 at 01:42

0 Answers0