0

I am new to Angular 2 and want to compare user input value with data in json. I am attaching code which i am trying to code. Login form has username and password for which user to need to validate it with data in json. if validation is true i need other adjacent objects related to that key in a variable.

This is just a practice code i am trying and not a actual project

// login.component

import { Component, OnInit } from '@angular/core';
import { Http, Response } from '@angular/http';
import { DataService } from '../data.service'

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css'],
  providers: [DataService]
})
export class LoginComponent implements OnInit {
login:any;
loginList:any;
  constructor(private dataService: DataService) { }

  ngOnInit() {
    this.dataService.getLoginData()
    .subscribe(
      (data => this.login = data)
    );
  }

  loginSubmit(value){
    console.log(value);
  }

}


// JSON

[{
	"username": "jay",
	"password": "jay",
	"userType": "standard"
}, {
	"username": "Admin",
	"password": "Admin",
	"userType": "admin"
}, {
	"username": "newuser",
	"password": "newuser",
	"userType": "standard"
}, {
	"username": "anonmyous",
	"password": "anonmyous",
	"userType": "standard"
}]
<div class="container" id="lgBox">
<form class="form-group" #lgForm="ngForm" (ngSubmit)="loginSubmit(lgForm.value)">
  <div class="loginRow"><h4 class="text-center">Login</h4></div>
  <div class="loginRow"><input type="text" #username name="username" ngModel class="form-control" placeholder="Username" required /></div>
  <div class="loginRow"><input type="password" #password name="password" ngModel class="form-control" placeholder="Password" required  /></div>
  <div class="loginRow"><button class="btn btn-primary btn-block/">Login</button></div>
</form>
</div>

4
  • You can use JSON.parse to convert ypur json into an object and then compare the parsed json with the input the user has entered Commented Apr 9, 2017 at 15:05
  • what is the key variable and where is the adjacent object Commented Apr 9, 2017 at 15:07
  • want to check username and password and if it matches in json file want to return username, usertype of the same Commented Apr 9, 2017 at 16:33
  • @jay, will you pleasem also upvote my answer , please/ Commented Apr 9, 2017 at 16:39

1 Answer 1

1

Here is your function to check login :

loginSubmit(value){
    for(let i=0 ; i< this.login.length; i++)
    {
        if (this.login[i].username === value.username && this.login[i].password === value.password)
        {
            console.log("User Found" , this.login[i]);
        }
    }    
}
Sign up to request clarification or add additional context in comments.

Comments

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.