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

Instring function?

Status
Not open for further replies.

csiwa28

Programmer
Joined
Apr 12, 2001
Messages
177
I have a form with one drop-down list with several choices and you are allowed to select multiple choices. For my form check, how can i search the value if it contains "Other (please specify below)"? I want my form check to require another text box if "Other (please specify below)" is in the field's output. Is there an InString?
 
Look at indexOf. You test a string to see if another character or string of characters exists inside it and it returns the position of the first match or -1 if not found. You just test that the return value is > -1 to know you have a match.

Look here for info on string functions in Javascript:

It's hard to think outside the box when I'm trapped in a cubicle.
 
Thanks. I tried the indexOf and it kind of worked. Here's how my list looks like.

Accounting
Computers
Other (please specify below)
Real Estate
Sales

If "Other (please specify below)" is the only choice or if it's the top choice it works. If I select "Accounting" or "Computers" with "Other..." then it doesn't work. Any ideas?
 
Show your actual Javascript code, please. Without seeing what you've written, it's difficult to figure out what's going wrong for you.

Lee
 
Sorry, here it is. My multiple choice drop-down list is called "areaexpertise" and the other text box is called "areaexpertiseother".

function formCheck(formobj){
var AreaofExpertise = formobj.areaexpertise.value;
if (AreaofExpertise.indexOf("Other (please specify below)") != -1)
{
var checkOther = formobj.areaexpertiseother.value;
if (checkOther ==""){
alertMsg += "\n - Please Specify Other";
}
}

//display any errors
if (alertMsg.length == l_Msg){
return true;
}else{
alert(alertMsg);
return false;
}
}
 
Try it like this:
Code:
function formCheck(formobj){
  var selOptions = '';
  for (var i=0;i<formobj.AreaofExpertise.options.length; i++) {
    if (formobj.AreaofExpertise.options[i].selected) {
      if (selOptions != '') selOptions+=',';
      selOptions+=formobj.AreaofExpertise.options[i].text;
    }
  }
  if (selOptions.indexOf("Other (please specify below)") != -1)
  {
    var checkOther = formobj.AreaofExpertiseOther.value;
    if (checkOther ==""){
      alertMsg += "\n - Please Specify Other";
    }
  }
  //display any errors
  if (alertMsg.length == l_Msg){
    return true;
  }else{
    alert(alertMsg);
    return false;
  }
}

selectedIndex will only return the FIRST selected option which is not useful for what you want so you have to get the object of the select box then loop through all of the options to see if they are selected. Every time I find a selected option I append it to the variable selOptions and separate the values by a comma. The separation may not be necessary for what you are doing now but if you wanted to pull those values out again it will be useful.

It would be helpful if you changed your variable names to be more distinctive though. The names for use locally inside the array should not be so similar to the field names on the form or even each other as it gets confusing as to which one you are working with especially when the only difference is capitalization.


It's hard to think outside the box when I'm trapped in a cubicle.
 
It worked! Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top