0

I'm new in programming and need to validate this class email.
Anybody can help?

public class Email {

    private String email;

    public Email() {
        email = "";
    }

    public Email(String emailAddress) {
        this.email = emailAddress;
    }

    public String getEmail() {
        return email;
    }

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

Edited by Dani: Formatting fixed

2
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by bigginger
0

Little confused by this request...
Your class is OK, but you may want to change setter access from public to private. You already provided user with way to set email value through constructor public Email(String emailAddress) .
As for validation you need to check out if submitted string contains "@" and dot "." after it example test@test.com. That would be the simplest validation process. In real life you would want to send email to entered address and receive confirmation from owner so you are most likely have something like unconfirmed and confirmed state that can be addressed in various ways.
Is this answer you been looking for or was it something else?

0

Little confused by this request...
Your class is OK, but you may want to change setter access from public to private. You already provided user with way to set email value through constructor public Email(String emailAddress) .
As for validation you need to check out if submitted string contains "@" and dot "." after it example test@test.com. That would be the simplest validation process. In real life you would want to send email to entered address and receive confirmation from owner so you are most likely have something like unconfirmed and confirmed state that can be addressed in various ways.
Is this answer you been looking for or was it something else?

0

I've the class Diagram as:
Email
________
-email : String
__________
+Email()
+Email(emailAddress : String)
+setEmail(email : String) : void
+getEmail() : String
+validEmail(address : String) : boolean

0

well then you need only write this new method validEmail(String) with functionality as given in my previous reply and result of the operation return as a boolean type

0

Like that?

public  boolean validEmail(String address) {
        if (email == null) {
            return false;
        }
        //Assigning the email format regular expression
        address = "^([A-Za-z0-9_\\-\\.])+\\@([A-Za-z0-9_\\-\\.])+\\.([A-Za-z]{2,4})";
        return email.matches(address);
    }

Edited by Dani: Formatting fixed

0

Like that?

public  boolean validEmail(String address) {
        if (email == null) {
            return false;
        }
        //Assigning the email format regular expression
        address = "^([A-Za-z0-9_\\-\\.])+\\@([A-Za-z0-9_\\-\\.])+\\.([A-Za-z]{2,4})";
        return email.matches(address);
    }

Edited by Dani: Formatting fixed

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.