Form Validation - What is wrong with this?

Afternoon Chaps,

A little help if I may?

I have a form set up and I need to set the validation so if one option is selected from a picklist, they need to complete a text field as well.

This is the code i’m using:



<script type="text/javascript">
<!--

function validate_form ( )
{
    valid = true;

    if ( ( document.listmenu0.Reason.selectedIndex == 1 ) && ( document.listmenu0.Row_Number.value == "" ) )

   {
        alert ( "Please add a row number" );
        valid = false;
    }

    return valid;
}

//-->
</script>

Reason is a picklist, I want it to show the alert when option 1 from the picklist is selected and no text is in the Row_Number field.

If any of the other options are selected, I want to make sure the Row_Number field is blank.

As I said, I’m sure this is a very simple thing, but I been working on it for ages, and cannot get it to work, I get the alert every time the Row_Number field is blank.

Any help would be much appreciated.

Skinn

selectedIndex is a zero based index so the first item has selectedIndex equal to zero.

If any of the other options are selected, I want to make sure the Row_Number field is blank.

You do not have any code for this.

I get the alert every time the Row_Number field is blank.

Is this really true?

Here is a simple html page using a slightly modified version of your script.

<html>
<head>
<script type="text/javascript">
<!--

function validate_form ( )
{
    valid = true;

    if ( document.listmenu0.Reason.selectedIndex == 0 )
      {
        if ( document.listmenu0.Row_Number.value == "" )
           {
              alert ( "Please add a row number" );
              valid = false;
           }
        else
           {
              alert("input OK");
           }
      }
    else
        if ( document.listmenu0.Row_Number.value == "" )
           {
              alert("input OK");
           }
        else
           {
              alert ( "A row number is not required" );
              valid = false;
           }


    return valid;
}

//-->
</script>
</head>
<body>
<form name="listmenu0">
<select name="Reason">
<option value="2">2</option>
<option value="1">1</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
</select>
<input type="text" name="Row_Number" />
<a href="javascript:validate_form()">test</a>
</form>
</body>
</html>

Seems to work OK

Thanks, it works :tup: