Apply a red border to any form inputs that contain incorrect information when the submit

How can I add red borders to my form inputs only when they are invalid after submission, without displaying the red borders from the start? Currently, I am using the input:invalid selectors in CSS, but this causes the red borders to appear immediately. I want the red borders to show up only on invalid inputs that prevent successful form submission. My project involves Vue.js and here is a snippet of my code.

input,
textarea {
  font-size: 14px;
  padding: 7px;
  filter: none;
  &[type='email'],
  &[type='password'],
  &[type='text'],
  &[type='search'] {
    height: 20px;
    font-family: inherit;
    font-weight: bold;
    border: 1.4px solid white;
    border-radius: 3px;
    background: #fff;
    color: #002169;
    transition: all 0.3s;
    &:invalid {
      border: 1.4px solid rgb(255, 18, 18);
    }
    &::placeholder {
      color: #0378a6;
      opacity: 1;
    }
  }
}

Answer №1

When the submit-button is clicked by the user, an event will be triggered to validate the form field.

<input type="submit" value="Send" @click="checkForm()">

Alternatively,

<form onsubmit="return checkForm();">

If everything is correct, proceed with sending the information. However, if there are any errors, prevent submission and apply a specific class to highlight the incorrect input fields. This class should have CSS rules for :invalid styling.

For more details on form validation using JavaScript, you can refer to this post.

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

The overlay image is not positioned correctly in the center

I am struggling with positioning a play icon overlay on the image in my list-group-item. The current issue is that the icon is centered on the entire list-group-item instead of just the image itself. I am working with bootstrap 4.4.1. Here is the HTML str ...

Using Jquery to Select Start and End Dates

I currently have two date input fields labeled as Start Date and End Date for a specific Project. If the user chooses a start date that is earlier than today's date but does not choose an EndDate, I want to indicate that the project is ongoing. How c ...

Exploring ways to customize the styles of MUI Accordion components in a Next.js project using CSS modules

I'm trying to customize the default MUI CSS for the accordion component using a CSS module. The issue I'm facing is that the class is dynamically added by MUI, making it impossible to target directly. Here's the solution I attempted, but it ...

Executing a script on a different HTML page without the need to reload it

I'm currently working on incorporating a notification system inspired by Facebook's design. This system will feature an icon in the header that, when clicked, reveals a drop-down containing a table of sorted notifications for the user. My goal is ...

Adding an event listener dynamically in NativeScript-Vue programmatically

For my debut app project with NativeScript Vue, I'm encountering a hurdle. How can I programmatically set an event listener to a component? In a typical Vue application, you would utilize the syntax: component.$on('eventName', eventHandler), ...

Validating American phone numbers using regular expressions

I came across a Javascript regex that is used to validate the different formats in which US phone numbers can be written. However, there seems to be an issue with it: it fails to match the second rule within this specific group: The first group of three ...

Personalizing Bootstrap 5 button designs and attributes for a customized look

In my project, I've set up my custom.scss file with the following code: $primary: #e84c22; $theme-colors: ( primary: $primary, ); @import "~bootstrap/scss/bootstrap.scss"; Currently, the color looks like this: https://i.sstatic.net/lbL ...

able to move through the content without displaying the scrollbar

I am looking to keep my content able to scroll, but I don't want the scrollbar to be visible. I have written my CSS like this: #content-three{ width:100%; height:100%; position:relative; overflow:hidden; } .block_s{ width:90%; ...

How can I effectively search a text file in PHP and display all the results on a webpage?

I've been experimenting with building a webpage that allows users to search through a .txt file using only html and php. Below is the code I have come up with: <?php $file = 'myfile.txt'; if (preg_match_all($pattern, $contents, $matches ...

Trigger the function in ng-change each time the same option is clicked repeatedly

Here is the code I am working with: angular.module("myApp", []).controller("myController", function($scope) { $scope.currentOption; $scope.showWindow = false; $scope.myOptions = [{ id: 1, name: 'This opens the window' }, { ...

In React development, a div (button) is visible, but in the production build, it becomes hidden from

I have been working on a website for my job, and I have added a "Gallery" button on top of two slideshows. Everything works perfectly fine on the development build, but when it comes to the production build, the button doesn't show up for some reason. ...

Plugins for Jquery validation, such as those for verifying files and handling dynamic form fields

Using ng-repeat in AngularJS, I am generating dynamic form elements that include file upload fields. I'm looking for a plugin to help me validate these fields. Can anyone recommend one? ...

Update the default base URL configuration for Axios

My axios configuration looks like this: const configAxios = { baseURL: 'http://127.0.0.1:8000/api', timeout: 30000, }; Vue.prototype.$axios = axios.create(configAxios) When making a call inside my component, I use the following syntax: this ...

Using target="_blank" is necessary for iOS tel: and mailto: links to function properly

Although this question has been circulating widely, I have yet to come across a global solution that fits my needs. Here is the HTML snippet in question: <div class="contact-details__content-container"> <h4 class="contact-details__title">Pr ...

Create a layout consisting of two rows, each containing three blocks. Ensure that the layout is responsive by using only HTML and CSS, without relying

Hello there! I am looking to create a responsive layout without using bootstrap or any other framework..... I want to build it from scratch. https://i.stack.imgur.com/GAOkh.png I am aiming for responsive blocks that collapse into one column when the page ...

The promise for fetching is still not defined, despite the value appearing in the console.log output

Currently, I am attempting to retrieve data from an API (AlphaVantage) and then passing this data as arguments to an axios.post request in order to update my MongoDB. However, I am encountering an error indicating that the variable is undefined, despite be ...

Is there a way to store mathematical equations in a database efficiently?

My website is built in Asp.net and I am utilizing MySql as my database. I have several sample math papers saved in Microsoft Office Word format. What I need is an HTML input tool, such as a textbox or editor, that will allow me to directly copy mathematica ...

In Angular 4, the Bootstrap modal now only opens after a double click instead of opening on the first click

Working on an eCommerce application, there is a cart icon that triggers a modal screen displaying user-selected product data when clicked. However, the issue I'm facing is that upon initial page load, the modal screen opens only after double-clicking; ...

Title with lower border shorter than width

Looking to achieve an underline effect with a bottom border that is narrower than the width of the h2 title. Though I typically avoid uploading images, here is one to provide further clarification of my question: ...

Can you please explain how to utilize Vue's methods within a navigation guard?

I utilized the tutorial found here: and implemented a new method into the Vue application within the main.js: new Vue({ router, data: {}, methods: {customMethod: ()=> {}}, render: h => h(App), }).$mount('#app'); Afterwards, in my ...