The pseudo-class for invalid input triggers CSS invalidation, even when the input field is left blank

The input property for handling invalid input is currently malfunctioning. It triggers an error even when the input field is left empty. I would like the input border to be goldenrod when it's empty, turn red if the input doesn't match the specified pattern, and green if it meets the pattern requirement. The valid state works fine as it turns green, but the invalid state does not.

Here is the HTML code:

<input
        className="text-field__input"
        id={id}
        name={name}
        type={type}
        placeholder={placeholder}
        defaultValue={defaultValue}
        required={required}
        minLength={minlength}
        title={title}
        pattern={pattern}
      />
      <label className="text-field__label" htmlFor={id}>
        {label}
</label>

<TextField
          id="username"
          label="Username"
          name="username"
          type="text"
          placeholder="user123"
          pattern="[A-Za-z\d\.]{4,12}"
          title="Username must be between 4 and 12 characters in length and contain only letters, numbers and periods"
          required
        />

CSS (SCSS) styling:

.text-field__input {
  display: flex;
  width: 100%;
  height: 4.8rem;
  font-size: 1.6rem;
  border-radius: 4px;
  background-color: transparent;
  padding: 0 1.6rem;
  font-weight: bold;
  border: 3px solid goldenrod;

  &:focus {
    outline: none;
  }
  &:valid {
    border: 3px solid green;
  }
  &:invalid {
    border: 3px solid red ;
  }
  &::placeholder {
    color: rgba($hextechgold2, 0.5);
    font-size: 0;
    transition: 0.3s;
  }

  &:focus::placeholder {
    font-size: 1.6rem;
  }
}

Answer №1

Although this information may be a few months old, I wanted to provide assistance for anyone who comes across it.

The Root Cause

The main issue appears to stem from the fact that required fields are being validated as invalid when empty upon page load.

Solution Steps

To address this issue, you can utilize JavaScript to apply the required property on input (typically triggered by 'blur' so it doesn't change instantly with each character input) and/or before submitting the form to ensure proper validation.

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 font styles shift and vertical bars vanish when placed under images

I'm currently facing some challenges with the text placement beneath the images on my website that I am constructing: 1) The font for "Back to home page" unexpectedly changes from the specified style (Georgia, 0.9em) in Firefox but remains consistent ...

What is causing the table to not be displayed in this Javascript program when it is used in a

I am currently experimenting with incorporating an infinite loop before the prodNum and quantity prompts to consistently accept user input and display the output in a table. Although the program is functional when executed, it fails to showcase the table ...

Tips for scrolling ion-items vertically to the bottom and top using arrow icons in Ionic 4

I'm developing an Ionic 4 app with Angular and looking to incorporate Up and Down Arrow buttons for vertical scrolling from top to bottom and vice versa. ...

"What is the best way to insert data column by column in a material-ui table within a ReactJS application

Is there a way to vertically add data in a Material-UI table in ReactJS? For instance, if I have 100 branches for my business and each branch has a list of employees, how can I create a table with the branch names as headers and the corresponding column ...

Modify the icon for the action menu in Material-UI data grids

I am currently utilizing the free version of @mui/x-data-grid. Below is the column definition I am working with: const columns = [ { field: "actions", type: "actions", width: 100, getActions: (cell) => [ <GridActionsCell ...

Retrieving CSS style values with Node.js

I am looking to design a new CSS style that includes the lowest and highest values. table[my-type="myStyle"] { width: 255.388px !important; } This code snippet is included in numerous CSS files within my style directory. ...

Tips for creating a unique design for a form that includes a non-binary mask, inspired by bitmasks

Currently, I am in the process of creating an app using Reactjs and Material-UI. The main requirement is for users to input a mask of specific length containing various letters such as I, V, and C. In search of a suitable design pattern or solution for thi ...

Unable to retrieve dropdown value using JavaScript and display it in a div

javaScript code: Retrieving data from a dropdown and displaying it in the inner HTML of a div. function showcost() { var days = document.getElementById("Ddays"); var Dudays = days.options[days.selectedIndex].text; var div = docu ...

Expecting a declaration statement for exporting React

When attempting to export my component, I encounter an error in my editor stating export declaration statement expected Here is the code snippet: export Header from './Header/Header'; However, if I modify it like this: export {default as Head ...

Tips for creating a responsive container for mobile and smaller devices using Bootstrap 3

Apologies in advance for the lengthy post, but I welcome any suggestions and ideas. Thank you! I'm facing an issue with my layout breaking when the screen size is below 1200px (images and text drop down). I don't mind if they stack vertically on ...

Using jQuery to insert a new string into the .css file

I'm currently working on a jQuery function to make my font size responsive to changes in width. While I am aware of other options like Media Query, I prefer a solution that offers smoother transitions. Using vw or vh units is not the approach I want t ...

Exploring NextJS: Where can I find the exportPathMap page data? It seems to be missing from getStaticProps

After configuring exportPathMap, I am encountering an issue where I receive an empty object when exporting getStaticProps from my component. I am confused about how to access the data properly. Can anyone provide guidance? // next.config.js exportPathMap: ...

Unable to locate specified POST endpoint in Node.js server

I currently have a MongoDB database in Mlab with two collections. I'm working on creating a POST endpoint where users can submit comments entered in a comment box. However, I seem to be encountering an issue as testing the endpoint with Postman result ...

Displaying multiple div elements when a button is clickedLet multiple divs be

ISSUE Hello there! I'm facing a challenge where I need to display a div when a button is clicked. The issue arises from having multiple divs with the same class, making it difficult for me to target each individual div... Image1 Image2 Desired Outco ...

Ways to prevent blank class from occupying space until filled with Javascript

I've been working on a checkout calculator using JavaScript, and here's how I'm inputting the data: <p class="car-rent-class-info h6 pb-1"></p> <p class="car-rent-pickupdropoff-info h6 pb-1"></p> <p class="car-ren ...

Ways to enlarge the diameter of the inner circle

I've implemented some CSS with JQuery, as seen in this JSFiddle. However, I'm having trouble increasing the width and height of the green circle to be slightly larger than the gray circle (referred to as the main circle) which is positioned at a ...

Incorporating Swagger-UI into an Angular 10 project

Looking to integrate Swagger UI into your Angular application? After researching extensively, I found that the recommended approach is now to use the swagger-ui package instead of swagger-ui-dist for single-page applications. For those using React, there ...

Images in Angular Firebase causing scroll problems in Chrome

In regard to Angular 6, a common issue arises with slow image loading specifically in Chrome, not other browsers. The project utilizes Firebase and the snapshotchange method to fetch images for the Angular project. Image URLs used are like this: . The iss ...

Vanilla JavaScript: Enabling Video Autoplay within a Modal

Can anyone provide a solution in vanilla JavaScript to make a video autoplay within a popup modal? Is this even achievable? I have already included the autoplay element in the iframe (I believe it is standard in the embedded link from YouTube), but I stil ...

Tips for preserving specific information obtained through an API call

I am currently working on a project where I need to make a POST request to my Ruby on Rails backend using information obtained from an API. My tech stack includes React.js and Ruby on Rails. https://i.stack.imgur.com/4Fls9.png Here's a brief overview ...