Numeric keypad causing issues with setting minimum and maximum lengths and displaying submit button

I designed a password pin page that resembles a POS NUMPAD for users to enter their password.

I am struggling to make the condition specified in the JavaScript function properly.

Rule: Hide the submit button if the minimum input is less than 4 characters.

The functionality doesn't work when using the numpad to enter numbers, but it works fine without the numpad.

Could someone please assist me in fixing this issue?

$(document).ready(function() {
$('button[type="submit"]').attr('disabled', true);

$('input').on('keyup',function()
{
    var input_val = $("#cc-pin").val();
 
    var minLength = $("#cc-pin").attr( 'minlength' );
 
    if(input_val != '' && input_val.length >= minLength)
    {
        $('button[type="submit"]').attr('disabled' , false);
    }
    else
    {
        $('button[type="submit"]').attr('disabled' , true);
    }
});

});



function addNumber(element){
  document.getElementById('cc-pin').value = document.getElementById('cc-pin').value+element.value;
}
input[type=text] {
  width: 100%;
  padding: 12px 20px;
  border: 2px solid red;
  border-radius: 4px;
}

element.style {
    text-align: center;
    color: red;
}

.fbutton { 
  background-color: #655F5F; /* #4CAF50 */
  border: 2px solid #000;
  border-radius: 12px;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="POST" action="" accept-charset="UTF-8" id="creditCard">
<div class="pinalert">
<input id="cc-pin" style="text-align: center; color: red;" class="validate mbs date" name="cc-pin" type="password" maxlength="6" minlength="4" value="" data-mask="######" required>
        <label for="cc-pin"> </label><br /><br /> 

    <input id="cc-pin" type="button" class="fbutton" name="1" value="1" id="1" onClick=addNumber(this); />
<input id="cc-pin" type="button" class="fbutton" name="2" value="2" id="2" onClick=addNumber(this); />
<input id="cc-pin" type="button" class="fbutton" name="3" value="3" id="3" onClick=addNumber(this); /> <br /><br>
<input id="cc-pin" type="button" class="fbutton" name="4" value="4" id="4" onClick=addNumber(this); />
<input id="cc-pin" type="button" class="fbutton" name="5" value="5" id="5" onClick=addNumber(this); />
<input id="cc-pin" type="button" class="fbutton" name="6" value="6" id="6" onClick=addNumber(this); /> <br /><br>
<input id="cc-pin" type="button" class="fbutton" name="7" value="7" id="7" onClick=addNumber(this); />
<input id="cc-pin" type="button" class="fbutton" name="8" value="8" id="8" onClick=addNumber(this); />
<input id="cc-pin" type="button" class="fbutton" name="9" value="9" id="9" onClick=addNumber(this); /> <br /><br>
<input id="cc-pin" type="button" class="fbutton" name="0" value="0" id="0" onClick=addNumber(this); />


</div>;

                    
                    <button type="submit" class="btn btn-medium wide-sm center"></button>

                            

                                                    </div>;
                                                </form>}

Answer №1

It seems that your numpad buttons are not triggering the $('input').on('keyup',function(), causing the validation for input_val.length >= minLength to never be executed. To solve this issue, separate your validation logic into a separate function and ensure that both your on('keyup') and addNumber() functions call this new function.

$(document).ready(function() {
  $('button[type="submit"]').attr("disabled", true);

  $("input").on("keyup", function() {
    validateInput();
  });
});

function validateInput() {
  var input_val = $("#cc-pin").val();

  var minLength = $("#cc-pin").attr("minlength");

  if (input_val != "" && input_val.length >= minLength) {
    $('button[type="submit"]').attr("disabled", false);
  } else {
    $('button[type="submit"]').attr("disabled", true);
  }
}

function addNumber(element) {
  document.getElementById("cc-pin").value =
    document.getElementById("cc-pin").value + element.value;
  validateInput();
}

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

Error: [$injector:unpr] Oh no! The AuthServiceProvider Angular Service seems to be MIA

I am currently working on a small AngularJS project and encountering an issue with my service files not being successfully injected into the controllers. I have double-checked for any syntax errors, but despite trying different variations, the problem pers ...

JavaScript and CSOM updates cause SharePoint list item properties to disappear

Within my SharePoint environment, there exists a website where users can load a single list item based on their selection using JavaScript and CSOM. This particular list item contains approximately 60 properties defined in its list definition. Users have ...

Encountering a 404 error when trying to navigate to the next route using the Link component

Having issues with my login route. I've added a Link to the login route inside a button tag in my Navbar component, but when I try to access the route, I'm getting a 404 error page. --app ----pages ------component --------Navbar.js ----- ...

Achieving consistent outcomes across various devices: What's the secret?

Hey there, I am facing an issue with my form page that I created using HTML, CSS, and Javascript. It looks good on my computer but appears messy on a mobile phone. The elements are getting out of the white box (div) making the entire page look chaotic. Sin ...

A message appeared in the console warning about Vue using canvas-datagrid

Everything is displaying correctly as I intended. However, there is a warning in the console: > vue.js:2 [Vue warn]: Unknown custom element: <canvas-datagrid> - did > you register the component correctly? For recursive components, make > sur ...

What is the best way to retrieve a string from a URL?

Is there a way to extract only a specific string from a URL provided by an API? For instance: I'm interested in extracting only: photo_xxx-xxx-xxx.png Any suggestions on how to split the URL starting at photo and ending at png? ...

Error: Unable to access attribute 'item_name' as it is not defined

I'm new to React.js and I'm attempting to build a todo app. The main feature is a text input field where users can add items to their list. When I run "npm start," the app works perfectly and displays the expected output on my designated port. Ho ...

Obtaining a string value through a promise retrieval

Within the following code snippet, I am executing an HTTP request where I extract a "token" (a string) from the response. My objective is to assign this token value to the variable foo. foo = request.post( { url: 'http://10.211.55 ...

My code seems to be malfunctioning

I'm attempting to conceal the chatname div. Once hidden, I want to position the chatid at the bottom, which is why the value 64px is utilized. However, I encountered an error stating The function toggle3(); has not been defined. <div id="chat ...

Tips on sending references from child to parent components in ReactJS

Upon rendering my child component, I aim to focus on the input box right away. To achieve this, I am attempting to pass the ref of the input box up to its parent component, which contains a modal. My goal is to focus on the input field upon entering the mo ...

transforming JSON into CSV structure

I am attempting to utilize a JSON file as input data. Below is an excerpt of sample data. [ { id: 1671349531, name: "A Wild Restaurant Expansion", blurb: "We are looking to expand from our current location to a new and better facility...", goal: 17000, pl ...

Is there a way to convert an array into an object where the first value in the array becomes the name and the properties are an array of the remaining values within subarrays?

Looking to efficiently group elements of a multidimensional array with an unknown list, and transform it into an object while removing duplicate values in the first element of each subarray: For instance, here's the initial array: const arr = [[a, 1 ...

jQuery fade in problem or alternate solutions

When I send a post request to a file and input the response into id='balance', I would like it to have a flickering effect or fadeIn animation to alert the user that it is being updated in real time. I attempted to use the fadeIn() method but it ...

What is the best way to extract values from this PHP script?

I recently delved into the d3 javascript library and successfully created a scatter plot chart that pulls random values from an array. Below is the code snippet: <!DOCTYPE html> <meta charset="utf-8"> <head> <script type="text/ja ...

Removing a SubDocument from an Array in Mongoose

For my project, I am utilizing mongoose to create a database. The schema for my Class looks like this: const mongoose = require('mongoose') const classSchema = mongoose.Schema({ _id: mongoose.Schema.Types.ObjectId, consultant: { type: mo ...

Refresh a table using jQuery Mobile, PHP, and AJAX without having to reload the entire page by clicking a

Currently, I am working on a web app that includes a pop-up feature. When the user clicks on the pop-up to close it, I want the table data to refresh without having to reload the entire page. This pop-up allows users to modify existing data in the table. A ...

Following the upgrade of Angular, the webpack module source-map-loader is encountering an error: "this.getOptions is not a function"

Currently in the process of constructing my angular project using webpack alongside source-map-loader to extract source maps, like this: module.exports = { // ... module: { rules: [ { test: /\.js$/, enforce: "pre&quo ...

Solving the pyramid of doom with Node.js

I have been struggling to find a way to simplify my code structure. Here is what I have come up with so far: router.use('/create', function(res,req,next) { try { var data = { startDate: new Date(req.body. ...

Accessing Cognito using ReactJS and fetching data with specific parameters

I'm currently attempting to retrieve the email of the user who is logged in and use it within my fetch() call. I have successfully obtained the email using getfirstapi() and used it in my form, but I am encountering issues when trying to pass it into ...

Creating an HTML form to collect user data and then automatically saving it to an Excel file stored in a shared Dropbox account

I am looking to extract data from a user form on a website and then automatically save it as an Excel file in a designated Dropbox account once the user clicks submit. Instead of receiving multiple emails every time the form is filled out, I would like t ...