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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

alert/dialog boxes

Status
Not open for further replies.

Efa

Programmer
Jul 9, 2001
18
0
0
AU
Does anyone have any basic code on javascript alert boxes or dialog boxes within a servlet.

I have a html form embedded in my servlet doGet method. If the user fails to fill in a textfields i want to alert them with an alert/dialog box "Some fields have been left blank". Etc.

Please help.

Thanks

Efa
 
What you need to do is have a validate function that gets run before the data is submitted. To do this a validate function must be written in Javascript. Like the following

<Script language=&quot;Javascript&quot;>
function isEmpty(field)
{
var contents = field.value;
if (contents.length < 1)
{
return true;
}
else
return false;
}//end isEmpty

function validate(form){
if(isEmpty(form.txtName))
{
alert(&quot;The Name field requires a value!&quot;)
form.txtName.setFocus
return true;
}
else
{
return false;
}
}
</Script>

Then with in the form tag in the html place the following

<form name=&quot;test&quot; action=&quot;destinationform.jsp&quot; method=&quot;post&quot; onSubmit=&quot;return validate(this)&quot;>

<input name=&quot;txtName&quot; type=&quot;Text&quot; Value=&quot;&quot;>

</form>

Now whenever this form is submitted the validate function is run. If the textbox txtName is empty an alert message will be shown and the form will not be submitted, otherwise it will be submitted as normal.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top