/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Validate Select tag (drop down box)

Gang,

I have 3 html pages that use the same validation code. Two of three pages work fine. For some strange reason the third html page will not execute the validation code. Also, there are no form tags inside of any form tags.

The ERROR I get is ‘form2’ is undefined

Here is the code:

[CODE]
<script language=”JavaScript”>

function Validate(form2)

{
//alert(document.form2.PegNumberDrawn.value);
if(document.form2.PegNumberDrawn.value = ”)
{

alert(“Please choose the Peg Number Drawn for this angler.”);
document.form2.PegNumberDrawn.focus();
return false;
}

else
{
document.form2.submit();
}

}

</script>
[/CODE]

to post a comment
JavaScript

19 Comments(s)

Copy linkTweet thisAlerts:
@FangJan 20.2009 — Why a [I]form2[/I] variable which is not used and confusing the browser; remove it.
Copy linkTweet thisAlerts:
@ASPSQLVBauthorJan 20.2009 — [CODE]
<script language="JavaScript">

function Validate()

{
//alert(document.form2.PegNumberDrawn.value);
if(document.form2.PegNumberDrawn.value = '')
{

alert("Please choose the Peg Number Drawn for this angler.");
document.form2.PegNumberDrawn.focus();
return false;
}

else
{
document.form2.submit();
}

}

</script>
[/CODE]


Still the same error.
Copy linkTweet thisAlerts:
@FangJan 20.2009 — and the name form2 exists?
Copy linkTweet thisAlerts:
@ASPSQLVBauthorJan 20.2009 — Yes.

[CODE]
<form method="post" action="AssignPegNumberDrawnUpdate_Second.asp" name="form2">
<table>
<tr>
<td>
<input type="hidden" value="<&#37;=RSCustomers("CustomerId")%>" name="CustomerId">
<input type="hidden" value="<%=FishId%>" name="FishId">
<input type="hidden" value="Second" name="DatabaseNumber">
<font color="white" size="5"><u><b>Peg Number Drawn<b></u></font>
<br>
<select NAME="PegNumberDrawn" SIZE="1">
<option VALUE="<%=PegNumberDrawn%>"><%=PegNumberDrawn%>
<option VALUE="0">0
<option VALUE="1">1
<option VALUE="2">2
<option VALUE="3">3
<option VALUE="4">4
<option VALUE="5">5
<option VALUE="6">6
<option VALUE="7">7
<option VALUE="8">8
<option VALUE="9">9
<option VALUE="10">10

</select>

</td>

</tr>
</table>

<input type="BUTTON" onclick="Validate(form2);return false;" value="Update Peg" name="submit1" align="left">
</form>
[/CODE]
Copy linkTweet thisAlerts:
@FangJan 20.2009 — function Validate()

<i> </i>{
<i> </i> var sel=document.form2.PegNumberDrawn;
<i> </i> if(sel.options[sel.selectedIndex].value == '')
<i> </i> {

<i> </i> alert("Please choose the Peg Number Drawn for this angler.");
<i> </i> document.form2.PegNumberDrawn.focus();
<i> </i> return false;
<i> </i>}

<i> </i> else
<i> </i>{
<i> </i>document.form2.submit();
<i> </i> }

<i> </i> }

&lt;input type="BUTTON" onclick="return Validate(form2);" value="Update Peg" name="submit1" align="left"&gt;
Copy linkTweet thisAlerts:
@ASPSQLVBauthorJan 20.2009 — Still the same ERROR.?
Copy linkTweet thisAlerts:
@FangJan 20.2009 — This doesn't give an error. Is the server side code correct?&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;title&gt;&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;

&lt;script type="text/javascript"&gt;
function Validate()

<i> </i>{
<i> </i> var sel=document.form2.PegNumberDrawn;
<i> </i> if(sel.options[sel.selectedIndex].value == '')
<i> </i> {

<i> </i> alert("Please choose the Peg Number Drawn for this angler.");
<i> </i> document.form2.PegNumberDrawn.focus();
<i> </i> return false;
<i> </i>}

<i> </i> else
<i> </i>{
<i> </i>document.form2.submit();
<i> </i> }

<i> </i> }
&lt;/script&gt;

&lt;style type="text/css"&gt;
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;form method="post" action="AssignPegNumberDrawnUpdate_Second.asp" name="form2"&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;input type="hidden" value="&lt;&amp;#37;=RSCustomers("CustomerId")%&gt;" name="CustomerId"&gt;
&lt;input type="hidden" value="&lt;%=FishId%&gt;" name="FishId"&gt;
&lt;input type="hidden" value="Second" name="DatabaseNumber"&gt;
&lt;font color="white" size="5"&gt;&lt;u&gt;&lt;b&gt;Peg Number Drawn&lt;b&gt;&lt;/u&gt;&lt;/font&gt;
&lt;br&gt;
&lt;select NAME="PegNumberDrawn" SIZE="1"&gt;
&lt;option VALUE=""&gt;PegNumberDrawn
&lt;option VALUE="0"&gt;0
&lt;option VALUE="1"&gt;1
&lt;option VALUE="2"&gt;2
&lt;option VALUE="3"&gt;3
&lt;option VALUE="4"&gt;4
&lt;option VALUE="5"&gt;5
&lt;option VALUE="6"&gt;6
&lt;option VALUE="7"&gt;7
&lt;option VALUE="8"&gt;8
&lt;option VALUE="9"&gt;9
&lt;option VALUE="10"&gt;10

&lt;/select&gt;

<i> </i>&lt;/td&gt;

&lt;/tr&gt;
&lt;/table&gt; <br/>
&lt;input type="BUTTON" onclick="return Validate();" value="Update Peg" name="submit1" align="left"&gt;
&lt;/form&gt;&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@ASPSQLVBauthorJan 20.2009 — Yes. I copied and pasted the code from the working two HTML pages. I looked it over countless times.

Ive experienced this sort of problem before with the validation code of radio buttons.
Copy linkTweet thisAlerts:
@beau_kangJan 20.2009 — Just a small question...

Why don't you put an id on your select tag and do a document.getElementById('PegNumberDrawn') for your conditions? Then you wouldn't have to worry about the form being in your js at all.
Copy linkTweet thisAlerts:
@ASPSQLVBauthorJan 20.2009 — How would that appear?
Copy linkTweet thisAlerts:
@ASPSQLVBauthorJan 21.2009 — I think I figured out why the code will not execute.

I have another form at the top of the page within the body tag. When i remove that the code will execute. But, thats not gonna help the situation because I need that form tag also.

The two must coexist.
Copy linkTweet thisAlerts:
@ASPSQLVBauthorJan 21.2009 — I put the onsubmit="Validate()" in the form2 tag and changed the input type="button" to input type="submit".

When I click on the submit button the form2 action is not executed but instead the form1 action is executed taking me to the wrong destination page.

This is confusing me becuase the form tags do not overlap.....they are both confined to themselves.
Copy linkTweet thisAlerts:
@FangJan 21.2009 — The first form won't cause a problem unless it has the same name.

Running a JavaScript validation function is best attached to the form tag rather than a button.
Copy linkTweet thisAlerts:
@ASPSQLVBauthorJan 21.2009 — There is only two form tags in the HTML document. They have different names. The first form name is "FORM1" and the second form name is "FORM2".

In the Javascript validation code, the program I am using recognizes form2 as a document object. But I still get the error [B]"FORM2 is UNDEFINED"[/B]



[CODE] <script language="JavaScript">

function Validate(form2)

{

if(document.form2.PegNumberDrawn.value == '')
{
alert("Please choose the Peg Number Drawn for this angler.");
document.form2.PegNumberDrawn.focus();
return false;
}

else
{
document.form2.submit();
}

}

</script>
[/CODE]


[CODE]
<form method="post" action="AssignPegNumberDrawnUpdate_Second.asp" name="form2" onsubmit="return Validate(document.form2.PegNumberDrawn.value);">
<table>
<tr>
<td>
<input type="hidden" value="<%=RSCustomers("CustomerId")%>" name="CustomerId">
<input type="hidden" value="<%=FishId%>" name="FishId">
<input type="hidden" value="First" name="DatabaseNumber">
<font color="white" size="5"><u><b>Peg Number Drawn<b></u></font>
<br>
<select NAME="PegNumberDrawn" SIZE="1">
<option VALUE="<%=PegNumberDrawn%>"><%=PegNumberDrawn%>
<option VALUE="0">0
<option VALUE="1">1
<option VALUE="2">2
<option VALUE="3">3
<option VALUE="4">4
<option VALUE="5">5
<option VALUE="6">6
<option VALUE="7">7
<option VALUE="8">8
<option VALUE="9">9
<option VALUE="10">10

</select>

</td>

</tr>
</table>

<input type="BUTTON" onclick="Validate(form2);return false;" value="Update Peg" name="submit1" align="left">
</form>
[/CODE]
Copy linkTweet thisAlerts:
@FangJan 21.2009 — The input button causes an accessibility problem; no JavaScript, no form submission.

Use a submit button and pass the form in Validate in the form tag:&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;title&gt;&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;

&lt;script type="text/javascript"&gt;
function Validate(f)

<i> </i>{
<i> </i> var sel=f.PegNumberDrawn;
<i> </i> if(sel.options[sel.selectedIndex].value == '')
<i> </i> {

<i> </i> alert("Please choose the Peg Number Drawn for this angler.");
<i> </i> f.PegNumberDrawn.focus();
<i> </i> return false;
<i> </i>}
<i> </i>return true;
<i> </i> }
&lt;/script&gt;

&lt;style type="text/css"&gt;
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;form method="post" action="AssignPegNumberDrawnUpdate_Second.asp" name="form2" onsubmit="return Validate(this);"&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;input type="hidden" value="&lt;&amp;#37;=RSCustomers("CustomerId")%&gt;" name="CustomerId"&gt;
&lt;input type="hidden" value="&lt;%=FishId%&gt;" name="FishId"&gt;
&lt;input type="hidden" value="Second" name="DatabaseNumber"&gt;
&lt;font color="white" size="5"&gt;&lt;u&gt;&lt;b&gt;Peg Number Drawn&lt;b&gt;&lt;/u&gt;&lt;/font&gt;
&lt;br&gt;
&lt;select NAME="PegNumberDrawn" SIZE="1"&gt;
&lt;option VALUE=""&gt;PegNumberDrawn
&lt;option VALUE="0"&gt;0
&lt;option VALUE="1"&gt;1
&lt;option VALUE="2"&gt;2
&lt;option VALUE="3"&gt;3
&lt;option VALUE="4"&gt;4
&lt;option VALUE="5"&gt;5
&lt;option VALUE="6"&gt;6
&lt;option VALUE="7"&gt;7
&lt;option VALUE="8"&gt;8
&lt;option VALUE="9"&gt;9
&lt;option VALUE="10"&gt;10

&lt;/select&gt;

<i> </i>&lt;/td&gt;

&lt;/tr&gt;
&lt;/table&gt; <br/>
&lt;button type="submit"value="Update Peg" name="submit1"&gt;Update Peg&lt;/button&gt;
&lt;/form&gt;&lt;/body&gt;
&lt;/html&gt;
If you still get undefined then there is a problem with the html, validate the document.
Copy linkTweet thisAlerts:
@ASPSQLVBauthorJan 21.2009 — The validation is not working. Also, the onsubmit is sending me to the web page in the action of the first form tag.
Copy linkTweet thisAlerts:
@FangJan 21.2009 — I need to see the full document to find the error
Copy linkTweet thisAlerts:
@ASPSQLVBauthorJan 21.2009 — May I send you a PM with the attachment ?.....
Copy linkTweet thisAlerts:
@FangJan 21.2009 — No problem
×

Success!

Help @ASPSQLVB spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...