After following a tutorial, my client side form validation didnt work. As in nothing. It’s like I never wrote any validation code. :S I don’t know what I did wrong…
Here:
[code=php]
<script language=”JavaScript” type=”text/javascript”>
function checkForm()
{
var cby, cproduct, cprice, cdesc, cname, ccontact;
with(window.document.post)
{
cby = by;
cproduct = product;
cprice = price;
cdesc = desc;
cname = name;
ccontact = contact;
}
if(trim(cby.value) == ”)
{
alert(‘Indicate whether you are Buying or Selling’);
cby.focus();
return false;
}
else if(trim(cproduct.value) == ”)
{
alert(‘Please Enter the Product Name’);
cproduct.focus();
return false;
}
else if(trim(cprice.value) == ”)
{
alert(‘Please indicate the Price of the product’);
cprice.focus();
return false;
}
else if(trim(cdesc.value) == ”)
{
alert(‘Please enter a short description about the product’);
cdesc.focus();
return false;
}
else if(trim(cname.value) == ”)
{
alert(‘Please enter the name of the person to be contacted’);
cname.focus();
return false;
}
else if(trim(ccontact.value) == ”)
{
alert(‘Please enter the contact details, such as: cell number, email add, etc.’);
ccontact.focus();
return false;
}
}
function trim(str)
{
return str.replace(/^s+|s+$/g,”);
}
</script>
…
<form action=”post.php” method=”post” name=”post”>
<table cellpadding=”10″ cellspacing=”0″ border=”0″>
<tr valign=”top”>
<td width=”150″ align=”left”>
Are you Buying or Selling?
</td>
<td align=”left”>
<input type=”radio” name=”by” value=”buying” />Buying <input type=”radio” name=”by” value=”selling” />Selling
</td>
</tr>
<tr>
<td align=”left”>
Product Name:
</td>
<td align=”left”>
<input type=”text” name=”product” size=”50″ maxlength=”99″ />
</td>
</tr>
<tr>
<td align=”left”>
Price:
</td>
<td align=”left”>
<input type=”text” name=”price” size=”10″ maxlength=”11″ /> (In Philippines Pesos)
</td>
</tr>
<tr valign=”top”>
<td align=”left”>
Short Description:
</td>
<td align=”left”>
<textarea class=”input” name=”desc” rows=”3″ cols=”40″></textarea>
</td>
</tr>
<tr>
<td align=”left”>
Contact Person:
</td>
<td align=”left”>
<input type=”text” name=”name” size=”50″ maxlength=”99″ />
</td>
</tr>
<tr valign=”top”>
<td align=”left”>
Contact Details:
</td>
<td align=”left”>
<input type=”text” name=”contact” size=”50″ maxlength=”99″ /><br />(e.g. cell number, email add, etc.)<br />
</td>
</tr>
<tr>
<td align=”left”>
</td>
<td align=”left”>
<input class=”formbtn” type=”submit” value=”SUBMIT AD” name=”sub” onclick=”return checkform();” />
</td>
</tr>
</table>
</form>