There seems to be a glitch with the email validation process

The validation issue in the email field is causing the is-valid effect to appear incorrectly. Although the email format is not valid (not 'gmail.com' format), a green outline and checkmark are displayed. How can I adjust this to work properly like the password field?

        <form action="/members/new" role="form" method="post"
              class="was-validated" th:object="${memberFormDTO}" novalidate>

            <h2 class="mb-3">Sign Up</h2>
            <hr class="my-4">

            <div class="row g-3">

                <div class="col-12">
                    <label th:for="name" class="form-label">Name</label>
                    <input type="text" th:field="*{name}" class="form-control"
                           placeholder="name" required>
                    <div th:if="${#fields.hasErrors('name')}"
                         th:errors="*{name}"
                         class="invalid-feedback">
                    </div>
                </div>

                <div class="col-12">
                    <label th:for="email" class="form-label">Email</label>
                    <input type="email" th:field="*{email}" class="form-control"
                           placeholder="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dca5b3a99cb9a4bdb1acb0b9f2bfb3b1">[email protected]</a>" required pattern="^[\w-.]+@([\w-]+\.)+[\w-]{2,}$">
                    <div th:if="${#fields.hasErrors('email')}"
                         th:errors="*{email}"
                         class="invalid-feedback">
                    </div>
                </div>

                <div class="col-12">
                    <label th:for="password" class="form-label">Password</label>
                    <input type="password" th:field="*{password}" class="form-control"
                           placeholder="At least 8 characters" required>
                    <div th:if="${#fields.hasErrors('password')}"
                         th:errors="*{password}"
                         class="invalid-feedback"gt;
                    </div>
                </div>

                <div class="col-12">
                    <label th:for="address" class="form-label">Address</label>
                    <input type="text" th:field="*{address}" class="form-control"
                           placeholder="1234 Main St" required>
                    <div th:if="${#fields.hasErrors('address')}"
                         th:errors="*{address}"
                         class="invalid-feedback">
                    </div>
                </div>

                <button class="btn btn-primary btn-dark btn-lg col-6" type="submit">Sign Up</button>
            </div>
        </form>

Looking to resolve the email validation issue.

Answer №1

The problem in your code is that the email input field does not dynamically apply the 'is-invalid' class when the email is invalid. The code snippet below should help you with this issue:

<form action="/members/new" role="form" method="post" class="was-validated" th:object="${memberFormDTO}" novalidate>
    <h2 class="mb-3">Sign Up</h2>
    <hr class="my-4">

    <div class="row g-3">
        <div class="col-12">
            <label th:for="name" class="form-label">Name</label>
            <input type="text" th:field="*{name}" class="form-control" placeholder="name" required>
            <div th:if="${#fields.hasErrors('name')}" th:errors="*{name}" class="invalid-feedback"></div>
        </div>

        <div class="col-12">
            <label th:for="email" class="form-label">Email</label>
            <input type="email" th:field="*{email}" class="form-control" placeholder="email@example.com" required pattern="^[\w-.]+@([\w-]+\.)+[\w-]{2,}$"
                   th:classappend="${#fields.hasErrors('email')} ? ' is-invalid' : ''">
            <div th:if="${#fields.hasErrors('email')}" th:errors="*{email}" class="invalid-feedback">Please enter a valid email address.</div>
        </div>

        <div class="col-12">
            <label th:for="password" class="form-label">Password</label>
            <input type="password" th:field="*{password}" class="form-control" placeholder="At least 8 characters" required>
            <div th:if="${#fields.hasErrors('password')}" th:errors="*{password}" class="invalid-feedback"></div>
        </div>

        <div class="col-12">
            <label th:for="address" class="form-label">Address</label>
            <input type="text" th:field="*{address}" class="form-control" placeholder="1234 Main St" required>
            <div th:if="${#fields.hasErrors('address')}" th:errors="*{address}" class="invalid-feedback"></div>
        </div>

        <button class="btn btn-primary btn-dark btn-lg col-6" type="submit">Sign Up</button>
    </div>
</form>

In my updated code, I have added

th:classappend="${#fields.hasErrors('email')} ? ' is-invalid' : ''"
to apply the 'is-invalid' class based on the validity of the email input.

I hope this modification resolves the issue for you. Thank you.

Answer №2

For a successful email validation process, it is crucial to update the pattern attribute within the email input field and make sure that Bootstrap validation classes are utilized correctly. While the provided pattern seems generally accurate, I will suggest a more precise approach and confirm that the surrounding code effectively handles the validation.

<div class="row g-3">

    <div class="col-12">
        <label th:for="name" class="form-label">Name</label>
        <input type="text" th:field="*{name}" class="form-control" placeholder="name" required>
        <div th:if="${#fields.hasErrors('name')}" th:errors="*{name}" class="invalid-feedback"></div>
    </div>

    <div class="col-12">
        <label th:for="email" class="form-label">Email</label>
        <input type="email" th:field="*{email}" class="form-control" placeholder="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4d3422380d28352c203d2128632e2220">[email protected]</a>" required pattern="^[\w\.-]+@([\w-]+\.)+[\w-]{2,}$">
        <div th:if="${#fields.hasErrors('email')}" th:errors="*{email}" class="invalid-feedback"></div>
        <div class="invalid-feedback">Please enter a valid email address.</div>
    </div>

    <div class="col-12">
        <label th:for="password" class="form-label">Password</label>
        <input type="password" th:field="*{password}" class="form-control" placeholder="At least 8 characters" required>
        <div th:if="${#fields.hasErrors('password')}" th:errors="*{password}" class="invalid-feedback"></div>
    </div>

    <div class="col-12">
        <label th:for="address" class="form-label">Address</label>
        <input type="text" th:field="*{address}" class="form-control" placeholder="1234 Main St" required>
        <div th:if="${#fields.hasErrors('address')}" th:errors="*{address}" class="invalid-feedback"></div>
    </div>

    <button class="btn btn-primary btn-dark btn-lg col-6" type="submit">Sign Up</button>
</div>

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

Performance issues with the iOs CSS keyboard appearance are causing delays in

Recently updated my application to be mobile-friendly. I've noticed that at times, the keyboard seems to have a slight delay in appearing when I tap on an input field. I have around 20 inputs on a single page. I conducted a test with the same number ...

Tips for modifying the appearance of a text file within an iframe

Lately, I've been facing a dilemma on my website. I have an iframe code that looks like this: <iframe src ="http://hokuco.com/cde/user.txt" id ="myIframe"></iframe> However, the consolas font used in the iframe makes my website appear ...

Create a visually appealing Zoom Effect with CSS while ensuring the div size remains constant

Update: What should I do if the width size is unknown? Check out the demo on JS Fiddle If you visit the provided link, I am trying to achieve a zoom effect where only the image inside the div zooms, not the entire div itself. However, I seem to be missin ...

Concentrate on a specific component within an overlay using jQuery

How can I focus on a specific area within an overlay when adding an item to the cart? I want to make the rest of the overlay darker and the targeted area more visible. See a quick mockup here: https://i.sstatic.net/UpJuc.png Here's the HTML code: & ...

Enhance the appearance of an HTML select tag for optimal viewing on iOS Safari browser for

I have customized three dropdown lists using CSS. I set the style as follows: .dropdown{ font-size: 20px; background: white; border:none; position: relative; } While the dropdowns appear fine in Chrome, there seems to be a slight differen ...

Adjusting the "lang" attribute to modify the font style

By using the HTML lang attribute and setting it to "en", I noticed that some of the div fonts and colors were changing to the default ones in Bootstrap. Although I am utilizing the Bliss2 Regular font, the default font style seems to be taking precedence. ...

Converting a data type into a CSS format

Currently, I am in the process of creating a table and would like to implement a 4-color scheme randomly selected from a pool of 10 schemes. Below is my CSS/PHP code: <?php header('Content-type: text/css'); // 0 grey $color00 = '#95 ...

Is there a way to prevent EasyTabs from automatically selecting the second tab?

I'm facing an issue with setting the default tab using easytabs. Currently, the second option is automatically selected as the default tab. Previously, I had successfully set the first tab (statistics) as the active tab but lost the file. Now, I am s ...

Having trouble sharing an image - error

I have a project in HTML where I need to send an image to a PHP script for uploading to a directory on the server. Here is the HTML form I am using: <form role="form" action="upload.php" method="post" enctype="multipart/form-data"> <div class="f ...

How can you set an input field to be initially read-only and then allow editing upon clicking a button using Vue.js?

//I have two divs that need to be set as readonly initially. When an edit button is clicked, I want to remove the readonly attribute and make them editable. <div> <input type="text" placeholder="<a href="/cdn-cgi/l/email-protection ...

Transform JSON data into an HTML layout

I'm looking to design a structure that showcases JSON information using HTML div elements. For example, utilizing the h4 tag for headers, p tag for text descriptions, and img tag for images. Can anyone provide guidance on the most efficient approach ...

Animate a dotted border with CSS

How can I make a text block with a dotted style border move like a gif image using CSS at runtime? ...

Update options in HTML form dropdowns dynamically based on selections from other dropdowns using Node.js

In my Node.js application, I have the following file system structure: public elegant-aero.css stadium-wallpaper.jpg team-results.html public.js app.js teamresults.mustache I am trying to dynamically populate a dropdown menu based on user sele ...

Issue with displaying Bootstrap 3 dropdown menus

I've been experimenting with the latest Bootstrap 3 and everything seems to be working well, except for the dropdowns I'm trying to create. Please let me know if anyone has any insights into what I might be doing incorrectly. Here is the code I&a ...

Bootstrap is having trouble properly organizing the columns within the row

I am facing a challenge with arranging three columns in a row: Interested listings | Basic profile | Connections However, when the screen size is reduced to that of a phone, they should be stacked like this: Basic profile Interested listings ...

Troubleshooting email form validation issues in Vue.js

Even after entering the email id, I am still receiving the error message stating that the email is required. Here is a screenshot for reference: . I want the error message to be hidden when the email id is entered. HTML: <form class="footer-f ...

Dynamic background image that fills the entire webpage, adjusts to different screen sizes, and changes randomly

Currently, I am working on designing a web page with a dynamic background image that adjusts responsively in the browser without distortion. The challenge is to randomly select an image from an array using jQuery. Unfortunately, my knowledge of jQuery is ...

"Ajax has the ability to alter the vertical scroll position

I have a quick query for you. Can anyone assist me in understanding why the page scroll resets to the top after an Ajax request? I suspect it has something to do with JQuery. There isn't much information available online, so I'm reaching out for ...

The data is not being displayed in the table

I am encountering an issue while attempting to populate the table with data received through props by looping over it. Unfortunately, the data is not rendering on the UI :( However, when I manually input data, it does show up. Below is my code: Code for P ...

Creating "search result pages" with HTML, PHP, Javascript, and MySQL

Consider the scenario where you have a table with two columns: id and name, containing thousands of rows. You have a search engine that allows searching by free text, using the query: SELECT * FROM `table` WHERE `name` LIKE '%$search%' The res ...