Reputation: 385
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>
Upvotes: 0
Views: 4574
Reputation: 58543
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]);
}
}
}
Upvotes: 1