Java电子邮件验证–电子邮件正则表达式Java

时间:2020-02-23 14:34:27  来源:igfitidea点击:

今天,我们将研究Java电子邮件验证程序。
有时我们必须验证电子邮件地址,并且可以为此使用电子邮件正则表达式。
在上一篇文章中,我通过一些示例详细解释了Java正则表达式。
在这个真实的示例中,我试图使用Java正则表达式来验证电子邮件地址。

Java电子邮件验证

如果您在Wikipedia文章中查找有效的电子邮件地址,则它支持许多特殊字符。
但是,为了覆盖大多数有效的电子邮件地址,我假设电子邮件地址由字符,数字和特殊字符+,-,组成。
和_。
另外,每个电子邮件地址必须仅包含一个@符号。

对于域名,我们也可以有子域。
因此,子域可以包含字符,数字和特殊字符–和_。
同样,子域可以具有多个级别,例如xyz.abc.com。
因此,中级顶级域名(TLD)必须以句点(。
)开头,并且可以包含字符,数字和特殊字符-和_。
最后一个TLD的最小长度应为2,并且只能包含单词。

需要注意的另一件事是电子邮件地址不区分大小写。
因此,我们将使用不区分大小写的标志来创建模式对象。

有效的电子邮件地址示例

考虑到所有这些因素,一些有效的电子邮件地址是:

  • theitroad @ yahoo.com,theitroad-100 @ yahoo.com,theitroad.100 @ yahoo.com
  • theitroad111 @ theitroad.local,theitroad-100 @ theitroad.net,theitroad.100 @ theitroad.local.au
  • theitroad @ 1.com,theitroad @ gmail.com.com,[email protected]
  • theitroad-100 @ yahoo-test.com,theitroad_100 @ yahoo-test.ABC.CoM

无效的电子邮件地址示例

现在,让我们看看一些无效的电子邮件地址是导致它们无效的原因。

  • theitroad –否@ symbole

  • theitroad @ .com.my – @符号后的点

  • [email protected] –最后的TLD长度小于2

  • theitroad123 @@。
    com.com –两个@符号

  • .theitroad @ theitroad.local-ID不能以开头。

  • theitroad()* @ gmail.com – ID中的无效特殊字符

  • theitroad@%*.com – TLD中的无效特殊字符

  • theitroad..2002 @ gmail.com-ID不能有两个点

  • theitroad。
    @ gmail.com – ID不能以点号结尾

  • theitroad @ theitroad @ gmail.com –两个@符号

  • [email protected] –最后一个TLD只能包含字符

电子邮件正则表达式Java程序

因此,使用Java中的所有信息进行电子邮件验证,我编写了EmailValidator类,该类可用于使用正则表达式来验证电子邮件地址。

package com.theitroad.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Java email validation program
 * 
 * @author hyman
 *
 */
public class EmailValidator {
	//Email Regex java
	private static final String EMAIL_REGEX = "^[\w-\+]+(\.[\w]+)*@[\w-]+(\.[\w]+)*(\.[a-z]{2,})$";

	//static Pattern object, since pattern is fixed
	private static Pattern pattern;

	//non-static Matcher object because it's created from the input String
	private Matcher matcher;

	public EmailValidator() {
		//initialize the Pattern object
		pattern = Pattern.compile(EMAIL_REGEX, Pattern.CASE_INSENSITIVE);
	}

	/**
	 * This method validates the input email address with EMAIL_REGEX pattern
	 * 
	 * @param email
	 * @return boolean
	 */
	public boolean validateEmail(String email) {
		matcher = pattern.matcher(email);
		return matcher.matches();
	}
}

现在,我们将使用另一个类" EmailValidatorTest"来测试此电子邮件正则表达式Java类。

package com.theitroad.util;

public class EmailValidatorTest {

	//list of valid email addresses
	private static final String[] validEmailIds = new String[] { "[email protected]", "[email protected]",
			"[email protected]", "[email protected]", "[email protected]",
			"[email protected]", "[email protected]", "[email protected]",
			"[email protected]", "[email protected]", "[email protected]" };

	//list of invalid email addresses
	private static final String[] invalidEmailIds = new String[] { "theitroad", "[email protected]",
			"[email protected]", "[email protected]", "[email protected]", "[email protected]",
			"theitroad()*@gmail.com", "theitroad@%*.com", "[email protected]", "[email protected]",
			"theitroad@[email protected]", "[email protected]" };

	private static EmailValidator emailValidator;

	public static void main(String args[]) {
		emailValidator = new EmailValidator();
		for (String temp : validEmailIds) {
			boolean valid = emailValidator.validateEmail(temp);
			System.out.println("Email ID " + temp + " is valid? " + valid);
		}
		System.out.println("\n\n");
		for (String temp : invalidEmailIds) {
			boolean valid = emailValidator.validateEmail(temp);
			System.out.println("Email ID " + temp + " is valid? " + valid);
		}
	}
}

下图显示了上述Java电子邮件验证示例程序的输出。

Email ID [email protected] is valid? true
Email ID [email protected] is valid? true
Email ID [email protected] is valid? true
Email ID [email protected] is valid? true
Email ID [email protected] is valid? true
Email ID [email protected] is valid? true
Email ID [email protected] is valid? true
Email ID [email protected] is valid? true
Email ID [email protected] is valid? true
Email ID [email protected] is valid? true
Email ID [email protected] is valid? true

Email ID theitroad is valid? false
Email ID [email protected] is valid? false
Email ID [email protected] is valid? false
Email ID [email protected] is valid? false
Email ID [email protected] is valid? false
Email ID [email protected] is valid? false
Email ID theitroad()*@gmail.com is valid? false
Email ID theitroad@%*.com is valid? false
Email ID [email protected] is valid? false
Email ID [email protected] is valid? false
Email ID theitroad@[email protected] is valid? false
Email ID [email protected] is valid? false

因此,我们的电子邮件正则表达式Java程序似乎可以正常运行,如果您想添加更多特殊字符,则可以轻松扩展它。
这就是有关Java电子邮件验证示例的全部内容。