Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6792123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:55:48+00:00 2026-05-26T17:55:48+00:00

Below is my code, I am needing to get to where the label name

  • 0

Below is my code, I am needing to get to where the label name will match the behavior that my field currently does. If nothing is entered the field is changed to a red background. If it is focused it goes back to white. I am needing the font color of the label to match color of the field and match the same behavior.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Lab06 - Form Validation</title>
<script type="text/javascript" charset="utf-8">



    function passcheck(){
        var pw1 = document.forms[ 0 ].password.value;
        var pw2 = document.forms[ 0 ].passcomf.value;

        if ( pw1 != pw2 ) {
            alert ("You did not enter the same new password twice. Please re-enter your password.");
            return false;
        }else{
            return true;
        }
    }


    function validate( ) {
        var errors = new Array( );

        for( var i = 0; i < document.forms[ 0 ].elements.length ; i++ ){

            if( document.forms[ 0 ].elements[ i ].type == "text" ) {
                if( document.forms[ 0 ].elements[ i ].value == "" ){
                    errors.push( "The " + document.forms[ 0 ].elements[ i ].name + " field cannot be blank.\n");

                    document.forms[ 0 ].elements[ i ].className = "in_error";



                }


            }

            if( document.forms[ 0 ].elements[ i ].type == "select-one") {
                if(document.forms[ 0 ].elements[ i ].selectedIndex == 0 ) {
                    errors.push( "The " + document.forms[ 0 ].elements[ i ].name + ' was not "Yes, I agree!", you must agree to the "User Agreement."\n');

                    document.forms[ 0 ].elements[ i ].className = "in_error";
                }
         }

            if( document.forms[ 0 ].elements[ i ].type == "select-one") {
                if(document.forms[ 0 ].elements[ i ].selectedIndex == 2 ) {
                    errors.push( "The " + document.forms[ 0 ].elements[ i ].name + ' was not "Yes, I agree!", you must agree to the "User Agreement."\n');

                    document.forms[ 0 ].elements[ i ].className = "in_error";
                }
         }







    }
        if( errors.length == 0 ) {
            return true;
        } else {
            clear_errors( );
            show_errors( errors );

            return false;
        }


    }



    function clear_errors( ){
        var div = document.getElementById( "errors" );
        while( div.hasChildNodes( ) ){
            div.removeChild( div.firstChild );
        }
    }

    function show_errors ( errors ) {
        var div = document.getElementById( "errors" );
        for( var i = 0; i < errors.length; i++ ){
            var error = document.createTextNode( errors[ i ] );
            var p = document.createElement( "p" );
            p.appendChild( error );
            div.appendChild( p );
        }
    }


    window.onload = function( ) {
        document.forms[ 0 ].onsubmit = validate;

    }



</script>


<style type="text/css" media="screen">
    #errors {
        color: #F00;
    }
    .in_error {
        background-color: #F00;
    }
    input:focus {
        background-color: #FFF;
    }
    select:focus {
        background-color: #FFF;
    }
</style>

</head>

<body>

    <h1>Form Validation</h1>

    <div id="errors"></div>

    <form action="" method="post" onsubmit="return passcheck()">
         <p>
            <label for="firstname" id="labelfirstname">First name:</label>
            <input type="text" name="First name" value="" id="firstname" />
        </p>
         <p>
            <label for="lastname" id="labellastname">Last name:</label>
            <input type="text" name="Last name" value="" id="lastname"/>
        </p>
         <p>
            <label for="middlei" id="labelmiddlei">Middle initial:</label>
            <input type="text" name="Middle initial" value="" id="middlei"/>
        </p>
         <p>
            <label for="address" id="labeladdress">Street address:</label>
            <input type="text" name="Street address" value="" id="address"/>
        </p>
         <p>
            <label for="city" id="labelcity">City:</label>
            <input type="text" name="City" value="" id="city"/>
        </p>
         <p>
            <label for="State" id="labelstate">State:</label>
            <input type="text" name="State" value="" id="state"/>
        </p>
         <p>
            <label for="zipcode" id="idzipcode">Zipcode:</label>
            <input type="text" name="Zipcode" value="" id="zipcode"/>
        </p>
         <p>
            <label for="username" id="labelusername">Username:</label>
            <input type="text" name="Username" value="" id="username"/>
        </p>
         <p>
            <label for="password" id="labelpassword">Password:</label>
            <input type="text" name="Password" value="" id="password"/>
        </p>
         <p>
            <label for="passcomf" id="labelpasscomf">Password comfirmation:</label>
            <input type="text" name="Password comfirmation" value="" id="passcomf"/>
        </p>
         <p>
            <label for="agreement" id="labelagreement">User Agreement</label>
            <select name="User Agreement" id="agreement">
                <option></option>
                <option>Yes, I agree!</option>
                <option>No, I do not agree!</option>
                </select>
        </p>
        <p><input type="submit" value="Register &rarr;" onclick="return passcheck(); return true;"/></p>
        </form>
</body>

    </html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:55:49+00:00Added an answer on May 26, 2026 at 5:55 pm

    If you move your label element after the input elements, then position them by a float (or some other means to get them right), then you could change your css to use the adjacent sibling selector to get your effect. Here is a test sample of css:

    input,
    select {
        float: right;
        clear: right;
    }
    label,
    .in_error:focus + label {
        color: black;
    }
    #errors,
    input.in_error + label {
        color: #F00;
    }
    .in_error {
        background-color: #F00;
    }
    input:focus {
        background-color: #FFF;
    }
    select:focus {
        background-color: #FFF;
    }
    

    Of course, the down side is the html is not in a logical order with the label following the input. Other than a javascript solution, I am not aware of a way to do it with pure css without reordering the elements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I enter the code below, I get an error. Basically, the foreach will
The below code will not join, when debugged the command does not store the
Hey all. Here is the scenario. I have the below code, and I'm needing
in below code when i run it i get y=-124 and z=4294967172 can you
I am needing to extract the max zoom level as this JavaScript code does,
below code can not run def map = [name:Test :: ( %2f %25 \$
below code: def http = new HTTPBuilder( 'http://twitter.com/statuses/' ) http.get( path: 'user_timeline.json', query: [id:'httpbuilder',
With below code, I am trying to match a text in a web page
Below code saying error incorreect syntax near Main INSERT INTO tbl ( 'Week', Main,
The below code is very simple. I have a jQuery autocomplete bound to an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.