Mail address validation JAVA

冷暖自知 提交于 2021-02-05 08:24:30

问题


I'm trying to write a little program where I ask the user to enter an email address. And then I verify if its a valid email address or not. Like "example@example.com" would be valid, but "example@@example.com.org" would be invalid.

Here's my code, I have made it work to detect if there is an @ and . character in there, but I need to make sure it only appears once, and that the @ appears before the dot (.)

 import java.util.Scanner;

public class email {
    public static void main(String args[])
    {
        Scanner skanni = new Scanner(System.in);

        System.out.println("Sládu inn email adressuna thína.");
        System.out.println("Email: ");
        String mail = skanni.nextLine();

        boolean b1=mail.contains("@") && mail.contains(".");

        if (b1)
        {
            System.out.println("This email is valid");
        }else{
            System.out.println("this email is invalid");
        }


    }
}

If anyone could help, it would be greatly appreciated!

EDIT: Thanks everyone for your answers and help. I'm posting the final code below if anyone else will be doing something similar in the future. Oh and you can ignore the comment. You probably won't understand it :)

import java.util.Scanner;
import java.util.regex.*;

public class email {
    public static void main(String args[])
    {
        Scanner skanni = new Scanner(System.in);

        System.out.println("Sládu inn email adressuna thína.");
        System.out.println("Email: ");
        String mail = skanni.nextLine();

        /*Skilgreining á eftirfarandi kóda
         * ^ táknid er byrjunin á strengnum
         * thad sem ég set innan sviga á eftir, er thad sem ég legg áherslu á í hverju sinni
         * [A-Za-z0-9] eru leyfilegir stafir í fyrsta lid emailsins, sbr: (birgir09)dæmi@gmail.com
         * + á milli táknar ad naesti svigi byrjar, thad er ad segja. (\\-[A-Za-z0-9-])
         * *@ táknar ad @ merkid er á milli sviganna
         * "[A-Za-z0-9] er thad sem kemur fyrir framan punktinn sbr. (gmail).com
         * (\\.[A-Za-z0-9]) er thad sem kemur fyrir aftan punkt sbr gmail.(com)
         */
        Pattern p = Pattern.compile("^[A-Za-z0-9-]+(\\-[A-Za-z0-9])*@"
                + "[A-Za-z0-9-]+(\\.[A-Za-z0-9])");
        Matcher m = p.matcher(mail);

        if (m.find())
        {
            System.out.println("Thetta email er loglegt.");
        }else{
            System.out.println("Thetta email er ekki loglegt.");
        }



    }
}

回答1:


Here you have a regex for validating mail addresses:

Java regex email




回答2:


Use javax.mail api as you have been told before.

From http://crunchify.com/how-to-validate-email-address-using-java-mail-api/

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

private boolean crunchifyEmailValidator(String email) {
    boolean isValid = false;
    try {
        //
        // Create InternetAddress object and validated the supplied
        // address which is this case is an email address.
        InternetAddress internetAddress = new InternetAddress(email);
        internetAddress.validate();
        isValid = true;
    } catch (AddressException e) {
        System.out.println("You are in catch block -- Exception Occurred for: " + email);
    }
    return isValid;
}



回答3:


You could compare the return of mail.indexOf("@") and mail.lastIndexOf("@"). If they're different, then this character is present multiple times.

EDIT : @fge comment is very true. If you need only email validation, better use a specific validator, which will validate the email way more thoroughly.




回答4:


If you all you care is validating an email you can apply a regular expression. This will check that @ symbol comes only once.

Validate email with regular expression



来源:https://stackoverflow.com/questions/23598387/mail-address-validation-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!