1

I been working a script that will check if the new input 'value/string' is already exist, but so far I cant find the correct/efficient solution.

<input type="text" class="existingKeywords" value="string1, string2, string3">
<input type="text" class="newKeyword" value=""> 

the value of .newKeyword will defend on user input and once submit button is click, it will check if the newKeyword.val() already exist from .existingKeywords.val(); separated by ','

I tried but returns 'json parse unexpected character'

  var arr    = JSON.parse("[" + $('.existingKeywords').val() + "]");
  var newKey = $('.newKeyword').val();
  if( jQuery.inArray (newKey, arr) ){
    alert('Found!');
  }

Can someone tell me, why its not working or Is there a better approach than this?

Thanks!


Thank you so much guys, the script is now perfectly working. I want to 'Vote Up' everyone's answer but it requires 15 Reputation, and as of this writing I only have 13 reputation. And again gracias everyone.

1

4 Answers 4

1

It is not working because this line:

var arr    = JSON.parse("[" + $('.existingKeywords').val() + "]");

interprets this way:

var arr    = JSON.parse("[string1, string2, string3]");

which is not a correct JSON - strings inside shall be in quotes, otherwise JSON.Parse doesn't know what is string1.

As other suggested you need to use split here instead, I'd like to add only trimming the leading/trailing spaces:

var arr = $('.existingKeywords').val().split(',').map(function(s) { return s.trim();})
var newKey = $('.newKeyword').val().trim();
if( $.inArray (newKey, arr) != -1 ){
  alert('Found!');
}

But why don't you use select with multiple choises? It is more convenient that type keywords I think.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Mr. GRaAL, my script is now working perfect, and everyone's answer is acceptable answer.
1
 var arr    = $('.existingKeywords').val().split(',');
  var newKey = $('.newKeyword').val();
  if( $.inArray (newKey, arr) ){
    alert('Found!');
  }

Comments

1

This will check for case-insensitive duplicate values as well and please try getting values of input with ID instead of class it is safer...

  var array    = $('.existingKeywords').val().split(',');
  var new_array =[] ;

  for(var key in array)
  new_array.push(array[key].toLowerCase());

  var newkeyword = $('.newKeyword').val().toLowerCase();

 if( $.inArray (newkeyword , new_array)!== -1 ){
     //found your value
  }

Comments

1

Maybe you could use the good old String.split()?

Something like this:

var arr = $('.existingKeywords').val().split(',');
var newKey = $('.newKeyword').val();

if( jQuery.inArray(newKey, arr) != -1 ){
    alert('Found!');
}

4 Comments

I tried but the .inArray() always return true, whatever the newKeyword's value.
This is because inArray does not return true/false. When keyword is found it returns index in array - from 0 to arr.length-1. When not found it returns -1 which is also interpreted as true
thanks to Mr. GRaAL and your answer, its now working perfect!
I will be back here once, my reputation reaches 15 and Vote Up everyone's answer. ^__^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.