/    Sign up×
Community /Pin to ProfileBookmark

Need help with searching within an array

I am learn Javascript, and I have been stuck on a assignment for weeks. Been asking for advice but it hasn’t been very helpful at all.

What I want to do is have two fields one for the name and the other for an email. The email has to be verified with all the characters of a email address. (@, .) I have made all this in my code. (I am stuck at this part.) When clicking a add button the name and email gets stored in an array. Another button you can search the array by characters and it writes out the name or email.

I hope someone can help me out here.
Here’s my code

Thanks

[code]
<html>
<head>
<script>
var array = new Array();

function submitForm()
{

var myForm = document.registrationForm;

var myRegExp = / .+@.+..{2,3}$/;
var characters = myRegExp;

var user_name = name;
var name = document.getElementById(“enterName”);

var user_email = email;
var email = document.getElementById(“enterEmail”);

var myUserName = myForm.user_name.value;
var myUserEmail = myForm.user_email.value;

//Both are fields blank
if (myUserName == “”)
{
myName = “<p style=color:red>Please enter your First name.</p>”;
name.innerHTML = myName;

return false;
}
else
{
myName = “”;
name.innerHTML = myName;
}

var invalidChars = ” /:,;”;
for (var k=0; k<invalidChars.length; k++)
{
var badChar = invalidChars.charAt(k);
if (myUserEmail.indexOf(badChar) > -1)
{
myEmail = “<p style=color:red>Only valid characters are letters, ampersand, hyphen, underscore, and period</p>”;
email.innerHTML = myEmail;
return false;
}
}

if (myUserEmail == “”)
{
myEmail = “<p style=color:red>Email must have at least 7 characters</p>”;
email.innerHTML = myEmail;

return false;
}
else
{
myEmail = “”;
email.innerHTML = myName;
}

var periodPos = myUserEmail.indexOf(“.”,atPos);
if (periodPos+7 > myUserEmail.length)
{
myEmail = “<p style=color:red>Email must have at least 7 characters</p>”;
email.innerHTML = myEmail;

return false;
}

var atPos = myUserEmail.indexOf(“@”,1);
if (atPos == -1)
{
myEmail = “<p style=color:red>Email must have an @ in it</p>”;
email.innerHTML = myEmail;

return false;
}

var atPoirt = myUserEmail.indexOf(“.”,1);
if (atPoirt == -1)
{
myEmail = “<p style=color:red>Email must have an period (.) in it</p>”;
email.innerHTML = myEmail;

return false;
}

if (periodPos == -1)
{
return false;
}

//Name field is blank
if (!myUserName == “”)
{
myName = “”;
name.innerHTML = myName;
myForm.user_name.focus();

return false;
}

//Email field is blank
if (!myUserEmail == “”)
{
myName = “”;
email.innerHTML = myName;
myForm.user_email.focus();

return false;
}
return true;
}

</script>
</head>
<scprit>

<body>
<form name=”registrationForm” onsubmit=”return submitForm()”>

<table bgcolor=”silver”>
<thead>
<tr>
<th><h1>String Manipulation Assignment<h1></th>
</tr>

</thead>
<tbody>
<tr>
<td><h2>Search/Add Students<h2></td>
</tr>
<tr>
<td>Name<input id=”user_name” type=”text” name=’user_name’></td>
<td><p align=”left” id=”enterName”></p></td>
</tr>
<tr>
<td>Email<input id=”user_email” type=”text” name=’user_email’></td>
<td><p align=”left” id=”enterEmail”></p></td>
</tr>

<tr>
<td>
<input name=”btnSearch” type=”submit” id=”btnSearch” value=”Search” onclick=”btnSearch ();”>
<input name=”btnAdd” type=”submit” id=”btnAdd” value=”Add” onclick=”btnAdd ();”>
<input name=”btnReset” type=”reset” id=”btnReset” value=”Reset” onclick=”btnReset();”></td>

</tr>
</tbody>
</table>
</form>
</body>

</script>
</html>[/code]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@FangApr 02.2009 — The function submitForm is never called; both submit buttons have different (undefined) functions associated with them.

In function submitForm atPos is used before it is defined: var periodPos = myUserEmail.indexOf(".",[COLOR="Blue"]atPos[/COLOR]);alert(periodPos+":"+myUserEmail.length)
if (periodPos+7 &gt; myUserEmail.length)
{ <br/>
myEmail = "&lt;p style=color:red&gt;Email must have at least 7 characters&lt;/p&gt;";
email.innerHTML = myEmail;

<i> </i> return false;
<i> </i>}

<i> </i>var [COLOR="Blue"]atPos[/COLOR] = myUserEmail.indexOf("@",1);
×

Success!

Help @wb31 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...