How to use IndexOf in JQuery

99,468

That's because it would be looking for the string '4289||78843', which doesn't exist in the target I'm assuming. Logical operators can't just be tossed in anywhere, only where there are actual values to logically operate on. Something like this:

if(($('#this').val().indexOf('4289') > -1) ||
   ($('#this').val().indexOf('78843') > -1))

The return value of the indexOf() function is the numeric index of that value in the target value, or -1 if it's not found. So for each value that you're looking for, you'd want to check if it's index is > -1 (which means it's found in the string). Take that whole condition and || it with another condition, and that's a logical operation.

Edit: Regarding your comment, if you want to abstract this into something a little cleaner and more generic you might extract it into its own function which iterates over a collection of strings and returns true if any of them are in the target string. Maybe something like this:

function isAnyValueIn(target, values) {
    for (var i = 0; i < values.length; i++) {
        if (target.indexOf(values[i]) > -1) {
            return true;
        }
    }
    return false;
}

There may even be a more elegant way to do that with .forEach() on the array, but this at least demonstrates the idea. Then elsewhere in the code you'd build the array of values and call the function:

var values = ['4289', '78843'];
var target = $('#this').val();
if (isAnyValueIn(target, values)) {
    // At least one value is in the target string
}
Share:
99,468

Related videos on Youtube

Blessing Thinker
Author by

Blessing Thinker

Updated on July 09, 2022

Comments

  • Blessing Thinker
    Blessing Thinker almost 2 years
    if($('#this').val().indexOf('4289')){
        Do something
    else
        Do something. 
    

    This works only with that 4289,
    When I try to add other numbers to be indexed next to it using 'or', it doesn't work. How should I put other number. E.g

    IndexOf('4289||78843') 
    

    I want this to check this numbers and if the number in the input field is not one of this, to echo error.

    Here's more which happens to die when one revisits the field.

    $('#Zip').blur(function(){
            if (($(this).val().indexOf('0860') > -1)||($(this).val().indexOf('0850') > -1)){
            $('#Status_Zip').html("No way.")
            $(this).alterClass('*_*', 'Success')
            return false;
            }else{$('#Status_Code').hide()
            $(this).alterClass('*_*', 'Error')
            $(this).css('border-color', '#F00').css('background-color', '#FFC').effect("pulsate",{times:4},2)
                return true;
            }
        })
    
  • Blessing Thinker
    Blessing Thinker almost 11 years
    Ok, that's nice. It's working. The only thing is if I'm gonna put more than 20 of this numbers, the code is just gonna be long with a lot of these indexes. Can't we shorten something somewhere there. ?
  • David
    David almost 11 years
    @BlessingThinker: Sure, it can be abstracted into its own general purpose function. Any time there's a lot of copy/paste going on in the code then it's likely that at the very least a function can be extracted. There are probably more approaches besides this one, but it seems straightforward enough. I've updated the answer with an example.
  • Blessing Thinker
    Blessing Thinker almost 11 years
    I used blur, then wen one puts the right number it validates right, but when you go back and change that number to something else, the function appears to have died.
  • David
    David almost 11 years
    @BlessingThinker: Sounds like a different problem entirely, and one for which I'd need to see more complete code.
  • David
    David almost 11 years
    @BlessingThinker: Define "happens to die." Does the blur handler get called? What happens?
  • Blessing Thinker
    Blessing Thinker almost 11 years
    Ok. After load, enter a wrong number, it will say error, but now enter the right one, I accepts, and then change that number, now it's no longer saying error. Don't delete the number, just add a digit.
  • David
    David almost 11 years
    @BlessingThinker: Without a live example, a description of the end results alone doesn't really provide much information. When it doesn't work, does the event handler get called at all? When debugging that call, at what point does it fail? I'd like to help, but so far all I know is "it doesn't work" which I can't really use.
  • Blessing Thinker
    Blessing Thinker almost 11 years
    I see where the problem is, this function doesn't get called when you change the number in the input, "zip", only when you clear that field and type again does it gives the error. So I need something that will make it recognize that the previously validated number has been changed.
  • David
    David almost 11 years
    @BlessingThinker: If it's on blur then it should be called any time the focus leaves that field. You might also try the change event.