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

`Anomaly detected in image grid display`

The images are not resizing according to the container. Despite setting both height and width to 100%, the image is not taking the width of the outer column container. https://i.sstatic.net/10eMg.jpg This is what I expected This is the result I am current ...

The specified height for input[type=button] in Firefox/Safari is adjusted by subtracting the padding and border

When implementing the following CSS: input[type=button] { background-color: white; border: 1px solid black; font-size: 15px; height: 20px; padding: 7px; } along with this HTML: <input type="button" value="Foo" /> I anticipate that the t ...

What is the best way to display a string state value in a React component?

I need assistance with displaying a state value that is in string format. Despite my efforts, I have not been successful in finding a solution and am feeling quite frustrated. Can anyone provide some guidance? ...

What is the best way to incorporate an HTML or JSX file (string) retrieved from an API into a React or NextJS project?

In my NextJS project, I have an admin page where I can access data from my application. One particular data object on the admin page that I can edit is called htmlBody, which is related to the home page. Currently, I fill this htmlBody value using an HTML ...

Removing a checker piece in the game of checkers after successfully jumping over it

Recently completed a game of checkers and managed to figure out the logic for moving and jumping checker pieces. However, I'm now facing an issue with implementing the logic for removing or deleting a jumped-over checker piece. How can I automaticall ...

Adding a button label value to a FormGroup in Angular

I've been working on a contact form that includes inputs and a dropdown selection. To handle the dropdown, I decided to use the ng-Bootstrap library which involves a button, dropdown menu, and dropdown items. However, I'm facing difficulties inte ...

Implementing a loading spinner when navigating with router.push() in next.js

When using "react-hook-form" and shadcn, I encountered an issue where I needed to display a loading spinner when the user submits a form. The new data is fetched on the page.tsx file every time the query changes, making the process time-consuming. To impro ...

Issues with Internet Explorer causing headaches with CSS :first-child selector malfunctioning, experiencing strange behavior when utilizing Google Web Fonts

Having some trouble with the layout of a website, particularly in IE. The issue can be observed at . When comparing the page in IE to Firefox, there are noticeable differences. Firstly, the top module on the right side is not displaying correctly in IE. I ...

AJAX Username verification failing to validate

Working with PHP and MySQL, I created a basic form which includes a textfield for the username and a submit button. The page is named checkusername.php. Additionally, I implemented an AJAX function for handling this process. Subsequently, there is another ...

Space between flex content and border increases on hover and focus effects

My code can be found at this link: https://jsfiddle.net/walshgiarrusso/dmp4c5f3/5/ Code Snippet in HTML <body onload="resize(); resizeEnd();"> <div style="margin: 0% 13.85%; width 72.3%; border-bottom:1px solid gray;"><spanstyle="visibilit ...

Style your index with a Wordpress plugin

I've been attempting to modify a plugin's stylesheet, but I'm having trouble locating the CSS file that contains the necessary styles. Despite Chrome's developer tool indicating that it is styled directly on the index site, I have searc ...

Guide to automatically converting Google fonts to base64 for inline use

I am currently in the process of creating my own personal blog using next.js. While everything is going smoothly with the use of Google Fonts for font styling, I have encountered an issue with initial content shift upon loading. This occurs when a new fon ...

Alignment in the vertical direction of two lines

I've been struggling to align some HTML elements visually the way I want. There are two lines of text followed by a link. I'm trying to get the link to be vertically centered between the two lines, rather than just after the second line. Using a ...

Expand the accordion to reveal all the content

I'm facing an issue with my accordion where a scrollbar appears in every content section. To fix this, I tried setting overflow: hidden in the CSS: .ui-accordion .ui-accordion-content { padding: 1em 2.2em; border-top: 0; overflow: hidd ...

Is it possible to incorporate a similar box on a webpage?

I am trying to find a way to add a similar box to a webpage like the one shown in this image. The design I am referring to is from Reddit's theme, but I would like to know how to create a box with a different color and then have another box next to i ...

5 horizontal navbar with dropdown menu in Bootstrap

I am currently utilizing bootstrap 5. Is it possible to display my navbar menus horizontally like the image provided? show submenus horizontally as shown in this picture I would also like to implement submenu for other submenus up to 4 levels deep. Any ...

What is the best way to ensure that the search box automatically adjusts its position and size regardless of the screen resolution?

I'm currently working on a responsive website project and facing an issue with the absolute positioning of the search bar on the main page, which is crucial for me. Below is the code snippet: <div class="span4"> <form class="well form ...

Having trouble with a FlatList component causing a blank screen in React Native?

I've been attempting to display data in a view using FlatList but instead of showing the predefined values, it's just showing a blank screen. This is what I have tried so far. Note: I purposely excluded import elements from react import { ...

"Troubleshooting: Issue with ng-click in AngularJS not triggering ng-show

I can't figure out why my ng-click isn't showing the ng-show message. I've tried searching for a solution but no luck. Here is my controller function: $scope.showLogoutMessage = function() { $scope.logoutmsg = true; }; This is my Logi ...

The execution of a function within context is not triggered

I have developed a decentralized application (dApp) that utilizes a context folder to interact with a smart contract. Within the context, there is a function named loadAuth which verifies if the user is authenticated and then assigns the account state to u ...