Implementing character limits in VueJS fields

new Vue({
  el: '#app',
  data() {
    return {
      terms: false,
      fullname:'',
       maxfullname: 10,
      mobile: '',
      maxmobile: 10,
      area: '',
      maxarea: 12,
      city: '',
      maxcity: 12,
    };
  },
  computed: {
    isDisabled: function(){
        return !this.terms || !this.fullname || !this.mobile || !this.area || !this.city;
    }
  }
})
<div id="app">
  <p>
    <label for='terms'>
      <input id='terms' type='checkbox' v-model='terms'/> I agree to the terms and conditions.
      <input id="fullname" type='text' v-model='fullname'  :maxlength="maxfullname"/> Full Name
      <input id="mobile" type='text' v-model='mobile'/ :maxlength="maxmobile"> Mobile Number
       <input id="area" type='text' v-model='area' :maxlength="maxarea"/> Area
      <input id="city" type='text' v-model='city':maxlength="maxcity"/> City
    </label>
    
  </p>
  <button :disabled='isDisabled'>Submit Form</button>
</div>

I am currently experiencing an issue where the button becomes enabled if I enter 2 or 3 characters in each field and click on it. However, I would like the button to only become enabled when the user enters the maximum number of characters allowed in all fields.

Answer №1

The computed prop isDisabled could be used to determine if the input fields meet the required minimum lengths, as illustrated in the example below.

new Vue({
  el: '#app',
  data() {
    return {
      terms: false,
      fullname:'',
       maxfullname: 10,
      mobile: '',
      maxmobile: 10,
      area: '',
      maxarea: 12,
      city: '',
      maxcity: 12,
    };
  },
  computed: {
    isDisabled: function(){
        return !this.terms || (this.fullname.length < this.maxfullname) || (this.mobile.length < this.maxmobile) || (this.area.length < this.maxarea) || (this.city.length < this.maxcity);
    }
  }
})
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4432312104766a726a7576">[email protected]</a>/dist/vue.min.js"></script>
<div id="app">
  <p>
    <label for='terms'>
      <input id='terms' type='checkbox' v-model='terms'/> I agree to the terms!
      <input id="fullname" type='text' v-model='fullname'  :maxlength="maxfullname"/> Name
      <input id="mobile" type='text' v-model='mobile' :maxlength="maxmobile"/> Mobile
      <input id="area" type='text' v-model='area' :maxlength="maxarea"/> Area
      <input id="city" type='text' v-model='city':maxlength="maxcity"/> City
    </label>
  </p>
  <button :disabled='isDisabled'>Submit Form</button>
</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

Looking for a solution to fix the VueJS calculator that only works once?

My calculator project using VueJS, HTML and CSS is almost complete. However, I'm facing an issue where it only works once. For example, if I input 6x3, it correctly gives me 18. But if I then clear the result and try to input a new calculation like 3 ...

Choosing options from an API response in a REACT-JS application

I have a Select Component in my application and I want it to automatically show the selected data once the response is received. import Select from "react-select"; <Select menuPlacement="auto" menuPosition="fixed" ...

Set up the el-date-picker

Can someone provide guidance on configuring an el-date-picker in element-plus to set the first day of the week? The documentation mentions that it can be configured using day.js, but advises against configuring day.js within element. This is what I have ...

Show the input from one HTML form on a different HTML form

Seeking guidance on utilizing local storage in HTML5. How can I extract information from one form and present it on another HTML form page? Picture this: two forms at play here. Upon populating form 1 and hitting the submit button, the content should be v ...

Unable to process jquery AJAX request

Hello, I've been attempting to make a simple ajax call to a method in my code behind. Despite keeping it straightforward for testing purposes, I keep encountering errors. It seems like a basic mistake, but I'm at a loss on how to proceed. The pro ...

Tips on extracting the value from the chosen option in 2 separate rows with identical IDs

While looping through my table, each row is identified by the id="batchNo". I am trying to retrieve the selected value of each select option in every row. Although I attempted to achieve this with the provided code snippet, it only retrieves the selected ...

Tips for fetching a response after sending an ajax request using XMLHttpRequest

/* The following **frontend** function is executed to transmit a new post (in JSON) to the Node server */ addPost(postData) { const xhr = new XMLHttpRequest(); xhr.open('POST', `${process.env.REACT_APP_BACKEND}/posts`); xhr.setRe ...

Why does my partial file keep eluding me?

Currently, I am facing issues setting up my nodejs app. I am using Webstorm's project creator to select a nodejs express app with handlebars as the engine. Additionally, I have included express-handlebars in my setup. However, when I try to include a ...

Show the JSON response from the controller on the view page

In my controller, I have a JsonResult action that returns a list of House objects. My goal is to retrieve this data onclick using ajax and display the JSON data within my view. While I can see the proper response and JSON result in Firebug, I'm not su ...

Preventing default behavior in a JQuery post request

Encountering an issue with jQuery functionality on a mobile device. function send() { $.post("scripts/post.php", { username: $("input[name=username]").val(), password: $("input[name=password]").val() }, function(data) { if ($(".data div" ...

How can a SESSION be initiated through the selection of a radio button in PHP?

On my website, I have a Form where users can choose the color of a car. I want to make it so that when a user selects a radio button, it updates a session variable dynamically using Ajax and PHP. Here is the HTML Form: <form method="post"> red: &l ...

Exploring Vue and Webpack: Optimizing global variables for development and production environments

I have been using vue-cli to create my web application. Throughout the app, I am making use of an API by including it in various places like this: axios.post(API + '/sign-up', data).then(res => { // do something }); The API variable is a c ...

What is the best way to refresh a page with AngularJS?

I have a link that, when clicked by the user, triggers a page reload. I accomplished this using the method below...... HTML <div data-ng-hide="backHide" class="offset6 pull-right"> <a href="" data-ng-click="backLinkClick()"><< Back ...

Navigating using passing data in ReactJs

The following data contains information about people: const people = [ { img: 11, name: "Ahmed", job: "developer", }, { img: 13, name: "Kazim", job: "Engineer", }, ...

Create compelling visual representations by utilizing Google charts to draw customized charts based on variable

I have been attempting to create a chart using Google Charts by passing some parameters to the function. I am trying to use these parameters to populate the data, but it doesn't seem to be working as expected. Is it possible to include parameters in t ...

Issue "RangeError: minimumFractionDigits value is invalid" when using ChartJS in a Next.js application

I'm currently working on developing an application utilizing react-chartjs-2.js. The functionality is smooth in my local environment, but when moved to production, I encounter the following error: Application error: a client-side exception has occurre ...

What is the process for verifying a Bootstrap form?

I have created a form using Bootstrap (Form component in ReactJS), but when I attempt to click on the submit button without entering any input, the form is still submitted. How can I implement form validation so that it only submits when all input fields a ...

Ways to ensure the text on my website scrolls into view as the user navig

Is there a way to have my text show up as I scroll? I came across this , but I'm interested in how it actually functions. I saw a similar inquiry before, but it doesn't make sense to me. Can someone please explain or provide some examples on how ...

Tips for maintaining the integrity of blank space within a text node?

When intercepting a paste event and cleaning HTML off of the content using textNodes, I am faced with an issue where all white space is reduced to a single space and new lines are disregarded. For example, pasting: "hello world !" ends up being "h ...

What is the best way to set up an on-change listener for material-ui's <CustomInput...>?

I'm currently utilizing the React dashboard created by Creative Tim. I have a question regarding how to set up an onChange listener for a Here is the code snippet for the custom input class: import React from "react"; import classNames from "classna ...