diff --git a/hutool-core/src/main/java/cn/hutool/core/util/IdcardUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/IdcardUtil.java index 3d8b9ce88..9d212bfd1 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/IdcardUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/IdcardUtil.java @@ -343,7 +343,7 @@ public class IdcardUtil { * @return 验证码是否符合 */ public static boolean isValidTWCard(String idcard) { - if (StrUtil.isEmpty(idcard)) { + if (StrUtil.isEmpty(idcard) || idcard.length() != 10) { return false; } String start = idcard.substring(0, 1); diff --git a/hutool-core/src/test/java/cn/hutool/core/util/IdcardUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/IdcardUtilTest.java index 010646455..24f423fc0 100644 --- a/hutool-core/src/test/java/cn/hutool/core/util/IdcardUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/util/IdcardUtilTest.java @@ -7,7 +7,7 @@ import org.junit.Test; /** * 身份证单元测试 - * + * * @author Looly * */ @@ -111,4 +111,17 @@ public class IdcardUtilTest { boolean flag=IdcardUtil.isValidHKCard(hkCard); Assert.assertTrue(flag); } + + @Test + public void isValidTWCardIdTest() { + String twCard = "B221690311"; + boolean flag = IdcardUtil.isValidTWCard(twCard); + Assert.assertTrue(flag); + String errTwCard1 = "M517086311"; + flag = IdcardUtil.isValidTWCard(errTwCard1); + Assert.assertFalse(flag); + String errTwCard2 = "B2216903112"; + flag = IdcardUtil.isValidTWCard(errTwCard2); + Assert.assertFalse(flag); + } }