日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2024/4/17 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ES01 数据类型、正则表达式、身份证校验 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1 基本數(shù)據(jù)類型

  參見W3C的教程即可

2 正則表達(dá)式

  參考博文:點(diǎn)擊前往

3 身份證校驗(yàn)

  參考博文:點(diǎn)擊前往

<div class="panel panel-primary"><div class="panel-heading">身份證編號(hào)驗(yàn)證</div><div class="panel-body div_body"><div class="form_div"><form (ngSubmit)="onNgSubmit()" [formGroup]="idcardForm"><mat-form-field><input type="number" matInput matTooltip="身份證前六位數(shù)字代表地址信息" placeholder="請(qǐng)輸入身份證前6位" formControlName="address" /><mat-error *ngIf="idcardForm.hasError('required', 'address')"><span>6為地址是必填項(xiàng)</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="請(qǐng)輸入年份" formControlName="year" /><mat-error *ngIf="idcardForm.hasError('required', 'year')"><span>4為年份是必填項(xiàng)</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="請(qǐng)輸入出生月份" /><mat-error *ngIf="idcardForm.hasError('required', 'month')"><span>2位出生月份是必填項(xiàng)</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位代表出生天數(shù)" placeholder="請(qǐng)輸入出生天數(shù)" formControlName="day" /><mat-error *ngIf="idcardForm.hasError('required', 'day')"><span>2位出生天數(shù)是必填項(xiàng)</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位數(shù)字代表順序碼" formControlName="sequenceCode" placeholder="請(qǐng)輸入順序碼" /><mat-error *ngIf="idcardForm.hasError('required', 'sequenceCode')"><span>順序碼是必填項(xiàng)</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="身份證最后一位是校驗(yàn)碼" formControlName="checkCode" placeholder="請(qǐng)輸入校驗(yàn)碼" /><mat-error *ngIf="idcardForm.hasError('required', 'checkCode')"><span>校驗(yàn)碼是必填項(xiàng)</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="中國的公民身份證編號(hào)總共有18位" formControlName="idcard" placeholder="請(qǐng)輸入身份證編號(hào)" /><mat-error *ngIf="idcardForm.hasError('required', 'idcard')"><span>身份證是必填項(xiàng)</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 : "你填了數(shù)據(jù)為什么要清除呢?"}}; } else {return valid ? null : {addressLengthValidator: { desc : "地址碼的長(zhǎng)度必須為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: "地址碼長(zhǎng)6位,以數(shù)字1-9開頭,后5位為0-9的數(shù)字。"}};} // TODO:如何將多個(gè)驗(yàn)證條件寫到一個(gè)驗(yàn)證器中去// 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: "地址碼長(zhǎng)6位。"}}// }// if (addressRegx.test(value)) {// return {addressValidator: {desc: "以數(shù)字1-9開頭,后5位為0-9的數(shù)字。"}};// }// }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: "年份長(zhǎng)度必須是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: "月份長(zhǎng)度必須是2位"}};} else {return valid ? null : {monthFormatValidator: {desc: "第一位數(shù)字為0,第二位數(shù)字為1-9;或者第一位數(shù)字為1,第二位數(shù)字為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: "出生天數(shù)的長(zhǎng)度必須是2位"}};} else {return valid ? null : {dayFormatValidator: {desc: "第一位數(shù)字為0-2,第二位數(shù)字為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: '順序碼的長(zhǎng)度必須是3位'}};} else {return valid ? null : {sequenceCodeValidator: {desc: '順序碼是數(shù)字'}};} }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: '校驗(yàn)碼的長(zhǎng)度必須是1位'}};} else {return valid ? null : {checkCodeValidator: {desc: '校驗(yàn)碼自能是數(shù)字或者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: '身份證的長(zhǎng)度必須是18位'}};} else {return valid ? null : {idcardValidator: {desc: '身份證格式錯(cuò)誤'}};} }} TS

?

轉(zhuǎn)載于:https://www.cnblogs.com/NeverCtrl-C/p/8575991.html

與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的ES01 数据类型、正则表达式、身份证校验的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。