Tips for Formatting Mandatory Message Input Fields and Text Boxes

I am in the process of creating a contact page for my Reactjs website, but I am unsure how to style the required message that should appear below the input or textarea upon clicking Submit. Can anyone guide me on how to achieve this?

Relevant Code

<form className='contact-form' onSubmit={this.handleSubmit}>
    Fields marked with an <span className='red'>∗</span> are mandatory.
    <div className='form-item'>
        <label htmlFor="name">Name <span className='red'>∗</span></label>
        <input className='name'
            type="text"
            name="name"
            value={this.state.name}
            onChange={this.handleChange}
            required />
    </div>
    <div className='form-item'>
        <label htmlFor="email">Email <span className='red'>∗</span></label>
        <input className='email'
            type="email"
            name="email"
            value={this.state.email}
            onChange={this.handleChange}
            required />
    </div>
    <div className='form-item'>
        <label htmlFor="subject">Subject <span className='red'>∗</span></label>
        <input className='subject'
            type="text"
            name="subject"
            value={this.state.subject}
            onChange={this.handleChange}
            required />
    </div>
    <div className='form-item'>
        <label htmlFor="message">Message <span className='red'>∗</span></label>
        <textarea name='message'
            rows='8'
            value={this.state.message}
            onChange={this.handleChange}
            required />
    </div>
    <button className='btn' type='submit' disabled={this.state.disabled}>Submit</button>
    <div className={this.state.isSent ? 'message-open':'message'}>Your message has been sent.</div>
</form>

Answer №1

Incorporating the constraint validation API into your code can be beneficial. There is a specific pseudo class called invalid that you can utilize.

To provide warning messages below each input element, you can apply CSS to set the visibility of a designated element to hidden. When the inputs become invalid due to constraints, you can then make them visible.

small {
  visibility: hidden;
}

small:invalid {
  visibility: visible;
}

This method is effective for all types of constraints. If you specifically want the behavior to apply only to required fields, you can target them using appropriate selectors.

input:required + small {
  visibility: hidden;
}

input:required + small:invalid {
  visibility: visible;
}

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

Interested in retrieving the dynamically changing value of LocalStorage

Hopefully I can articulate my issue clearly. I am implementing a feature where CSS themes change upon button clicks. When a specific theme button is clicked, the corresponding classname is saved to LocalStorage. However, since the key and value in LocalSt ...

There is no compatible version available for [email protected]

While executing npm install npm ERR! code ETARGET npm ERR! notarget No matching version found for <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1a7d6f766a37e61767776">[email protected]</a> npm ERR! notarget In m ...

React NextJS: Unable to retrieve cookies or properties post redirection

Within my nextJS application, when a user logs in on the login page, a cookie is created with a token and then they are redirected to the main page which utilizes the main component. However, within the Main component, I am encountering an issue where the ...

What is the correct way to iterate through a list of images fetched with getStaticProps and display them within the same component?

What is the proper way to map a list of images returned using getStaticProps? I had successfully implemented this by passing a prop to the gallery component in another page. However, I now want to consolidate all the getStaticProps code within the gallery ...

How can I stop a hyperlink from activating Jquery?

A script was created to highlight any <TR> element when a user clicks on it, and it is functioning correctly. <script> $(document).ready(function () { $(document).on('click', 'tbody tr', function (e) { ...

Switch the CSS class for the currently selected link

There are 5 links on the page. When I click on each link, it changes color. However, when I move my cursor to another area of the page, the active link loses focus and I can't show its current state. Can you please advise me on how to fix this issue? ...

re-establishing multiple selections in a dropdown menu in PHP/HTML

Hey, I've got this multi-select listbox... <select id="extfeat" name="extfeat[]" size="6" multiple=""> <option value="Wheel_Chair Accessible">Wheel Chair Accessible</option> <option value="Fruit_Trees">Fruit Trees& ...

Display a component by selecting a hyperlink in React

Currently working on a story in my storytelling, which might not be too important for the question I have. What I am trying to accomplish is creating a scenario where there is an anchor tag that, once clicked, triggers the opening of a dialog box or modal ...

Hey there, I'm looking to use different CSS fonts on Windows and Mac for the same page on a web application. Can someone please guide me on how to accomplish this?

In order to tailor the font based on the operating system, the following criteria should be followed: For Windows: "Segoe UI" For Mac: "SF Pro" Attempts have been made using the code provided below, but it seems to load before the DOM and lacks persisten ...

What is the process for animating classes instead of ids in CSS?

My webpage currently features a setup similar to this. function wiggle(){ var parent = document.getElementById("wiggle"); var string = parent.innerHTML; parent.innerHTML = ""; string.split(""); var i = 0, length = string.length; for (i; i ...

How to make a view go fullscreen in React Native

I am attempting to create a similar animation (the pesto bruschetta) as this one. Within my listView, I have multiple cards with specific dimensions. When clicked, I want one of the cards to expand into fullscreen, displaying additional information. My c ...

What is the reason behind Material UI's restriction on using camelCase props with "withStyles"?

Struggling with prop casing while diving into the new Material UI (version 1.0.0-beta.33 at the time of writing), I've encountered inconsistencies with withStyles allowing camelCased props in some scenarios but not others. For instance: <ChipInpu ...

Tips for showcasing a drop-down menu using Jquery

I am currently utilizing jQuery to showcase a drop-down menu. It is successfully working for a single menu and displaying the appropriate drop-down. However, when I attempt to use more than one menu, it displays all of the drop-down menus simultaneously. I ...

Issue with changing image source on hover using JQuery not functioning as expected

I've been attempting to modify the image src when hovering using JQuery. I'm currently working in Brackets, and when I preview it live in Chrome, everything seems fine. However, when I try to open the file directly in Chrome or IE, it doesn' ...

What is the best way to place an icon within a React Material Ui text field?

Is there a way to place the eye icon inside a text field used for passwords in Material UI? I've been trying but haven't found a way to do it yet. Here's the code snippet I'm using: <TextField variant="outlined" ...

Using Bootstrap 4: How to maximize the width of a span element placed near a label

I need to adjust the width of a span next to its label in my project. My goal is to show multiple groups {label / data} on the same line. To achieve this, I divided the row into 4 columns and tried to set a specific width for a span. However, no matter wh ...

In order to update a query parameter in a Next.js 14 app directory, you can incorporate the following code snippet: <input type="text" onChange={updateQueryParam}

Hey everyone, I could really use some assistance. I need to update the query parameter when the input changes. Important Points to Consider I specifically require a solution that updates the value parameter of the query params without involving parent to ...

JavaScript not functional, database being updated through AJAX calls

I have created a game using Phaser, a JavaScript library for games. Now I am looking to implement a score table using JS/PHP. My main focus is on transferring a variable from JS to PHP in order to update the database. I have researched numerous topics and ...

Discovering elements using Selenium in a JavaScript popup box

The issue at hand is rather straightforward. I am faced with the task of clicking on an element within a popup that has been dynamically generated by JavaScript code. The challenge arises as the page is solely accessible in Internet Explorer and the elemen ...

Detect if the user is using Internet Explorer and redirect them to a different

My web application is having trouble rendering in Internet Explorer. In the meantime, I would like to detect if the user is using IE and redirect them to a different page specifically for IE visitors. What is the best way to accomplish this? Should I use ...