Looking to validate a text box and use JavaScript to redirect to a specific URL?

I've been attempting to create a "password protected" page for my customers to track the progress of their page, but it should only be accessible with a specific code.

After spending several hours on this task, I have managed to display a popup screen with a login code input field, but I am struggling to make it redirect to another page.

Below is the code I have been working on:

$(document).ready(function() {
  $('.accesbutton').click(function() {
    $('.login').fadeIn('fast');
    $('.opening').fadeTo('fast', 0.2);
  });

  var $code = $('.logincode').val();
  var $check = 'DDT4DHFD';
  $('.submit').click(function() {
    if ($code === $check) {
      window.location = 'homepage.html';
    } else {
      $('.logincode').animate({
        color: 'red'
      });
    }
  });
});
.login {
  background-color: #FFFFFF;
  position: absolute;
  margin: auto;
  padding: 0 1em;
  left: 35%;
  top: 10em;
  width: 30%;
  height: auto;
  z-index: 1000;
  display: none;
}
.accesbutton {
  background-color: #FF3333;
  width: 10em;
  height: 3em;
  line-height: 3em;
  margin: 2em auto;
  border-radius: 25px;
  text-align: center;
}
.text {
  color: #ffffff;
  text-align: center;
  font-family: "Open Sans";
  font-size: 1.2em;
}
<body>
  <html>
  <div class="accesbutton">
    <p class="text">View Website</p>
  </div>
  <div class="login">
    <p class="text" style="color:#000000">You can log in here with an access code.
      <br>
      <br>Don't have an access code? Contact The Design Company or your Administrator.
      <br>
    </p>
    <p class="text" style="color:#000000">
      <label for="usermail">Access Code:</label>
      <input type="text" name="usercode" placeholder="XXXXXXXX" class="logincode" required>
      <input type="submit" value="Login" class="submit">
    </p>
  </div>
</body>
</html>

The issue lies in the fact that some of the text is in Dutch, including the button's label.

The first part triggers the popup screen, while the second part is supposed to redirect to the designated page upon entering the correct code.

What could possibly be the mistake in my implementation?

-Johr Claessens

Answer №1

UPDATE:

To enhance security, it is recommended to utilize get or post with $('.logincode').val() as the data parameter. On the server side (using your preferred language such as C#, JavaScript, PHP, etc.), you can compare the values and return either true or false to the client side. Here is an example of how you may implement this:

$.get("checklogin.php", { val: $('.logincode').val() } )
 .done(function(data) {})
 .fail(function() {});

OLD ANSWER:

The mistake in your code is:

var $code = $('.logincode').val();

This declaration is outside the event handler.

One suggestion is to store the value of the variable $check on the server side as a simple trick to prevent its visibility on the client side:

$(function () {
  $('.accesbutton').click(function() {
    $('.login').fadeIn('fast');
    $('.opening').fadeTo('fast', 0.2);
  });


  var $check = 'DDT4DHFD'; // store this variable in a text (or JSON) file on the server side
  $('.submit').click(function(e) {
    var $code = $('.logincode').val();  // this is where the typo exists
    
    // You have options to retrieve and test the value from your server
    // Consider checklogin as a text file on the server side
    /***************************
    $('.submit').prop('disabled', true);
    $.get('checklogin', function( data ) {
      if (data.trim() == $check) {
        window.location = 'homepage.html';
      } else {
        $('.logincode').animate({
          color: 'red'
        });
      }
      $(this).removeProp('disabled');
    }).fail(function() {
      console.log('Failed');
      $('.submit').removeProp('disabled');
    });
    ****************************/
    // Implementation up to here.........................   
    
    // Alternatively, you can proceed how you are currently doing...
    if ($code === $check) {
      window.location = 'homepage.html';
    } else {
      $('.logincode').animate({
        color: 'red'
      });
    }
  });
});
.login {
  background-color: #FFFFFF;
  position: absolute;
  margin: auto;
  padding: 0 1em;
  left: 35%;
  top: 10em;
  width: 30%;
  height: auto;
  z-index: 1000;
  display: none;
}
.accesbutton {
  background-color: #FF3333;
  width: 10em;
  height: 3em;
  line-height: 3em;
  margin: 2em auto;
  border-radius: 25px;
  text-align: center;
}
.text {
  color: #ffffff;
  text-align: center;
  font-family: "Open Sans";
  font-size: 1.2em;
}
<link href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

<div class="accesbutton">
    <p class="text">Bekijk website</p>
</div>
<div class="login">
    <p class="text" style="color:#000000">Je kunt hier inloggen met een toeganscode.
        <br>
        <br>Heb je geen toegangscode? Neem dan contact op met The Design Company of je Beheerder.
        <br>
    </p>
    <p class="text" style="color:#000000">
        <label for="usermail">Toegangscode:</label>
        <input type="text" name="usercode" placeholder="XXXXXXXX" class="logincode" required>
        <input type="submit" value="Login" class="submit">
    </p>
</div>

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

Issue: Sumoselect plugin unable to function properly when interacting with dynamically generated select dropdown

I recently started using the sumoselect plugin, which can be found at . In my project, I have implemented two select dropdowns. The first one is directly added to the HTML page, while the second one is generated dynamically using jQuery. Unfortunately, t ...

Error: CSRF token not found or invalid. What is the procedure for transmitting a CSRF token from the frontend to the backend?

In my Django project, I have a task that involves sending a date string from the frontend to the backend. On the frontend, I am utilizing JavaScript's fetch method. async function getCustomerInDeliveryDate(deliveryDate : String) { con ...

Having issues with getting Bootstrap to function properly in my create-react-app

I've been attempting to use traditional Bootstrap instead of react-bootstrap to render my React component, but I can't seem to get it right. I've installed Bootstrap via npm and included the CDN links and scripts as well. Despite trying vari ...

Discovering the distinction between arrays of JQuery objects

I have a task that requires the following steps: var elems = $('.lots-of-elements') Next, I need to make an AJAX request and then do this: var moreElems = $('.lots-of-elements') Finally, I am looking to identify only the new element ...

Is it possible to create tags in Material UI Autocomplete using events other than just pressing the 'Enter' key?

In my current project, I am utilizing the freesolo Autocomplete feature. My specific requirement is to create tags when input text is followed by commas or spaces. Currently, tags are created on the Enter event by Autocomplete, but I am looking for a way t ...

Please provide the text input for the specified version numbers (1.1, 1.2, 3.0, etc.)

I'm currently working on a form that includes a section for searching by version number: <label class="formLabel">Version</label> <html:text property="valueVersion" styleClass="value" tabindex="11"/> <br/& ...

Tips for maintaining UV mapping integrity while updating map textures

I have a GLTF model that needs its map texture updated, but when I do so, the new texture displays without preserving the UV mapping of the model. Is there a method to maintain the UV mapping when loading a new texture? Below is the snippet of code I util ...

Navigating advanced search through nuanced filters dynamically with AngularJS

Here you will find the advanced search form: https://i.stack.imgur.com/g7Hiz.png I have successfully created a URL and parameters for document sections, but I am struggling to come up with a solution for managing the "Add Property Restrictions" section w ...

Efficiently updating arrays within object arrays using MongoDB

I have the following data structure: { data: [ {pos:"0", moreData: ["a", "b"] }, {pos:"1", moreData: ["a", "c"] }, ]} I want to update this structure by adding a letter to moreData where pos ...

Is safeguarding JSON data in Javascript important?

After employing jQuery Ajax to communicate with a php script, it retrieves JSON data. This JSON Array Object is then stored in a JavaScript Variable called var myJSON = ajaxReturn; Typically, the JSON values returned are not visible in the Page Source or ...

What is the best way to ensure that the content container's max-width for a split tier is the same as the width of a full-width tier?

My goal is to create a split tier on a webpage with a 60/40 layout. The size of this tier should be calculated based on the maximum width of the container above it. Using JavaScript and jQuery, I managed to derive the max-width value of the full-width-tier ...

To add a code snippet to the right side of a paragraph using CSS, place it at the end of the

Is there a way to insert code at the end of each paragraph and have it aligned to the right on the same line as the last sentence? Update: I initially tried using float:right, but encountered an issue with vertically aligning the code with the last line ...

Creating a User-friendly Layout for Admin Pages in Next.js Version 13

Hey there, I'm facing an issue with the layout while using Next.js 13 Experimental App Directory. On my website's index page or routes '/', I want to show a landing page and use a specific layout for all pages except for those under the ...

Does ng-include fetch the included HTML files individually or merge them into a single HTML file before serving?

Is there a performance impact when using the ng-include Angular directive, in terms of having included HTML files downloaded as separate entities to the user's browsers? I am utilizing a CDN like AWS CloudFront instead of a node server to serve the H ...

A dynamic and versatile solution for achieving uniform column heights across different web browsers using child elements

Get straight to the point. I'm looking for a solution to create a responsive two-column layout like the one shown in the image below. https://i.sstatic.net/mHhfc.png The width ratio of the .left and .right columns should always be 75/25% relative t ...

What is the best way to implement a forEach loop within a JavaScript file?

$(document).ready(function() { $("form").on("click", ".addRow", function(){ var newRow = '<div class="row add tr">'+ '<div class="col" ...

The performance of HTTP requests is significantly decreased in Internet Explorer compared to expected speeds

I am currently developing an Angular site where I need to send a significant amount of data (approximately 1 MB) in an API call. In the Chrome and Firefox browsers, everything works smoothly and quickly, with a response time under one second. However, whe ...

Incorporate image into Vue.js form along with other information

I have been successfully sending the content of multiple fields in a form to the Database. Now I am looking to add an additional field for uploading images/files and including it with the other form fields, but I am unsure about how to accomplish this task ...

Uncovering the secrets of incorporating axios with Vue in Typescript

I'm facing difficulties while trying to incorporate axios into my Vue project using Typescript. Below is a snippet of my main.ts file: import axios from 'axios' Vue.prototype.$axios = axios axios.defaults.baseURL = 'http://192.168.1.22 ...

What steps should I take to ensure that this accordion is responsive?

I am currently working on an accordion design and trying to figure out how to make it full width and responsive. I have experimented with various width and display attributes, but I haven't been able to achieve the desired result yet. If you'd l ...