Link to home
Start Free TrialLog in
Avatar of johnjmk
johnjmk

asked on

Search array and print results

I am writing a javascript program that allows the user to enter the names and email addresses of various people (which are stored in an array) and also search the array (using something like indexOf() I assume) for strings matching the search in the name or email input and printing out the name and email of each person it finds.

I'm having trouble getting the search function to work. I've tried looping through the array to match the search input and send that to another array which will be printed out but I never end up with the desired results. Any help would be greatly appreciated.

Here's my code so far:


<html>
<head>
	<title>String Manipulation</title>
 
<script language = "JavaScript">
 
 
 
	var index = 0;
	var people = new Array();
 
 
	function addPerson()
	{
 
		var myRegExp = /[^a-z\d-_.@]/g;
 
		var name = document.form.name.value;
		var email = document.form.email.value;
 
	//	nwsName = name.replace(" ", "");
	//	nwsEmail = email.replace(" ", "");
 
	    nameCheck = document.getElementById("name");
	    emailCheck = document.getElementById("email");
 
		printOut = document.getElementById("printout");
 
 
		if(name.length == 0)
		{
			nameCheck.innerHTML = "Please enter a name";
		}
 
		else
		{
			nameCheck.innerHTML = "";
		}
 
 
 
		if(email.length == 0)
		{
			emailCheck.innerHTML = "Please enter an email";
		}
 
 
		else if(email.length < 7)
		{
			emailCheck.innerHTML = "Email must have at least 7 characters";
		}
 
 
	    else if(myRegExp.test(email))
		{
			emailCheck.innerHTML = "Only valid characters are letters, numbers, ampersand ( @ ), hyphen ( - ), \
			underscore (  _  ), and period ( . )";
		}
 
 
		else if(email.indexOf("@") == -1)
		{
			emailCheck.innerHTML = "Email must have an @ in it";
		}
 
 
		else if(email.indexOf(".") == -1)
		{
			emailCheck.innerHTML = "Email must have a period (.) in it";
		}
 
		else if(name.length > 0)
		{
			emailCheck.innerHTML = "";
 
			printOut.innerHTML = ("<font color=009900>" + name + ", " + email + " </font> has been added to the array");
 
			people[index] = (name + ", " + email);
			index++;
		}
	}
 
 
 
	function searchArray()
	{
		/* 
		tried various methods here and couldn't get any to work  
		*/
		   
	}
 
 
 
	function resetEntries()
	{
		nameCheck.innerHTML = "";
		emailCheck.innerHTML = "";
		printOut.innerHTML = "";
	}
 
 
</script>
 
 
</head>
<body bgcolor="#c0c0c0">
<h1>String Manipulation Assignment</h1>
<h2>Search/Add Students</h2>
 
<form name="form">
 
<table>
 
<td>Name
<td><input type="text" name="name"> <font color="ff0000"  id="name"></font><br>
<tr>
<td>Email
<td><input type="text" name="email"><font color="ff0000"  id="email"></font>
 
</table>
 
<br><br>
 
<input type="button" value="Search" onclick = searchArray()>
<input type="button" value="Add" onclick = addPerson()>
<input type="reset" name="myReset" value="Reset" onclick = resetEntries()>
 
<br><br>
 
<p>Results:</p>
 
<div id="printout"></div>
 
</form>
</html>

Open in new window

SOLUTION
Avatar of Graceful_Penguin
Graceful_Penguin
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial