ES01 数据类型、正则表达式、身份证校验

1 基本数据类型

  参见W3C的教程即可

2 正则表达式

  参考博文:点击前往

3 身份证校验

  参考博文:点击前往

<div class="panel panel-primary">
  <div class="panel-heading">身份证编号验证</div>
  <div class="panel-body div_body">
    <div class="form_div">
      <form (ngSubmit)="onNgSubmit()" [formGroup]="idcardForm">
        <mat-form-field>
            <input type="number" matInput matTooltip="身份证前六位数字代表地址信息" placeholder="请输入身份证前6位" formControlName="address" />
            <mat-error *ngIf="idcardForm.hasError(‘required‘, ‘address‘)">
              <span>6为地址是必填项</span>
            </mat-error>
            <mat-error *ngIf="idcardForm.hasError(‘addressFormatValidator‘, ‘address‘)">
              <span>{{idcardForm.getError(‘addressFormatValidator‘, ‘address‘)?.desc}}</span>
            </mat-error>
            <mat-error *ngIf="idcardForm.hasError(‘addressLengthValidator‘, ‘address‘)">
              <span>{{idcardForm.getError(‘addressLengthValidator‘, ‘address‘)?.desc}}</span>
            </mat-error>
        </mat-form-field>
        <mat-form-field>
          <input type="number" matInput matTooltip="身份证的第7到10位代表出生年份" placeholder="请输入年份" formControlName="year" />
          <mat-error *ngIf="idcardForm.hasError(‘required‘, ‘year‘)">
            <span>4为年份是必填项</span>
          </mat-error>
          <mat-error *ngIf="idcardForm.hasError(‘yearFormatValidator‘, ‘year‘)">
            <span>{{ idcardForm.getError(‘yearFormatValidator‘, ‘year‘)?.desc }}</span>
          </mat-error>
        </mat-form-field>
        <mat-form-field>
          <input type="number" matInput matTooltip="身份证第11到12位代表出生月份" formControlName="month" placeholder="请输入出生月份" />
          <mat-error *ngIf="idcardForm.hasError(‘required‘, ‘month‘)">
            <span>2位出生月份是必填项</span>
          </mat-error>
          <mat-error *ngIf="idcardForm.hasError(‘monthFormatValidator‘, ‘month‘)">
            <span>{{idcardForm.getError(‘monthFormatValidator‘, ‘month‘)?.desc}}</span>
          </mat-error>
        </mat-form-field>
        <mat-form-field>
          <input type="number" matInput matTooltip="身份证的第13到14位代表出生天数" placeholder="请输入出生天数" formControlName="day" />
          <mat-error *ngIf="idcardForm.hasError(‘required‘, ‘day‘)">
            <span>2位出生天数是必填项</span>
          </mat-error>
          <mat-error *ngIf="idcardForm.hasError(‘dayFormatValidator‘, ‘day‘)">
            <span>{{idcardForm.getError(‘dayFormatValidator‘, ‘day‘)?.desc}}</span>
          </mat-error>
        </mat-form-field>
        <mat-form-field>
          <input type="number" matInput matTooltip="身份证的15到17位数字代表顺序码" formControlName="sequenceCode" placeholder="请输入顺序码" />
          <mat-error *ngIf="idcardForm.hasError(‘required‘, ‘sequenceCode‘)">
            <span>顺序码是必填项</span>
          </mat-error>
          <mat-error *ngIf="idcardForm.hasError(‘sequenceCodeValidator‘, ‘sequenceCode‘)">
            <span>{{idcardForm.getError(‘sequenceCodeValidator‘, ‘sequenceCode‘)?.desc}}</span>
          </mat-error>
        </mat-form-field>
        <mat-form-field>
          <input type="text" matInput matTooltip="身份证最后一位是校验码" formControlName="checkCode" placeholder="请输入校验码" />
          <mat-error *ngIf="idcardForm.hasError(‘required‘, ‘checkCode‘)">
            <span>校验码是必填项</span>
          </mat-error>
          <mat-error *ngIf="idcardForm.hasError(‘checkCodeValidator‘, ‘checkCode‘)">
            <span>{{idcardForm.getError(‘checkCodeValidator‘, ‘checkCode‘)?.desc}}</span>
          </mat-error>
        </mat-form-field>
        <mat-form-field>
            <input type="text" matInput matTooltip="中国的公民身份证编号总共有18位" formControlName="idcard" placeholder="请输入身份证编号" />
            <mat-error *ngIf="idcardForm.hasError(‘required‘, ‘idcard‘)">
              <span>身份证是必填项</span>
            </mat-error>
            <mat-error *ngIf="idcardForm.hasError(‘idcardValidator‘, ‘idcard‘)">
              <span>{{idcardForm.getError(‘idcardValidator‘, ‘idcard‘)?.desc}}</span>
            </mat-error>
          </mat-form-field>
        <br />
        <br />
        <div>
          <button mat-raised-button [disabled]="!idcardForm.valid" type="submit" >提交</button>
        </div>
      </form>
    </div>
  </div>
  <div class="panel-footer">2018-3-15 09:42:00</div>
</div>

HTML

import { Component, OnInit } from ‘@angular/core‘;
import { HttpClient } from ‘@angular/common/http‘;
import { Http } from ‘@angular/http‘;
import { TestService } from ‘../test.service‘;
import { ClientBaseInfo } from ‘../../model/ClientBaseInfo‘;
import { FormControl, FormGroup, FormBuilder, Validators } from ‘@angular/forms‘;
import ‘rxjs/add/operator/debounceTime‘;      

@Component({
  selector: ‘app-test-demo‘,
  templateUrl: ‘./test-demo.component.html‘,
  styleUrls: [‘./test-demo.component.scss‘]
})
export class TestDemoComponent implements OnInit {

  idcardForm: FormGroup;

  constructor(
    private formBuilder: FormBuilder
  ) {

  }

  ngOnInit() {
    this.idcardForm = this.formBuilder.group({
      address: [‘502225‘, [Validators.required, this.addressFormatValidator, this.addressLengthValidator], []],
      year: [‘1993‘, [Validators.required, this.yearFormatValidator], []],
      month: [‘11‘, [Validators.required, this.monthFormatValidator], []],
      day: [‘12‘, [Validators.required, this.dayFormatValidator], []],
      sequenceCode: [‘123‘, [Validators.required, this.sequenceCodeValidator], []],
      checkCode: [‘X‘, [Validators.required, this.checkCodeValidator], []],
      idcard: [‘500225199310031919‘, [Validators.required, this.idcardValidator], []]
    });
  }

  onNgSubmit() {
    console.log(this.idcardForm.value);
  }

  addressLengthValidator(fc: FormControl): {[key: string]: any} {
    let valid = String(fc.value).length === 6;
    if (fc.value === null) {
      return valid ? null : {addressLengthValidator: { desc : "你填了数据为什么要清除呢?"}};
    } else {
      return valid ? null : {addressLengthValidator: { desc : "地址码的长度必须为6"}};
    }
  }
  addressFormatValidator(fc: FormControl): {[key: string]: any} {
    const value = fc.value;
    const addressRegx: RegExp = new RegExp(/^[1-9]\d{5}/);
    let valid = addressRegx.test(value);
    return valid ? null : {addressFormatValidator: {desc: "地址码长6位,以数字1-9开头,后5位为0-9的数字。"}};
  } 

  // TODO:如何将多个验证条件写到一个验证器中去
  // addressValidator(fc: FormControl): {[key: string]: any} {
  //   const value = fc.value;
  //   const addressRegx: RegExp = new RegExp(/^[1-9]\d{5}/);
  //   if (String(value).length !== 6) {
  //     return {addressValidator: {desc: "地址码长6位。"}}
  //   }
  //   if (addressRegx.test(value)) {
  //     return {addressValidator: {desc: "以数字1-9开头,后5位为0-9的数字。"}};
  //   }

  // }

  yearFormatValidator(fc: FormControl): {[key: string]: any} {
    const yearRegx: RegExp = new RegExp(/(17|18|19|20)\d{2}/);
    const valid = yearRegx.test(fc.value);
    if (String(fc.value).length !== 4) {
      return {yearFormatValidator: {desc: "年份长度必须是4位"}};
    } else {
      return valid ? null : {yearFormatValidator: {desc: "年份必须是17、18、19、20作为开头"}};
    }
  }

  monthFormatValidator(fc: FormControl): {[key: string]: any} {
    const monthRegx: RegExp = new RegExp(/(0[1-9])|(1[0-1])/);
    const valid = monthRegx.test(fc.value);
    if (String(fc.value).length !== 2) {
      return  {monthFormatValidator: {desc: "月份长度必须是2位"}};
    } else {
      return valid ? null : {monthFormatValidator: {desc: "第一位数字为0,第二位数字为1-9;或者第一位数字为1,第二位数字为0-2"}};
    }

  }

  dayFormatValidator(fc: FormControl): {[key: string]: any} {
    const dayRegx: RegExp = new RegExp(/(([0-2][1-9])|10|20|30|31)/);
    const valid = dayRegx.test(fc.value);
    if (String(fc.value).length !== 2) {
      return {dayFormatValidator: {desc: "出生天数的长度必须是2位"}};
    } else {
      return valid ? null : {dayFormatValidator: {desc: "第一位数字为0-2,第二位数字为1-9;或者是10,20,30,31。"}};
    }

  }

  sequenceCodeValidator(fc: FormControl): {[key: string]: any} {
    const sequenceCodeRegx: RegExp = new RegExp(/\d{3}/);
    const valid = sequenceCodeRegx.test(fc.value);
    if (String(fc.value).length !== 3) {
      return {sequenceCodeValidator: {desc: ‘顺序码的长度必须是3位‘}};
    } else {
      return valid ? null : {sequenceCodeValidator: {desc: ‘顺序码是数字‘}};
    }
  }

  checkCodeValidator(fc: FormControl): {[key: string]: any} {
    const checkCodeRegx: RegExp = new RegExp(/[0-9xX]/);
    const valid = checkCodeRegx.test(fc.value);
    if (String(fc.value).length !== 1) {
      return {checkCodeValidator: {desc: ‘校验码的长度必须是1位‘}};
    } else {
      return valid ? null : {checkCodeValidator: {desc: ‘校验码自能是数字或者X、x‘}};
    }
  }

  idcardValidator(fc: FormControl): {[key: string]: any} {
    const checkCodeRegx: RegExp = new RegExp(/^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/);
    const valid = checkCodeRegx.test(fc.value);
    if (String(fc.value).length !== 18) {
      return {idcardValidator: {desc: ‘身份证的长度必须是18位‘}};
    } else {
      return valid ? null : {idcardValidator: {desc: ‘身份证格式错误‘}};
    }
  }

}

TS

原文地址:https://www.cnblogs.com/NeverCtrl-C/p/8575991.html

时间: 2024-10-11 17:03:24

ES01 数据类型、正则表达式、身份证校验的相关文章

身份证校验,绝对严格

身份证校验,绝对严格 function checkIdCard(idcard) {  let ereg;  // const Errors = new Array(  //   '验证通过',  //   '身份证号码位数不对',  //   '身份证号码出生日期超出范围或含有非法字符',  //   '身份证号码校验错误',  //   '身份证地区非法'  // );  const area = {    11: '北京', 12: '天津', 13: '河北', 14: '山西', 15:

身份证校验

1 namespace CheckIdenoDemo 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 Console.WriteLine("请输入您的18位身份证号码:"); 8 string readstring = Console.ReadLine(); 9 10 IdentifyCheck idenCheck = new IdentifyCheck(); 11 12 idenCheck.Identit

java实现身份证校验

原文来自:java教程网 题目:身份证校验 身份证校验 如果让你设计个程序,用什么变量保存身份证号码呢?长整数可以吗?不可以! 因为有人的身份证最后一位是"X" 实际上,除了最后一位的X,不会出现其它字母! 身份证号码18位 = 17位 + 校验码 校验码的计算过程: 例如:身份证前17位 = ABCDEFGHIJKLMNOPQ A~Q 每位数字乘以权值求和(每位数字和它对应的“权”相乘后累加) 17位对应的权值分别是: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8

证件类型为身份证号时进行身份证校验

需求:代办人身份证号改为代办人证件类型(默认为身份证号),后面跟着号码,房主和代办人的证件类型为身份证号时进行身份证校验 sf_ghgl_add.jsp <td class="labelCss">代办人证件类型:</td> <td><select id="zjlx" width="10px" class="easyui-combobox" data-options="url:

JS身份证校验

身份证校验 function identityCodeValid(code) { var city={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江 ",31:"上海",32:"江苏",33:"浙江",34:

最严格的身份证校验(Java版)

上一篇博客贴出了JavaScript版本的身份证校验,本篇博客将贴出Java版的身份证校验:         import java.util.Date; /** * 校验身份证号码是否有效 * * @author GaoHuanjie */         public class CheckCertificateNo { @SuppressWarnings("deprecation") publicstaticvoid main(String[] args) { String ce

最严格的身份证校验(JavaScript版)

在JavaWeb研发过程中为了获取有效的用户信息,校验其数据的有效性非常是必要,以下贴出在项目中用到的关于身份证的校验: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312&quo

1010: 身份证校验

1010: 身份证校验 时间限制: 10 Sec  内存限制: 128 MB提交: 298  解决: 229[提交][状态][讨论版] 题目描述 我国国标[GB 11643-1999]中规定:公民身份号码是18位特征组合码,由十七位数字本体码和一位数字校验码组成.排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码.其校验码(最后一位)计算方法和步骤为: (1)十七位数字本体码加权求和公式 S = Sum(Ai * Wi), i = 0, ... , 16 

java日期正则表达式精准校验

function checkDate(obj) {       var date=obj.value;     var re = new RegExp("(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0