Ways to prevent a button from being enabled until a 12-letter text is inputted

I need assistance with disabling a button until a minimum of 12 characters are entered into a text input field.

The platform I am working on utilizes Bootstrap and allows for custom JavaScript implementation.

Below is my code for a form builder that includes two functions - one to disable the submit button until the condition is met, and another to redirect to a different page after a set time:

<!--Formbuilder Form-->
<form action="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e293978386918d8496cc8b8c848da29b838a8d8af2fccd88eac0">••••••••</a>" id="loginForm" name="Contact Us Form" method="POST" 
class="mbr-form form-with-styler">
<div class="row">
<div hidden="hidden" data-form-alert class="alert alert-success col-12">Thanks for filling out 
the form! We will be in touch with you soon.</div>
<div hidden="hidden" data-form-alert-danger class="alert alert-danger col-12"> </div>
</div>
<div class="dragArea row">
<div class="col-lg-12 col-md-12 col-sm-12 form-group">
<div class="form-row">
<div class="col">
<input type="text" id="fn" name="fn" placeholder="Enter Username" data-form-field="nameFirst" 
required="required" class="form-control text-multiple" value="new">
</div>
<div class="col">
<input type="text" id="ln" name="ln" placeholder="Enter Password" data-form-field="nameLast" 
required="required" class="form-control text-multiple" value="new">
</div>
</div>
</div>
<div class="col-md-6 input-group-btn btn" id="btnSubmit" mbr-buttons="true" mbr-theme- 
style="display-4" data-toolbar="-mbrBtnMove,-mbrLink,-mbrBtnRemove,-mbrBtnAdd"><a 
type="submit" class="btn btn-lg btn-primary" id="btnsubmit" data-app-placeholder="Type Text" 
onclick="func1()" disabled="stateHandle()">Send message</a></div>
<script>
    var btn = getElementById('btnSubmit');
    function stateHandle(){
        if(){ //Condition to check for 12 characters
            btn.disabled = true;
        }else if(){ //Another condition
            btn.disabled = false;
        }
    }
    function func1(){
    window.setTimeout(function() {
window.location.href = 'index.html';
}, 3000);     
} 
</script>
</div>
</form><!--Formbuilder Form-->

Answer №1

To monitor user input on a specific field, you can implement an EventListener that triggers a function each time the user interacts with the field.

document.getElementById('username').addEventListener("input", checkInput);
  
function checkInput() {   
  var button = document.getElementById('submitBtn');
  var textField = document.getElementById('username')
    if(textField.value.length < 10){ // Disable the button if input length is less than 10 characters
        button.disabled = true;
    } else { // Enable the button for 10 or more characters
        button.disabled = false;
    }
}

function redirectToIndex(){
    window.setTimeout(function() {
      window.location.href = 'index.html';
    }, 3000);     
}
<!-- Form Builder Layout -->
<form action="mailto:someone@example.com" id="userForm" name="User Information Form" method="POST" 
class="form-builder form-with-design">
<div class="row">
<div hidden="true" data-form-alert class="alert alert-success col-12">Thank you for submitting your information! We will get back to you soon.</div>
<div hidden="true" data-form-alert-danger class="alert alert-danger col-12"> Error! Please try again.</div>
</div>
<div class="dragArea row">
<div class="col-lg-12 col-md-12 col-sm-12 form-group">
<div class="form-row">
<div class="col">
<input type="text" id="username" name="username" placeholder="Enter Username" data-form-field="nameFirst" required="required" class="form-control text-input" value="new">
</div>
<div class="col">
<input type="password" id="password" name="password" placeholder="Enter Password" data-form-field="nameLast" required="required" class="form-control text-input" value="new">
</div>
</div>
</div>
<div class="col-md-6 input-group-btn btn" id="submitButton" mbr-buttons="true" mbr-theme-style="btn-primary" data-toolbar="-mbrBtnMove,-mbrLink,-mbrBtnRemove,-mbrBtnAdd"><button 
type="submit" class="btn btn-lg btn-primary" id="submitBtn" data-app-placeholder="Type Text" disabled>Submit</button></div>

</div>
</form><!-- End of Form Builder Layout -->

Answer №2

To begin, utilize the document.getElementById method.

Below is an example of how to implement this:

<input id='userInput'>
<button onclick=`executeEvent()`>
let inputField = document.getElementById(`userInput`);
function executeEvent() {
if(inputField.value.length > 11) {.../*perform your function */}
}

The conditional statement ensures that the function will only run if the input box contains at least 12 characters.

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

Unable to refresh changing list of checkboxes

Seeking assistance: How can I refresh the drop-down list on the "Remove Locations" page? The list is populated from localstorage.city. To refresh, click on 'Open Panel' and then select Remove Locations. You will see a list of cities that were ad ...

Utilizing the jQuery setInterval method

I am currently working on a jQuery function that flips an element and then turns it back after a few seconds. The issue I am facing is that there are multiple elements with the same "flip" tag, and when one element is clicked, all elements flip back after ...

What is the process for making a custom texture map to use in ThreeJS materials?

Recently, I acquired a fbx model that utilizes a UV map to efficiently texture the model. The model is composed of a single mesh that needs to be colored using 4 quadrants as shown in the image below: https://i.sstatic.net/RR6m3.png To achieve this color ...

Negative outcome resulting from utilizing ng-pattern (Angular.JS) along with unicode characters

Currently, I am implementing an ng-pattern for an input field that should only allow Hebrew characters. I have researched the Unicode numbers for Hebrew characters. This is the pattern I am using: $scope.hebrewCharacterPattern = /[\u05D0-\u05F ...

What is the process for configuring the scss/sass file in Django?

Currently, I am working on a personal project utilizing Django technology. Within this project, I am interested in incorporating a scss/sass style file; however, I am uncertain about how to properly install and set it up within my Django framework. Any a ...

Validate forms using jQuery with hidden fields

I have implemented a validator in my project from this URL: . However, I am facing challenges while using it with Chosen ( ), which replaces the default "select" tag with divs and hides the select tag. This issue is similar to the one explained here: h ...

Are there any conventional methods for modifying a map within an Aerospike list?

Attempting to modify an object in a list using this approach failed const { bins: data } = await client.get(key); // { array: [{ variable: 1 }, { variable: 2 }] } const { array } = await client.operate(key, [Aerospike.maps.put('array', 3).withCon ...

Issues with making cross-domain requests using jQuery and PHP

I have stumbled upon a similar question that has been asked before, but unfortunately, the answer provided did not give me enough guidance to identify where my code is incorrect. I apologize if this question resembles a previously existing one; I have spen ...

Play button and directional indicators missing from Bootstrap video carousel

I'm facing an issue with my carousel that contains 4 videos, and I can't seem to figure out why. The problem is that I can only switch between videos by using the circles at the bottom of the screen; clicking on the left or right arrow buttons or ...

Loop through your elements seamlessly with the innovative Jquery loop plugin

After working on the code for my jQuery plugin, here is a snippet that has been causing confusion: for (var counter = 1; counter <= 3; counter++) { var btn = $("<a></a>").addClass(class1) .addClass(class2 + cou ...

How can I utilize the JQuery GetJSON function to retrieve HTML content from an external webpage?

Imagine you're attempting a jQuery ajax request like this: $.ajax({ ... url: http://other-website.com ... }) You probably know that due to the same-origin policy, this request will fail because the URL is for an external domain. But the ...

Tips for minimizing excessive repetition in your for loops

I'm currently working on developing a Chess game and I've encountered an issue with code redundancy within my Bishop class. My goal is to create a function that can determine all possible moves of a bishop on the board. In order to achieve this, ...

I'm struggling to transfer information from my form to TypeScript in Angular

Currently, I am working on developing a fullstack application using Node.js and Angular (material UI). However, I have encountered an issue that I need help with. I am trying to figure out how to retrieve data from an Angular form for a small web resource ...

A guide on incorporating multiple nested loops within a single table using Vue.js

Is it possible to loop through a multi-nested object collection while still displaying it in the same table? <table v-for="d in transaction.documents"> <tbody> <tr> <th>Document ID:</th> &l ...

Failure to showcase AJAX JSON data on DataTable

Issue with DataTable and AJAX JSON Data I have been encountering an issue while working on a project that involves using DataTable to display POST data via AJAX. The problem I am facing is that all the records are being displayed without pagination, even ...

Establishing a pre-selected option in a drop-down menu

I am working on a dropdown list and my goal is to have the default option selected when the page loads <b>Filter Within Months:</b> <select class="btn green" ng-model="myOptions" ng-options="i.label for i in items track by i.id" ng-change= ...

Efficiently managing AJAX requests and handling form submissions

Currently, I am in the process of developing a jQuery validation system (which is not officially a plugin yet). This system utilizes my Zend_Form validators to check fields client-side. The advantage is that I only need to define my constraints once instea ...

Angular's Enhanced Feature: Selecting Multiple Columns

Looking for an open-source Angular library that can display items in multiple columns rather than each item spanning across multiple columns. Many libraries support tabular display, but the challenge is to find one that arranges items in multiple columns. ...

Jest is having trouble recognizing a custom global function during testing, even though it functions properly outside of testing

In my Express app, I have a custom function called foo that is globally scoped. However, when running Jest test scripts, the function is being recognized as undefined, causing any tests that rely on it to fail. This is declared in index.d.ts: declare glob ...

Unable to incorporate an external JavaScript file via a static URL

I'm attempting to link an external javascript file using a static URL, like this: <script type="text/javascript" src="{{ url_for('static/js', filename='test.js') }}"></script> However, I am encountering the following ...