Validating Email and Password in JavaScript



Suppose, we have this dummy array that contains the login info of two of many users of a social networking platform −

const array = [{
   email: '[email protected]',
   password: '123'
 },
 {
   email: '[email protected]',
   password: '123'
 }
];

We are required to write a JavaScript function that takes in an email string and a password string.

The function should return a boolean based on the fact whether or not the user exists in the database.

Example

Following is the code −

const array = [{
   email: '[email protected]',
   password: '123'
}, {
   email: '[email protected]',
   password: '123'
}];
const matchCredentials = (email, password) => {
   const match = array.find(el => {
      return el.email === email && el.password === password;
   });
   return !!match;
};
console.log(matchCredentials('[email protected]', '123'));
console.log(matchCredentials('[email protected]', '1423'));

This will produce the following output on console −

true
false
Updated on: 2020-10-01T10:16:38+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements