I have been trying different methods and looking in many different places, and it really looks like the following code should work. I am stumped as to why it isn’t… I am a novice to html and JavaScript, so I’m guessing I keep overlooking something I did wrong? If so, hopefully someone can point it out.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
<!--
function validateUsername() {
if (document.getElementById("username") == "") {
document.registrationform.username.style.background = 'Yellow';
}
}
//-->
</script>
</head>
<body>
<form name="registrationform" id="registrationform" method="post" action="register.php">
<div class="formlabel">*Username:</div>
<div class="formfield"><input type="text" name="username" id="username" size="30" onblur="validateUsername()"/></div>
<div style="float:left;">
<input type="submit" value="Submit" />
</div>
</form>
</body>
</html>
When I leave it blank and click out of it, it does not change color like I’d like. Why?
should be
That should get you started. However, your implementation won’t catch cases where the user has entered a space (” “) into the textbox. For that, you’ll use the trim function to get rid of the spaces: