[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 619
  • Last Modified:

What is the Javascript equivalent of using Like *?

I want to check an ID value in on my form and use the equivalent of "Like *Combo" as you would in VB.  How does one do this in JS?
0
MitchellVII
Asked:
MitchellVII
  • 4
  • 4
  • 3
  • +1
4 Solutions
 
BadotzCommented:
str = 'Multi-COMBO';
var pos = str.indexOf('COMBO');

// pos will = 6

var pos = str.indexOf('combo');

// pos will = -1 (not found)

JavaScript is case sensitive, so compare CASE to CASE:

str = 'Multi-COMBO';
var pos = str.toUpperCase().indexOf('MULTI');

// pos will = 0
0
 
MorcalavinCommented:
There are a number of options.
You can use the "indexOf" function to check if the string you are looking for contains the text.  The function returns the index the search string was found in, or a negative one (-1) if no value was found.
Or you can use a regular expression for a more robust form of pattern matching.



//indexOf() function
alert('This is a test'.indexOf('is')); //returns 2
alert('This is a test'.indexOf('foo')); //returns -1
 
//regular expression(using the test function)
alert(/is a/.test('This is a test'));  //returns true;
alert(/is A/.test('This is a test'));  //returns false;
alert(/IS A/i.test('This is a test'));  //returns true, i flag means ignore case;

Open in new window

0
 
MitchellVIIAuthor Commented:
Badotz,

Lol, now I'm totally confused :)

Let me post the code where I wish to use this and maybe you can show me how.  Basically, I want to cycle through all of my Combo controls on my form and change their background to blue using JS, but I only want to change those with the words "*Combo" in their name.

For instance, I want to change 'Language1Combo' to blue, but leave 'Language1' unchanged, even though they are both combo boxes.

function elmLoop(){
var theForm = document.forms[0]

for(i=0; i<theForm.elements.length; i++)
      {
            if(theForm.elements[i].type == "select-one" && theForm.elements[i].name Like "*Combo") << I know this is wrong, but this is what i want to do...
            {
                  theForm.elements[i].style.backgroundColor='#EBEBF5';
            }
      }
}      
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
BadotzCommented:
Try this:


if(theForm.elements[i].type == "select-one" && theForm.elements[i].indexOf('Combo') <> -1)

Open in new window

0
 
MorcalavinCommented:

function elmLoop(){
var theForm = document.forms[0]  //you probably want to use document.getElementById if it's available...
 
for(i=0, e=theForm.elements.length; i<e; ++i)
      {
            if(theForm.elements[i].type === 'select-one' && /Combo/.test(theForm.elements[i].name)) //that would test for combo anywhere in the word.  If you want to search for those that end with combo you could use /Combo$/ instead.
            {
                  theForm.elements[i].style.backgroundColor='#EBEBF5';
            }
      }
}      

Open in new window

0
 
MitchellVIIAuthor Commented:
So, using:

theForm.elements[i].indexOf('Combo') <> -1

is the same as saying:

theForm.elements[i].name Like "*Combo"?
0
 
JohnSixkillerCommented:
There is a little typo in Badotz expression:

theForm.elements[i].indexOf('Combo')  != -1 means that source string does NOT contains "Combo"
0
 
MorcalavinCommented:
So, using:

theForm.elements[i].indexOf('Combo') <> -1

is the same as saying:

theForm.elements[i].name Like "*Combo"?


Yes and no.  It's more like say "Like '*Combo*'", since as it stands, it will match the word Combo ANYWHERE in the string, not just at the end.
0
 
MitchellVIIAuthor Commented:
Thanks guys.  I am reasonably proficient in Visual Basic but it is hard getting used to Javascript.  Not  a programmer, just a business guy trying to tweak his webforms a bit.

All good answers, I'll spread the points around :).
0
 
BadotzCommented:
JohnSixkiller: My bad - mixing VBScript with JavaScript always gives me a headache ;-)

Thanks for the correction.
0
 
JohnSixkillerCommented:
You are welcome, I'm glad to help.
0
 
MitchellVIIAuthor Commented:
It is hard to keep the differences between Visual Basic, VB Script and Javascript all straight in your head sometimes.
0
 
BadotzCommented:
Well, considering I never use VBScript (anymore), it usually isn't so hard.

But I was working on a client's Excel spreadsheet problem, and that uses VBA, so...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now