Hi, I'm a student learning web design and having a problem with some javascript code, I'm validating a text area, i dont have a problem limiting how many characters can be typed in the textarea, but I cant get it to give an error if there is no text in the text area.
In the code below the validateMes() function is not working, the other functions work fine
Javascript code:
Code:
function validateLimit(fld){
var error ="";
if(fld.value.length > 300){
error = "Cannot exceed 300 characters.\n"
fld.style.background = 'Yellow';
}else{
fld.style.background = 'White';
}
return error;
}
function validateMes(fld) { //this function not working
var error = "";
if (fld.value.length == 0) {
fld.style.background = 'Yellow';
error = "The Message field has not been filled in.\n"
} else {
fld.style.background = 'White';
}
return error;
}
function validateFormOnSubmit(form1) { //master validating function
var reason = "";
reason += validateLimit(form1.mess);
reason += validateMes(form1.mess);
if (reason != "") {
alert("Some fields need correction:\n" + reason);
return false;
}else{
alert("Message has been submitted");
return true;
}
}
Html form code:
Code:
<form name="form1" id="form1" onsubmit="return validateFormOnSubmit(this)" >
<div class="box">
<h1>CONTACT FORM :</h1>
<label>
<span>Full name</span>
<input type="text" class="input_text" name="name" id="name" value=""/>
</label>
<label>
<span>Email</span>
<input type="text" class="input_text" name="emailbox" id="emailbox" value=""/>
</label>
<label>
<span>Subject</span>
<input type="text" class="input_text" name="subject" id="subject" value=""/>
</label>
<label>
<span>Message</span>
<textarea class="message" name="mess" id="mess" ></textarea> <span>*Message limited to 300 characters</span>
<input type="submit" class="button" value="Submit Form" />
</label>
</div>
</form>
I can't see what I'm doing wrong, would really appreciate some help
Thanks