Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What the heck is wrong with this function??

Status
Not open for further replies.

mtorbin

Technical User
Nov 5, 2002
369
US
I sure as hell can't find it (and it's probably staring at me right in the face):

function checkEmail() {
var userEmail = document.processLogin.emailAddr.value;

for (i = 0; i <= peiEmail.length; i++) {
peiEmail = peiEmail.toLowerCase();

if (peiEmail == userEmail) {
return true;
}
}

alert("I never get here!!");
return false;
}

I NEVER get to the point where it says "I never get here". Why?

- MT
 
I apologize if this seems like a silly question, but have you ruled out the possibility that it is always returning inside the for-loop (return true;)?

--Dave
 
also, if you're running this in IE, sometimes script errors can cause your scripts to fail silently. try it in firefox and look at the javascript console for errors.

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
mtorbin,
A couple things.
1. Has the loop variable i been declared?
Try:
for (var i = 0; i <= peiEmail.length; i++)

ALSO
should you be using peiEmail.length-1 ???
The array will start at 0. If you have 5 items in the array then the peiEmail.length will return 5, but in your for loop you have to loop 0 - 4.

And just for haha's, right after the
var userEmail = document.processLogin.emailAddr.value;

put in:
alert(userEmail);
Just to make certain you are getting a value.

 
one thing I noticed recently was that a book i was reading says to use
Code:
peiEmail.charAt[i] = peiEmail.charAt[i].toLowerCase();
instead of
Code:
peiEmail[i] = peiEmail[i].toLowerCase();
now I have yet to test both ways to see what the difference is but i mean give it a shot and see if it solves your problems
 
Ooh, then he'd have to double-index/for-loop like:

Code:
for(var i=0; i<peiEmail.length; i++)
{
 for(var j=0; j<peiEmail[i].length; i++)
 {
  peiEmail[i].charAt[j] = peiEmail[i].charAt[j].toLowerCase();
 }
}

I'd be interested (sincerely) in your book's theory as to why this should be done.

--Dave
 
I'm not quiet sure on the reasoning behind it. See I'm used to using C++ programming and there we use stringname but i was reading the book about javascript and it said to change the value of a single character you should use .charAt(i). and i made a mistake on writing that it should have been .charAt(i)
 
Ah, yes, it is certainly true that one should use charAt(i) to change the case of a single character. That could be a problem if that is what mtorbin was doing.

I was assuming that peiEmail was an array of e-mail addresses, although in the code provided, that would mean that these are redundantly made into lower case each time the function is called.

Hmm....

--Dave
 
I did not read thoroughly enough myself.
I also assumed he was comparing to a list of stored addresses in an array and glossed over the toLowerCase() thinking the entire value was being cast to lower rather than individual characters.
 
Hmm.... indeed. I think that since peiEmail was never declared (or atleast not in this function) it might not exist. If it was declared make sure that if its an array you use .charAt(i) or i guess u could use a multi dimensional array [j]. Could you specify on what peiEmail is?
 
OK... Most of the "Why" doesn't make sense because all of the bases were covered in one form or another, but here's the final:

function checkEmail() {
var userEmail = document.processLogin.emailAddr.value;

for (var i = 0; i <= peiEmail.length-1; i++) {
peiEmail = peiEmail.toLowerCase();

if (peiEmail == userEmail) {
return true;
}
}

return false;
}

Works, thanks all!

- MT
 
Looks like theniteowl hit it, but, mtorbin, if you were a conformist, you would write:

< peiEmail.length

rather than

<= peiEmail.length-1

FYI.

'glad you got it.

--Dave
 
I probably should... on a side note, it's amazing how rusty one gets from not "practicing" every day.

I can't find this in the books, so I thought I'd post it here. I need the opposite of this:

if (GetCookie(cookieName)) {

As in if the function GetCookie(cookieName) is null...

- MT
 
Yes, at the time I did. I have since solved that issue. Mr. Plow, the project I'm working on jumps back and forth between JS and PERL, so it's been pretty scattered. I'm pretty much done, minus a few key issues. Unfortunately, it seems like most of those now are PERL-related.

Thanks for the help,

- MT
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top