我試着做角2定製驗證字段自定義驗證,我已經包括認爲需要在我的知識,但是我無法驗證Angular2:用數字只是
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引導
我希望只要用戶不要輸入唯一的數字輸入字段改變其顏色,以指示錯誤,並顯示msg「數字是隻允許」 – blackHawk
是不夠的返回,返回{inval:true};從裏面的功能 – blackHawk
確定它夠了。我只是懷疑目前它並沒有返回你想要的狀況,但你沒有提供足夠的信息讓我知道你實際上想要完成什麼。 –