如何在angularjs2中进行表单验证 [英] How to produce form validations in angularjs2

查看:73
本文介绍了如何在angularjs2中进行表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过一种验证方式创建一个表单,以便在提交表单时必须用红色突出显示无效字段,但是我不确定我在哪里错了有人可以帮助我

I am trying to create a from validations in such a way that when form is submitted the invalid fields must be highlighted with red color but i am not sure where i am wrong can someone help me

我的模板

 <form id="login-form" name="login-form" class="nobottommargin" [formGroup]="form" (ngSubmit)="onSubmit(form.value)">
    <p *ngIf="activation" class="activation">We have sent an activation link to your email</p>
    <div class="form-group col-sm-offset-2">
           <label class="radio-inline">
                <input type="radio"  [formControl]="form.controls['type']" value = 'personal' name="optradio">Personal
            </label>
            <label class="radio-inline">
                <input type="radio"  [formControl]="form.controls['type']" value = 'professional' name="optradio">Professional
            </label>
    </div>
    <div class="form-group">
        <input type="text" [formControl]="form.controls['firstname']" id="login-form-firstnamee" name="login-form-firstname" value="" placeholder="First Name" class="sm-form-control not-dark formcontrolheight required">
   </div>
    <div class="clear"></div>
    <div class="col-sm-12 labeltopmargin nopadding">
        <button class="col-xs-12 button buttonmeroon button-mini   nomargin" id="login-form-submit" name="login-form-submit"  value="login">Sign Up</button>
    </div>

    <div class="clear"></div>
</form>

我的ts

export class SignUp {
http: Http;
emailfailure: any;
activation: any;
profilefailure: any;
form:any;
constructor(fbld: FormBuilder, http: Http, public router: Router) {
    this.http = http;
    this.form = fbld.group({
        firstname: ['', Validators.required],
        lastname: ['', Validators.required],
        profilename: ['', Validators.required],
        email: ['', Validators.required],
        password: ['', Validators.required],
        repeatpassword: ['', Validators.required],
        image: [''],
        phone: ['', phoneValidator],
        type: ['',],
    }

推荐答案

这是angular 2中的简单表单验证

This is simple form validation in angular 2

  <form   #heroForm="ngForm">
     <div class="form-group">
     <label for="name">Name</label>
     <input type="text" class="form-control" id="name"
           required
           [(ngModel)]="info.name" name="name"
           #name="ngModel" >
    <span *ngIf="!name.valid && !name.pristine" style="color:red;">   <span>name is Required</span></span>
  </div>

  <div class="form-group">
    <label for="alterEgo">Alter Ego</label>
    <input type="text" class="form-control" id="alterEgo"
           [(ngModel)]="info.age" name="alterEgo" >
  </div>

  <button type="button" class="btn btn-default" (click)="enter()">ENTER</button>

这篇关于如何在angularjs2中进行表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆