This is how you make a contact form required using javascript. This is for more basic contact forms that do not use their own database.
- First drag a Javascript control onto the page.
- Next paste the javascript below into the Javascript control.
- Edit the form and add onclick="return Validate()" to the submit button.
- Then test it and make sure it works.
KEEP IN MIND: This will only work if the user has javascript enabled. Rarely serious spammers do, so if you add this to a contact form which the person was getting a lot of the same spam shit in, don't be surprised if it doesn't stop them.
Javascript:
function Validate()
{
var isValid = true;
$('input,textarea').each(
function(i)
{
var el = $(this);
if(el[0].name.indexOf('.') >= 0)
{
elname = el[0].name;
switch(elname.substring(elname.lastIndexOf('.')+1).toLowerCase())
{
case "n":
case "name":
if((!el.val() || el.val() == el[0].defaultValue) && isValid)
{
isValid = false;
alert('Please enter your Name');
}
break;
case "p":
case "phone":
if((!el.val() || el.val() == el[0].defaultValue) && isValid)
{
alert('Please enter your Phone Number');
isValid = false;
}
break;
case "e":
case "email":
if((!el.val() || el.val() == el[0].defaultValue) && isValid)
{
alert('Please enter your Email Address');
isValid = false;
}
break;
case "m":
case "message":
case "text":
case "description":
case "comments":
if((!el.val() || el.val() == el[0].defaultValue) && isValid)
{
alert('Please enter your Message');
isValid = false;
}
break;
}
}
}
);
return isValid;
}