Find jobs - US Business Directory - Debt Consolidation - Paintball guns - Submit articles

PDA

View Full Version : need validation code for contact form


creative_007
Feb 19th 2007, 1:28 am
Hi,
I am thinking of keeping a contact/feedback form on my website and am in search of a validation code for the sections.
I have found an e-mail validation at w3schools but since i am a complete noob at javascript, i am unable to expand it to other sections.
If anybody can tell me where can i find the codes, or even examples of how to expand it, i will be pretty grateful

designcode
Feb 19th 2007, 1:40 am
Post your HTML form here and I will write a javascript for you :D

creative_007
Feb 19th 2007, 2:34 am
Designcode, thanks for your help..here's the code

<html>
<head>
<title>Contact Form</title>

</head>
<body>
<form action="http://www.yoursitename.com/cgi-sys/formmail.pl" name="myform" >
<input type="hidden" name="recipient" value="email">
<input type="hidden" name="subject" value="FormMail E-Mail">
<table cellspacing="2" cellpadding="2" border="0">
<tr>
<td align="left">Name:
<input type="text" name="Name" size="50"></td>
</tr>
<tr>
<td align="left">EMail:
<input type="text" name="Email" size="50"></td>
</tr>
<tr>
<td align="left">Feedback</td></tr>
<tr>
<td><textarea cols="50" rows="10" name="Feedback"></textarea></td>
</tr>
<tr>
<td align="left">
<input type="submit" value="Submit"><input type="reset" value="Reset">
<input type="hidden" name="redirect" value="thankyou.html"></td>
</tr>
</table>
</form>

designcode
Feb 19th 2007, 3:55 am
Here is your code with JS validation :D


<html>
<head>
<title>Contact Form</title>

</head>
<body>
<script type="text/javascript">

function validateForm()
{
var f = document.myform;
if(f.Name.value == "")
{
alert("Please enter your name");
f.Name.focus();
return false;
}
else if(!checkEmail(f.Email.value))
{
alert("Please enter your email address");
f.Email.focus();
return false;
}
else if(f.Feedback.value == "")
{
alert("Please enter some message");
f.Feedback.focus();
return false;
}
}

function checkEmail(str)
{
// Function from SmartWebby.com, modified by designcode
if(str == "")
return;

var at="@"
var dot="."
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)
if (str.indexOf(at)==-1)
return false;

if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
return false;

if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
return false;

if (str.indexOf(at,(lat+1))!=-1)
return false;

if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
return false;

if (str.indexOf(dot,(lat+2))==-1)
return false;

if (str.indexOf(" ")!=-1)
return false;

return true;
}
</script>
<form action="http://www.yoursitename.com/cgi-sys/formmail.pl" onsubmit="return validateForm();" name="myform" >
<input type="hidden" name="recipient" value="email">
<input type="hidden" name="subject" value="FormMail E-Mail">
<table cellspacing="2" cellpadding="2" border="0">
<tr>
<td align="left">Name:
<input type="text" name="Name" size="50"></td>
</tr>
<tr>
<td align="left">EMail:
<input type="text" name="Email" size="50"></td>
</tr>
<tr>
<td align="left">Feedback</td></tr>
<tr>
<td><textarea cols="50" rows="10" name="Feedback"></textarea></td>
</tr>
<tr>
<td align="left">
<input type="submit" value="Submit"><input type="reset" value="Reset">
<input type="hidden" name="redirect" value="thankyou.html"></td>
</tr>
</table>
</form>

creative_007
Feb 19th 2007, 4:21 am
Designcode, Thanks so much for your prompt help..I am very grateful.

designcode
Feb 19th 2007, 11:45 am
Please add to my reputation :D and I will be grateful to you :D