1
<form>
  <div class="ui-input-group">
    <input #model="ngModel" type="text" class="form-control" required placeholder="Name*" name="visitorName" [(ngModel)]="visitorName">
    <span class="input-bar"></span>
    <div  *ngIf="model.errors && (modeltitle.dirty || model.touched)"  style="color: red">
       Required
    </div>

   <button class="icon-btn" id="addVisitor" title="Add" (click)="addVisitor()">
   <i class="fa fa-plus-circle"></i></button>
  </div>
</form>

Button click should be disabled if the input field is blank, How can I achieve that ?Currently validation message is not being displayed, what is wrong with the above code.

This like default validation message : enter image description here

2
  • You should see Form validation: angular.io/guide/form-validation Commented Jul 24, 2017 at 6:35
  • Did any of the answers suit your need? :) Commented Aug 1, 2017 at 11:00

3 Answers 3

1
<form #myForm="ngForm">
    <div class="ui-input-group">
        <input #model="ngModel" type="text" class="form-control" required placeholder="Name*"
               name="visitorName" [(ngModel)]="visitorName">
        <span class="input-bar"></span>
        <div *ngIf="model.errors && (modeltitle.dirty || model.touched)" style="color: red">Required
        </div>
    </div>

    <button class="icon-btn" id="addVisitor" title="Add" (click)="addVisitor()"
            [disabled]="!myForm.form.valid">
        <i class="fa fa-plus-circle"></i></button>
</form>
Sign up to request clarification or add additional context in comments.

2 Comments

Can I use default form validation , instead of this message ? <div *ngIf="model.errors && (modeltitle.dirty || model.touched)" style="color: red">Required</div>
@user630209 what do you mean with default form validation?
0

You can do something like this

<form #f="ngForm">
    <div class="ui-input-group">
        <input #model="ngModel" 
                type="text" class="form-control" placeholder="Name*"
               name="visitorName" [(ngModel)]="visitorName">
        <span class="input-bar"></span>
        <div *ngIf="model.errors && (modeltitle.dirty || model.touched)" 
         style="color: red">Required
        </div>
    </div>
    <button class="icon-btn" id="addVisitor" title="Add" (click)="addVisitor()" 
    [disabled]="!f.valid"> <i class="fa fa-plus-circle"></i></button>
</form>

If you don't want the message Please fill out this field, remove the require attribute in your input and add novalidate in your form like this

<form novalidate #f="ngForm"> ...

2 Comments

Why to use ngModel inside form. You need to use formControl.
@YoavSchniederman the code provided is using template-driven forms that's why. If it is reactive-form, formControl should have been used
0

Sounds like you want native validation. As of Angular 4, novalidate is set by default. So now you have to explicitly tell Angular to provide it. We can do that with

ngNativeValidate

in the form tag.

Furthermore, you also have to make some changes to your form. You need to make the button of type submit, otherwise when you click submit when form is not valid, the method addVisitor() will be fired anyway. But if your button is of type="submit" the method will not be fired. This also means, that you should set (ngSubmit)="addVisitor()" in the form tag. So change your form to this:

<form #myForm="ngForm" ngNativeValidate (ngSubmit)="addVisitor()">
  <div class="ui-input-group">
    <input #model="ngModel" type="text" class="form-control" required 
           placeholder="Name*" name="visitorName" [(ngModel)]="visitorName">
  </div>
  <button type="submit">Submit</button>
</form>

1 Comment

How I can call ngNativeValidate validator function without submit. I mean with different button?

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.