What is the best way to verify values in Vuejs based on their length?

 

 <button type="submit"
    :disabled="(user.password && !$v.user.password.valid) ||
                (user.confirmPassword && !$v.user.confirmPassword.sameAsPassword)">sda </button>

  

By implementing a method based on character length, I want to disable the button until the user has entered an equal number of characters in both fields. Both field values need to be checked for this functionality.

Is it possible to achieve this using character length? If so, how can I implement this with the code above?

The current issue is that it only checks if the first character in the confirm password field matches with the entered password before proceeding further.

Answer №1

It seems like you might be able to improve the validation by simply adding

&& user.password.length>8
or utilizing vuelidate to incorporate this validation: https://codepen.io/sibellek/pen/oNBPVbN

 <div id="app">
 <input
                      v-model="user.confirmPassword"
                      id="confirmPassword"
                      name="confirmPassword"
                      placeholder="Confirm password"
                      autocomplete="off"
                      :disabled="user.password.length < 8"
        @change="disabledSubmit"
                    />
                    
                    
<div
                      class="error-messages-pass"
                    >
                    
<input
                      v-model="user.password"
                      id="password"
                      name="password"
                      value=""
                      placeholder="Enter new password"
                      autocomplete="off"
       @change="disabledSubmit"
                    />
</div>
  <button type="submit"
    :disabled="disableButton">sda </button>
</div>


new Vue({
  el: "#app",
  data: {
    user: {
      password: "",
      confirmPassword: "",
      },
    disableButton: false,
  },
  validations: {

    user: {
      password: {
        valid: function (value) {
          const containsUppercase = /[A-Z]/.test(value)
          const containsLowercase = /[a-z]/.test(value)
          const containsNumber = /[0-9]/.test(value)
          const containsSpecial = /[#?!@$%^&*-]/.test(value)
          return containsUppercase && containsLowercase && containsNumber && containsSpecial
        },
        required, minLength: minLength(8), maxLength: maxLength(20)
      },
      confirmPassword: { required, sameAsPassword: (value, vm) =>
        value === vm.password.substring(0, value.length) },
    },
    },
  methods: {
  disabledSubmit() {
   this.$v.user.$touch();
   this.disableButton = this.user.password.length<8 || 
 this.$v.user.password.$error || this.user.password!==this.user.confirmPassword;
    }
  },
  mounted() {
    this.disabledSubmit();
  }
})



You can maintain your code structure in this manner.

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

Having trouble with playing audio from an array in Javascript

I've been working on creating a Drum Kit website. My approach involves using an array to hold all the sound files, and using a loop to call the play() function. However, I encountered an issue when trying to load the sounds - the debug console showed: ...

"Rearranging the Firefox ghost image by dragging and dropping it

My drag and drop game is working perfectly on all browsers except for Firefox. In Firefox, the ghost image that appears when an item is dragged seems to be positioned very far away from the cursor. Although the ghost image can still be seen on the page, it ...

Calculating every number within a range of dates and storing them in the format of [day, hour]

Given two date pairs represented as numbers [hour, weekday], where hour ranges from 0-23 and weekday ranges from 1-7. I am tasked with generating all the hours in between each pair. For example, if given [13, 2] and [2, 3], the output would be: [13,2] [14 ...

Guide on setting up a dynamic sidebar linked to the route in Nuxt.js

My pages folder is structured like this: pages/ --index/ ----page1/ ------_slug.vue ----page2/ ------_slug.vue ----page1.vue // contains different content ----page2.vue // contains different content --index.vue The routes can be accessed as follows: /i ...

What strategies can I use to keep an element in place while implementing parallax scrolling?

Looking for some assistance with my Codepen project. I am attempting to replicate a layout similar to this BBC article design from the past, but hitting a roadblock with getting my image to be position fixed based on scrolling. I believe that if I can suc ...

invoke two JavaScript functions without displaying any message

I'm facing an issue with Ajax as it's not displaying the message I intend to show. Let me elaborate. Within my PHP code, there is an input tag: <input type="submit" id="login_button_add" name="submit" value="Add" onclick="add_building(); sho ...

What is the syntax for linking to a different file in the frontmatter of a markdown file?

Currently, I am in the process of setting up Astro's Content Collections. One particular task I would like to achieve is referencing a specific author from my `authorCollection` within an article. In attempting to accomplish this, I considered utiliz ...

The router component in "react-router-dom" is not functioning properly

My goal is to explicitly utilize history in my project. While I am familiar with BrowserRouter, I prefer to use Route and make history one of its properties. Upon running the program, I encounter this page: enter image description here Below is my AppRout ...

Tips for effectively structuring material-ui Grid in rows

I am currently using the material-ui framework to create a form. Utilizing the Grid system, I want to achieve the following layout: <Grid container> <Grid item xs={4} /> <Grid item xs={4} /> <Grid item xs={4} /> </Gr ...

"In strict mode, the object is subjected to specific rules

I am facing a challenge with an object in my project that needs to run the content of the page within strict mode. Even after attempting to return it for global scope usage, I still haven't been able to resolve the issue. (function($) { "use stric ...

Waiting for VueX to execute the mutation

Can VueX be used to create a method in the mounted() function that waits for a mutation in another component to be executed? Sometimes this mutation is triggered before and other times after, so I am curious if it's possible to await its execution af ...

What is the most efficient way to extract dynamically generated JavaScript content from a webpage?

Currently, my method involves using selenium alongside PhantomJS to scrape dynamic content generated by JavaScript on a website. Although it provides me with the desired results, the process is quite slow as I have to wait for the entire page to load befor ...

Storing input data in a JavaScript array instead of sending it through an AJAX request

I've been grappling with this issue for quite some time now, and I just can't seem to wrap my head around it. While there are a few similar solutions out there, none of them address my exact problem. Here's the scenario: I have a form where ...

Best method for removing CrosshairMove event listener in lightweight charts

As per the documentation, using unsubscribeCrosshairMove allows us to remove a handler that was previously added with subscribeCrosshairMove. Our objective is to use unsubscribe... to eliminate previous handlers before re-subscribing with subscribe... af ...

React function causing website to freeze upon dispatch

I created a function in the child component to handle checkbox selection and trigger setDispatch(true). Unfortunately, whenever I check the checkbox, the website freezes and stops responding until I close and reopen it. Here is the function: const [ ...

Leverage the power of Vue Nuxt Auth to activate authentication middleware on a route-by-route

Is there a way to implement auth middleware for individual routes using Class Components? How can I achieve the same functionality as this: <script> export default { middleware: 'auth' } </script> The following method does n ...

What steps can be taken to stop the update function from inserting data into MongoDB from a Meteor application

Within my Meteor method, I have implemented a function to check for existing documents. If the document is not found, it gets inserted into the database. However, if it is found during a second attempt, the document is updated and a new one is also inserte ...

The animation feature in AngularJS when integrated with the easy pie chart appears to be malfunctioning

Having trouble getting the animation to work, any suggestions on how to fix it? I attempted to troubleshoot on jsfiddle but couldn't get it to work for some unknown reason. Here is the HTML code snippet: <script> var app = ang ...

jquery line break in textarea

There is a 'edit description' button that, when clicked, makes text disappear and reappear if (id == 'commedit') jQuery(this).html('<textarea>'+jQuery(this).text()+'</textarea>'); else i ...

Building a Multifaceted Website using React and Node.js

I've encountered a rather straightforward issue. My goal is to incorporate a second checkout page into my React and Node Website. I initially believed that the solution would be as simple as adding another 'checkout' Route to the Browser Ro ...