What is the method for making the input field border turn red in Vuejs when data is not entered?

.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)" />

Is there a way to change the border color of an input field to red using only CSS? Specifically, I want to achieve this effect when a user attempts to enter data in a second field without filling out the first one. The goal is for the border color of the first field to turn red in response. This needs to be accomplished without relying on Bootstrap Form-control.

Answer №1

CSS has the ability to style an input element based on its value attribute. For instance:

input#mobile:not([value]) {
   border: 1px solid red;
}

However, it is not possible to dynamically track the HTML Element/Node's value as it changes in real-time. To achieve this functionality, a scripted solution may be necessary.

One approach is to use the oninput event to capture user input and update the value attribute accordingly. Alternatively, if you are utilizing a framework, setting the value attribute through that platform might be more appropriate.

<input type="text" id="mobile" oninput="this.setAttribute('value', this.value);" />

Answer №2

Here is an example of how to implement this:

/* Styling for non-empty inputs */
input:not(:placeholder-on) {
  /* Make sure to include a placeholder in your input fields. For example: <input "placeholder=" "/> */
  border-color: green;
}

/* Styling for empty inputs */
input:placeholder-on {
  border-color: red;
}

Answer №3

You've mentioned JavaScript in your query, so here's how you can achieve it using JS.

Firstly, select all input elements with the class 'input-section' using querySelectorAll. Then, add a blur eventListener on each input and check if the value is empty or has only white space by using `e.target.value.trim().length == 0`. If condition is met, apply a CSS class to change the style of the input tag; otherwise, remove that class.

const inputs = document.querySelectorAll('.input-section')

inputs.forEach(input => {
  input.addEventListener('blur', (e) => {
    if(e.target.value.trim().length == 0){
      e.target.classList.add('warning')
    }else{
      e.target.classList.remove('warning')
    }
  })
})
.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;
}

.warning {
  border: solid 2px #F00;
}
<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)" />

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

Exploring with jQuery to discover the contents located at a specific X/Y position?

Hi there, I need help with the following code snippet: function getLocation() { var positionLeft = $('#element').position().left; var positionTop = $('#element').position().top; } I also have this line of code: $('ul#con ...

connecting data tables

I find myself navigating uncharted waters with a project I am currently immersed in. This project requires me to implement a login creation system for both administrators and users. Administrators should have the ability to create products, while users m ...

Error: The 'path' value is undefined and cannot be searched for using the 'in' operator

Hey there, I'm currently trying to grasp the concept of vue-router. I've watched multiple tutorials but encountered an error that says 'Cannot use 'in' operator to search for 'path' in undefined,' and the router-link ...

Choosing a hyperlink within a cell depending on the surrounding text in that cell

Attempting to pick out the hyperlink text from a table cell when the desired text is also present in the cell Following an answer in that discussion, I have reached this point with my query. This is my current progress: def click_me(myString): WebDr ...

Encountered a vue variable error while running db.each function

Recently, while working with ElectronJS, SQLite3, and Vue.js, I encountered an error message saying "undefined this.tb" when trying to use the `tb.push` method within `db.each`. Surprisingly, I was able to fix it without fully understanding why. The code ...

Conceal DIV containers during the loading of the page, and reveal them only upon the selection of Radio Buttons

In my user interface, I have implemented three radio buttons labeled as "Easy," "Medium," and "Hard." Each button is assigned to a distinct Javascript function that manipulates the visibility of corresponding div elements. The main purpose behind this setu ...

A property name needs to be included before the colon (' : ') in the declaration of a property value within an ASP.NET file

Just starting out with asp.net and decided to give lumen-bootstrap a try. Ran into an unexpected warning and now some of the buttons are not displaying properly. https://i.sstatic.net/WOH0N.jpg ...

Troubleshooting issues with Jquery fadeIn and fadeOut - tips for implementing fadein animation using addclass and mouseover

My concept involves a shop menu displayed as a list: <li class="shop-menu"><a href="{{url('shop')}}">shop</a></li> When the shop-menu is hovered over (using a mouseover jquery function), I aim to make a div named shop-m ...

Safari showing white flash upon setting background-image src using Intersection Observer?

I've done extensive research but I'm still struggling to solve this issue. It only seems to be happening on Safari (Mac & iOS), while everything works smoothly on Chrome, Firefox, Edge, and other browsers. UPDATE: The flickering problem also per ...

Should I Change or Focus in jQuery? What event is best to use with a select dropdown in order to trigger a call to a jQuery-AJAX function?

I have a single select dropdown element in my HTML code: <select id="student" name="student" class="form-control"></select> I am looking to implement a jQuery-AJAX function that will populate the option values in the select dropdown above. H ...

PHP: Sending multiple values under a single name

Here's the code I'm currently working with. It includes a form with 6 inputs. The last 3 inputs are named firstanswer, secondanswer, and thirdanswer. <form action="addsurvey.php" method="post"> <input type="text" name="surveyname"> ...

How to vertically align content using Zurb Foundation's equalizer feature?

I am currently utilizing Zurb Foundation 5 along with the equalizer component. In a scenario where I have three columns of equal width, I am aiming to achieve vertical center alignment for the content (text) in the middle column and vertical bottom alignme ...

A step-by-step guide on utilizing NuxtLink to successfully pass data between pages

Having an issue with Nuxt Link: -Initially, I use a v-for to generate some HTML <li v-for="store in stores"> <p>{{store.companyName}}</p> <p> {{store.url}} </p> <NuxtLink :to="nextPage">C ...

Troubleshooting a Vue.js issue: How to fix a watch function that stops working after modifying

I have encountered a problem with my code. It seems to be working fine initially after the beforeMount lifecycle hook, but when I try to modify the newDate variable within my methods, it doesn't seem to track the changes. data() { return { ...

Modifying the structure of serialized data

After serializing a JS form, the data looks like this: .....&xx=xxx&otherError=&input=SMS&message=sdfgs&...... Can anyone provide guidance on how to replace the value of message with the content of a textarea before making an ajax cal ...

Best practices for managing data loading with composition API and onBeforeRouteUpdate

I have a Vue 3 single-page component that contains the following script: export default defineComponent({ props: { id: String, }, setup(props) { const error = ref<boolean>(false) const thisCategory = ref<CategoryDetails>() ...

Adjustable table resembling the textbox editor in StackOverflow

Is it feasible to create a resizable table similar to the text box editor in Stack Overflow's "Ask Question" page? ...

Unable to retrieve module from directive template in Angular

I am currently working on creating a wrapper component for ngAudio. This wrapper will act as the player with controls and will interact with ngAudio's functions. However, I am facing some scope issues with it. I can inject ngAudio into the component&a ...

Tips for transferring a JavaScript variable to a servlet

I have implemented the selectable jquery plugin on my JSP page. I need to pass the selected string as a parameter to a servlet when the form is submitted. $(function() { $("#selectable").selectable({ stop: function() { var result = ...

What is the method for eliminating the box shadow on a table?

Seeking assistance on removing the shadow effect from ng-tables upon hovering. Can someone guide me on how to achieve this? See screenshot here: http://prntscr.com/jcpl5g widget-body { background-color: #fbfbfb; -webkit-box-shadow: 1px 0 10px 1px rgba(0, ...