简体   繁体   English

angular 模板驱动的表单验证消息

[英]angular template driven form validation messages

I am using angular template driven form.我正在使用 angular 模板驱动表单。 Currently I show the error messages inside the HTML page.目前我在 HTML 页面中显示错误消息。

    <input 
  type="email" 
  name="email" 
  ngModel 
  #email="ngModel" 
  pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$" 
  required>
<br/>

<div *ngIf="email.touched">
  <p *ngIf="email.errors?.required">Email is a required</p>
  <p *ngIf="email.errors?.pattern">This is not a valid Email!!!</p>
</div>

Example Form 示例表格

I need to move these error messages to my component file.我需要将这些错误消息移动到我的组件文件中。 is there any way to do it?有什么办法吗?

I have create a simple user registration form and implement some inbuilt validations on it.我创建了一个简单的用户注册表单并在其上实施了一些内置验证。 Along with the inbuilt validations, we will also implement some custom validations for the template-driven form.除了内置验证,我们还将为模板驱动的表单实现一些自定义验证。

We will consider the following custom validations for this demo:我们将为此演示考虑以下自定义验证:

  • Checking for user name availability.检查用户名的可用性。

  • Password pattern validation密码模式验证

  • Matching the password entered in two different fields匹配在两个不同字段中输入的密码

Here is the example of Template Driven Form:以下是模板驱动表单的示例:

https://stackblitz.com/edit/angular-ivy-eqbltt https://stackblitz.com/edit/angular-ivy-eqbltt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM