21

I'm doing form validation. when the form input is incorrect i add a red border to the textbox:

document.getElementById("fName").style.borderColor="#FF0000"

this then gives me a 2px red border. what i want to do is if the user putas in a correct value to go back to the original border...

can someone tell me how i change the border size and colour in javascript

0

7 Answers 7

28

Use CSS styles with CSS Classes instead

CSS

.error {
  border:2px solid red;
}

Now in Javascript

document.getElementById("fName").className = document.getElementById("fName").className + " error";  // this adds the error class

document.getElementById("fName").className = document.getElementById("fName").className.replace(" error", ""); // this removes the error class

The main reason I mention this is suppose you want to change the color of the errored element's border. If you choose your way you will may need to modify many places in code. If you choose my way you can simply edit the style sheet.

Sign up to request clarification or add additional context in comments.

3 Comments

Comment by Savas Vedova (rejected edit): classname is not valid. It's className instead.
This the class every time.
This is a little scary in case the div already has some other class that starts with the word "error" or what not... see stackoverflow.com/questions/11444640/… for some modern options, but the principle is sound, add a class then remove it :)
15
document.getElementById("fName").style.border="1px solid black";

Comments

7

You may try

document.getElementById('name').style.borderColor='#e52213';
document.getElementById('name').style.border='solid';

1 Comment

Please try to answer in words and not only in code. And since you answer so late, how does your answer differ from the other ones?
4

Add an onchange event to your input element:

<input type="text" id="fName" value="" onchange="fName_Changed(this)" />

Javascript:

function fName_Changed(fName)
{
    fName.style.borderColor = (fName.value != 'correct text') ? "#FF0000"; : fName.style.borderColor="";
}

Comments

2

document.getElementById("fName").style.borderColor="";

is all you need to change the border color back.

To change the border size, use element.style.borderWidth = "1px".

1 Comment

Ya but it gives me a 2px border... i want a 1px border if possible
2

I'm agree with Vicente Plata you should try using jQuery IMHO is the best javascript library. You can create a class in your CSS file and just do the following with jquery:

$('#fName').addClass('name_of_the_class'); 

and that's all, and of course you won't be worried about incompatibility of the browsers, that's jquery's team problem :D LOL

Comments

1

If the users enter an incorrect value, apply a 1px red color border to the input field:

document.getElementById('fName').style.border ="1px solid red";

If the user enters a correct value, remove the border from the input field:

document.getElementById('fName').style.border ="";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.