Personalizing HTML input fields

I am looking to create a contact form that will display an image depending on whether the input entered is correct or incorrect. My goal is to achieve this using only HTML and CSS, without needing any JavaScript. While there are similar posts on this topic, I have yet to find the exact solution I am seeking.

Here is what my HTML looks like:

<div id="contact">
    <div class="formulier">
        <div id="inline">
            <h2>Send us a Message</h2>
            <form id="contact" action="#" method="post" name="contact">
                <label for="email">Your E-mail : </label>
                <input id="email" class="txt" type="email" name="email" />
                <img src="assets/img/NoMark.png" alt="No Mark" height="25" width="24"><br>

                <label for="msg">Enter a Message : </label>
                <textarea id="msg" class="txtarea" name="msg"></textarea><br>

                <button id="send">Send E-mail</button>
            </form>
        </div>
    </div>
</div>

Currently, my form consists of an input field followed by an image (either a checkmark or an X). I am aiming to have the form dynamically switch between these two images based on the correctness of the email address inputted in the "email" field.

Thank you for your assistance,

thefellowes

Answer №1

Give this a shot:

Here is the HTML code:

<input id="email" class="txt" type="email" name="email" />
<img class="not-validated" src="assets/img/NoMark.png" alt="Not validated" height="25" width="24">
<img class="valid" src="assets/img/checkmark.png" alt="Valid" height="25" width="24">
<img class="invalid" src="assets/img/cross.png" alt="Invalid" height="25" width="24"><br>

And here is the CSS code:

#email ~ img.not-validated, #email:valid ~ img.valid, #email:invalid ~ img.invalid {
    display: inline-block;
}
#email:valid ~ img.not-validated, #email:invalid ~ img.not-validated, #email:valid ~ img.invalid, #email:invalid ~ img.valid {
    display: none;
}

For more information on HTML and CSS form validation only, check out:

Please note that browser support may vary. This method is supported in IE10+, Firefox 26+, Chrome 31+, Safari 5.1+ (partial support), Opera 19+, IE10.0 mobile (partial support), Blackberry browser 10.0. It is not supported in iOS safari, Android browser, and Opera mini. See: http://caniuse.com/#feat=form-validation

Answer №2

When it comes to input validation, relying solely on css and html is not enough; JavaScript is required. If you're looking for guidance on this, you may find the following link useful: JavaScript Form 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

Show the selected checkbox options upon clicking the submit button

Having trouble setting up a filter? I need to create a feature where checked values from checkboxes are displayed in a specific div after submitting. The display should include a 'Clear All' button and individual 'X' buttons to remove e ...

The header appears distorted on older versions of Internet Explorer, specifically IE 7 and IE

The website I am currently working on looks great in Chrome, Firefox, Opera, and Safari on both Mac and Windows. However, it is not displaying correctly in IE 7 & 8. The header should appear like this: But in Internet Explorer, it's showing up like t ...

Can you provide guidance on how to prioritize container div style over CSS class?

Here's the HTML code snippet I'm working with: <html> <head> <style> .text-box { color: red; } </style> </head> <body> <p class=&qu ...

What is the best way to toggle the visibility of a div when a button is clicked?

I have a container with a registration wizard, and I want to toggle the visibility of this container when a button is clicked. How can I achieve this? Check out the code snippet below. Thank you :) <div id="wizard" class="swMain"> <ul> ...

Simulating a mobile device screen size using an embedded iframe

Imagine this scenario: What if instead of adjusting the browser window size to showcase a responsive web design, we could load the site into an IFRAME with the dimensions of a mobile screen. Could this concept actually work? Picture having an IFRAME like ...

get the value of the last selected element using jQuery

I am facing an issue where I cannot retrieve the selected value from my second select element. Even though I have selected the second option in the dropdown, it keeps returning the value of the first option as empty. Here is a snippet of the HTML code: & ...

File-loader mistakenly generates 2 images and associates them with the incorrect one

I have encountered an issue with file-loader where it is creating two separate images in my build folder. One image is named correctly and saved in the correct path, while the other one is named [hash].png (default) and is stored directly in the build dire ...

Issue with Bootstrap tab display of content

I'm having trouble with the tabs in my page. When I click on each tab, the content doesn't display correctly. Here is my code: <div class="w470px exam" style="border: 1px solid #ddd; margin-top: 30px;"> <ul id="myTab" class="nav nav ...

NuxtLink sending users to incorrect destination URL

I am facing an issue with my static generated Nuxt site. Everything works perfectly fine when I host it locally, but once I load it on GitHub Pages, the NuxtLink elements' hrefs are incorrect. For instance, one of my links looks like this: <NuxtLi ...

Crash in the Android WebView of the Galaxy S3 device due to Signal 11 SIGSEGV

My Android WebView is experiencing issues with memory access and crashes on a Galaxy S3 running Android 4.0.4. The HTML5 content includes interactive battles where enemies appear for the user to slash, interspersed with normal HTML pages. Despite the use o ...

Verifying that the class name prefix aligns with the folder name using Stylelint

Currently, I am utilizing the "selector-class-pattern" rule from stylelint. My chosen pattern enforces the ECSS naming convention. The specific rule configuration is outlined below: "selector-class-pattern": ["^[a-z]([a-z0-9]){1,3}-[A-Z][a-zA-Z0-9]+(_[A-Z ...

The usage of an import statement is not permissible outside of a module

Having some trouble using the mathjs library to calculate complex solutions for the quadratic equation. No matter how I try to import the library into my code, I keep encountering errors. Initially, I attempted this method: At the top of my root.js file, ...

I'm having trouble making my carousel smaller and centering it on the page. How can I adjust it to be a more subtle feature on the page?

I'm having trouble adjusting the width and height of my images in the code. Whenever I set them to less than 100%, it messes up the layout and the images get cropped. I want them centered without taking up so much space. I've resized all my imag ...

Incorporate dynamic rules into a CSS stylesheet

I am trying to dynamically add a rule to my CSS for each element. Each rule should have a different background-image and name. However, I seem to be encountering an issue where the dynamically added div is not linking to the dynamically added CSS rule. I&a ...

iFrame initially loads the page and subsequently reloads it continuously in a separate tab

Encountering a strange issue with IE11 regarding a series of links: <a href="page1.html" target="iframe_name">Page #1</a> <a href="page2.html" target="iframe_name">Page #2</a> <a href="page3.html" target="iframe_name">Page #3 ...

What are the steps to effectively utilize <ul> for showcasing scrolling content?

I stumbled upon and found it to be a great inspiration for my project. I tried replicating the layout of the listed items on the site: .wrap { display: block; list-style: none; position: relative; padding: 0; margin: 0; border: ...

How can I prevent scrolling in a Vue mobile menu?

In this scenario, the toggle for the menu is controlled by a checkbox with the id "check" below. I attempted to bind v-bind:class="[{'antiscroll': checkboxValue}]" to the body in HTML, and also in the main App.vue file, but it did not work as exp ...

Incorporating Bootstrap into a fresh project

I'm completely new to the world of development, so I'll do my best to phrase this question correctly! Last month, I successfully created my first web application using RoR and Bootstrap for some visual enhancements. Currently, I am working on a ...

Creating a compilation of HTML file links using Gulp

Is there a way to utilize Gulp in order to compile a single HTML file containing links to all pages within a specific directory? For instance, if I have two files contact.html titled "Contacts" and faq.html titled "Frequently asked questions" located in t ...

Text within the footer section

When displaying the name of a subcategory, I want it in one line. However, if the subcategory name contains two words, it should be displayed in two lines. https://i.stack.imgur.com/CcDeL.png Code: .Footer { width: 100%; display ...