惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Announcements
Recent Announcements
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
MongoDB | Blog
MongoDB | Blog
H
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
人人都是产品经理
人人都是产品经理
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The GitHub Blog
The GitHub Blog
V
V2EX
Microsoft Security Blog
Microsoft Security Blog
V
Visual Studio Blog
A
About on SuperTechFans
博客园_首页
L
LangChain Blog
量子位
雷峰网
雷峰网
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Jina AI
Jina AI
月光博客
月光博客
阮一峰的网络日志
阮一峰的网络日志
博客园 - 聂微东
Microsoft Azure Blog
Microsoft Azure Blog
M
MIT News - Artificial intelligence
N
Netflix TechBlog - Medium

zodream梦想开源/个人编程日记

文件解析笔记-zodream梦想开源/个人编程日记 密码本开发笔记之读写与保存-zodream梦想开源/个人编程日记 SkiaSharp 把 pixel byte[] 转成 SKBitmap-zodream梦想开源/个人编程日记 nas 使用 Docker 安装 gogs-zodream梦想开源/个人编程日记 复制 android 手机中的文件到电脑-zodream梦想开源/个人编程日记 周报:寻找优质的周刊-zodream梦想开源/个人编程日记 开发日志:对Markdown的代码块新增引用来源支持-zodream梦想开源/个人编程日记 周报:怎么写技术类的教程文章-zodream梦想开源/个人编程日记 css display:flex 布局尺寸超出问题-zodream梦想开源/个人编程日记 周报:SEO优化的思考-zodream梦想开源/个人编程日记 Edge 浏览器不适用 Edge Image Viewer 打开图片 -zodream梦想开源/个人编程日记 SEO 学习笔记(一) 内容来源-zodream梦想开源/个人编程日记 PHP 实现双因素身份认证(2FA)-zodream梦想开源/个人编程日记 WPF MVVM 获取List 多选数据-zodream梦想开源/个人编程日记 Burp Suite 抓包-zodream梦想开源/个人编程日记 使用 indexnow 注意事项-zodream梦想开源/个人编程日记 Godot 使用字体图标 例如: Iconfont、FontAwesome-zodream梦想开源/个人编程日记 angular 15 对指定页面进行访问限制-zodream梦想开源/个人编程日记 CSS 使用 column-count 实现瀑布流出现内容分割的解决办法-zodream梦想开源/个人编程日记 input 确认按键事件在手机端不生效-zodream梦想开源/个人编程日记 C# 使用socket 进行通讯-zodream梦想开源/个人编程日记 Maui开发中Windows应用开启管理员权限-zodream梦想开源/个人编程日记 Maui 中自定义控件-zodream梦想开源/个人编程日记 angular 14 使用 ng-template 实现tree 结构显示-zodream梦想开源/个人编程日记 c# 动态安装和卸载dll-zodream梦想开源/个人编程日记 慎用 CompositionTarget.Rendering-zodream梦想开源/个人编程日记 c# 重写 c++ 程序笔记:数据初始化-zodream梦想开源/个人编程日记 源码编译 aseprite-zodream梦想开源/个人编程日记 记录一下字符串分隔split各语言之间的不同-zodream梦想开源/个人编程日记 c# Gzip解码无头内容-zodream梦想开源/个人编程日记
angular9教程之表单验证及确认密码验证-zodream梦想开源/个人...
zodream · 2020-05-26 · via zodream梦想开源/个人编程日记

FormGroup 的使用

这是一个注册表单,

export class RegisterComponent {
    public registerForm = this.fb.group({
        name: ['', Validators.required],
        email: ['', [Validators.required, Validators.email]],
        password: ['', [Validators.required, passwordValidator]],
        confirm_password: ['', [Validators.required]],
        agree: [false, Validators.requiredTrue]
    }, {
        validators: confirmValidator()
    });

    constructor(
        private fb: FormBuilder
    ) { }

    get email() {
        return this.registerForm.get('email');
    }

    get password() {
        return this.registerForm.get('password');
    }
}

1234567891011121314151617181920212223

通过 [formGroup] 绑定表单数据源

通过 formControlName 绑定表单项

<form class="form-ico login-form" [formGroup]="registerForm" (ngSubmit)="tapSignUp()">
  <div class="input-group">
        <input type="text" formControlName="name" placeholder="请输入昵称" required="">
        <i class="iconfont icon-user" aria-hidden="true"></i>
    </div>
    <div class="input-group" [class]="{error: email.invalid}">
        <input type="email" formControlName="email" placeholder="请输入邮箱" required="">
        <i class="iconfont icon-at" aria-hidden="true"></i>
    </div>
    <div class="input-group" [class]="{error: password.invalid}">
        <input type="password" formControlName="password" placeholder="请输入密码" required="">
        <i class="iconfont icon-lock" aria-hidden="true"></i>
    </div>
    <div class="input-group" [class]="{error: registerForm.errors && registerForm.errors.confirm}">
        <input type="password" formControlName="confirm_password" placeholder="请确认密码" required="">
        <i class="iconfont icon-check" aria-hidden="true"></i>
    </div>

    <div class="input-group">
        <div class="checkbox">
            <input type="checkbox" formControlName="agree" value="1" id="checkboxInput">
            <label for="checkboxInput"></label>
        </div>
        同意《
        <a href="https://zodream.cn/agreement">本站协议</a>
        》
    </div>

    <button type="submit" class="btn" [disabled]="registerForm.invalid">注册</button>
    <div class="other-box">
        <a routerLink="../">返回登录</a>
    </div>

</form>

12345678910111213141516171819202122232425262728293031323334

获取错误信息

AbstractControl.invalid 获取是否有误

AbstractControl.errors. 获取具体错误信息,注意 .errors 有可能为 undefined 需要先判断

在这里,自定义了两个验证器:

密码验证

  1. passwordValidator 可以验证密码的复杂程度。
export const passwordValidator: ValidatorFn = (control: AbstractControl): ValidationErrors | null => {
  return control.value && control.value.length < 6 ? {
    password_simple: true
  } : null;
};

12345

确认密码验证

  1. confirmValidator 可以验证确认密码是否一致
export const confirmValidator = (key: string = 'password', confirmKey: string = 'confirm_password'): ValidatorFn => {
  return (control: FormGroup): ValidationErrors | null => {
    return control.get(key).value !== control.get(confirmKey).value ? {
      confirm : true
    } : null;
  };
};

1234567

1). 接受两个参数,为需要比较的两个字段名,

2). 必须放在 FormGroup 上,不然获取不到两个值。

3). 通过 FormGroup.errors && FormGroup.errors.confirm 获取有误信息

转载请保留原文链接: https://zodream.cn/blog/id/148.html