邮件地址验证JAVA

时间:2014-05-11 21:57:21

标签: java validation boolean character detection

我试图写一个小程序,我要求用户输入一个电子邮件地址。然后我验证它是否是有效的电子邮件地址。喜欢" example@example.com"是有效的,但是"例如@@ example.com.org"会无效的。

这是我的代码,我已经让它检测是否有@和。在那里的角色,但我需要确保它只出现一次,并且@出现在点之前(。)

 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");
        }


    }
}

如果有人可以提供帮助,我们将不胜感激!

编辑:感谢大家的回答和帮助。如果其他人将来会做类似的事情,我会在下面发布最终代码。哦,你可以忽略评论。你可能不会理解它:)

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.");
        }



    }
}

4 个答案:

答案 0 :(得分:1)

按照之前的说法使用javax.mail api。

来自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;
}

答案 1 :(得分:0)

这里有一个用于验证电子邮件地址的正则表达式:

Java regex email

答案 2 :(得分:-1)

您可以比较mail.indexOf("@")mail.lastIndexOf("@")的回报率。如果它们不同,则此字符会多次出现。

编辑:@fge评论非常正确。如果您只需要电子邮件验证,最好使用特定的验证器,这将更彻底地验证电子邮件方式。

答案 3 :(得分:-1)

如果您只关心验证电子邮件,则可以应用正则表达式。这将检查@符号只出现一次。

Validate email with regular expression