Skip to main content
Participating Frequently
October 11, 2019
Question

How do I search a text field for multiple words?

  • October 11, 2019
  • 4 replies
  • 2667 views

I have a fillable PDF and in one of the fields I need an alert to appear if one of many words is not used. I have found the below script, but I'm struggling to get it to work for more than just one word. Does anybody know how I can make this work with a list of words?

 

var n = event.value.indexOf("leadership");

 

if (n == -1) { // substring not found

    app.alert("Does not contain one of DOR’s Core Values.");

    event.rc = false;

}

else {

    event.rc = true;

}

    This topic has been closed for replies.

    4 replies

    try67
    Community Expert
    Community Expert
    October 15, 2019

    Use this:

     

    var words = ["leadership", "greatness", "mobility", "fairness"];
    var wordFound = false;
    for (var i in words) { 
    	var n = event.value.indexOf(words[i]); 
    	if (n != -1) { // substring not found 
    		wordFound = true;
    	} 
    } 
    if (wordFound==false) { 
    	app.alert("Does not contain one of DOR’s Core Values.");
    	event.rc = false;
    } else {
    	event.rc = true;
    }
    
    rhneaAuthor
    Participating Frequently
    October 15, 2019
    This is working! Thank you soooo much! This has been such a headache for me 🙂
    try67
    Community Expert
    Community Expert
    October 15, 2019

    Use this:

     

    var words = ["leadership", "greatness", "mobility", "fairness"];
    var wordFound = false;
    for (var i in words) { 
    	var n = event.value.indexOf(words[i]); 
    	if (n == -1) { // substring not found 
    		wordFound = true;
    	} 
    } 
    if (wordFound==false) { 
    	app.alert("Does not contain one of DOR’s Core Values:\n" + missingWords.join("\n"));
    	event.rc = false;
    } else {
    	event.rc = true;
    }
    
    Bernd Alheit
    Community Expert
    Community Expert
    October 15, 2019
    Where does you set missingWords?
    try67
    Community Expert
    Community Expert
    October 15, 2019
    I'm not. It's not needed, according to the new description. I'm using the wordFound variable to check if /any/ of the required words was used, instead.
    Bernd Alheit
    Community Expert
    Community Expert
    October 15, 2019

    Use this:

    var words = ["leadership", "greatness", "mobility", "fairness"];
    var missingWords = [];
    for (var i in words) {
     var n = event.value.indexOf(words[i]);
     if (n == -1) missingWords.push(words[i]);
     } 
    if (missingWords.length>0) {
     app.alert("Does not contain one of DOR’s Core Values:\n" + missingWords.join("\n"));
     event.rc = false;
     } else {
     event.rc = true;
     }
    rhneaAuthor
    Participating Frequently
    October 15, 2019

    Thank you! The problem I'm running into is that this is requiring all words to be used. For instance, if type "leadership" in the text field, the alert will pop up saying the three other words are missing.

    Bernd Alheit
    Community Expert
    Community Expert
    October 15, 2019

    You can use:

    if (missingWords.length == words.length) {

    try67
    Community Expert
    Community Expert
    October 12, 2019

    You can use something like this:

     

    var words = ["leadership", "greatness", "mobility", "fairness"];
    var missingWords = [];
    for (var i in words) {
    	var n = event.value.indexOf("leadership");
    	if (n == -1) { // substring not found
    		missingWords.push(words[i]);
    	}
    }
    
    if (missingWords.length>0) {
    	app.alert("Does not contain one of DOR’s Core Values:\n" + missingWords.join("\n"));
    	event.rc = false;
    }
    else {
    	event.rc = true;
    }
    
    rhneaAuthor
    Participating Frequently
    October 15, 2019
    Thank you for replying! I've tried what you provided, but "leadership" is the only word I can use without getting the alert. I've tried adding the additional words in the parenthesis, but I still get the alert when entering them.
    try67
    Community Expert
    Community Expert
    October 15, 2019
    Can you post your code?