Why doesn't this javascript work?

Soldato
Joined
18 Oct 2002
Posts
18,306
Code:
function checkTitle() {
	var title = document.getElementById("title");
	
	if(title.value.length == 0) {
		alert("Your post must have a title.");
		return false;
	} else {
		return true;
	}
}

function checkLen() {
	var body = document.form1.bodytext.value.length;
	var maxlen = 10;
	if(body > maxlen) {
		alert('Message is too long.\nMax length: '+maxlen+'\nYour message length: '+body);
		return false;
	} else {
		return true;
	}
}

function validateForm() {
	var titleok = checkTitle();
	var lengthok = checkLen();
	
	if(titleok == true && lengthok == true) {
		return true;
	} else {
		return false;
	}
}

The validateForm function is called onclick of the submit button, doesn't work properly when called by onsubmit in the form tag either.

Basically, so long as I enter a title, it will accept a message in the bodytext textarea as long as you can type in.
However, if I type in a long message which exceeds the limit, but no title, hit submit it warns me that I have no title. I enter a title, hit submit again and now it tells me the message is too long.

Any ideas why it's giving this bizarre behaviour?
 
Back
Top Bottom