managing two variables in JAVASCRIPT

I don't have a database to compare the input values with.

Is there a way to validate user input against predetermined constants?

How can I redirect users only if their input matches one of the predefined constants?

Here is the JavaScript code snippet:



console.log('Welcome to the console');

var userInput = window.prompt ("Enter your username");

const usernames = ['Lorenzo', 'Angelo', 'David'];

console.log(usernames.includes(userInput));

if (usernames.includes(userInput)){
  alert('Welcome back ' + userInput + ', click ok to enter the site');
} else {
  alert('Incorrect username entered');
}

Answer №1

In order to successfully authenticate users, it is important to store their usernames as strings within an array and then validate if the user input matches any of the stored usernames.

console.log('Hello! Welcome to the console');

var userInput = window.prompt("Enter your username");

const usernames = ['Alice', 'Bob', 'Charlie'];

if (usernames.includes(userInput)){
  alert('Welcome back ' + userInput + ', click OK to enter the site');
  console.log(false);
}

else {
  alert('Invalid username');
  console.log(true);
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to run a JavaScript function once another function has finished executing?

I am looking to ensure that a JavaScript function runs only after another one has finished executing. Here is the code I currently have: $(window).load(function(){ $('#dvLoading').fadeOut(2000); }); window.addLoadEvent = function() { $('#p ...

Exploring the depths of JSON: Unraveling the secrets of reading dynamic object data

Currently, I am receiving a JSON file from another app and my goal is to parse it in order to extract the data contained within. The JSON includes user-defined dynamic data with changing key/value pairs, which has left me feeling uncertain about how to eff ...

Display specific content according to the hash in the URL

I have a website with a page (/categories.html) that contains around 50 p elements: <p id='BCI'>Blue_colored_items</p> <p id='RCI'>Red_colored_items</p> ... What I want is for the page to only display: Blue_co ...

Verify if the date and time in string format is the exact same as noon

In my data collection, there are multiple objects each containing a specific date and time value: [ {dt: "2019-11-29 12:00:00"}, {dt: "2019-11-29 3:00:00"}, {dt: "2019-11-29 6:00:00"}, {dt: "2019-11-30 12:00:00"}, {dt: "2019-11-30 6:00:00"} ] M ...

Leveraging jQuery/Javascript to extract data from a PHP array

Currently, I have a PHP array... <?php $tweets = array( '1' => array( "title" => "title", "challenge" => "challenge", "answer" => "answer", "type" => "type", "class" => "class", ...

Obtain the index of a selected option in a Select Tag using Node.js/Express

When you make a POST request with a form in Node.js/Express For example: <select name="selectname"> <option value="value1">Value 1</option> <option value="value2" selected>Value 2</option> <option value="value3"> ...

Can HTML text areas be designed to adjust their width automatically, as well as their height?

Among the numerous StackOverflow examples showcasing an auto-height Textarea, one noteworthy example can be found here: <textarea oninput="auto_grow(this)"></textarea> textarea { resize: none; overflow: hidden; min-heig ...

Rendering EJS Template Views Dynamically Based on Conditions within the Same Path

Hey there, I have a question as a complete beginner. I've been searching for hours and can't seem to find a solution for my specific issue (I'm sure it's something simple). I am working with data from an sqlite database table, filterin ...

Having trouble with running sudo commands on Hyper in Windows 10?

Working on a Windows 10 system without any VM software, I've installed hyper to utilize node and npm. My laptop has just one account, which is also the local account administrator. Surprisingly, even though I have all the permissions, I am unable to r ...

Not all API results are being displayed by the Nextjs API function

I am facing an issue with rendering all the returns from the API in my Next.js application. The API, which is created in Strapi, is only displaying 1 out of the 3 possible returns. I am a beginner when it comes to using APIs and I suspect that the issue li ...

The functionality of $state.go within $stateChangeStart in the app.run is not functioning properly in AngularJS

Having some trouble getting $state.go() function to work. The $on('$stateChangeStart'...); is functioning properly, and I can see the console message when trying to access a protected state without permission. However, the $state.go('toState ...

Integrating FlaskWTF with Vue frontend: sharing CSRF tokens securely

I'm currently developing an application that combines a Vue frontend with a Flask backend. While I am creating forms in Vue, I am looking to enhance security using FlaskWTF for CSRF/XSRF protection and form validation on the backend. To implement th ...

Does anyone know of a way to integrate a calendar feature into a React application using a library

Greetings to everyone, I trust you are all enjoying a fantastic day. I am in search of an interactive calendar similar to this one for one of my applications https://i.sstatic.net/D3S3a.png Does anyone know of a React library that could assist me in crea ...

Obtain the position of the click

My goal is to generate a 2dgrid with dimensions xMax = 10, yMax = 6 that looks like the following: x 0 1 2 3 4 5 6 7 8 9 _ _ _ _ _ _ _ _ _ _ y |_|_|_|_|_|_|_|_|_|_| 0 |_|_|_|_|_|_|_|_|_|_| 1 |_|_|_|_|_|_|_|_|_|_| 2 |_|_|_|_|_|_|_|_|_|_| 3 |_|_|_|_|_|_|_ ...

Improving the efficiency of calculating sliding window averages

I have a set of stock data that requires manipulation through various calculations. I have utilized numpy arrays for this purpose, which are considerably faster than Python's built-in functions. However, the execution time of my code is longer than an ...

Angular 6: TypeError - The function you are trying to use is not recognized as a valid function, even though it should be

I'm currently facing a puzzling issue where I'm encountering the ERROR TypeError: "_this.device.addKeysToObj is not a function". Despite having implemented the function, I can't figure out why it's not functioning properly or callable. ...

The presence of "label.error" appears to persist across all bootstrap tabs

I am facing an issue where the $('label.error') element appears on every bootstrap tab, even though it should only display on one specific tab. The problem arises when a field fails validation on a bootstrap tab and as a result, a label with clas ...

Calculating the ReLU derivative using NumPy

import numpy as np def relu(z): return np.maximum(0,z) def d_relu(z): z[z>0]=1 z[z<=0]=0 return z x=np.array([5,1,-4,0]) y=relu(x) z=d_relu(y) print("y = {}".format(y)) print("z = {}".format(z)) The code shown above displays: y = ...

Can debugging AngularJS in MVC be done in Visual Studio 2013 Ultimate?

Annoyance: When working on my application, I often encounter bugs that require debugging. I find myself opening Chrome, then Dev Tools, and using its less than ideal debug tools to troubleshoot the issue. Desire: It would be fantastic if I could simply se ...

The RadImageEditor onSaving Event does not permit the sending of response scripts

I am using a custom RadImageEditor control with a version of 2013.1.220.40. The control is placed on a page and displayed in a RadWindow. My goal is to either pass a URL or not pass it (for Update and Add scenarios, respectively). After cropping the image ...