1

I'm trying to search a particular word on the input field and perform if/else condition accordantly. for an example input field can be "iPhone 6" or "Apple iPhone 5" or " Samsung A7" o "S9 Samsung" etc. Please help me find word "iPhone" or "Samsung" from the input field apply if/ else condition.

<input type="text" id="ModelNo" />
<script>
var Model_Validation = document.getElementById("ModelNo").value;
var Model_1 = "iPhone";
var Model_2 = "Samsung";

function test() {
    if (Model_Validation == Model_1) {

    } else if (Model_Validation == Model_2) {

    } else {}
}
</script>

I doubt which logic to use in if condition as well. hope my question is clear.

shafik
  • 6,098
  • 5
  • 32
  • 50
Thaju1
  • 23
  • 4
  • 1
    https://stackoverflow.com/questions/1789945/how-to-check-whether-a-string-contains-a-substring-in-javascript – t q Jan 25 '19 at 18:08
  • 1
    Possible duplicate of [How to check whether a string contains a substring in JavaScript?](https://stackoverflow.com/questions/1789945/how-to-check-whether-a-string-contains-a-substring-in-javascript) – Tyler Roper Jan 25 '19 at 18:09

2 Answers2

1

You can use the includes method on your input's text.

let input = document.getElementById("ModelNo").value; 

if(input.includes(Model_1)){/* do something*/}
else (input.includes(Model_2)){/* do something else*/}

Or you can use a regular expression but includes should be more efficient and simple for what you want to do.

Zack ISSOIR
  • 964
  • 11
  • 24
-1

This could be achieved with JQuery:

<input id="search" type="input" value="type here"/>
<div id="result"></div>

$(document).ready(function(){
    $("#search").on('input',function(){
    var userinput = $(this)[0].value;
    if(userinput == 'samsung'){
        $("#result").html('user inserted "samsung"');
    }
    else{
        $("#result").html(userinput);
    }
  })
});

Working example here

Rod Ramírez
  • 1,138
  • 11
  • 22