This commit is contained in:
Looly 2024-03-01 15:49:58 +08:00
parent df9e71a596
commit 5c1ffbcc0c
4 changed files with 28 additions and 7 deletions

View File

@ -615,12 +615,18 @@ public class Validator {
}
/**
* 验证是否为可用邮箱地址
* 验证是否为可用邮箱地址<br>
* 邮箱地址限制长度为254个字符参考https://stackoverflow.com/questions/386294/what-is-the-maximum-length-of-a-valid-email-address/44317754
*
* @param value
* @return true为可用邮箱地址
*/
public static boolean isEmail(final CharSequence value) {
final int codeLength = StrUtil.codeLength(value);
if(codeLength < 1 || codeLength > 254){
return false;
}
return isMatchRegex(PatternPool.EMAIL, value);
}

View File

@ -61,9 +61,12 @@ public class PatternPool {
*/
public final static Pattern MONEY = Pattern.compile(RegexPool.MONEY);
/**
* 邮件符合RFC 5322规范正则来自<a href="http://emailregex.com/">http://emailregex.com/</a><br>
* <a href="https://stackoverflow.com/questions/386294/what-is-the-maximum-length-of-a-valid-email-address/44317754">https://stackoverflow.com/questions/386294/what-is-the-maximum-length-of-a-valid-email-address/44317754</a>
* 注意email 要宽松一点比如 jetz.chong@hutool.cnjetz-chong@ hutool.cnjetz_chong@hutool.cndazhi.duan@hutool.cn 宽松一点把都算是正常的邮箱
* 邮件符合RFC 5322规范注意email 要宽松一点比如 jetz.chong@hutool.cnjetz-chong@ hutool.cnjetz_chong@hutool.cndazhi.duan@hutool.cn 宽松一点把都算是正常的邮箱
* 参考
* <ul>
* <li>https://stackoverflow.com/questions/386294/what-is-the-maximum-length-of-a-valid-email-address/44317754</li>
* <li>https://stackoverflow.com/questions/201323/how-can-i-validate-an-email-address-using-a-regular-expression</li>
* </ul>
*/
public final static Pattern EMAIL = Pattern.compile(RegexPool.EMAIL, Pattern.CASE_INSENSITIVE);
/**

View File

@ -59,9 +59,13 @@ public interface RegexPool {
*/
String MONEY = "^(\\d+(?:\\.\\d+)?)$";
/**
* 邮件符合RFC 5322规范
* What is the maximum length of a valid email address? <a href="https://stackoverflow.com/questions/386294/what-is-the-maximum-length-of-a-valid-email-address/44317754">https://stackoverflow.com/questions/386294/what-is-the-maximum-length-of-a-valid-email-address/44317754</a>
* 注意email 要宽松一点比如 jetz.chong@hutool.cnjetz-chong@ hutool.cnjetz_chong@hutool.cndazhi.duan@hutool.cn 宽松一点把都算是正常的邮箱
* 邮件符合RFC 5322规范注意email 要宽松一点比如 jetz.chong@hutool.cnjetz-chong@ hutool.cnjetz_chong@hutool.cndazhi.duan@hutool.cn 宽松一点把都算是正常的邮箱<br>
* 正则来自<a href="http://emailregex.com/">http://emailregex.com/</a>
* 参考
* <ul>
* <li>https://stackoverflow.com/questions/386294/what-is-the-maximum-length-of-a-valid-email-address/44317754</li>
* <li>https://stackoverflow.com/questions/201323/how-can-i-validate-an-email-address-using-a-regular-expression</li>
* </ul>
*/
String EMAIL = "(?:[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])+)])";
/**

View File

@ -135,6 +135,12 @@ public class ValidatorTest {
Assertions.assertTrue(email5);
}
@Test
void issueI94YYUTest() {
final boolean ret = PatternPool.EMAIL.matcher(StrUtil.repeat("hutool\\\"", 1000)).matches();
Assertions.assertFalse(ret);
}
@Test
public void isMobileTest() {
final boolean m1 = Validator.isMobile("13900221432");
@ -143,6 +149,8 @@ public class ValidatorTest {
Assertions.assertTrue(m2);
final boolean m3 = Validator.isMobile("+8618600221432");
Assertions.assertTrue(m3);
final boolean m4 = Validator.isMobile("19312341234");
Assertions.assertTrue(m4);
}
@Test