1
我使用正則表達式如下驗證IP address pattern無效:模式驗證是IP地址的正則表達式
/\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/
我也檢查了它在regex tester,它工作正常。
但是,當我在模式驗證器中使用它時,有效的IP地址(例如:128.129.80.66)不會被識別爲有效。
app.component.ts:
export class AppComponent implements OnInit {
testForm: FormGroup;
constructor(private fb: FormBuilder) {}
ngOnInit(): void {
const ipPattern =
'/\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/';
this.testForm = this.fb.group({
inp: ['128.129.80.66', Validators.pattern(ipPattern)]
});
}
}
app.component.html
<form novalidate [formGroup]="testForm">
<input formControlName="inp"/>
{{testForm.status}}
</form>
其結果是:
這段代碼有什麼問題?