Having trouble with changing the border color in Vue.js?

.input-section {
  border-radius: 4px;
  width: 359px;
  height: 42px;
  line-height: 42px;
  padding: 0 45px;
  border-radius: 4px;
  border: solid 1px #b1b8c9;
  background-color: #ffffff;
}
<input 
  type="text" 
  name="fullname" 
  id="fullname" 
  v-model="fullname"
  v-model.trim="$v.fullname.$model" 
  :class="{ 'is-invalid': validationStatus($v.fullname) }" 
  class="input-section" 
  v-on:keypress="isLetter($event)" 
  placeholder="Enter your name"
/>

<input 
  class="input-section label-set" 
  type="text" 
  id="mobile" 
  v-model="mobile" 
  v-model.trim="$v.mobile.$model" 
  :class="{ 'is-invalid': validationStatus($v.mobile) }"
  placeholder="Enter your mobile number" 
  v-on:keypress="isMobile($event)"
/>

How can we dynamically change the border color of an input field based on user interaction? For example, if a user tries to enter a mobile number without entering their full name, the border of the fullname input should turn red.

Is it possible to alter the border color using a conditional class like ':class="{ 'is-invalid': validationStatus($v.fullname) }'?

Answer №1

It is not possible to have a method computed directly in a :class attribute. However, you can call a method on @input (v-on:keypress does not actually exist in VueJS) and utilize the result of a computed or data property within your class.

For example:

<input
  @input="validateStatus"
  :class="{ 'is-invalid': validEmail }"
>
data() {
  return {
    validEmail: false
  }
},
methods: {
  validateStatus(input) { // will be passed automatically from the template
    if (input.value === 'valid') { // your awesome validation
      this.validEmail = true
    } else {
      this.validEmail = false
    }
  }
}

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

Tips for eliminating the space between strings after pasting them into vue2

Dealing with two input fields that require removing spaces between strings has proven to be a challenge. I initially attempted to use event.clipboardData.setData, but unfortunately, it did not yield the desired outcome. Afterward, I resorted to using this. ...

Issues with Google fonts not displaying properly on WordPress site

I'm just starting out with Wordpress and I'm using the html5blank theme to bring in my stylesheets. However, I've run into an issue where the google fonts are not being applied on my wordpress site. In my header.php file, I have included th ...

What is the importance of using mb_convert_encoding in order to display the characters correctly?

My website and MySQL database are both set to use UTF-8 encoding. An issue I am encountering is that when retrieving text from the MySQL database to display on the website, I need to utilize the PHP function mb_convert_encoding(@db_field,'utf-8' ...

Substitute link with asynchronous JavaScript and XML

I need to enable/disable user accounts by clicking on an anchor. The list of users is created dynamically using a loop. Here's an example of an anchor tag: <a href="http://www.example.com/users/deactivate/44" class="btn btn-success" title="Deactiv ...

Create a visually appealing collage using CSS without the need for traditional IMG tags

I'm currently working on a fun little project. My goal is to create a photo grid in HTML5 that displays 3x4 pictures (without using Divs) and adding all the images via CSS, meaning no img tag in the HTML file. The challenge is to make this grid respo ...

Switch between various components using Vue

Hello, I am currently diving into the world of VueJS and eager to learn more about it. I recently created a simple tooltip that should appear when clicked and disappear when clicked again. I managed to achieve this with basic beginner-friendly code for a ...

Strange CSS/browser storage glitch

UPDATE: JUST REALIZED THIS ISSUE IS ONLY OCCURRING ON FIREFOX, NOT CHROME ANOTHER UPDATE: Oddly enough, this problem only occurs locally. When I push it to GitHub, everything works fine. So strange. I suppose that means it's not a major issue. On my ...

Hover over a particular element using a loop in Vue.js

Is there a way to change the price button to an Add to Cart button on mouseover, considering the true false data trigger for each item? How can we specify which item to target when hovering over the price section? Below is the code snippet: template: < ...

The SVG path is not aligned properly with the container's viewbox dimensions

Recently, I came across an icon provided by a plugin I am using called calendar. However, I wanted to enhance my icons set with a new addition - the twitter icon. svg { display: inline-block; height: 16px; width: 16px; margin: 2px 0; ...

A guide to showcasing JSON data on a webpage using JavaScript

I am currently working on a SOAP WSDL invocation application in MobileFirst. The response I receive from the SOAP WSDL is in JSON format and is stored in the result variable. When trying to access the length of the response using result.length, I encounter ...

Enhancing progress bar appearance with CSS styling

Is there a way to style a progress bar using CSS to make it look like the image below? View expected design I have already attempted to achieve this but ended up with a different result. See what I have done so far progress { border: 1.5px solid si ...

How can I store HTML5 input type date/time data accurately as Date Time in Rails?

Is there a way to handle two inputs in HTML5 so that both can be sent to the controller and saved as Date Time? Alternatively, is there any helper or gem that mimics a date and time picker from HTML5? HTML5 inputs: %input{:type => "date", :value => ...

Ways to conceal text that is not wrapped in an HTML tag

<div class="padd10_m"> <a href="http://www.caviarandfriends.com/job_board/user-profile/admin/" class="grid_view_url_thing1">admin</a> <img src="http://www.caviarandfriends.com/job_board/wp-content/themes/PricerrTheme/images/flags/us.pn ...

Leveraging html form submit to transmit data to php and retrieving it via javascript

I'm attempting to transmit data from an HTML form to PHP, where JavaScript will then extract the information. Below is the code for my HTML form: <form action="search.php" method='POST'> <input id="movie_name" name="movie_name ...

How to consistently hide a div with jQuery when a select option is changed

I'm encountering an issue with hiding a div using select and onchange function. Even though the jQuery function I've written is correctly showing and hiding the div based on select option values, when I revisit the page, the div is still displaye ...

Include the 'Calculating' Status Indicator in the Shiny App

I'm currently developing a web application using shiny. Some of the steps in my app require time-consuming calculations, so I want to implement a calculation in process indicator within the shiny application. During my search, I came across a helpful ...

How about: "Interactive web form featuring customizable options to determine which fields are shown?"

How can I design a dynamic web form that changes based on selected options? This form will include standard fields as well as customized fields depending on the user's previous selections. There are approximately 120 different combinations of forms p ...

Container slide-show fill error

I'm attempting to create a slide show with an overlapping caption that appears when hovering over the container or image. The image needs to fit exactly inside the container so no scroll bar is shown and the border radius is correct. I managed to achi ...

Generate a unique class for each img element randomly

Is there a way to assign each image a unique random class instead of giving all the images the same random class? Any help would be appreciated. $(document.body).ready(function () { bgImageTotal = 5; randomNumber = Math.round(Math.random() * (b ...

Every time I use formsubmit.co on my Github pages, I encounter a 404 error

Currently, I'm in the process of constructing a website with Bootstrap 5 and have integrated a form that is intended to be completed and forwarded to my email address. I've chosen to utilize formsubmit.co for this purpose. However, when I attempt ...