body is not a good choice for an identifier, but the fact that you’re getting to the alert, shows that body.value.length is defined. Perhaps you should post the entire form and validation function.
The onblur functions work great, to display the js errors, but I just can’t get the errors to show up upon hitting submit. Well they show up when I edit the code but do not function properly. Often they will continuously add labels. I don’t know if I should be repeating a lot of the same function for my addonload. I also don’t know if I can put my validationForm() function in the addonload function.
addOnload(function(){
// function to remove any old messages and show a new one
function show_error(label, msg){
removeChildren(label);
label.appendChild(document.createTextNode(msg));
}
// function to get the default error message container
function get_label(){
var error_container = document.createElement('label');
error_container.style.display = 'inline';
error_container.style.margin = '0 0 0 85px';
error_container.setAttribute('className', 'error');
error_container.setAttribute('class', 'error');
return error_container;
}
function get_label_body(){
var error_container = document.createElement('label');
error_container.style.display = 'inline';
error_container.style.margin = '5px 20px 0 0';
error_container.style.cssFloat = 'right';
error_container.style.styleFloat = 'right';
error_container.setAttribute('className', 'error');
error_container.setAttribute('class', 'error');
return error_container;
}
// duplicate the default
var fullname_msg = get_label();
var email_msg = get_label();
var subject_msg = get_label();
var body_msg = get_label_body();
// get the input field elements
var fullname = document.getElementById('fullname');
var email = document.getElementById('email');
var subject = document.getElementById('subject');
var body = document.getElementById('body');
var messagesend = document.getElementById('messagesend');
// add the new message elements to the screen
fullname_msg.for = 'fullname';
fullname.parentNode.appendChild(fullname_msg);
email_msg.for = 'email';
email.parentNode.appendChild(email_msg);
subject_msg.for = 'subject';
subject.parentNode.appendChild(subject_msg);
body_msg.for = 'messagesend';
messagesend.parentNode.appendChild(body_msg);
fullname.onblur = function fullnamev(){
if (fullname.value.length === 0){
show_error(fullname_msg, 'This field is required.');
}else if (fullname.value.match(/^[a-zA-Z]+$/) === null){
show_error(fullname_msg, 'Invalid name (letters only).');
}else{
removeChildren(fullname_msg);
}
}
email.onblur = function emailv(){
if (email.value.length === 0){
show_error(email_msg, 'This field is required.');
}else if (email.value.match(/^[a-z0-9\\-\\.\\_\\+]{1,64}\\@(?:[a-z0-9\\-\\_]+)\\.(?:[a-z]{2,4}|[a-z]{2,3}\\.[a-z]{2,3})$/) === null){
show_error(email_msg, 'Invalid e-mail address.');
}else{
removeChildren(email_msg);
}
}
subject.onblur = function subjectv(){
if (subject.value.length === 0){
show_error(subject_msg, 'This field is required.');
}else if (subject.value.match(/^[a-zA-Z]+$/) === null){
show_error(subject_msg, 'Invalid name (letters only).');
}else{
removeChildren(subject_msg);
}
}
body.onblur = function bodyv(){
if (body.value.length === 0){
show_error(body_msg, 'This field is required.');
}else if (body.value.match(/^[a-zA-Z]+$/) === null){
show_error(body_msg, 'Invalid name (letters only).');
}else{
removeChildren(body_msg);
}
}
});
function validateForm(){
var fullnameform = document.forms["contact"]["fullname"].value
var emailform = document.forms["contact"]["email"].value
var subjectform = document.forms["contact"]["subject"].value
var bodyform = document.forms["contact"]["body"].value
// function to remove any old messages and show a new one
function show_error(label, msg){
removeChildren(label);
label.appendChild(document.createTextNode(msg));
}
// function to get the default error message container
function get_label(){
var error_container = document.createElement('label');
error_container.style.display = 'inline';
error_container.style.margin = '0 0 0 85px';
error_container.setAttribute('className', 'error');
error_container.setAttribute('class', 'error');
return error_container;
}
function get_label_body(){
var error_container = document.createElement('label');
error_container.style.display = 'inline';
error_container.style.margin = '5px 20px 0 0';
error_container.style.cssFloat = 'right';
error_container.style.styleFloat = 'right';
error_container.setAttribute('className', 'error');
error_container.setAttribute('class', 'error');
return error_container;
}
// duplicate the default
var fullname_msg = get_label();
var email_msg = get_label();
var subject_msg = get_label();
var body_msg = get_label_body();
// get the input field elements
var fullname = document.getElementById('fullname');
var email = document.getElementById('email');
var subject = document.getElementById('subject');
var body = document.getElementById('body');
var messagesend = document.getElementById('messagesend');
// add the new message elements to the screen
fullname_msg.for = 'fullname';
fullname.parentNode.appendChild(fullname_msg);
email_msg.for = 'email';
email.parentNode.appendChild(email_msg);
subject_msg.for = 'subject';
subject.parentNode.appendChild(subject_msg);
body_msg.for = 'messagesend';
messagesend.parentNode.appendChild(body_msg);
if (fullnameform === null || fullnameform ==="" || emailform === null || emailform ==="" || subjectform === null || subjectform ==="" || bodyform === null || bodyform ==="")
{
if (body.value.length === 0){
alert('geez');
show_error(body_msg, 'This field is required.');
}else if (body.value.match(/^[a-zA-Z]+$/) === null){
show_error(body_msg, 'Invalid name (letters only).');
}else{
removeChildren(body_msg);
}
return false;
}
}