Jump to content

Changing border color of field for validation


Jason28

Recommended Posts

Hello, I searched Google on how to change the border color of a form field to red when there is an error and even though I found some examples I couldn't find a way to integrate it with my current javascript code.  Here is my working error validation code:

 

 

function checkme_station() 
{
    if (document.getElementById('station_name').value == "")
    {
	alert("Please enter a name for this station.");
	document.getElementById('station_name').focus();return(false)
    }

    if (document.getElementById('selection').value == "Enter song or musician name" || document.getElementById('selection').value == "")
    {
	alert("Please enter the name of the song or name of musician into selection field.");
	document.getElementById('selection').focus();return(false)
    }

}

Could you please edit this code so that the form fields have a red border when one of those errors are found?  Thanks!

 

function checkme_station() 
{
    stationObj = document.getElementById('station_name');
    if (stationObj.value == "")
    {
        stationObj.style.borderColor = '#ff0000';
        alert("Please enter a name for this station.");
        stationObj.focus();
        return false;
    }

    selectionObj = document.getElementById('selection');
    if (selectionObj.value == "Enter song or musician name" || selectionObj.value == "")
    {
        selectionObj.style.borderColor = '#ff0000';
        alert("Please enter the name of the song or name of musician into selection field.");
        selectionObj.focus();
        return false;
    }

    stationObj.style.borderColor = '';
    selectionObj.style.borderColor = '';
    return true;
}

What text? I'm assuming you don't meant the text in the input field since 2/3 of the validation is for when there is no text. So, I am guessing you are talking about the label.

 

yes, that can be done, but if you make your FULL request up front it is more conducive to receiving the best response. Depending on how your labels and fields are constructed I might do things different than what I did above. please post the code for your form and I can assist.

Thanks again and yes I am referring to the text before the input field on the html page.  Actually could you take my original code that I posted and only add the javascript code to make the text turn red?  The input fields look pink in firefox and a bright red in IE so I may just make the text red only.  Thanks! :)

Actually could you take my original code that I posted and only add the javascript code to make the text turn red?

How can I do that? You have not provided any sample of the HTML code.

 

please post the code for your form and I can assist.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.