Jump to content

{help} javascript validation - drop down code.


grk101

Recommended Posts

Hello.

 

I have this code that i will post working to the point that if someone selects " Other " from the drop down it will tell them to enter something in the other box which is what i want to do..

 

What I would like it to do though first is to CHECK if something else besides other was selected if not to force it, but if Other is selected to make them type something.

 

So far i got the Other and force to type down, i can't figure out the other part.

 

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

function validate_form ( )
{
    valid = true;

    if (( document.sample.where.selectedIndex == 6 )

       && ( document.sample.other.value == "" ))
    {
        alert ( "If other, Please specify." );
        valid = false;
    } 
      return valid;

}



//-->
</script>

<form name="sample"  method="post" onsubmit="return validate_form ( );">

                <table>

            <tr>

                    <td><div align="right">How did you find our website? </div></td>

                    <td><select name = "where">

                        <option selected="selected">Select</option>

                        <option>Camping In Ontario website</option>

                        <option>Google search listings</option>

                        <option>Yahoo search listings </option>

                        <option>MSN search listings </option>

                        <option>Camping in Ontario Directory </option>

                        <option value="other">Other</option>

                    </select></td>
                  </tr>
    <tr>

                    <td align="right"><label for ="other"> If Other, Please specify </label></td>

                    <td><input name= "other" id="other" type="text" maxlength="20" /></td>
                  </tr>
   <tr>

                    <td colspan="2" align="center"><input name="submit" type="submit" value="Send" />

                        <input name="reset" type="reset" /></td>
                  </tr>
  </table>
  
  
  </form>

Link to comment
Share on other sites

Not quite sure what you are asking since your script already seems to check if the select value is other. But, here is some additional functionality that I would incorporate int othe page:

 

<html>
<head>

<style>
.readOnly { background-color:#cecece; }
.txtInput { background-color:#ffffff; }

</style>

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

function validate_form()
{
    var error = false;
    var selObj = document.forms['sample'].elements['where'];
    var selValue = selObj.options[selObj.selectedIndex].value;
    var otherObj = document.getElementById('other');
//alert(selValue+":"+otherVal);
    if (!selValue)
    {
        error = "You must select an option.";
    }
    else if (selValue=='other' && otherObj.value=="")
    {
        error = "If other, Please specify.";
        otherObj.focus();
    } 

    if (error!=false)
    {
        alert(error);
        return false;
    }
    return true;
}

function setOther(selObj) {

  otherObj = document.getElementById('other');
  if (selObj.options[selObj.selectedIndex].value=='other')
  {
    otherObj.readOnly = false;
    otherObj.className = 'txtInput';
    otherObj.focus();
  }
  else
  {
    otherObj.readOnly = true;
    otherObj.className = 'readOnly';
    otherObj.value = '';
  }
  return;
}



//-->
</script>
</head>

<body>
<form name="sample"  method="post" onsubmit="return validate_form();">

<table>
  <tr>
    <td><div align="right">How did you find our website? </div></td>
    <td><select name="where" onchange="setOther(this);">
      <option selected="selected" value="">< - - Select - - ></option>
      <option value="Camping In Ontario website">Camping In Ontario website</option>
      <option value="Google search listings">Google search listings</option>
      <option value="Yahoo search listings">Yahoo search listings</option>
      <option value="MSN search listings">MSN search listings</option>
      <option value="Camping in Ontario Directory">Camping in Ontario Directory</option>
      <option value="other">Other</option>
    </select></td>
  </tr>
  <tr>
    <td align="right"><label for ="other"> If Other, Please specify </label></td>
    <td><input name="other" id="other" type="text" maxlength="20" class="readOnly" readonly="readonly" /></td>
  </tr>
  <tr>
    <td colspan="2" align="center">
      <input name="submit" type="submit" value="Send" />
      <input name="reset" type="reset" />
    </td>
  </tr>
</table>

</form>
</body>

Link to comment
Share on other sites

well what i wanted it to do was.

 

that if a choice besides other is not selected that it wont submit ..

 

currentlly with mine, if you leave it on " Select " the form still submits :(

 

Not to be rude, but your use of double negatives makes your statements difficult to follow. This statement

if a choice besides other is not selected

makes absolutely no sense. There are seven different choices in that select statement. So, there will always be choices besides "other" that are not selected. If English is not your native tongue please take this as constructive criticism. If not, then take it however you wish.

 

In any case, your last sentence seems to be the most informative as to what you are trying to accomplis - namely that you do not want the user to be able to select the option with the label of "select".

 

Well, one of the problems with your initial code was that none of the options (except the last one) had a value. So, the select list would be pretty useless. The code I provided corrected that error and it ensured:

1. If the user slected "other" that they have entered a value in the "other" field.

2. If the user selected anything except "other" there would be no value in the "other" field

3. The user cannot select the "select" option.

 

So, based upon what you stated I believe the code I provided meets all of your needs. Did you try it? If so, you didn't state if it did or did not meet your needs.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.