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.

1mr3yn
  • 74
  • 1
  • 9
  • possible duplicate of [Javascript: Determine whether an array contains a value](http://stackoverflow.com/questions/1181575/javascript-determine-whether-an-array-contains-a-value) – Rob Jan 30 '14 at 07:37

4 Answers4

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!');
}
Varinder
  • 2,634
  • 1
  • 17
  • 20
  • I tried but the .inArray() always return true, whatever the newKeyword's value. – 1mr3yn Jan 30 '14 at 07:46
  • 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` – GRaAL Jan 30 '14 at 07:49
  • thanks to Mr. GRaAL and your answer, its now working perfect! – 1mr3yn Jan 30 '14 at 07:56
  • I will be back here once, my reputation reaches 15 and Vote Up everyone's answer. ^__^ – 1mr3yn Jan 30 '14 at 08:10
1
 var arr    = $('.existingKeywords').val().split(',');
  var newKey = $('.newKeyword').val();
  if( $.inArray (newKey, arr) ){
    alert('Found!');
  }
P.Sethuraman
  • 705
  • 3
  • 5
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
  }
Aditya
  • 4,453
  • 3
  • 28
  • 39
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.

GRaAL
  • 616
  • 3
  • 14
  • Thank you Mr. GRaAL, my script is now working perfect, and everyone's answer is acceptable answer. – 1mr3yn Jan 30 '14 at 07:59