Mohamed El Habchi
Mohamed El Habchi

Reputation: 13

Check if text includes a specific value in text area

I have a JS where I can verify if the a value is being entered and warn the user to change the input. the script is working fine only if only that value exist in the text and if the value with some other text will not work.

$(function() {
  const setup = function(fieldSelector) {
    const field = $(fieldSelector);
    const applyStyle = function() {
      if (field.val() == 'urgent') 
      {
        alert("Text not allowed!");
        field.css({'background-color': 'red'});
      } else {
        field.css({'background-color': ''});
      }
    };
    field.on('change', applyStyle);
    applyStyle();
  }
  // Note: Change the ID according to the custom field you want to target.
  setup('#issue_custom_field_values_17');
});

this code is under redmine issue tracker. Any guidance will be much appreciated

Upvotes: 1

Views: 162

Answers (1)

Daniel
Daniel

Reputation: 1

I'm very unfamiliar with jQuery, but couldn't you just replace

if (field.val() == 'urgent')

with

if (field.val().includes('urgent'))

or even

if (field.val().indexOf('urgent')>-1)

Upvotes: 0

Related Questions