- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Validating email and password - 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: 'usman@gmail.com', password: '123' }, { email: 'ali@gmail.com', 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: 'usman@gmail.com', password: '123' }, { email: 'ali@gmail.com', password: '123' }]; const matchCredentials = (email, password) => { const match = array.find(el => { return el.email === email && el.password === password; }); return !!match; }; console.log(matchCredentials('usman@gmail.com', '123')); console.log(matchCredentials('usman@gmail.com', '1423'));
This will produce the following output on console −
true false
- Related Articles
- Validating a password using JavaScript
- Validating alternating vowels and consonants in JavaScript
- Validating a power JavaScript
- Validating push pop sequence in JavaScript
- Validating a boggle word using JavaScript
- Validating brackets in a string in JavaScript
- Validating a file size in JavaScript while uploading
- Validating a square in a 2-D plane in JavaScript
- Validating string with reference to array of words using JavaScript
- Validating a string with numbers present in it in JavaScript
- How to create a password validation form with CSS and JavaScript?
- How to validate email address in JavaScript?
- Masking email to hide it in JavaScript
- How to Toggle Password Visibility in JavaScript?
- How to create a password generator - JavaScript?

Advertisements