Skip to main content
Participant
February 1, 2023
Answered

Searching text field using script with wild card, key words, or not exact words

  • February 1, 2023
  • 1 reply
  • 2215 views

Hi,

 

I'm trying to come up with a script that searches two different text field with words (in this case a location/place) and yields a result (mileage). ex. if text field 1 contains XXX and text field 2 contains XXX then it equals XXX

 

So far, I was able to somehow come up with the following:

 

if(this.getField("PlaceRow1").value=="Cape Spencer" && this.getField("PlaceRow2").value=="Sitka"){event.value="320 miles"}

 

The script works but the downside is that it only works with "exact words and it is also case sensitive" which will not work out since not everyone will type how I have it recorded.

 

How do I change the above script so that it will not be case senstiive or doesn't follow exact wording?

 

For example, if they type SITKA, the script will still recognize it as Sitka and if they type just Spenc, it will work with Cape Spencer. 

 

 

 

Note: I have ZERO experince with scripting, I was just lucky that the above script happened to partially worked out from all the things I've read. If possible, may you please kindly make it as simple as possible for me to understand. Thank you so much in advance.

 

 

This topic has been closed for replies.
Correct answer try67

You can do it using a Regular Expression, for example:

 

if (/spen/i.test(this.getField("PlaceRow1").value) && /sit/i.test(this.getField("PlaceRow2").value)){event.value="320 miles"}

1 reply

try67
Community Expert
Community Expert
February 1, 2023

First part is easy to do. Second part is much trickier.

What's the logic behind "Spenc" matching "Cape Spencer"? What about "Spen", or "Spe", "Sp", or just "S"? And if they type just "S", shouldn't that also match "Sitka"? In other words, you have to define very clearly what are the rules for matching a partial string, or a fuzzy one (what if they enter "Cape Spancer", for example?).

Participant
February 2, 2023

I mean, I was hoping there's a way for the script to recognize not case sensitive or certain letters of the words. 

 

For instance, if the script says:

if(this.getField("PlaceRow1").value=="spen" && this.getField("PlaceRow2").value=="sit"){event.value="320 miles"}

  • employee enters cape spencer, Cape Spencer, spencer, or Spencer in the field, it will be recognized as "spen"
  • and Sitka, or SITKA as "sit"
  • it will then give the 320 miles

Another ex:

if(this.getField("PlaceRow1").value=="cea" && this.getField("PlaceRow2").value=="kut"){event.value="31 miles"}

  • employee enters ocean or ocean cape but the script will know its for "cea"
  • if they enter yakutat or Yakutat in the the field, the script will recognize "kut"
  • it will then give the 31 miles

 

I apologize if it isn't that clear. I attached an example of what I'm working on

 

 

 

 

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
February 2, 2023

You can do it using a Regular Expression, for example:

 

if (/spen/i.test(this.getField("PlaceRow1").value) && /sit/i.test(this.getField("PlaceRow2").value)){event.value="320 miles"}