Problems validating this form

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.

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;

sorry can you explain in more detail what I need to change to make this work? thanks

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;

    }

  }


http://www5.brinkster.com/hiflyer/jscript/formchecktest.htm

changed that but still no validation erros being given

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>&nbsp;</td>
            <td>&nbsp;</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>

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)

cheers, also is it necessary to use “” or null to identify empty fields, or can i just use one of them?

no harm in using both. It covers different eventualities.

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. :wink:

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 :slight_smile: