Forum Moderators: open

Message Too Old, No Replies

validating a form

debugging

         

ianb

2:21 pm on May 11, 2005 (gmt 0)

10+ Year Member



hi, i've written a form to send an email and the javascript to validate the fields. for some reason the form won't perform the validation on it. i've been looking at this code for a while and am not finding the problem. can anyone help?
------------------------------------------------
logproblem.php

<form action="logproblem_success.php" method="POST" name="logproblem" id="logproblem" onSubmit = "return checkLogProblemFormValid(this)" >
<table width="95%" border="0" cellspacing="2" cellpadding="5">
<tr>
<td scope="col"> <div align="left">Name</div></td>
<th scope="col">
<div align="left">
<input name="name" type="text" id="name">
*</div></th>
</tr>
<tr>
<td><div align="left">Company</div></td>
<td>
<div align="left">
<input name="company" type="text" id="company">
*</div></td>
</tr>
<tr>
<td>Email:</td>
<td><input name="email" type="text" id="email">
*</td>
</tr>
<tr>
<td><div align="left">Location</div></td>
<td>
<div align="left">
<input name="location" type="text" id="location">
</div></td>
</tr>
<tr>
<td><div align="left">Contact number </div></td>
<td><div align="left">
<input name="contactnum" type="text" id="contactnum">
*</div></td>
</tr>
<tr>
<td><div align="left">type of support</div></td>
<td><div align="left">
<select name="typesupport" id="typesupport">
<option selected>--select type--</option>
<option value="steve.gordon">hardware</option>
<option value="ian.blevins">software</option>
</select>
*</div></td>
</tr>
<tr>
<td><div align="left">IP Address </div></td>
<td><div align="left">
<input name="ipaddress" type="text" id="ipaddress">
</div></td>
</tr>
<tr>
<td><div align="left">Description of problem </div></td>
<td>
<div align="left">
<textarea name="probdesc" id="probdesc"></textarea>
*</div></td>
</tr>
<tr>
<td><div align="left">Has it been solved before (who, how) </div></td>
<td><div align="left">
<textarea name="hsolved" id="hsolved"></textarea>
</div></td>
</tr>
<tr>
<td><div align="right">
<input type="reset" name="Reset" value="Clear form">
</div></td>
<td><input name="Submit" type="submit" id="submit" value="Submit"></td>
</tr>
</table>
</form>
------------------------------------------------
validatelogproblem.js

// JavaScript Document
<!--
function checkLogProblemFormValid()
{
var lgpformname = document.logproblem.name.value;
var lgpformcompany = document.logproblem.company.value;
var lgpformmail = document.logproblem.email.value;
var lgpformlocation = document.logproblem.location.value;
var lgpformcontactnum = document.logproblem.contactnum.value;
var lgpformtypesupport = document.logproblem.typesupport.value;
var lgpformipaddress = document.logproblem.ipaddress.value;
var lgpformprobdesc = document.logproblem.probdesc.value;
var lgpformhsolved = document.logproblem.hsolved.value;

//if the field is not completed then alert the user and return false so that the submit is stalled
//code to check name field
if (lgpformname == "")
{
alert ("You must enter a name");
event.returnValue = false;
}

if (lgpformcompany == "")
{
alert ("You must enter a name");
event.returnValue = false;
}

if (lgpformmail == "")
{
alert ("You must enter a name");
event.returnValue = false;
}

if (lgpformprobdesc == "")
{
alert ("You must enter a name");
event.returnValue = false;
}

if (document.logproblem.typesupport.value == 0 )
{
alert ( "Please select the type of support needed." );
valid = false;
}

}

-->

Bernard Marx

3:55 pm on May 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1) Don't use <!-- --> comments in a script file.

2) The onSubmit call passes a reference to the function. Use it.

3) Careful with names.
It's best not to use the names (or ids): 'submit', 'name'.
These will clash with the form's own property names.

4) event.returnValue IE-only. Don't use it, unless you are 100% IE environment.
- even then it's not necessary.

I messes around with the function to keep all the configurables in one place, and to combine all messages into a single alert.

*! It assumes that you have changed the name of field, 'name', to 'namefield'.

function checkLogProblemFormValid(form)
{
var messageStart = 'Please enter ';
var namesAndMessages =
{
namefield: 'a name.',
company: 'a company.',
email: 'an email address.',
location: 'a location.',
contactnum: 'a contact number.',
ipaddress: 'an IP address.',
probdesc: 'a problem description.'
};

var messages = [];

for(var name in namesAndMessages)
if(! form[name].value )
messages.push( messageStart + namesAndMessages[name]);

if (! form.typesupport.value)
messages.push( messageStart + namesAndMessages[name]);

if(messages.length>0)
alert(messages.join('\n'));

return messages.length==0;
}

ianb

4:08 pm on May 11, 2005 (gmt 0)

10+ Year Member



thanks alot! helped alot

all good now