0

As I am new in angular 4.0 and I am stuck at the validation I am having a form which consists two fields here is the form

<form action="my-account.html" method="post" class="form-horizontal">
            <div class="form-group">
                <label for="exampleInputEmail1" class="col-sm-3">User name</label>
                <div class="col-sm-9">
                <input type="email" class="form-control" id="exampleInputEmail1"  placeholder="Username" required>

                </div>
            </div>
            <div class="form-group">
                <label for="exampleInputPassword1" class="col-sm-3">Password</label>
                <div class="col-sm-9">
                    <input type="password" class="form-control" id="exampleInputPassword1"  placeholder="Password" required>

                </div>
            </div>

            <div class="form-group">
                <div class="col-sm-offset-3 col-sm-9 ">
                    <button type="submit" class="btn btn-primary btn-sm"  [routerLink]="['/dashboard']">Submit</button>
                    <button type="submit" class="btn btn-primary btn-sm">I Forgot My Password</button>
                </div>
            </div>
        </form>

I want to do validation on a submit button

**<button type="submit" class="btn btn-primary btn-sm"  [routerLink]="['/dashboard']">Submit</button>**

In the simplest way so how can I do it?

1

1 Answer 1

1

Use this link and go through explains the whole scenario for validation

https://www.toptal.com/angular-js/angular-4-forms-validation

<form (ngSubmit)="onSubmit(loginForm)" [formGroup]="loginForm" novalidate class="form-horizontal">
  <div class="form-group">
      <label for="exampleInputEmail1" class="col-sm-3">User name</label>
      <div class="col-sm-9">
      <input type="email" formControlName="username" class="form-control" id="exampleInputEmail1"  placeholder="Username" required>
     <!-- put your validations here -->
      </div>
  </div>
  <div class="form-group">
      <label for="exampleInputPassword1" class="col-sm-3">Password</label>
      <div class="col-sm-9">
          <input type="password" formControlName="password" class="form-control" id="exampleInputPassword1"  placeholder="Password" required>
        <!-- put your validations here -->
      </div>
  </div>

  <div class="form-group">
      <div class="col-sm-offset-3 col-sm-9 ">
          <button type="submit" class="btn btn-primary btn-sm"  [disabled]="loginForm.$invalid">Submit</button>
          <button type="submit" class="btn btn-primary btn-sm">I Forgot My Password</button>
      </div>
  </div>
</form>

//and in you .ts file

 constructor(private fb: FormBuilder){}

ngOnInit() {
    this.profile = this.fb.group({
      userName: [null, [Validators.required]],
      password: [null, [Validators.required]]
    })
};
Sign up to request clarification or add additional context in comments.

7 Comments

loginForm: FormGroup; in the beginning and replace this.profile with this.loginForm
when i am trying to apply [formgroup] tag in form control then it is showing error like template parsing error
you have to include formsModule in app.module.ts
also go through the above link i have mentioned it carries all information step by step.
even if you stuck somewhere please post
|

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.