-1

I have a text box that users can enter information into. I need to, on the fly, check if any words that the user enter equals a word from my pre-set list and if a word the user entered matches a word in my list do X or Y.

I can do the checking no problem but I am stuck on how I can push full words from my input field using the onchange or another function.

Any help is welcome.

  • 2
    Show us what you've got so far, sir! – zfrisch Mar 28 '18 at 17:01
  • So your question is: _how to split a string into words_? You should be able to search for lots of answers to that question. For example: https://stackoverflow.com/questions/31105452/splitting-sentence-into-array-of-words – Matt Burland Mar 28 '18 at 17:02
  • I made this jsfiddle based on what I think what you're asking.. See if this is what you were after.. https://jsfiddle.net/ekfnbexb/7/ – PerrinPrograms Mar 28 '18 at 17:07
  • @MattBurland Yes and no. The main portion of my question is how can I "do" the check of the two arrays against each other on the fly. IE: as the person is typing as soon as they enter "yes" X happens. – Ryley Ameden Mar 28 '18 at 17:08

1 Answers1

1

I think maybe this is what you were looking to do. Am I right?

var array = [];
var words = ["test","okay","cool"];

$("#words").on("change",function(){

var str = $("#words").val() 
var splitwords = str.split(" ");

if(splitwords.some(r=> words.includes(r))== true)
{
alert("word exists");
//do some stuff
}
else
{
alert("word doesn't exist")
}

})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

enter a string <input type = "text" id = "words"> 
PerrinPrograms
  • 462
  • 3
  • 11
  • Yes this is close. But I need the user to be able to enter more than one word. If I type in just "test" right now it will trigger, but if I type in "I am a test" it does not. – Ryley Ameden Mar 28 '18 at 17:16
  • Okay. I've fixed it. Instead of onkeyup, it is onchange now. But you could make it onkeyup with some tweaking if you needed to. – PerrinPrograms Mar 28 '18 at 18:00
  • That is perfect. I wound up figuring it out and doing it another way but you post helped me a ton. This is how I did it https://gist.github.com/rameden/01224c3c1539108d0c8925e825030ffc – Ryley Ameden Mar 28 '18 at 18:15
  • How would you go about detecting phrases? IE: If the person typed "help me" – Ryley Ameden Apr 02 '18 at 17:56
  • for that.. You could use regular expressions. Check out this site to help you build the expressions for the phrases you're looking for. Here's one I made for 'help me' https://regexr.com/3n800 – PerrinPrograms Apr 02 '18 at 19:05
  • here. Check out this jsfiddle. It does what you're asking. https://jsfiddle.net/ekfnbexb/76/ – PerrinPrograms Apr 02 '18 at 19:35