0

i am trying to validate input type="text" using pattern , i want only text.

Component :

this.from = this.fb.group({
  name: ['',Validators.required,Validators.pattern('^[a-zA-Z]+$')],
});

Html :

<input type="text" formControlName="name"/>

I have have also tried :

this.from = this.fb.group({
    name: ['',Validators.required,Validators.pattern(/^[a-zA-Z]+$/)],
});

with no effect. Angular 4

4
  • same result.... Commented Oct 24, 2017 at 13:47
  • I think your required is useless since your pattern must match at least 1 character (the plus sign) Commented Oct 24, 2017 at 13:50
  • @ADreNaLiNe-DJ I need it because without it starts other backend checks. When I type in input I got ERROR Error: Expected validator to return Promise or Observable. Commented Oct 24, 2017 at 13:52
  • So you can update the question with the error message. It can be useful to help you. Commented Oct 24, 2017 at 13:56

1 Answer 1

2

Solved by adding validators in one array with []:

this.from = this.fb.group({
    name: ['',[Validators.required,Validators.pattern(/^[a-zA-Z]+$/)]],
});
Sign up to request clarification or add additional context in comments.

1 Comment

It's an array( []) not an object ({}).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.