0

I have a page of input fields used for data entry. No input field should have duplicates.

If a user enters "ABC" into an input field and tabs out the function should detect whether "ABC" exists in any other input field. If the value exists then an error message is displayed after each of the input fields, including the current input field.

If the value is changed (eg to "DBE") in any of these input fields and therefore, there are no duplicates then all messages should hide.

I am currently stuck with the following:

$("input[type='text']").on("keyup change", function() {
   var value = this.value;

    if ($("input[type='text']:contains('" + value"')").length > 1)
              {
                  $(this).find(".error").show();
              }
         else {
                  $(this).find(".error").hide();
               }
         }    
});         
2
  • 2
    your curly braces don't match up Commented Jan 20, 2016 at 4:52
  • 1
    you should also tell people what you're "stuck" on. Like are you encountering a certain unwanted behaviour, does the page crash, etc. Commented Jan 20, 2016 at 4:55

1 Answer 1

1

You have an extra closing bracket in your code. Also, you need to have another + sign to join the string for value

$("input[type='text']").on("keyup change", function() {
    var value = this.value;

    // added another '+' sign here ---------------|
    if ($("input[type='text']:contains('" + value + "')").length > 1) {
        $(this).find(".error").show();
    }
    else {
        $(this).find(".error").hide();
    }
    // } <--- remove this.
});     
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks to @Shijin for pointing out the plus sign in one of the edits.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.