我尝试在角度2中进行自定义验证,我已经包含了我的知识所需但我无法验证
import { FORM_DIRECTIVES, AbstractControl, ControlGroup
,FormBuilder,Control, Validators } from 'angular2/common';
@Component({
selector : 'cusValidate',
template: `
<form [ngFormModel] = "myform" (ngSubmit) = "onSubmit()">
<lable>Credit Card Number</lable>
<div>
<input type = "text" [ngFormControl] = "inputfield"/>
<div [class.has-error] = '!inval'>
</div>
</div>
<button type = "submit">Submit</button>
</form>
`,
directives : [FORM_DIRECTIVES]
})
export class cusValidation{
myform : ControlGroup;
inputfield : AbstractControl;
constructor(fb : FormBuilder){
this.myform = fb.group({
'inputfield' : ['' , Validators.compose([Validators.required
,this.formValidator])]
})
this.inputfield = this.myform.controls['inputfield']
}
formValidator(val : Control) :any {
if(val.value.match(/^123/)){
return {inval : true};
}
}
onSubmit(){
console.log('submit function called')
}
}
bootstrap(cusValidation);
也验证器功能在我包含的其他文件中但是在这里它在课堂上并且使用this.function进行访问,以及如何应用适当的twitter bootstrap
答案 0 :(得分:4)
您正在使用Angular2,
那么你可以使用html5表单字段。为什么不使用&#39; <input type ="number"/>
?
答案 1 :(得分:0)