简体   繁体   English

如何对角度必填字段进行表单验证

[英]how to do form validation for required fields in angular

I want to validate my form by using Angular form validation.I have read many articles which give info on how to validate form before submitting. 我想使用Angular表单验证来验证我的表单。我阅读了许多文章,这些文章提供了有关如何在提交之前验证表单的信息。 But My question is how can i validate form for required field eg For my login form I have two fields One user name and Password. 但是我的问题是我如何验证必填字段的表单,例如对于我的登录表单,我有两个字段,一个是用户名,另一个是密码。 Without entering any field values ,user clicks on login button. 在不输入任何字段值的情况下,用户单击登录按钮。 So in this scenarion how to do validation in angular. 因此,在这种情况下,如何进行角度验证。

Thanks in advance, 提前致谢,

Here is the code for validating form (Used email build in validator as per my requirement) 这是验证表单的代码(根据我的要求,在验证器中使用了内置的电子邮件)

Login.Component.html Login.Component.html

<form id="loginForm" #validform="ngForm" (ngSubmit)="onSubmit(validform)">
    <div class="example-container">

        <mat-form-field>
            <input matInput placeholder="UserName" [formControl]="userName" autofocus>
            <mat-error *ngIf="userName.hasError('email') && !userName.hasError('required')">
                Please enter a valid email address
            </mat-error>
            <mat-error *ngIf="userName.hasError('required')">
                Email is
                <strong>required</strong>
            </mat-error>
            <br>
        </mat-form-field>
        <mat-form-field>
            <input matInput type="password" placeholder="Password" [formControl]="password">
            <mat-error *ngIf="password.hasError('required')">
                Password is
                <strong>required</strong>
            </mat-error>
        </mat-form-field>


    </div>
    <button type="submit" class="btn btn-success btn-block" [disabled]="userName.hasError('email') || password.hasError('required')"
        id="login_btn">Login</button>
</form>

Login.Component.ts Login.Component.ts

import { Component,OnInit, OnChanges, Injectable } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';
import { Validators,NgForm, NgModel } '@angular/forms';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css'
  ]
})


export class LoginComponent implements OnInit {

/* validate form */
validform;

userName : any;
password : any;

constructor(){ }

ngOnInit(){
         this.buildForm();
}
buildForm(){
   this.userName = new FormControl("", Validators.compose([
        Validators.required,
        Validators.email,
      ])),
      this.password =   new FormControl('', [
        Validators.required,
      ])}
onSubmit = function(validform: NgForm){
       //Call the login api with validForm Data
}


 }

Hope this will help. 希望这会有所帮助。

Just put an ng-disabled on the submit button. 只需在提交按钮上添加ng-disabled。 Something like this should work.You must give the form a name for angular validation to work on it. 这样的事情应该起作用。您必须为表单命名,以进行角度验证。

<form name="myform">
  <input type="email" name="email" ng-model="email" placeholder="Email Address" required /></input>
  <input type="password" name="password" required></input>
  <button type="submit" ng-disabled="myform.$invalid"> Submit </button>
</form>

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

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