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

form validation

Status
Not open for further replies.

ianjenn

Programmer
Sep 25, 2001
14
GB
I don't know if anyone could have a quick look at the validation below as I have spent hours trying to get it to work without success. The general gist is that there is 5 text boxes that have various checks before submitting. The field ward checks the entry against and array. There is two submit buttons and depending on which is selected the form is sent to different locations. It works if I use each validation separately but only partially works when combined. I suspect I have made a school boy error somewhere. Can anyone double check my logic.
thanks

<HTML>
<HEAD>

<TITLE>New Patient</TITLE>

<script>
<!--
function setFocus() {
document.Newpatient.Ward.focus();
}
//-->
</script>
<SCRIPT>
function submitFunction(i)
{
var titles = new Array("A1", "A2", "A3", "A4");
var titleValid = false;
var theTitle = document.Newpatient.Ward.value.toUpperCase();
for(var x=0; x<titles.length; x++)
{
if(theTitle == titles[x])
{
titleValid = true;
}
}
var returnValue = false;

if(!titleValid)
{
alert("That wasn't a valid ward please try again");
document.Newpatient.Ward.focus();
}
else if (document.Newpatient.Ward.value == "")
{
alert("please enter a value for ward");
document.Newpatient.Ward.focus();
}
else if(document.Newpatient.Item.value < 0)
{
alert("please enter a value greater than zero");
document.Newpatient.Item.focus();
}
else if(document.Newpatient.Item.value == "")
{
alert("please enter a value ");
document.Newpatient.Item.focus();
}
else if(document.Newpatient.fname.value == "")
{
alert("please enter a first name");
document.Newpatient.fname.focus();
}
else if(document.Newpatient.Surname.value == "")
{
alert("please enter a surname");
document.Newpatient.Surname.focus();
}
else if(document.Newpatient.Item.value >= 25)
{
alert("maximum number of items allowed is 25");
document.Newpatient.Item.focus();
}
else returnValue = true;
{
return returnValue;
}
if (i==1) document.Newpatient.action=
"if (i==2) document.Newpatient.action=
"wrpatupdate.asp";;
document.Newpatient.submit()
}

</SCRIPT>

</HEAD>
<BODY onLoad="setFocus()">
<P ALIGN="center">Enter Prescription Details<BR>
<CENTER>



<FORM ACTION="METHOD="POST"
NAME=Newpatient
onclick="return Newpatient_onsubmit()">

<TABLE BORDER=0 WIDTH="553">

<TABLE BORDER=0 WIDTH="553">
<TR>
</CENTER>

<TD WIDTH="164">
<P ALIGN="right"><INPUT TYPE="CHECKBOX" Name="pharwrit" value="ON"><FONT SIZE="1">Tick if written by pharmacist</FONT></P>
</TD>
</TR>
<CENTER>


<TR>

<TD WIDTH="161" ALIGN="right">Ward:</TD>
<TD WIDTH="328" COLSPAN="2"><INPUT NAME="Ward" SIZE="6" tabindex="1">
<FONT SIZE="1"> i.e For &quot;urology&quot; enter &quot;a2&quot;, &quot;neonates&quot; enter
&quot;SCB&quot;</FONT></TD>
</TR>
<TR>

<TD WIDTH="161" ALIGN="right">First Name:</TD>
<TD WIDTH="328" COLSPAN="2"><INPUT NAME="fname" Value="<%=fname%>"
SIZE="21" tabindex="2"></TD>
</TR>
<TR>
<TD WIDTH="161" ALIGN="right">Surname:</TD>
<TD WIDTH="328" COLSPAN="2"><INPUT NAME="Surname" Value="<%=surname%>"
SIZE="26" tabindex="3"></TD>
</TR>
<TR>
<TD WIDTH="161" ALIGN="right">Items:</TD>
<TD WIDTH="328" COLSPAN="2"><INPUT NAME="Item"
SIZE="4" tabindex="4"></TD>
</TR>

<TR>

<TD WIDTH="161" ALIGN="right">Notes:</TD>
<TD WIDTH="328" COLSPAN="2"><INPUT NAME="note"
SIZE="40" tabindex="7"><FONT SIZE="1"> &nbsp;<BR>
Field can be left blank</FONT></TD>
</TR>
<TR>
<TD WIDTH="161"><input type="checkbox" name="dispward" value="ON"><font size="1">Tick
if dispensed on ward</font></TD>
<TD ALIGN=CENTER WIDTH="328" COLSPAN="2"><BR>
&nbsp;&nbsp;&nbsp;
<INPUT TYPE="button" VALUE="with check" onClick="submitFunction(1)" tabindex="5">
<INPUT TYPE="button" VALUE="no check" onClick="submitFunction(2)" tabindex="6"> <INPUT TYPE="RESET"></TD>
</TR>
</TABLE>
</FORM>

<HR>
<H3><A HREF="menumain.asp"><FONT SIZE="2">Back to Menu</FONT></A></H3>
</CENTER>


</HTML>
 
Just changing along the way, not touching the big structure.

[1] submitFunction(i)
[tt]
function submitFunction(i)
{
var titles = new Array("A1", "A2", "A3", "A4");
var titleValid = false;
var theTitle = document.Newpatient.Ward.value.toUpperCase();
for(var x=0; x<titles.length; x++)
{
if(theTitle == titles[x])
{
titleValid = true;
[blue]break;[/blue] //no need to go further
}
}
var returnValue = false;

if(!titleValid)
{
if (document.Newpatient.Ward.value == "") //it would necessarily be the case titleValid false
alert("please enter a value for ward");
else
alert("That wasn't a valid ward please try again");
document.Newpatient.Ward.focus();
[blue]//better to give a short timeout for the focus()
//similarily for all other cases, up to you
//setTimeout("document.Newpatient.Ward.focus();",50);[/blue]
}
else if(document.Newpatient.Item.value < 0)
{
alert("please enter a value greater than zero");
document.Newpatient.Item.focus();
}
else if(document.Newpatient.Item.value == "")
{
alert("please enter a value ");
document.Newpatient.Item.focus();
}
else if(document.Newpatient.fname.value == "")
{
alert("please enter a first name");
document.Newpatient.fname.focus();
}
else if(document.Newpatient.Surname.value == "")
{
alert("please enter a surname");
document.Newpatient.Surname.focus();
}
else if(document.Newpatient.Item.value >= 25)
{
//if 25 is allowed, why >=25???
alert("maximum number of items allowed is 25");
document.Newpatient.Item.focus();
}
else returnValue = true;
[blue]if (!returnValue)[/blue]
{
return returnValue;
}
if (i==1) document.Newpatient.action = " if (i==2) document.Newpatient.action="wrpatupdate.asp";
document.Newpatient.submit()
}
[/tt]
[2] And then the action and onclick of the form should be eliminated. Why is there an action at all if you want to control whereto by the submitFunction(i)?
[tt]
<FORM METHOD="POST" NAME=Newpatient>
[/tt]
 
I'd make the Ward an SELECT/OPTION control so you don't have to do any more than check for not blank.
 
thanks tsuji,
it was the onclick that was calling the problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top