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!

 

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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! :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.