...

View Full Version : Simple Javascript Form Validation



OpenIntro
08-23-2010, 07:45 AM
I'm trying to do a simple validation to see if the field is blank, if it is, I want to write a message to a div. I've added the function call to an onclick. It seems to be set off no matter if there is a value or not, and what's more, the fields are clearing out when the link is clicked. My brain is mush right now, so what am I missing?


function validateThis() {
document.getElementById('messages').innerHTML = ''
var theMessage = '';
var reqMsg = 'must have a value.';

for (var i = 0; i <document.theForm1.elements.length; i++) {
if (document.theForm1.elements[i].value = ' ') {
theMessage += document.theForm1.elements[i].name +' ' + reqMsg +'<br/>';
}}

document.getElementById('messages').innerHTML = theMessage;
}

OpenIntro
08-23-2010, 08:23 AM
disregard. I figured it out.


if (document.theForm1.elements[i].value = ' ') {

should be


if (document.theForm1.elements[i].value == ' ') {

Told you my brain was mush!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum