below is the code snippet for angular reactive form field. The button stays disable until I valid input in mobile field but the same doesn't work for text field. I want second input text field to be disabled until I enter a valid input to mobile field.
<div id="mobile_verification">
<input class="form-control" id="mobile" formControlName="mobile" placeholder="Mobile no." maxlength="10">
<input class="form-control" id="otp" [disabled]= "!contactForm.controls['mobile'].valid" formControlName="otp" placeholder="OTP" maxlength="6">
<button type="submit" [disabled]="!contactForm.controls['otp'].valid" class="btn btn-success">Verify</button>
<button type="submit" [disabled]="!contactForm.controls['mobile'].valid" class="btn btn-success">Resend</button>
</div>
Is there any another way for text field or am I missing anything? Any help is appreciated.
Thanks.
disabled
attribute does not work on form controls in reactive forms. You need to "manually" enable and disable the form field. This could be done in a couple of ways that I can think of. UsevalueChanges
onmobile
field and then disable/enable theotp
field based on the validity ofmobile
field.I like to listen to some change event, where
(keyup)
would perhaps be most suitable here. You could call a method or use the content of method in template. I like to keep the template clean tho and handle logic in component. So you could set a event for themobile
field:and then the corresponding method:
Also if this is an empty form initially, you would want to set the
otp
field as disabled initially:DEMO: http://plnkr.co/edit/SbN3lJNjvXrE26UyVl6j?p=preview