java判断邮箱合法性_Java判断邮箱是否合法

public class Test {

public static void main(String[] args) {

//电子邮件

String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";

Pattern regex = Pattern.compile(check);

Matcher matcher = regex.matcher("dffdfdf@qq.com");

boolean isMatched = matcher.matches();

System.out.println(isMatched);

}

public static void main(String[] args) {

Scanner scanner=new Scanner(System.in);

String mail=null;

System.out.println("请输入E-Mail:");

mail=scanner.next();

Pattern pattern=Pattern.compile("\\w+@(\\w+.)+[a-z]{2,3}");//\w表示a-z,A-Z,0-9(\\转义符)

Matcher matcher=pattern.matcher(mail);

boolean b=matcher.matches();

if (b) {

System.out.println(mail+"有效的邮箱地址!");

}else {

System.out.println(mail+"的格式错误!!");

}

}

/**

*javascript电子邮箱的合法性验证

*/

function isEmail(email)

{

var srt=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;

if(srt.test(email))

{

//不合法时

return false;

}

else

{

//合法时

return true;

}

}

}

public static boolean validateEmail(String email) {

boolean flag = false;

int pos = email.indexOf("@");

if (pos == -1 || pos == 0 || pos == email.length() - 1) {

return false;

}

String[] strings = email.split("@");

if (strings.length != 2) {// 如果邮箱不是xxx@xxx格式

return false;

}

CharSequence cs = strings[0];

for (int i = 0; i < cs.length(); i++) {

char c = cs.charAt(i);

if (!Character.isLetter(c) && !Character.isDigit(c)) {

return false;

}

}

pos = strings[1].indexOf(".");// 如果@后面没有.,则是错误的邮箱。

if (pos == -1 || pos == 0 || pos == email.length() - 1) {

return false;

}

strings = strings[1].split(".");

for (int j = 0; j < strings.length; j++) {

cs = strings[j];

if (cs.length() == 0) {

return false;

}

for (int i = 0; i < cs.length(); i++) {//如果保护不规则的字符,表示错误

char c = cs.charAt(i);

if (!Character.isLetter(c) && !Character.isDigit(c)) {

return false;

}

}

}

return true;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用正则表达式判断邮箱格式的方法如下: 首先,要编写一个符合邮箱格式的正则表达式。邮箱的格式通常是 username@domain.com,因此可以使用以下正则表达式:^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$。 接下来,可以使用Java中的正则表达式方法来判断是否匹配。可以通过Pattern类和Matcher类来实现。代码示例如下: ```java import java.util.regex.Pattern; import java.util.regex.Matcher; public class EmailFormatValidator { public static boolean isEmailValid(String email) { // 定义邮箱格式的正则表达式 String emailRegex = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$"; // 创建Pattern对象 Pattern pattern = Pattern.compile(emailRegex); // 创建Matcher对象 Matcher matcher = pattern.matcher(email); // 匹配成功,返回true;否则,返回false return matcher.matches(); } } ``` 在需要判断邮箱格式的地方,可以调用`isEmailValid`方法,并传入一个邮箱字符串作为参数。方法会返回一个布尔值,表示该邮箱格式是否合法。 使用示例: ```java public class Main { public static void main(String[] args) { String email = "example@example.com"; if (EmailFormatValidator.isEmailValid(email)) { System.out.println("邮箱格式正确"); } else { System.out.println("邮箱格式错误"); } } } ``` 以上代码在判断邮箱格式时,会输出"邮箱格式正确"。如果将`email`变量修改为一个不符合邮箱格式的字符串,例如"example.example.com",则会输出"邮箱格式错误"。 通过正则表达式,可以方便地判断邮箱格式的正确性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值