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

Form Validation on Submit

Status
Not open for further replies.

pwinters

Programmer
Sep 12, 2002
34
Hi,

I am trying to validate a form when the user hits the submit button. Currently, when a field is left blank, an error message pops up to instruct the user to enter something in the blank field, but when you click Ok on the error message, it processes the submit (which processes a query). I want the user to be able to go back and update the field before the query is processed. Can anyone help me with this?

Here is my validation code...

<script language=&quot;JavaScript&quot;>
<!-- hide this script from non-javascript-enabled browsers




function validateData()
{

var AddProjForm = new Object();
AddProjForm = document.addproj;



if (txtCheckPrintable(AddProjForm.initnum, true, &quot;Initiative Number Missing. Error: &quot;) &&
txtCheckPrintable(AddProjForm.projname, true, &quot;Project Name Missing. Error: &quot;))
{
if (AddProjForm.relmonth.options[AddProjForm.relmonth.selectedIndex].value == &quot;&quot;)
{
alert(&quot;Must select a Release&quot;);
AddProjForm.relmonth.focus();
return false;
}
{
if (AddProjForm.primstatus.options[AddProjForm.primstatus.selectedIndex].value == &quot;&quot;)
{
alert(&quot;Must select a Primary Status&quot;);
AddProjForm.primstatus.focus();
return false;
}
{
if (AddProjForm.projstage.options[AddProjForm.projstage.selectedIndex].value == &quot;&quot;)
{
alert(&quot;Must select a Project Stage&quot;);
AddProjForm.projstage.focus();
return false;
}
{
if (AddProjForm.spmid.options[AddProjForm.spmid.selectedIndex].value == &quot;&quot;)
{
alert(&quot;Must select a SPM ID&quot;);
AddProjForm.spmid.focus();
return false;
}
{
if (AddProjForm.spin.options[AddProjForm.spin.selectedIndex].value == &quot;&quot;)
{
alert(&quot;Must select a Filespin Option&quot;);
AddProjForm.spin.focus();
return false;
}
{
if (AddProjForm.adjstatus.options[AddProjForm.adjstatus.selectedIndex].value == &quot;&quot;)
{
alert(&quot;Must select an Adjustment Status&quot;);
AddProjForm.adjstatus.focus();
return false;
}
}
}
}
}
}
}


return true;
{
return false;
}
}



// -->
</script>


And this is in my form:

<form method=&quot;post&quot; name=&quot;addproj&quot; action=&quot;../handlers/addproj_hdlr.jsp&quot; onSubmit=&quot;return validateData();&quot;>


THANKS
 
to simplify I copied the last if statement. you need to get rid of the last return false and use a else before the return true like this
if (AddProjForm.adjstatus.options[AddProjForm.adjstatus.selectedIndex].value == &quot;&quot;)
{
alert(&quot;Must select an Adjustment Status&quot;);
AddProjForm.adjstatus.focus();
return false;
}
else
{
return true;
}
if that is not the corrective action then your logic in the if statements is incorrect and jsut take a closer look at if what your code says is what you really want.

also why do you have this
AddProjForm
when the name of the form is just
AddProj
all that should be referenced is the name A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
sorry just saw these lines
var AddProjForm = new Object();
AddProjForm = document.addproj;
A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
onpnt,

Thanks so much for your response.
I started from the bottom, and worked my way up. Every validation works until I get to the textboxes...

if (txtCheckPrintable(AddProjForm.initnum, true, &quot;Initiative Number Missing. Error: &quot;) &&
txtCheckPrintable(AddProjForm.projname, true, &quot;Project Name Missing. Error: &quot;))


The error message is popping up, but it doesn't allow me to edit the field before submitting the form.

Here's the code:

<script language=&quot;JavaScript&quot;>
<!-- hide this script from non-javascript-enabled browsers

function validateData()
{
var AddProjForm = new Object();
AddProjForm = document.addproj;

if (txtCheckPrintable(ModProjForm.initnum, true, &quot;Initiative Number Missing. Error: &quot;))

else
{
return true;
}
}

// -->
</script>

Is there a different way to validate text boxes?
Also, is there an easier way to do this? Maybe by having just one popup box listing all the fields missing input?

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top