4

I' using ReactiveForms in my Angular App. I want if user put less than 7 numbers so error message should be show there but unfortunately it's not working if there is any mistake or solution so please provide me. Thanks

Service.ts

PhoneNo: ['', [Validators.required, Validators.minLength(7)]],

html

<input class="full-width has-padding has-border" formControlName="PhoneNo" type="tel" placeholder="Mobile *" />
        <img src="../../../../assets/img/required-img.png" class="required-img"
          *ngIf="service.formModel.get('PhoneNo').touched && service.formModel.get('PhoneNo').errors?.required"
          title="This fields is mandatory" data-toggle="tooltip" />
          <label *ngIf="service.formModel.get('PhoneNo').touched && service.formModel.get('PhoneNo').errors?.minLength">Minimum 7 characters required</label>
5
  • maybe not the cause of the problem but should be cleaned up as well: you are mixing up template- and reactive forms validation. there is a maxLength attribute on the input with a value of "7", you are having a minLength validator with a value of "7" and printing an error message stating that at least 9 characters are required... Commented Jun 19, 2019 at 10:51
  • share more code Commented Jun 19, 2019 at 10:52
  • @MarkusS. . yes there is a mistake in text of label but it should not be effected on my code. and also when I removed maxLenth it's not working. Commented Jun 19, 2019 at 10:55
  • @ShahidIslam which part you want to see? Commented Jun 19, 2019 at 10:56
  • form model declaration in service.. Commented Jun 19, 2019 at 11:20

5 Answers 5

16

This is well misleading from Angular side. Inside the component you set validators in the following way:

[Validators.minLength(2)]

Then, you would expect the following to work:

errors?.minLength

for whatever reason they decided to use the following format for error messages inside templates though:

errors?.minlength

The only thing you have to do is replace L with l

Sign up to request clarification or add additional context in comments.

2 Comments

that's a bummer. Your answer definitely helped me,
Just to update you guys they still have not fixed this funny bug until now, I am using Angular 13.2.6. Thanks pete85 your answer saved me.
6

Minimum 9 characters required

user error key as minlength not minLength

Comments

3

You have maxLength as an attribute in your html. You should remove it

Instead of

<input class="full-width has-padding has-border" formControlName="PhoneNo" type="tel" placeholder="Mobile *" maxlength="7" />

Do this

<input class="full-width has-padding has-border" formControlName="PhoneNo" type="tel" placeholder="Mobile *">

Also, <input> and <img> should not have an enclosing tag at the end <img/> should be <img>

Instead of using minLength and maxLength together, you can create a regexp for it instead

PhoneNo: ['', [Validators.required, Validators.pattern(^[\s\S]{7}$)]],

Also remember to use service.formModel.get('PhoneNo').errors?.pattern instead of .errors?.minLength in order to display the label.

EDIT @malbarmawi was kind enough to make a demo here: https://stackblitz.com/edit/angular-forms-pattern-bernj2. He also has another approach to solve the same problem himself.

5 Comments

@WebMaster did you check my answer ??
Did you add Validators.pattern(^[\s\S]{7}$) instead of maxLength?
yes but it show me error. "The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type."
@John I have test you solution it's working so both of ours answer are correct 👍
The error related to arithmetic operations must be something else. Thanks to @malbarmawi for the demo.
2

add maxLength validators , this will restrict the enter characters to 7 characters 👇

PhoneNo: ['', [Validators.required, Validators.maxLength(7) , Validators.minLength(7)]],

demo 🔥🔥

3 Comments

but I want that user cannot enter less than 7 numbers.
you can add both min and max in that case 👍, I have update the answer @WebMaster
@WebMaster I have include a live example
0

the reason why it's not working - you should write maxlength in lowercase, not camelcase. same goes to minlength

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.