HTML form field for JavaScript input

Can someone guide me on how to implement the following code snippet in order to redirect the page using window.location.href if the input contains an item from a JavaScript array? Here is the current code I have:

  var myArray = ["1", "2", "3"];


              function validateForm() {
                var inputValue = document.forms["myForm"]["value"].value;
                if (inputValue != "59864") {
                  alert("Input doesn't contain the desired value");
                  return false;
                }

                var inputValue = document.forms["myForm"]["value"].value;
                if (inputValue == "59864") {
                  window.location.href = "index.php";
                  return false;
                }
              }

Answer №1

One way to verify if any of the input matches an array value is by employing the indexof method.

let userInput = document.forms["myForm"]["value"].value;
if(yourArray.indexOf(userInput) != -1)
{
window.location.href = "index.php";
return false;
}

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

Creating a Node server exclusively for handling POST requests is a straightforward process

I'm looking to set up a Node server that specifically handles POST requests. The goal is to take the data from the request body and use it to make a system call. However, my current setup only includes: var express = require('express'); var ...

How to ensure an element is always aligned at the end of a line in ReactJS, no matter the screen dimensions

I have a unique element that rotates words at a set interval. <div className="inline-block flex justify-center items-center before:content-['lore_ipsum_dolor_lore_ipsum_dolor'] drop-shadow-[0_3px_3px_rgba(0,0,0,1)] t ...

A guide to importing a Vue component in a JavaScript file

I am looking to achieve a specific behavior in my project: depending on a condition stored in my database, I want to load a particular vue.component instead of another. For example, within a file named discover.js, there is the following code: Vue.compone ...

Using the if-else statement in an email form: A step-by-step guide

I have successfully created an email form, but now I want to enhance it by adding options for the subject field. If the subject is set to cancel, then a cancel message like your service is cancelled should be displayed in the Message (body) field. On the o ...

Ways to generate multiple void components side by side using React

What is the most efficient method for creating multiple empty inline elements using React in a declarative manner? Suppose I need 8 empty divs, I tried the following approach but it didn't work. Is there a more effective way? render() { return ( ...

Switching the file format from HTML to .ASP resulted in the loss of the website's design elements and images

After initially creating an HTML file, I decided to rename it with a .asp extension for uploading online. To do this, I copied the original HTML file and moved it into a folder containing .asp files. However, after replacing the existing file, my website l ...

When I properly initialize the value, my function in node.js, passport.js, and express.js returns null

Recently, I encountered a problem while implementing login authentication. I decided to use JWT tokens for added security, specifically utilizing both refresh and access tokens for enhanced protection. My approach involved dynamically passing different to ...

jQuery deduct a value from a given value

i have a full duration that is divided into multiple sections, the user needs to input the duration for each section i would like the system to calculate and display the remaining duration from the full duration, regardless of whether the user fills out ...

Is it possible to display just the progress bar and controls on a YouTube video that is embedded?

I've spent the last few hours trying to figure out how to turn a YouTube video into an audio player, but I haven't had any success. So far, I've come across three main options: 1. Adjust the height of the iframe to 25px, but I find that visu ...

When a directive mandates the controller from a component, the controller may sometimes be returned empty

After struggling for the past 4 hours, I am still trying to solve this issue. The main problem lies within a parent directive that is being dynamically compiled into the DOM. Inside this directive, there is a component that is being transcluded. $compile( ...

Angular 4 application fails to occupy the entire screen height

Here is how my app.component looks: <div class="wrapper"> <app-header></app-header> <router-outlet></router-outlet> <footer> <section class="page group"> {{ 'COMPANY.address' | translate ...

Modifying the font style within an ePub document can affect the page count displayed in a UIWebView

Currently in the development phase of my epubReader app. Utilizing CSS to customize the font style within UIWebView, however encountering a challenge with the fixed font size causing fluctuations in the number of pages when changing the font style. Seeki ...

CSS Troubles

Just starting out with CSS and encountering an issue where the main content and sidebar are not aligning correctly. Any ideas on what could be causing this? Below is the HTML and CSS being used: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "ht ...

What is the recommended approach for displaying data enclosed in double quotes in PHP?

Looking to store HTML text in a variable without using ?> .... <?. The challenge lies with the quotes — wanting to have double quotes instead of single quotes. Using single quotes for variables with interpolation doesn't work: $var = ' ...

Ignoring the "order" class on mobile in BootstrapLearn how to exclude the "order" class when

Is there a way to target the order classes specifically for larger screens? I have two input fields with corresponding labels structured like this; <div class="row"> <div class="col-12 col-md-5 order-first order-md-5"><label class="f ...

How come my transform command is not functioning on both axes as intended?

My transform: translate(10px 10px) isn't functioning as expected. When I only provide a value for the x-axis, it works fine. Using translateX and translateY separately works as well, but using both at the same time doesn't work. This means I can ...

"Modify the color of a div element by changing it from the color name to the hexadecimal

Is there a way to use color codes instead of typical color names, like #e06666 for red? content.push('<p><div class="color red"></div>Whole Foods Market</p>'); Any suggestions on how to achieve this? ...

Creating custom folding icons for the Vue Treeselect Component: A step-by-step guide

I am currently working on a Vue TreeSelect component within my Nuxt application. However, I am facing an issue with customizing the folding icons in the Treeselect component: https://i.sstatic.net/46XvO.png Is there a way to achieve this? I attempted to ...

The error message "ReferenceError: process is not defined" occurs when the 500 process is

Every time I try to import a library or use puppeteer, I keep encountering this problem and I'm not sure how to resolve it. My goal is to extract data from LinkedIn using https://www.npmjs.com/package/linkedin-client. Here's the simple code snipp ...

Encountering issues with resolving dependencies in webdriverIO

I'm attempting to execute my WebdriverIo Specs using (npm run test-local) and encountering an error even though I have all the necessary dependencies listed in my package.json as shown below: [0-2] Error: Failed to create a session. Error forwardin ...