I have a form that I want to validate before the user is able to submit it. To do this I have written a basic js file that checks a value is not left blank. In the event that this is the case I want the background colour of the text field to update to be red. I have looked around online and am struggling to get this working. Here is what I have so far:
HTML Form:
<script language="javascript" src="validateForm.js"></script>
<form name="contact form">
<input type="text" name="name"></td>
<input type="button" value="Send" onsubmit="return validateForm()" method="post">
</form>
Javascript:
function validateForm()
{
var result = true;
var form = document.forms["contact form"];
// Name
var name = form["name"].value;
if ( name == null || name == "" )
{
form["name"].style.backgroundColor = red;
result = false;
}
return result;
}
Please could someone help me get this working?