Allow the submission button to be activated only when both the FromDate and ToDate have been

I am looking to activate the submit button once both the FromDate and ToDate fields are selected.

In my scenario, all form fields must be filled out in order to enable the submit button.

 $('#myform > input').on('input', function() {
   var empty = false;
   $('form > input, form > select').each(function() {
     if ($(this).val() == '') {
       empty = true;
     }
   });

   if (empty) {
     $('#register').attr('disabled', 'disabled');
   } else {
     $('#register').removeAttr('disabled');
   }
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="myform">
  Name
  <input type="text" id="" name="name" />
  <br/>
  <br/>From Date
  <input type="date" name="fdate" />
  <br/>
  <br/>To Date
  <input type="date" name="fdate" />
  <br/>
  <br/>

  <input type="submit" id="register" value="Register" disabled="disabled" />
</form>

My goal is to have the submit button enabled only when both the FromDate and ToDate fields are selected.

Answer №1

$('#myform > input').on('input', function () {
        var empty = false;
        $('form > input[type=date], form > select').each(function () {
            if ($(this).val() == '') {
                empty = true;
            }
        });
        if (empty) {
            $('#register').attr('disabled', 'disabled');
        } else {
            $('#register').removeAttr('disabled');
        }
    });
<form id="myform">
Name
<input type="text" id="" name="name" /><br/><br/>

From Date
<input type="date"  name="fdate1" /><br/><br/>
To Date
<input type="date"  name="fdate2" /><br/><br/>

<input type="submit" id="register" value="Register" disabled="disabled" />
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>></script>

This solution works perfectly ;), your implementation was commendable, though just keep in mind that $("form > input") will validate all input fields including dates and names ;)

Answer №2

To ensure that the submit button is disabled whenever the user provides input or modifies the date fields, you can implement the following script:

var $formInputsDate = $('#myform > input[type="date"]'),
    $submit = $('#register');

$formInputsDate.on('input change', function () {
  var isEmpty = false;
  $formInputsDate.each(function () {
    $(this).val() === '' && (isEmpty = true);
  });
  return isEmpty ? $submit.attr('disabled', 'disabled') : $submit.removeAttr('disabled');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form id="myform">
  Name
  <input type="text" id="" name="name"><br><br>

  From Date
  <input type="date"  name="fdate1"><br><br>

  To Date
  <input type="date"  name="fdate2"><br><br>

  <input type="submit" id="register" value="Register" disabled="disabled">
</form>

Remember, you can apply the event handler function to all form inputs using $('#myform > input').

Answer №3

Here is a solution using the submit event:

$('#myform').on('submit', function () {
    var empty = false;
    $('form > input, form > select').each(function () {
        if ($(this).val() == '') {
            empty = true;
        }
    });
    if (empty) {
        return false;
    }
});

Additionally, make sure to remove the disabled attribute from the registration button:

<input type="submit" id="register" value="Register"/>

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

Is your Firebase Google sign-in popup window flashing and not successfully signing in while using Vue.js?

After implementing the Google sign-in pop-up method in Vue.js, I encountered an issue where the live Google sign-in popup window kept flashing and never successfully signed in. However, this problem did not occur when testing locally. Below is the code fo ...

The check is ineffective: if (isset ($_PHP["form_name"]))

I believe there is an issue with the form validation in the file. It seems that all forms with queries are being executed without proper checks. The code snippet below shows my attempt to address this using if (isset($_POST ['form_name'])), but i ...

Image is positioned absolutely and appears when the parent element is flipped

I've encountered an interesting issue in Chrome (works perfectly in Firefox), and I'm unsure how to resolve it. http://jsfiddle.net/GkXA7/1/ When hovering over the card, the image appears flipped on the backside. However, if I adjust the image&a ...

Show a directional indicator on hover in the date selection tool

I am currently using a datepicker that looks like this: https://i.sstatic.net/XsrTO.png When I hover over it, three arrows appear to change days or show the calendar. However, I would like to remove these arrows. Here is the code snippet: link: functi ...

jQuery: Issue Encountered with POST Request when Offline (iOS & Chrome)

After developing an HTML5 web application with offline capabilities using AppCache, the process flow is as follows: Online: While connected to the network, the app loads base information in advance. Offline: Users can take the app offline on their tablet ...

What is the best method for incorporating a JavaScript redirect into an Android WebView?

My issue involves loading a page into a webview. Typically, when I use the code webview.loadUrl(url); it functions as expected. The url contains a javascript code that redirects the page. Here is the javascript code: <script type="text/javascript"> ...

`How to implement a dark mode feature in Tailwind CSS with Next.js and styled-jsx`

My website is created using Next.js and Tailwind CSS. I followed the default setup instructions to add them to my project. In order to style links without adding inline classes to each one, I also integrated styled-jsx-plugin-postcss along with styled-jsx ...

Shift all content to the right when viewing on mobile devices

I'm looking to create space for a menu on the left side, similar to the one on Mashable's mobile view. How can I move all the content to the right? Feel free to resize the window and compare with . Thank you. Best regards, Marius ...

Refresh dynamically generated list item (produced through PHP) following ajax request

Displayed below is the HTML code, generated using a while loop in PHP to add list items after retrieving data from a database. PHP: echo ' <li> <div class="collapsible-header"> <div class = "left"> <div class = "is ...

I keep receiving an error in Angular JS but I'm unsure of the reason

I've been working on AngularJS and created a basic module and controller. I'm attempting to show the data of an element inside the controller on the HTML view page, but all I see is {{student.name}} and I'm receiving an error message that sa ...

AJAX Response Type in ASHX Handler

Currently, I am working on a webpage that requires communication with a SQL server. To ensure portability and avoid using ASP on the IIS 7 server, I have decided to write the page in pure HTML. For the server-side tasks, I am employing a generic handler (A ...

javascript/jquery - steps to design an effective modal page overlay

Struggling for the past few weeks to create a modal dialog using javascript/jQuery. While iOS and Android devices seem to work fine, Windows mobile/Opera mobile often present issues with the modal size. It either requires excessive scrolling or ends up bei ...

Is there a way to attach a canvas image to an email using VB.NET?

In my project, I have successfully converted an SVG to a canvas image using canvg, then converted it to a bytearray in vb.net on the client side. I saved the image to a folder on my server in order to attach it to an email: Protected Sub Button1_Clic ...

The popover displays the text "[object Object]" when triggered by an ajax event

Upon observing the image, I noticed that my bootstrap popover is triggering and displaying “[object Object]” instead of the database data I retrieved. After adding logs to the JavaScript AJAX code, I can see that the data is being retrieved, but it i ...

When the CKEDITOR is set to fullPage mode, it cannot properly apply styles from an external stylesheet that is configured in the config.contentscss setting

To create a custom StyleSet in CKEditor using styles from an external stylesheet, I configured the settings as follows: config.extraPlugins = 'stylesheetparser'; config.contentsCss = '/css/externalStyleSheet.css'; config.stylesSet = [ { ...

Using HTTP POST to subscribe and implementing a try-catch block

When using Angular2's http.post, I encountered an issue where the headers were not sent to the CORS server. To address this, I attempted to create a loop that would retry the request until it succeeds. However, this resulted in an endless loop. How ca ...

What is the method for acquiring the final shape of a particular type?

Is there a way to locate the last path or circle in a paper so that I can perform additional calculations to add more elements? Currently, calling paper.bottom only retrieves the last element. Is it possible to access shapes of specific types, like bottom. ...

Issues with Firebase-admin preventing writing to the database are occurring without any error messages being displayed

Issues with Firebase admin writing to the database. The database is instantiated as follows: var db = admin.database(); A reference to the desired table is then set up: var systemsRef = db.ref("systems/"); A function is created to check if a 'sys ...

Utilizing jQuery to confine the function's scope solely to the specified element

The following script represents a toggle function that is linked to various widgets within an admin panel. jQuery(document).ready(function() { jQuery(document).on('click','.toggleExtras', function(e){ jQuery('.extras&a ...

What is the recommended method for establishing synchronous communication between the client and server during calls?

My client-server communication involves making multiple calls where each call depends on the previous one to finish and return a value before initiating the next one. Below is a simplified version of my current approach: Client: function doOrder() { v ...