!997 邮箱校验添加对中文的支持。

Merge pull request !997 from Yiwyn/v5-dev
This commit is contained in:
Looly 2023-05-14 15:11:43 +00:00 committed by Gitee
commit 721faf353a
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 33 additions and 0 deletions

View File

@ -54,6 +54,11 @@ public class PatternPool {
* 注意email 要宽松一点比如 jetz.chong@hutool.cnjetz-chong@ hutool.cnjetz_chong@hutool.cndazhi.duan@hutool.cn 宽松一点把都算是正常的邮箱
*/
public final static Pattern EMAIL = Pattern.compile(RegexPool.EMAIL, Pattern.CASE_INSENSITIVE);
/**
* 规则同EMAIL添加了对中文的支持
*/
public final static Pattern EMAIL_WITH_CHINESE = Pattern.compile(RegexPool.EMAIL_WITH_CHINESE,Pattern.CASE_INSENSITIVE);
/**
* 移动电话
*/

View File

@ -52,6 +52,11 @@ public interface RegexPool {
* 注意email 要宽松一点比如 jetz.chong@hutool.cnjetz-chong@ hutool.cnjetz_chong@hutool.cndazhi.duan@hutool.cn 宽松一点把都算是正常的邮箱
*/
String EMAIL = "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)])";
/**
* 规则同EMAIL添加了对中文的支持
*/
String EMAIL_WITH_CHINESE = "(?:[a-z0-9\\u4e00-\\u9fa5!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9\\u4e00-\\u9fa5!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9\\u4e00-\\u9fa5](?:[a-z0-9\\u4e00-\\u9fa5-]*[a-z0-9\\u4e00-\\u9fa5])?\\.)+[a-z0-9\\u4e00-\\u9fa5](?:[a-z0-9\\u4e00-\\u9fa5-]*[a-z0-9\\u4e00-\\u9fa5])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9\\u4e00-\\u9fa5-]*[a-z0-9\\u4e00-\\u9fa5]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)])";
/**
* 移动电话
* eg: 中国大陆 +86 180 4953 13992位区域码标示+11位数字

View File

@ -55,6 +55,12 @@ public class Validator {
* 邮件
*/
public final static Pattern EMAIL = PatternPool.EMAIL;
/**
* 邮件包含中文
*/
public final static Pattern EMAIL_WITH_CHINESE = PatternPool.EMAIL_WITH_CHINESE;
/**
* 移动电话
*/
@ -684,6 +690,21 @@ public class Validator {
return isMatchRegex(EMAIL, value);
}
/**
* 验证是否为可用邮箱地址兼容中文邮箱地址
*
* @param value
* @param includChinese 包含中文标识
* @return true为可用邮箱地址
*/
public static boolean isEmail(CharSequence value,boolean includChinese) {
if (includChinese){
return isMatchRegex(EMAIL_WITH_CHINESE, value);
}
return isEmail(value);
}
/**
* 验证是否为可用邮箱地址
*

View File

@ -113,6 +113,8 @@ public class ValidatorTest {
Assert.assertTrue(email3);
boolean email4 = Validator.isEmail("xiaolei.Lu@aaa.b");
Assert.assertTrue(email4);
boolean email5 = Validator.isEmail("luxiaolei_路小磊@路小磊.com",true);
Assert.assertTrue(email5);
}
@Test