常用表单验证java处理校验格式

@package com.jkys.cis.promote.common.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**

  • @author 方蝶

  • @Auther: fangDie

  • @Date: 2020/9/3 15:17

  • @Description:
    */
    public class CheckUtil {

    /**

    • 返回状态 1为空

    */
    public static final String RESULT_NULL = “1”;

    /**

    • 2-不符合规则
      */
      public static final String RESULT_Mismatch_RULE = “2”;

    /**

    • 3-验证通过
      */
      public static final String RESULT_TRUE = “3”;

    /**

    • 校验str中是否包含特殊字符
    • @param str 字符
    • @return true-有字符 false-没有包含字符
      /
      public static String checkName(String str) {
      //判断姓名是否为空 空-返回false
      if(StringUtils.isEmpty(str)){
      return RESULT_NULL;
      }
      String regEx = "[ _`!@#$%^&*()+=|{}’:;’,\[\].<>/?!@#¥%……&
      ()——+|{}【】‘;:”“’。,、?]|\n|\r|\t";
      Pattern p = Pattern.compile(regEx);
      Matcher m = p.matcher(str);
      boolean b = m.find();
      //为true姓名包含字符 返回false 不让验证通过
      if (b){
      return RESULT_Mismatch_RULE;
      }
      return RESULT_TRUE;
      }

    /**

    • 校验手机号是否1开头并且11位数字

    • @param mobile 手机号

    • @return true-格式正确 false-格式不正确
      */
      public static String checkMobile(String mobile){
      if (StringUtils.isEmpty(mobile)){
      return RESULT_NULL;
      }

      if (!mobile.matches( “1\d{10}”)){
      return RESULT_Mismatch_RULE;
      }
      return RESULT_TRUE;
      }

    /**

    • 校验验证码为四位数字
    • @param verifyCode 验证码
    • @return true-格式正确 false-格式不正确
      */
      public static String checkVerifyCode(String verifyCode){
      if (StringUtils.isEmpty(verifyCode)){
      return RESULT_NULL;
      }
      boolean matches = verifyCode.matches("\d{4}");
      if (!matches){
      return RESULT_Mismatch_RULE;
      }
      return RESULT_TRUE;
      }

    public static String checkIdCard(String idCard){
    if (StringUtils.isEmpty(idCard)){
    return RESULT_NULL;
    }
    String regularExpression = “(1\d{5}(18|19|20)\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx] ) ∣ " + " ( [ 1 − 9 ] d 5 d 2 ( ( 0 [ 1 − 9 ] ) ∣ ( 10 ∣ 11 ∣ 12 ) ) ( ( [ 0 − 2 ] [ 1 − 9 ] ) ∣ 10 ∣ 20 ∣ 30 ∣ 31 ) d 3 )|" + "(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3} )"+"([19]d5d2((0[19])(101112))(([02][19])10203031)d3)”;
    boolean matches = Pattern.matches(regularExpression, idCard);
    if (!matches){
    return RESULT_Mismatch_RULE;
    }
    return RESULT_TRUE;
    }

    public static void main(String[] args) {
    String idCard=“520202109318107722”;
    String s = CheckUtil.checkIdCard(idCard);
    System.out.println(s);
    }

}


  1. 1-9 ↩︎

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值