Simple Contact Form With JavaScript Validation

This is a very simple contact form script with JavaScript validation.

This form has three fields name, email address and message but can be easily modified with new fields.

Here is an example of the contact form

I hope to see some examples of what you do with this script. Like Mark Ireland has done with my previous Simple JavaScript Calculator script.

<script language = "JavaScript">
<!-- This function is called to validate a field in a form, when the function is called the field that needs to be validated and the text to display if the field is not valid is specified -->
function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value=="")
{alert(alerttxt);return false}
else {return true}
}
}
<!-- This function is called to validate the value entered into the email fieldof the table, when the function is called the field that needs to be validated and the text to display if the field is not valid is specified -->
function validate_email(field,alerttxt)
{
with (field)
{
apos=value.indexOf("@")
dotpos=value.lastIndexOf(".")
if (apos<1||dotpos-apos<2)
{alert(alerttxt);return false}
else {return true}
}
<!-- This function is called when the form is submitted, it goes through and calls different functions to validate different field types, when calling the function it specifies the field to be validated and the text to display if the field is not valid -->
}function validate_form(thisform)
{
with (thisform)
{
if (validate_required(name,"Please enter your name!")==false)
{name.focus();return false}

if (validate_email(email,"Not a valid e-mail address!")==false)
{email.focus();return false}

if (validate_required(message,"Please enter a message!")==false)
{message.focus();return false}

}
}
</script>

<!-- when the form is submitted it calls the function validate_form to validate the form before it is posted. -->
<form name="contact" action="http://www.sam-i-am.com/cgi-bin/echoform.cgi" method="post" onSubmit="return validate_form(this);">
<table border="0">
<tr>
<td colspan="2" id="counttitle">Contact Us</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td colspan="2"><table border="0" cellpadding="0" cellspacing="1" bgcolor="#000000">
<tr>
<td width="150" bgcolor="#FFFFFF">Name:</td>
<td bgcolor="#FFFFFF"><input type="text" name="name" size="30" maxlength="30" value=""></td>
</tr>
<tr>
<td bgcolor="#FFFFFF">Email Address:</td>
<td bgcolor="#FFFFFF"><input type="text" name="email" size="30" maxlength="30" value="" id="email"></td>
</tr>
<tr>
<td valign="top" bgcolor="#FFFFFF">Message:</td>
<td width="400" bgcolor="#FFFFFF"><textarea name="message" cols="45" rows="5"></textarea></td>
</tr>
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td bgcolor="#FFFFFF"><input type="submit" name="submit" value="Send"></td>
</tr>
</table></td>
</tr>
</table>
</form>

Have Fun

Related Blog Entries

Comments

Archives By Subject

Advertising (4) [RSS]
Blog Design (1) [RSS]
CFProject Scripts (2) [RSS]
Coldfusion Charts (3) [RSS]
Coldfusion Functions (5) [RSS]
Coldfusion Overview (1) [RSS]
Coldfusion Tutorials (17) [RSS]
For Sale (2) [RSS]
Image Manipulation (1) [RSS]
Java (1) [RSS]
JavaScript (4) [RSS]
PHP (1) [RSS]
Railo (2) [RSS]
SQL (1) [RSS]
Useful Tools (3) [RSS]

Recent Comments

Hashing Passwords
Richard Lord said: You really need to salt the passwords before hashing them, otherwise you're exposed to a rainbow-tab... [More]

Using Railo and Resin with domain aliases
Jonny Shaw said: Thanks for that. I am already using <host regexp="(.+)"> <host-name>${ho... [More]

Using Railo and Resin with domain aliases
Gert Franz said: Hi Jonny, it's even easier if you do it this way: <host id="allmead.com" root-dir... [More]

BlogCFC on Railo
Jonny Shaw said: Thanks for the advice ray. I was surprised at how easily BlogCFC went accross to to railo. The onl... [More]

BlogCFC on Railo
Gert Franz said: Congratulations to your new websites. I just wanted to add two things: CFIMAGE and CFCHART are suppo... [More]

Recent Entries

No recent entries.
ColdFusion Blog | ColdFusion Hosting | ColdFusion Q & A