Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

empty field validation not working

Status
Not open for further replies.

london01

Technical User
Jun 13, 2004
18
CA
hello everybody,
the following javascript should check each field as user passes through but its not working.
anyideas?
all i want to do at this time is check to see if the field is empty or not as client moves on to the next field.

<html>
<head>
<title> field test </title>
<head>
<script Language="JavaScript">

function checkfield(entered, alertbox)
{ with (entered)
{ if (value==null || value=="")
{ if (alertbox!="")
{ alert(alertbox);
}
return false;
}
else
{ return true;
}
}
}
</script>
</head>
<body>

<form method="post" action="test2.html">
<p>First name: <input type="text" name="fname" size="32" value="" onChange = "checkfield(this, 'required field');"><br>

Last name: <input name="lname" type="text" size="32" value ="" onChange = "checkfield(this, 'required field');"><br>

<p><input type="submit" value="Submit" name="submit"></p>
</form>

</body>
</html>
 
Your indentation is confusing the bejeezus out of me.....

Anyway, if you want to check for an empty field as someone moves onto the next field, then onchange will not be a sufficient handler. Onchange only checks if the previous value is different from the current value of the text field. If someone enters an empty text field, then moves on to the next text field without typing anything, then the function will not be evaluated. Try something like this:
I simplified your validation check as well
Code:
<html>
<head>
<title> field test </title>
<head>
<script Language="JavaScript">

function checkfield(entered, alertbox) {
   if (entered.value==null || entered.value=="") {
      alert(alertbox);
      return false;
   }
   return true;
} 
</script>
</head>
<body>

<form method="post" action="test2.html">
  <p>First name: <input type="text" name="fname" size="32" value="" onBlur = "checkfield(this, 'required field');"><br>

Last name: <input name="lname" type="text" size="32" value ="" onBlur = "checkfield(this, 'required field');"><br>
 
<p><input type="submit" value="Submit" name="submit"></p>
</form>  

</body>
</html>
I took out the if statement for the alertbox, I'm assuming you're gonna want it to throw an error every time the field is empty, so there's no sense in checking to make sure you didn't send an error message.

Also, I used onBlur as the event handler for the text fields, but a better solution would be to wait until the user submits the page and check all the fields at once (using an onsubmit handler on the form tag), it's a bit less intrusive.

-kaht

banghead.gif
 
Thanks,

It worked and will look at your recomendation regarding, implementing the check at the submit button.

Robin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top