Validate select

A

Alonso

Hello everybody! :-D

I am trying to validate a select object in a form. It does not work fine
in Explorer 6.0 because it always send the form, but in Netscape 7.0 it
works really fine. The code is below:

//----------------------------------------------------------------------
<head>
<script >
function validate()
{
if( document.results.p1.value=="1"
alert('Value not valid')
else
document.results.submit()

}
</script>
</head>

<FORM name=result action="page.php" method=post>
<TABLE cellSpacing=0 cellPadding=0 align=center>
.....
.....
<TR>
<TD align=center>
<select name=p1 id=p1>
<option selected>1</option>
<option>X</option>
<option>2</option>
</select>
</TD>
</TR>
.....
.....
.....
.....
<TR class="style">
<TD align=center>
<INPUT type=button value="Send" name=Submit onclick="validate()">
</TD>
</TR>
</table>
</form>
//----------------------------------------------------------------------

I think it's very very very strange. Isn't it? :?

Any ideas, plese?

Thanks!!!
 
I

Ivo

I am trying to validate a select object in a form. It does not work fine
in Explorer 6.0 because it always send the form, but in Netscape 7.0 it
works really fine. The code is below:

function validate()
{
if( document.results.p1.value=="1"

Do you see the opening bracket? Good. Now do you see the corresponding
closing bracket? Right, unbalanced brackets, it 's a miracle that Netscape
gave you the impression that all is fine.
This direct assessing of select values is only possible in newer browsers.
To cater for versions 4, you need to access the element via the
selectedIndex property (watch for wrap):
if(
document.forms.results.elements.p1[
document.forms.results.elements.p1.selectedIndex].value )
or
var p = document.forms.results.elements.p1;
if( p[p.selectedIndex].value=="1" ) ...
alert('Value not valid')
else
document.results.submit()

The submit() method should not be called like this, submission is already
happening by the time that function is called. Form validation functions are
best set up like this: the function is called "onsubmit" of the form itself,
not "onclick" of some button. This way all manners of form submission,
including pressing the Enter key, are captured. The validation returns true
or false depending on what you have coded, and the forms submits depending
on that return value. Like so:

<form ... onsubmit="return validate()">

function validate() {
if(document.forms.results.p1.value=="1") {
alert('Value not valid'); return false;
}
return true;
}

<select name=p1 id=p1>
<option selected>1</option>
<option>X</option>
<option>2</option>
</select>

These options don't have values, any of them! Compare with:
<option selected value="1">1</option>
<option value="X">X</option>

hth
 
L

Lee

Alonso said:
Hello everybody! :-D

I am trying to validate a select object in a form. It does not work fine
in Explorer 6.0 because it always send the form, but in Netscape 7.0 it
works really fine. The code is below:

//----------------------------------------------------------------------
<head>
<script >
function validate()
{
if( document.results.p1.value=="1"
alert('Value not valid')
else
document.results.submit()

}
</script>
</head>

<FORM name=result action="page.php" method=post>
<TABLE cellSpacing=0 cellPadding=0 align=center>
....
....
<TR>
<TD align=center>
<select name=p1 id=p1>
<option selected>1</option>
<option>X</option>
<option>2</option>
</select>
</TD>
</TR>
....
....
....
....
<TR class="style">
<TD align=center>
<INPUT type=button value="Send" name=Submit onclick="validate()">
</TD>
</TR>
</table>
</form>
//----------------------------------------------------------------------

I think it's very very very strange. Isn't it? :?

No, it's not strange. When you click on a Submit button, the
form is submitted. What you do in the onClick handler should
not change that.

Form validation should be performed in the form's onSubmit
handler. If that handler returns false, the form will not
be submitted.
 
R

Randy Webb

Alonso said:
Hello everybody! :-D

I am trying to validate a select object in a form. It does not work fine
in Explorer 6.0 because it always send the form, but in Netscape 7.0 it
works really fine. The code is below:

It does not "work really fine" in NS7, view the Javascript Console.

//----------------------------------------------------------------------
<head>
<script >
function validate()
{
if( document.results.p1.value=="1"

You are missing the closing ) on the if test:

if (document.results.p1.value == "1")
alert('Value not valid')
else
document.results.submit()

}
</script>
</head>

<FORM name=result action="page.php" method=post>
<TABLE cellSpacing=0 cellPadding=0 align=center>
.....
.....
<TR>
<TD align=center>
<select name=p1 id=p1>
<option selected>1</option>
<option>X</option>
<option>2</option>
</select>
</TD>
</TR>
.....
.....
.....
.....
<TR class="style">
<TD align=center>
<INPUT type=button value="Send" name=Submit onclick="validate()">
</TD>
</TR>
</table>
</form>
//----------------------------------------------------------------------

I think it's very very very strange. Isn't it? :?

No, see above. But instead of using a button to submit a form, use a
submit button and onsubmit, and have the function return true or false.
 
M

Marky

Hi!!!

Thanks to Ivo, Randy Webb & Lee!! :D I have properly changed my code and
now it works in both web navigator.

Thanks from sunny spain!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top