This JavaScript has 3 functions, the first checks the details entered into the contact form an should the criteria be met, the form is processed.
The second and third functions count down the characters entered in to a texarea
I use this JavaScript in conjunction with the post
Adding PHP Contact Formfunction Contact()
{
re = "no";
remail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
rename = /^((?:[A-Z](?:('|(?:[a-z]{1,3}))[A-Z])?[a-z]+)|(?:[A-Z]\.))(?:([ -])((?:[A-Z](?:('|(?:[a-z]{1,3}))[A-Z])?[a-z]+)|(?:[A-Z]\.)))?$/;
remess = /^[0-9A-Za-z\,\.\'\-\s]+$/;
if(rename.test(document.contact.name.value))
{
re="yes";
}
else
{
alert("Please check your name");
document.contact.name.select();
document.contact.name.focus();
return false;
}
if(remail.test(document.contact.email.value))
{
re="yes"
}
else
{
alert("Please check your email address");
document.contact.email.focus();
document.contact.email.select();
return false
}
if(remess.test(document.contact.question.value))
{
re="yes";
}
else
{
alert("Please check your Message");
document.contact.question.select();
document.contact.question.focus();
return false;
}
if(re=="yes")
{
return true
}
}
function getObject(obj)
{
var theObj;
if(document.all)
{
if(typeof obj=="string")
{
return document.all(obj);
}
else
{
return obj.style;
}
}
if(document.getElementById)
{
if(typeof obj=="string")
{
return document.getElementById(obj);
}
else
{
return obj.style;
}
}
return null;
}
function Contar(entrada,salida,texto,caracteres)
{
var entradaObj=getObject(entrada);
var salidaObj=getObject(salida);
var longitud=caracteres - entradaObj.value.length;
if(longitud <= 0)
{
longitud=0;
texto='
'+texto+' ';
entradaObj.value=entradaObj.value.substr(0,caracteres);
}
salidaObj.innerHTML = texto.replace("{CHAR}",longitud);
}