Enhanced subscription feature with robust data verification

My script hides the input element and displays a "thanks message" after validation of the input field called emailfield. The "thanks message" is only shown if the email field is validated. Thank you!

var nextStep = document.querySelector('#nextStep');

nextStep.addEventListener('click', function(e) {
  e.preventDefault()
  // Hide first view
  document.getElementById('my_form').style.display = 'none';

  // Show thank you message element
  document.getElementById('thank_you').style.display = 'block';
});
<form class="row row-cols-lg-auto g-2 align-items-center justify-content-end">
  <div class="col-12" id="my_form">
    <input id="emailfield" type="email" class="form-control" placeholder="Enter your Email" required="required">
  </div>

  <div class="col-12" id="thank_you" style="display: none;">
    Thanks for subscribing!
  </div>
  <div class="col-12">
    <button type="submit" class="btn btn-primary-soft m-0" name="subscribe" id="nextStep">Subscribe</button>
  </div>

</form>

Answer №1

var nextStep = document.querySelector('#nextStep');

nextStep.addEventListener('click', function (e) {
  e.preventDefault()

  let inputEmail = document.querySelector('#emailfield')
  if (/.*\@.*\..*/.test(inputEmail.value)) {
    // Custom validation passed


    // Hide initial form
    document.getElementById('my_form').style.display = 'none';

    // Display thank you message
    document.getElementById('thank_you').style.display = 'block';
  } else {
    // Custom validation not passed
  }
});

You have the option to use any regular expression for validation

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

Each time the CSV reader finishes processing a row, it appends a newline

I exported a CSV file from Excel and am now attempting to parse it using Python's CSV library. However, I am noticing that a newline character is being added after each row. How can I remove this newline character? Below is the code I am using: with ...

Utilizing Vue JS to pass props from various routes to a shared component

Currently, I am working on developing a menu application using Vue JS. As per the advice given to me, it is suggested that I only need to use one component if the styling remains consistent throughout. This implies that I need to incorporate dynamic data i ...

Ways to enable JavaScript code to accept input from both a text field and a text

I have a JavaScript code that allows users to input text and choose to make it bold, italicized, or underlined. For example, if the user checks the bold option, the text will appear in bold format. Here is the JavaScript code I am using: $('input[n ...

How come my JavaScript regular expression doesn't function properly when applied to elements in an array?

let numbers = new Array('1','2','3'); let letters = new Array('a','b','c'); let length = numbers.length; let str = 'abcdefgabcdefg'; for (let i=0; i<length; i++) { let regex = new ...

Utilizing reactjs (MERN stack) to dynamically update content on a single page based on both URL parameters and database queries

Hello everyone, please excuse my English Imagine I have page1 with content in a database, and page2 with different content in another database. Both page1 and page2 share the same template, but I want to dynamically change the content based on the URL or ...

Developing an npm console application that is "installable" similar to tools like yeoman, gulp, or grunt

Recently dipping my toes into the world of NPM, I've been itching to create a package that functions as a console app (think gulp and grunt). My goal is simple: I want users to be able to run npm install -g mypackage followed by mypackage This sh ...

Pressing the button is not functioning correctly as it results in an Uncaught ReferenceError: toggleText has not been defined

While I'm in the process of diving into JavaScript, I've encountered an issue I'm struggling to resolve. I'm attempting to modify the text on a button after it has been clicked, but I keep receiving an error message indicating that the ...

What is the best way to test the validity of a form while also verifying email availability?

I am currently working on implementing async validation in reactive forms. My goal is to disable the submit button whenever a new input is provided. However, I am facing an issue where if duplicate emails are entered, the form remains valid for a brief per ...

It appears that the SignalR proxy is not defined

Why is $.connection.connectionhub showing as undefined? I am using webform. <script src="/scripts/jquery-1.6.4.min.js"></script> <!--Reference the SignalR library. --> <script src="/scripts/jquery.signalR-2.2.1.min.js">< ...

Upon shutting down the Alert Window, the submit button remains unresponsive

I am facing an issue where, upon detecting an identical value during submission and triggering an error, the button becomes unclickable and feels like it is stuck on saving. Can anyone provide assistance with this situation? Here is the jQuery code I am u ...

Capture a unique error and return a personalized response

Often, I encounter a common issue where I throw a custom error from the middlewares or services. My goal is to catch this custom error and send back a well-formatted response like the example below. { "error" : { "status":422, "message ...

How can I pass an Angular parameter to an onclick function?

Let's dive in... I am dealing with a controller $scope.selectedScript = {}; $scope.selectedScript.scriptId = null; $scope.selectScript = function(script, index) { $scope.selectedScript = script; ...

Exploring JSON Array Data and Its Sub-Properties in React

I have a JSON array containing personal data that I want to access from my React app. This JSON file is already included in my React application. { "person": [ { "id": "userId", "sellerImage": "https://i.pravatar.cc/300", ...

A guide to disabling daily checkboxes and retrieving the chosen values using Angular.js

Within a single table, I have incorporated a dynamic drop-down list that spans over 7 days. Additionally, I have implemented a "+" button that allows for the creation of additional rows dynamically for each day. Each row within the table features a checkb ...

What causes directive templates to be fetched from the server at times (resulting in a 304 response), while other times they are retrieved from the browser cache without

I've noticed that when I reload the page, Angular directive templates load in two different ways. The first way is when the browser sends a request to the server and receives a 304 response - everything works fine. However, the second way is puzzlin ...

Learn the process of updating a nested document within an array in MongoDB

I have a data structure as shown below: { "name":"xxxxxx", "list":[ { "listname":"XXXXX1", "card":[ { "title":"xxxxxx", "descip":"xxxxxxx ...

What is the best way to transmit extra data when tunneling a TLS connection?

I have developed a basic HTTP proxy that utilizes the HTTP CONNECT method for HTTP tunneling. const http = require('http'); const https = require('https'); const pem = require('pem'); const net = require('net'); con ...

Position the div in the center and enhance the function to dynamically adjust when the user attempts to resize the window

var windowHeight = $(window).height(); var windowWidth = $(window).width(); var scrollTop = $(window).scrollTop(); var scrollLeft = $(window).scrollLeft(); jQuery.fn.center = function () { this.css("position","absolute"); this.css("top", Math.max( ...

Stretch out single column content vertically in bootstrap for a uniform look

I've been struggling to make multiple buttons vertically stretch to fit the container, but I can't seem to remember how I achieved this in the past. I have experimented with various options outlined on https://getbootstrap.com/docs/4.0/utilities/ ...

PHP not receiving values when making an AJAX POST request

My AJAX file below is where I pass all the values from my text boxes and post them to edu.php. In edu.php, I intend to update two tables - details and test. However, nothing seems to be updating in my database. When I checked the values with var_dump, the ...