drtanz
March 28, 2010, 11:35pm
1
Im having problems getting this form to validate using the js functions im using, can anyone tell me what im doing wrong? http://myjs.pastebin.com/kjckY3My
When i click submit (with empty fields) it just goes to the form processing file, without displaying any errors as its supposed to.
force
March 28, 2010, 11:53pm
2
You need to use something like this to get the field and the value from the field.
var theField = document.getElementById(fieldId);
var theValue = theField.value;
drtanz
March 28, 2010, 11:57pm
3
sorry can you explain in more detail what I need to change to make this work? thanks
force
March 29, 2010, 12:11am
4
Try this
function validate_required(field,alerttxt)
{
var theField = document.getElementById(field);
var fieldVal = theField.value;
if (fieldVal==null||fieldVal=="")
{
alert(alerttxt);return false;
}
else
{
return true;
}
}
drtanz
March 29, 2010, 9:58am
6
changed that but still no validation erros being given
drtanz
March 29, 2010, 10:49am
7
I’ve changed the code around using a more straightforward method, still cant get it to work though
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Guestbook</title>
<script type="text/javascript">
function checkform()
{
if (nickname==""||nickname==null)
{
// something is wrong
alert('Please fill in your Nickname');
return false;
}
else if (comment==""||comment==null)
{
// something else is wrong
alert('Please fill in the Comment field');
return false;
}
return true;
}
</script>
</head>
<body>
<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong>Sign Guestbook </strong></td>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form id="form1" name="form1" method="post" action="addguestbook.php" onsubmit="return checkform()">
<td><table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td width="117">Nickname</td>
<td width="14">:</td>
<td width="357"><input name="nickname" type="text" id="nickname" size="40" /></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td><textarea name="comment" cols="40" rows="3" id="comment"></textarea></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" />
<input type="reset" name="Submit2" value="Reset" /></td>
</tr>
</table></td>
</form>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong><a href="viewguestbook.php">View Guestbook</a> </strong></td>
</tr>
</table>
</body>
</html>
SpikeZ
March 29, 2010, 11:28am
8
You are missing one vital component.
The script doesn’t know what nickname is so it cant compare it to anything
if (nickname==""||nickname==null)
if you dont tell it where nickname is coming from and what it is, then it cant do anything with it.
if (document.form1.nickname.value ==""||document.form1.nickname.value ==null)
drtanz
March 29, 2010, 11:30am
9
cheers, also is it necessary to use “” or null to identify empty fields, or can i just use one of them?
SpikeZ
March 29, 2010, 12:20pm
10
no harm in using both. It covers different eventualities.
drtanz
March 29, 2010, 12:38pm
11
but aren’t they referring to the same thing?
Not necessarily - a variable defined using var will be null but not an empty string.
BTW - I wasn’t suggesting you use my code exactly; it was intended to give you ideas.
drtanz
March 29, 2010, 8:22pm
13
ok i see, so if i define a var and then not use it in the form it will remain null, but if i define it and use it in the form but leave it blank it will be a blank string.
yes i got that you were just giving me a clue, am slow at times