【问题标题】:Email Address not found in Database validation in Angular 6在 Angular 6 的数据库验证中找不到电子邮件地址
【发布时间】:2019-04-24 03:29:05
【问题描述】:

我正在对必填字段和无效的电子邮件地址进行电子邮件验证。 单击提交按钮后,我想对“如果在数据库中找不到电子邮件”进行验证,那么它应该显示一些错误消息,就像我为必需和模式显示的那样。 下面是我的代码。我正在使用 Angular 6 材料设计。

             <form [formGroup]='loginForm' id="loginForm">       
             <mat-form-field>
              <mat-label>Email Address</mat-label>             
              <input matInput placeholder="" formControlName="emailid" required>
              <mat-error *ngIf="formControl.emailid.errors && (formControl.emailid.dirty || formControl.emailid.touched)">
                <p *ngIf="formControl.emailid.errors.required">Email is required</p>
                <p *ngIf="formControl.emailid.errors.pattern">Invalid email address</p>

              </mat-error>
              </mat-form-field>
              <button mat-button type="submit" (click)="login()">Login</button>
               </form>

下面是 ts 文件的代码 id。

               loginForm: FormGroup;
               emailId: any;
               status: any;

               ngOnInit(){
                this.loginForm = this.formBuilder.group({
                  emailid: ['', [Validators.required, Validators.pattern('^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$')]]
                });
               }

                get formControl() { return this.loginForm.controls; }

                 login() {
                    this.emailId = this.loginForm.controls['emailid'].value;
                        this.service.get({{ email: this.emailId}, 'url').subscribe((response) => {
                           this.result = response;
                              this.status = this.result.status;
                              if (this.status === 200) {
                                               //navigate to home page
                            } else If(this.status === 404) {
                                console.log("email not found");
                                // here i want to show this message like aboove i am showing for required and pattern field.
                            }
                     });

一旦显示消息,然后在电子邮件输入字段中执行退格后,错误消息应该会消失。如果数据库中没有“电子邮件未找到”消息,如何显示。 任何人都可以帮助我如何实现这一目标。

【问题讨论】:

标签: angular angular6 email-validation angular-material-6


【解决方案1】:

请试试这个

this.loginForm.controls['email'].setErrors({'incorrect': true});

【讨论】:

    【解决方案2】:

    在您的 html 中,在您的输入字段上添加一个事件处理程序,以检查它是否是退格键和 resetForm,这将删除表单上的错误。

    在 HTML 中:

    <input matInput placeholder="" formControlName="emailid" (keypress)="ResetForm($event)" required>
    

    &lt;mat-error&gt; 标签之后:

    <p *ngIf="isEmailPresent">Email Address Not found</p>
    

    在您的 ts 文件中,初始化 isEmailPresent=false 和 在你得到 404 的 else 部分,this.isEmailPresent=true;

    在 ts 文件中:确保在下面导入

    import { Component, ViewChild } from '@angular/core';
    import {
      FormGroup,
      FormBuilder,
      FormGroupDirective,
      Validators,
    } from '@angular/forms';
    

    将 formdirective 添加为 viewchild:

     @ViewChild(FormGroupDirective) formGroupDirective: FormGroupDirective;
    

    在您的按键事件重置表单中

    ResetForm(event)
    {
      if(event && event.keycode==8)//(keycode for backspace)
        {
           this.formGroupDirective.resetForm();
         }
    }
    

    【讨论】:

    • 好的 @Ashok 我会试试这个。但我的实际问题是如果在数据库中找不到电子邮件地址,如何显示错误消息。在我检查状态是否为 404 的其他部分,它应该显示错误消息,如找不到电子邮件。你能帮我怎么做吗?
    • 显示错误信息有效。但是退格的东西没有用。我在输入中使用 (keyup) 执行此操作。如果输入字段值的长度为 0,则在 keyup 上,然后我将“isEmailPresent”更改为 false。 @Ashok
    猜你喜欢
    • 2015-10-06
    • 2017-09-01
    • 2014-06-12
    • 1970-01-01
    • 2016-06-04
    • 2012-09-27
    • 2020-03-02
    • 2019-06-30
    • 2018-03-13
    相关资源
    最近更新 更多