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

Drawing on Canvas with Html5, shifting canvas results in significant issues

I've been working on developing an HTML5 drawing app, and while I have all the functionality sorted out, I'm facing challenges during the design phase. My main issue is centered around trying to make everything look visually appealing. Specifical ...

The property you are trying to access is not found within the declared type of 'IntrinsicAttributes & { children?: ReactNode; }'

In my React project created using Create-React-App, I have included the following packages (relevant to the issue at hand): "react": "^16.13.1", "react-dom": "^16.13.1", "react-router-dom": "^5.1.2", "react-scripts": "3.4.1", "typescript": "^3.9.2", "@typ ...

Using jQuery to create two identical widgets

Just starting out with JQuery and had a question about a specific problem I'm facing: Imagine you want to include 2 textboxes with datepickers on an HTML page using jQuery UI. Since jQuery works based on "ID" rather than "Class", the following code w ...

Ways to evaluate the amount of traffic on a webpage?

Recently, I encountered an issue while creating a page to showcase blog posts. Each post had the typical social media share buttons like "Facebook like," "tweet this post," and "+1." Additionally, there were some extra JavaScript functions added for variou ...

Is there a way to customize the color of the bar displaying my poll results?

My poll features two results bars that are currently both blue. I attempted to change the color of these bars but was unsuccessful. I've searched for solutions on stack overflow, specifically How can I change the color of a progress bar using javascr ...

In HTML, data can be easily accessed, however, JavaScript does not have the same

When trying to access the 'data' element in a JSON object, I have encountered an issue. The element is accessible when called from HTML, but not when called in JavaScript. HTML: <p>{{geoJson.data}}</p> JavaScript: let scope; let d ...

Unusual occurrence while creating a unique identifier for a React component

I am working on creating a unique identification number for each React component, which will be assigned to the component upon mounting. Here is the approach I am taking: The callOnce function is used to ensure that a specific function is only executed on ...

Inaccurate audio timestamps in Chrome

I am currently working on a web application that features an audio component sourced from a local .mp3 file lasting approximately 1 hour. I have encountered an issue where, upon clicking the seekbar to jump to a specific point in the audio (e.g., 00:01:00) ...

PHP Arrays and HTML Form Select Elements

I am facing a challenge with a form that contains multiple rows and numerous "select" drop-down menus in each row. While I'm presenting a simplified 2x2 example here, the actual form could extend up to 5x5. Here is an outline of my HTML form: <fo ...

Issue with displaying Font Awesome icons in Bootstrap 4 navbar brand

Recently, I've been setting up a local Laravel website to explore the latest features and changes in Laravel 5.6. However, I've encountered an issue while trying to integrate a Font Awesome icon into the navbar-brand of Bootstrap 4. The icon does ...

Modifying the default actions of Material UI components: A step-by-step guide

In my project, I am using materialUI to showcase an Expansion Panel. Here is the code snippet: import React from 'react' import ExpansionPanel from '@material-ui/core/ExpansionPanel'; import ExpansionPanelSummary from '@material-u ...

I'm curious if anyone has firsthand knowledge they can share about their experience with the useEffect hook?

While I have a basic understanding of the useEffect hook, I know there is much more to learn. Some information seems to be missing from the documentation. Any insights or contributions would be greatly appreciated. Here are some questions I have: Is us ...

Incorrect outcome when utilizing ajax to update a div within a for each loop

For a while now, I've been facing an issue with a div and form within a forEach loop. When one of the forms in the loop is submitted, the content inside the corresponding div is updated in the database and refreshed using JavaScript and Ajax. The upda ...

The component in React does not refresh after updating the state

I have a React page where I am displaying a list of quizzes fetched from an external API. There's also a "New Quiz" button that opens a dialog with a form for users to create a new quiz. My issue is with making the table re-render once the POST reque ...

How can I adjust the Bootstrap container width without it changing when resizing the browser window?

As I work on developing my website, I am utilizing Twitter's bootstrap grid system. Despite my efforts, I have been unable to find a solution for fixing the width of the site. My goal is to lock the size in place so that it remains constant regardless ...

Issue with disablePortal prop in Mui 5 Autocomplete not functioning as expected

Previously, I was successfully using version 4.11.4 of Mui with the Autocomplete component and had no issues with the disablePortal prop. However, after updating to Mui 5, I noticed that the autocomplete dropdown list sometimes appears at the top instead ...

Removing characters from a string with regular expressions

I need to eliminate instances of << any words #_ from the given text. stringVal = "<<Start words#_ I <<love#_ kind <<man>>, <<john#_ <<kind man>> is really <<great>> <<end words#_ "; The d ...

Expanding the row beyond the container to match the width of the viewport

Can Bootstrap 5 be used to extend a row outside a container without causing a horizontal scrollbar? After researching the questions related to this topic, it seems that pseudo-elements are commonly used. However, when attempting to use a pseudo-element, a ...

The formatting of the list is not being correctly displayed in the Ajax Jquery list

Trying to dynamically generate an unordered list from XML onto a jQuery mobile page has been a bit of a challenge for me. While I can display the items on the page, the styling never seems to work properly, only showing up as plain text with blue links. Is ...

Testing Material UI v5 components that utilize sx props using @testing-library/react

When using React Testing Library, the sx props of Material UI components are not applied during rendering. For instance, I set properties to hide an element at specific breakpoints. <> <AppBar data-testid="mobile" ... sx={ ...