通过最小长度、最大长度、电子邮件、所需号码等验证表格.角度2
Validating forms by minlenght, maxlenght, email, number, required etc.. angular 2
我在角度中有一个样本形式
app.html
<form [ngFormModel]="newListingForm" (ngSubmit)="submitListing(newListingForm.value)">
<input type="radio" #typeRadio="ngForm" [ngFormControl]="newListingForm.controls['typeRadio']" value="Event" id="type-event_radio" required>
<input type="radio" #typeRadio="ngForm" [ngFormControl]="newListingForm.controls['typeRadio']" value="Venue" id="type-venue_radio" required>
<input type="text" placeholder="New Title" #mainTitleInput="ngForm" [ngFormControl]="newListingForm.controls['mainTitleInput']" id="main-title_input" class="transparent">
<input type="email" #emailAddressInput="ngForm" [ngFormControl]="newListingForm.controls['emailAddressInput']" id="email-address_input" required>
<!-- etc -->
</form>
app.ts
import {Component} from 'angular2/core';
import {FORM_DIRECTIVES, FormBuilder, ControlGroup, Validators} from 'angular2/common';
@Component({
templateUrl : 'app/components/new-listing/app.html',
directives: [FORM_DIRECTIVES]
})
export class NewListingComponent {
//Helpers
newListingForm: ControlGroup;
//Costructor
constructor(
fb: FormBuilder
){
//New Listing Form
this.newListingForm = fb.group({
'typeRadio': ['', Validators.required],
'mainTitleInput': ['', Validators.required],
'emailAddressInput': ['', Validators.required],
});
}
//TODO Form submission
submitListing(value: string): void {
console.log("Form Submited");
}
}
目前,我看到的唯一验证是谷歌在必填字段上提供的默认验证。一切都过去了。我一直在研究文档,最小/最大长度似乎很容易用我当前的设置来实现,但还有另一个有趣的asp NG_VALIDATORS,我认为(不确定)它通过查看表单中的type="email"
、required
等来提供验证。本质上,我希望能够通过angular2显示invalid email
、this field is required
等消息。
只需使用这样的表达式(Bootstrap示例)来利用与输入相关联的控件的属性(有效与否、错误等):
<form [ngFormModel]="newListingForm">
<!-- Field name -->
<div class="form-group form-group-sm"
[ngClass]="{'has-error':!newListingForm.controls.mainTitleInput.valid}">
<label for="for"
class="col-sm-3 control-label">Name:</label>
<div class="col-sm-8">
<input [ngFormControl]="newListingForm.controls.mainTitleInput"
[(ngModel)]="newListing.mainTitleInput"/>
<span *ngIf="!newListingForm.controls.mainTitleInput.valid"
class="help-block text-danger">
<span *ngIf="newListingForm.controls.errors?.required">
The field is required
</span>
</span>
</div>
</div>
</form>
使用此示例,包含错误的字段将显示为红色,并动态显示错误消息。
此外,您还为字段添加了实现自定义验证器:
export class CityValidator {
static validate(control: Control) {
var validValues = [ 'mycity', ... ];
var cnt = validValues
.filter(item => item == control.value)
.length;
return (cnt == 0) ? { city: true } : null;
}
}
只需将其添加到字段的验证器中:
this.newListingForm = fb.group({
'myfield': ['', Validators.compose([
Validators.required, CityValidator.validate])]
(...)
});
希望它能帮助你,Thierry
@Ilja您正处于良好的轨道上,使用以下代码改进您的代码:
this.newListingForm = fb.group({
'typeRadio': ['', Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(40)])],
'mainTitleInput': ['', Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(40)])],
'emailAddressInput': ['', Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(40)])],
});
考虑这个例子(使用自定义电子邮件验证器)https://plnkr.co/edit/5yO4HviXD7xIgMQQ8WKs?p=info
相关文章:
- Form获胜'没有ID主联系表格无法验证
- 我这里有两个表格,在提交验证第一个表格后,我如何转到第二个表格进行验证
- 如果所有验证都是正确的,我如何提交我的表格
- 在HTML服务中验证电子表格
- 如何关闭Angular中的表单验证,当点击按钮时,应该't发送表格
- 滚动至联系人表格7中的已验证字段(wordpress)
- 验证表单并打印到表格
- 验证通过后提交表格
- 如何从联系表格 7 谷歌验证码更改验证码错误语言
- 通过最小长度、最大长度、电子邮件、所需号码等验证表格.角度2
- 角度如何提交和验证表格与
- 使用API文件在提交的表格上验证图像尺寸高达150X50px
- 为什么不'我的表格没有显示验证错误
- 验证表格
- 如何使用javascript上传excel表格,并验证其中的内容,例如时间表验证
- 客户端验证.防止张贴表格
- 用javascript在表格模式下验证表单
- Google Scripts外部电子表格数据验证
- 在ORACLE APEX 5.0中以表格形式进行动态计算和验证
- ng重复内表格的验证