Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove previously set border color

Tags:

function validate() {
	var username = document.getElementById("username").value;
	var password = document.getElementById("password").value;

	if (username == "") {

		document.getElementById("message").innerHTML = "USERNAME CANNOT BE EMPTY";
		document.getElementById("username").style.borderColor = "red";
		return false;
	}
	if (password == "") {


		document.getElementById("message").innerHTML = "PASSWORD CANNOT BE EMPTY";
		document.getElementById("password").style.borderColor = "red";
		return false;
	}
}
#username:focus {
	background-color: yellow;
	border-color: green;
}

#password:focus {
	background-color: yellow;
	border-color: green;
}

#message {
	color: red;
}
<form onsubmit=" return validate()">
    LOGIN:-
    <br>
    <input id="username" type="text" name="username" placeholder="USERNAME">
    <br>
    <input id="password" type="password" name="password" placeholder="PASSWORD">
    <br>
    <input type="submit" value="SUBMIT">
    <p id="message">
</form>
  • When i focus on the text fields, the background and border color changes to yellow and green respectively (through css).
  • If i click on submit without entering anything, the border color changes to red (through javascript).
  • But when i bring the focus on the text field again, the red border color does not go away, instead i get both green and red borders.

I want it to be green only. Can you also explain the reason for this behavior.

like image 895
SuperAadi Avatar asked Dec 27 '19 06:12

SuperAadi


People also ask

How to turn off window border color in Windows 11?

You can turn off the window border color in Windows 11 from the Personalization section in the Settings app. Here is where to find and change the option. Open Windows 11 Settings app. Click on the “ Personalization ” option on the sidebar. Press the “ Colors ” option on the main panel.

What happens if border-color is not set?

Note: If border-color is not set, it inherits the color of the element. The border-color property can have from one to four values (for the top border, right border, bottom border, and the left border). You can learn more about HEX, RGB and HSL values in our CSS Colors chapters.

How do I change the color of a CSS border?

CSS Border Color. The border-color property is used to set the color of the four borders. The color can be set by: name - specify a color name, like "red". HEX - specify a HEX value, like "#ff0000". RGB - specify a RGB value, like "rgb (255,0,0)". HSL - specify a HSL value, like "hsl (0, 100%, 50%)". transparent.

How do I get rid of window borders on my screen?

In the Settings app, go to the “Personalization > Colors” page. Next, scroll down and turn off the “Show accent color on title bars and windows borders” option. As soon as you turn off the option, you will no longer see the window borders.


1 Answers

This is happening because you have updated the element style instead of CSS class property. Element style has the highest weight for CSS. Instead add an error class dynamically on error and remove it when the form field is valid.

As per the documentation, the order of style in decreasing order will be.

  1. Inline style (inside an HTML element)
  2. External and internal style sheets (in the head section)
  3. Browser default

Here is a working example

function validate() {
    var username = document.getElementById("username").value;
    var password = document.getElementById("password").value;

    if (username == "") {
        document.getElementById("message").innerHTML = "USERNAME CANNOT BE EMPTY";
        document.getElementById("username").classList.add("invalidInput");
        return false;
    } else {
        document.getElementById("username").classList.remove("invalidInput")
    }
    if (password == "") {
        document.getElementById("message").innerHTML = "PASSWORD CANNOT BE EMPTY";
        document.getElementById("password").classList.add("invalidInput")
        return false;
    } else {
        document.getElementById("password").classList.remove("invalidInput")
    }
}
#username:focus {
    background-color: yellow;
    border-color: green;
}

#password:focus {
    background-color: yellow;
    border-color: green;
}

.invalidInput {
    border-color: red; 
}

#message {
    color: red;
}
<form onsubmit=" return validate()">
    LOGIN:-
    <br />
    <input id="username" type="text" name="username" placeholder="USERNAME" />
    <br />
    <input id="password" type="password" name="password" placeholder="PASSWORD" />
    <br />
    <input type="submit" value="SUBMIT" />
    <p id="message"></p>
</form>
like image 72
Nitheesh Avatar answered Sep 20 '22 17:09

Nitheesh