0

I have the following which contains a list of names

 var myList = @Html.Raw(new JavaScriptSerializer().Serialize(Model.Names));

I have a textbox in which the user enters a name and I have to search through myList and see if it contains that name?

How can I do that? I can see the data in myList are as follows:

Albert, Jack, Jim, Tom

Read textbox value :

var NameEntered = $("#Name").val(); 

e.g, If the user enters the name "Albert", it should be able to give me a true as answer in javasript or razor

learning
  • 11,415
  • 35
  • 87
  • 154

1 Answers1

1

Assuming modern browser:

if(NameEntered && myList.indexOf(NameEntered) >= 0){
    alert("This name is in the list: " + NameEntered );
}

However, this is very primitive. A better solution would be to split the string into array and check each member for the existance of name.

Check this out: JavaScript find names in strings

Community
  • 1
  • 1
GEMI
  • 2,239
  • 3
  • 20
  • 28
  • @Bergi what browser are you using? Are you getting an error message? – GEMI May 16 '13 at 12:28
  • No but `if(["Albert",…].indexOf("Albert")){ alert(…); }` does not alert (and any browser that does is buggy) – Bergi May 16 '13 at 12:35
  • Sorry my bad, fixed. But anyway this is a very simple solution because it does not account for capitalization and will work with strings like "bert" or "im" :) – GEMI May 16 '13 at 12:38