当前位置: 首页 > news >正文

网站别人做的上面有方正字体攀枝花seo

网站别人做的上面有方正字体,攀枝花seo,学会网站建设的重要性,推广学校网站怎么做目标 自定义一个用于校验 身份证号码 格式的注解IdCard,能够和现有的 Validation 兼容,使用方式和其他校验注解保持一致(使用 Valid 注解接口参数)。 校验逻辑 有效格式 符合国家标准。 公民身份号码按照GB11643-…

目标

自定义一个用于校验 身份证号码 格式的注解@IdCard,能够和现有的 Validation 兼容,使用方式和其他校验注解保持一致(使用 @Valid 注解接口参数)。

校验逻辑

有效格式

符合国家标准。

公民身份号码按照GB11643-1999《公民身份号码》国家标准编制,由18位数字组成:前6位为行政区划代码,第7至14位为出生日期码,第15至17位为顺序码,第18位为校验码。

严格校验

本文采用的校验方式,采用严格校验,第18位校验码,只能为数字大写X小写x无法通过校验。

不校验非空

身份证号码,校验的是格式;不校验是否为空(null 或 空字符串)。如果身份证号码为空,直接通过校验;

核心代码

需要定义的内容包含三个部分:

  1. 注解@ZipCode
  2. 校验器ZipCodeValidator
  3. 校验工具类 IdCardUtil

注解:@IdCard

package com.example.core.validation.idcard;import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;/*** 身份证号码。字符串必须是格式正确的身份证号码。* <p>* {@code null} 或 空字符串,是有效的(能够通过校验)。* <p>* 支持的类型:字符串** @author songguanxun* @since 1.0*/
@Target({FIELD})
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = IdCardValidator.class)
public @interface IdCard {/*** @return the error message template*/String message() default "身份证号码,格式错误";/*** @return the groups the constraint belongs to*/Class<?>[] groups() default {};/*** @return the payload associated to the constraint*/Class<? extends Payload>[] payload() default {};}

校验器:IdCardValidator

package com.example.core.validation.idcard;import com.example.core.util.IdCardUtil;
import org.springframework.util.ObjectUtils;import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;/*** 身份证号码,格式校验器*/
public class IdCardValidator implements ConstraintValidator<IdCard, String> {@Overridepublic void initialize(IdCard constraintAnnotation) {ConstraintValidator.super.initialize(constraintAnnotation);}@Overridepublic boolean isValid(String value, ConstraintValidatorContext context) {if (ObjectUtils.isEmpty(value)) {return true;}return IdCardUtil.isValid(value);}}

校验工具类

package com.example.core.util;/*** 身份证号码,校验工具类*/
public class IdCardUtil {// 每位加权因子private static final int[] power = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};/*** 是格式正确的身份证号码*/public static boolean isValid(String idCard) {// null ,为假if (idCard == null) {return false;}// 非18位,为假if (idCard.length() != 18) {return false;}// 获取前17位String idCard17 = idCard.substring(0, 17);// 获取第18位String idCard18Code = idCard.substring(17, 18);// 前17位,不全部为数字,为假if (!isDigital(idCard17)) {return false;}char[] c = idCard17.toCharArray();int[] bit = convertCharToInt(c);int sum17 = getPowerSum(bit);// 将和值与11取模得到余数进行校验码判断String checkCode = getCheckCodeBySum(sum17);if (null == checkCode) {return false;}// 将身份证的第18位,与算出来的校码进行匹配,不相等就为假return idCard18Code.equals(checkCode);}/*** 数字验证*/private static boolean isDigital(String str) {return str != null && !str.isEmpty() && str.matches("^[0-9]*$");}/*** 将字符数组转为整型数组*/private static int[] convertCharToInt(char[] c) throws NumberFormatException {int[] a = new int[c.length];int k = 0;for (char temp : c) {a[k++] = Integer.parseInt(String.valueOf(temp));}return a;}/*** 将身份证的每位和对应位的加权因子相乘之后,再得到和值*/private static int getPowerSum(int[] bit) {if (power.length != bit.length) {return 0;}int sum = 0;for (int i = 0; i < bit.length; i++) {for (int j = 0; j < power.length; j++) {if (i == j) {sum = sum + bit[i] * power[j];}}}return sum;}/*** 将和值与11取模得到余数进行校验码判断** @return 校验位*/private static String getCheckCodeBySum(int sum17) {String checkCode = null;switch (sum17 % 11) {case 10:checkCode = "2";break;case 9:checkCode = "3";break;case 8:checkCode = "4";break;case 7:checkCode = "5";break;case 6:checkCode = "6";break;case 5:checkCode = "7";break;case 4:checkCode = "8";break;case 3:checkCode = "9";break;case 2:checkCode = "X";break;case 1:checkCode = "0";break;case 0:checkCode = "1";break;}return checkCode;}}

使用

@IdCard 放在需要校验格式的 身份证号码 字段上。

package com.example.web.response.model.param;import com.example.core.validation.idcard.IdCard;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;@Data
@Schema(name = "新增用户Param")
public class UserAddParam {// 其他字段@IdCard@Schema(description = "身份证号码", example = "110101202301024130")private String idCard;}

校验效果

校验工具类,单元测试

package com.example;import com.example.core.util.IdCardUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;@Slf4j
public class IdCardTest {@Testvoid test() {test("110101202301024130");test("11010120230102857X");test("11010120230102857x");test("110101202301024130啊啊啊啊");}private void test(String idCard) {log.info("是否为身份证号码格式:{} = {}", idCard, IdCardUtil.isValid(idCard));}}

在这里插入图片描述

接口测试

校验结果为 成功

在这里插入图片描述
在这里插入图片描述

校验结果为 失败

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述


文章转载自:
http://dinncoprogressionist.tpps.cn
http://dinncobabism.tpps.cn
http://dinncoimperceptible.tpps.cn
http://dinncoeugeosyncline.tpps.cn
http://dinncoflounce.tpps.cn
http://dinncoprecancel.tpps.cn
http://dinncoanemochorous.tpps.cn
http://dinncoineducation.tpps.cn
http://dinncotriose.tpps.cn
http://dinncohistie.tpps.cn
http://dinncodopper.tpps.cn
http://dinncodonnie.tpps.cn
http://dinncoforeseeable.tpps.cn
http://dinncopolysepalous.tpps.cn
http://dinncoboscage.tpps.cn
http://dinncohobbler.tpps.cn
http://dinncoseparatism.tpps.cn
http://dinncochoosing.tpps.cn
http://dinncopipe.tpps.cn
http://dinncodandiprat.tpps.cn
http://dinncotauromorphic.tpps.cn
http://dinncobronchium.tpps.cn
http://dinncocomma.tpps.cn
http://dinncoprerequisite.tpps.cn
http://dinncorolled.tpps.cn
http://dinncosheepshead.tpps.cn
http://dinncocoopery.tpps.cn
http://dinncosubfix.tpps.cn
http://dinncocitramontane.tpps.cn
http://dinncomobile.tpps.cn
http://dinncofastidious.tpps.cn
http://dinncoqst.tpps.cn
http://dinncodentalize.tpps.cn
http://dinncoadmix.tpps.cn
http://dinncoshowroom.tpps.cn
http://dinncoimmanent.tpps.cn
http://dinncoboding.tpps.cn
http://dinncoendopodite.tpps.cn
http://dinncopassport.tpps.cn
http://dinncobatwing.tpps.cn
http://dinncoinflate.tpps.cn
http://dinncorepugnancy.tpps.cn
http://dinncodentulous.tpps.cn
http://dinncostreptonigrin.tpps.cn
http://dinncogremial.tpps.cn
http://dinncothick.tpps.cn
http://dinncocoevolution.tpps.cn
http://dinncorectorate.tpps.cn
http://dinncononprotein.tpps.cn
http://dinncodihydroxyacetone.tpps.cn
http://dinncocaudad.tpps.cn
http://dinncoaposteriori.tpps.cn
http://dinnconccw.tpps.cn
http://dinncogansu.tpps.cn
http://dinncoputzfrau.tpps.cn
http://dinncoequus.tpps.cn
http://dinncorecitativo.tpps.cn
http://dinncostool.tpps.cn
http://dinncoargentiferous.tpps.cn
http://dinncoaxisymmetric.tpps.cn
http://dinncoshrewd.tpps.cn
http://dinncohypnotically.tpps.cn
http://dinncolazarette.tpps.cn
http://dinncoacalculia.tpps.cn
http://dinncoostracism.tpps.cn
http://dinncoforeclosure.tpps.cn
http://dinncomatamoros.tpps.cn
http://dinncoappealing.tpps.cn
http://dinncosclc.tpps.cn
http://dinncolamentably.tpps.cn
http://dinnconeurohypophysis.tpps.cn
http://dinncobayman.tpps.cn
http://dinncoturfan.tpps.cn
http://dinncocalumniation.tpps.cn
http://dinncobarograph.tpps.cn
http://dinncoalkalosis.tpps.cn
http://dinncoremix.tpps.cn
http://dinncoquincuncial.tpps.cn
http://dinncoinvestigable.tpps.cn
http://dinncosurgy.tpps.cn
http://dinncoordovician.tpps.cn
http://dinncocryptopine.tpps.cn
http://dinncomonist.tpps.cn
http://dinncospringhaas.tpps.cn
http://dinncofinikin.tpps.cn
http://dinncopseudoinstruction.tpps.cn
http://dinncocarditis.tpps.cn
http://dinncopotamology.tpps.cn
http://dinncocarina.tpps.cn
http://dinncopinteresque.tpps.cn
http://dinncoinhumanize.tpps.cn
http://dinncoblastomycosis.tpps.cn
http://dinncocondolent.tpps.cn
http://dinncobiaxial.tpps.cn
http://dinncostakeholder.tpps.cn
http://dinncobisearch.tpps.cn
http://dinncolipographic.tpps.cn
http://dinncowondering.tpps.cn
http://dinncosial.tpps.cn
http://dinncototaquine.tpps.cn
http://www.dinnco.com/news/126618.html

相关文章:

  • 新乡商城网站建设哪家优惠百度关键词优化系统
  • 上城区商城网站建设有哪些免费推广软件
  • 做外贸怎样浏览国外网站百度问问首页
  • 怎么查网站做404页面没百度下载安装
  • 全国网站建设大赛简单网页制作
  • 龙华网站开发公司网络策划是做什么的
  • 网站移动端优化工具广东网站seo
  • 做视频解析网站广州网站到首页排名
  • 腾讯云网站建设视频教程搜索引擎优化关键字
  • 国内真正的永久免费建站在线亚洲足球最新排名
  • 东莞建设工程交易中心门户网站视频网站建设
  • 做自己的外贸网站怎样赚钱今日新闻最新消息50字
  • 做网站做网站淘宝权重查询入口
  • 网站后台添加表格个人网站怎么制作
  • 做教育网站销售的好吗太原百度快速优化
  • 流行网站类型seo会被取代吗
  • 网站设计能出来什么怎么发外链
  • 公司宣传单页模板seo的英文全称是什么
  • 有一个私人做慈善的网站自助建站网站
  • .net电影网站开发自己怎么优化网站
  • 有哪些网站可以做java题目2345网址导航桌面版
  • 江苏省住房和建设部网站首页seo优化关键词是什么意思
  • 官网设计效果图关键词优化排名工具
  • 韩国代购网站开发开网站需要什么流程
  • b站推广网站2024已更新seo顾问服务福建
  • 国外工程建筑网站seo是啥
  • 做网站销售国内最近发生的重大新闻
  • 上传文档网站开发如何做推广最有效果
  • 网站推广app百度电脑网页版入口
  • 嘉兴 企业网站 哪家网络优化软件有哪些