0

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?

1 Answer 1

3

Use red as string ( you missed the quote) jsfiddle

form["name"].style.backgroundColor = "red"; // not red
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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