Error in Formatting Labels in CSS

I currently have a form with textboxes that include validation. Everything works smoothly when clicking the save button for the first time, but if we fill out the textboxes and then remove the text, an error message is displayed on the textboxes. You can view this behavior in action here:

http://jsfiddle.net/9fYxb/2/

The CSS styling used for this form is as follows:

#field
{
  margin-left: .5em;
  float: right;
}
#field, label
{
  float:left;
  font-family: Arial, Helvetica, sans-serif;
  font-size: small;
}
br
{
  clear: both;
}
input
{
  border: 1px solid black;
  margin-bottom: .5em;
  width: 72px;
}
input.error
{
  border: 1px solid red;
}
label.error
{
  position:fixed;
  Background-image:url("../images/unchecked.jpg");
  background-repeat:no-repeat;
  padding-left: 20px; 
  margin-left: .3em;
  vertical-align:middle;
  background-color: #FFEBE8;  
  border: solid 1px red;
  width:210px;
  height:15px;
}
label.valid
{
  position:absolute;
  Background-image:url("../images/checked.gif") ;
  background-repeat:no-repeat;
  padding-left: 20px; 
  margin-left: .3em;
  vertical-align:top;
  background-color:White;
  border:none;
  width:1px;
  height: 16px;
}

Answer №1

Do you think having a float left for elements with IDs "#field" and "label" is necessary?

 #field, label {
     float:left;
     font-family: Arial, Helvetica, sans-serif;
     font-size: small;
 }

If you decide to remove the float left property and also eliminate the position from labels with an error and valid:

http://jsfiddle.net/kimiliini/8SM7f/

You should also consider fixing the mobile number check. Your current regex pattern:

/[789]\d{9}$/

Would validate

// This as OK
7111111111
// But this would also validate this as OK:
123155551333321212121521251215421212487884655487111111111

Consider using the following regex pattern instead:

/^[789]\d{9}$/

The ^ denotes the start of a string.

Also, note that background-image is incorrectly written with a capital "B". The correct CSS property does not have a capital letter in "Background-image".

Answer №2

Revamp your CSS for the label.error to achieve a more sleek look:

label.error
{
    clear:both;
    float:right;
    Background-image:url("../images/unchecked.jpg");
    background-repeat:no-repeat;
    padding-left: 20px; 
    margin-left: .3em;
    vertical-align:middle;
    background-color: #FFEBE8;
    border: solid 1px red;
    width:210px;
    height:15px;
}

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

Enhance your website's accessibility by using JavaScript to allow the down and up arrow keys to function

Thank you for taking the time to read this, any feedback is greatly appreciated. The current script on my website is only partially functional (click "i" in the bottom right corner). Currently, the script will focus on the first link AFTER pressing the T ...

Is it feasible to block indent the second line of a URL?

Is there a way to indent a second line of a URL so that it aligns perfectly with the first letter of the sentence? I attempted using <ul>, but encountered issues as it inherited small text and bullet points from the main .css file. Check out this li ...

Contrasting gatsby-theme-material-ui and gatsby-plugin-material-ui

I'm in the process of creating a website using Gatsby, and I'd like to incorporate Material UI, but I'm unsure about which plugin I should use. Here are my questions: What is the difference between these two options, and how can I integra ...

Jquery selector failing to target correct <form> element within Shopify theme

I'm feeling a bit lost here! Trying to zero in on a form within my page and utilize jQuery's serializeArray() function to extract all the values from said form. <div class="page-width"> <header class="section-header ...

Protractor Cucumber: Issue with locating spec file patterns (identifying 2 features)

I am facing an issue with running 2 different cucumber features. After adding the following lines to my protractor.conf.js file: specs: ['add.feature', 'delete.feature'] I encountered a problem when running the tests stating pattern ...

The parameters remain consistent across all Angular directives

I have created a directive called 'filterComponent' with the following code: app.directive('filterComponent', function() { return { restrict: 'E', templateUrl: 'filter-component.html', link: function ...

Why is my code throwing an error stating "Unable to assign value to innerHTML property of null"?

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> </head> <body> <div class="container">Lorem ipsum</div&g ...

Discovering the most recent Node.js version: A step-by-step guide

Is it possible to check the latest available Nodejs version using npm? While node -v allows us to see the current version, I am curious if there is a way to access the most recent version through JavaScript. For example, process.version can be used to vi ...

Attempting to retrieve assets from an incorrect directory in a rush

Within my express project, I have encountered an issue when rendering the product route with a specific id. It seems that the assets are attempting to load from /products in the public folder (even though this directory does not exist). However, when I ren ...

What is the best way to package JS files in an asp.net framework?

In my Angular application, I am dealing with numerous JS files. What is the best way to bundle all of these files together? Most sources online suggest using BundleConfig for bundling JS files, but I am working with an empty web application template in AS ...

When adjusting window size, the input shifts towards the left

Is there a way to prevent my input and login button from moving to the left when resizing the window? I am new to HTML and CSS and would appreciate any tips on keeping them in place. Below is my CSS code: #absolute { position: absolute; top: 411px; le ...

Perform an Ajax request with JQuery in an HTML document and transfer the response to a different HTML page

This is my first attempt at using AJAX and jQuery to retrieve data from another HTML page. Below is the code I have written: Here is my JavaScript code: <script type="text/javascript> $(document).ready(function() { $('#submit').click( ...

Photoswipe using identical source code, yet the output is malfunctioning

Having an issue with my code that refreshes the content of a ul element. Initially, it works fine by directly loading the ul content, but I am constantly creating new content every 10 seconds to provide users with fresh updates. Even though the source co ...

Encountering an error when attempting to access undefined property while using a method as a callback

Exploring OOP and angular is new to me. I am currently trying to implement a reusable table with pagination that triggers an API request when the page changes (pagination within the table component). The issue arises when I attempt to access my method usi ...

Ensure that you wait for all asynchronous $http requests to finish in AngularJS before continuing

I am facing a challenge with a page that generates a varying number of $http requests based on the length of a certain variable. I aim to update the scope with the data only after all requests have been completed. Without relying on jQuery for this project ...

Issue with Ajax request not redirecting to correct URL

I have been successfully using ajax requests in my document without any issues. I am looking to retrieve the user's coordinates as they load the document and then pass this data on to other methods for distance calculations. On the loading of the ind ...

Send a res.json response and retrieve it using res.render in a different router

Trying to retrieve a JSON from the route "/api/product" and display it using hbs on the "/product" route. The code seems to be working, but is it done correctly? The router in this case is: '/api/product' router.get('/', this.controll ...

What's the best approach: Backbone-Relational discovery or retrieval?

Although the model caching feature in Backbone-Relational is effective, loading a simple model securely requires considerable code. For example: // Attempt to locate a model in the cache this.model = MyModel.find({id:id}); if(this.model){ // Model re ...

The cookies() function in NextJS triggers a page refresh, while trpc consistently fetches the entire route

Is it expected for a cookies().set() function call to trigger a full page refresh in the new Next 14 version? I have a chart component that fetches new data at every interval change, which was working fine when fetching the data server-side. However, since ...

Updating React state using a form input

Seeking assistance on retrieving form values and storing them in state. Despite following various guides (mostly in class style react), I keep encountering the same error: "Nothing was returned from render. This usually means a return statement is m ...