Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



Reply
What's wrong with this routine?
Old 09-19-2004, 06:52 PM What's wrong with this routine?
Yizit's Avatar
Experienced Talker

Posts: 44
Location: CT
Trades: 0
Hi,

I can't figure out why the condition I've highlighted in red below doesn't work. I'm checking to be sure that neither the "@" or the "." is the last character of the input. It functions properly when checking for the "@" but not the "." Even if I eliminate checking for the "@" and just check for the "." it still won't work.

Code:
function isValidEmail(email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            contact.email.focus();
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            contact.email.focus();
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        contact.email.focus();
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        contact.email.focus();
        return false;
    } else if ((email.indexOf("@") || email.lastIndexOf(".")) == email.length) {  // @ or .  must not be the last character
        contact.email.focus();
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        contact.email.focus();
        return false;
    }
	
    return true;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}
What is it that I'm missing here?

--Yizit
Yizit is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-19-2004, 07:12 PM
Christopher's Avatar
Iced Cap

Latest Blog Post:
Cross-domain AJAX with JSONP
Posts: 3,110
Location: Toronto, Ontario
Trades: 0
They have to be full statements. Right now your condition would read. "If '@', or if '.' not the last character".
Code:
else if ((email.indexOf("@") == email.length || email.lastIndexOf(".")) == email.length) {
As long as '@' was in the string, email.indexOf("@") would evalute to true.
__________________

Please login or register to view this content. Registration is FREE
- Latest Articles:
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE

--
Please login or register to view this content. Registration is FREE

Christopher is offline
Reply With Quote
View Public Profile
 
Old 09-19-2004, 08:01 PM
Yizit's Avatar
Experienced Talker

Posts: 44
Location: CT
Trades: 0
Hi Chroder,

Hmmm... I understand what you are saying and I did make the changes you suggested but it still doesn't work. When the entry is xxx@xxx. (i.e. - with the dot at the end) it still returns true.

Also, I'm a bit confused by your last statement:
Quote:
As long as '@' was in the string, email.indexOf("@") would evalute to true.
Isn't the evaluation more like: If '@' is the last character?

So far - Boolean=1 Me=0

--Yizit
Yizit is offline
Reply With Quote
View Public Profile
 
Old 09-19-2004, 08:17 PM
Christopher's Avatar
Iced Cap

Latest Blog Post:
Cross-domain AJAX with JSONP
Posts: 3,110
Location: Toronto, Ontario
Trades: 0
Sorry, I was thinking indexOf returned false if the string was not found. Disregard that statement I should have said, email.indexOf("@") would always evalute to true (unless it's the first character in which it would be 0 and this, false). If the string is not found, indexOf returns -1, which still evaluates to true.
Quote:
Isn't the evaluation more like: If '@' is the last character?
This is the main problem. The "OR" (||) operator is splitting the condition into two bits, it doesn't "extend" one into the other. Your condition is really saying (in most cases, excluding the '@' being first character like stated above) "If true, or if '.' is the last character" when you want it to say "If '@' is the last character, or if '.' is the last character".

With that said, I think the problem lies within the numbers. The length property holds the length of the string (obviously), but the indexOf() method returns the index of the string. The indexes start at zero, not one. For example, in the string "123", "1" would be in spot 0, "2" in spot 1 etc. So you need to alter it a bit further and subtract one from the length (or alternatively, add one to the indexOf):
Code:
else if ((email.indexOf("@") == email.length-1 || email.lastIndexOf(".")) == email.length-1) {
__________________

Please login or register to view this content. Registration is FREE
- Latest Articles:
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE

--
Please login or register to view this content. Registration is FREE

Christopher is offline
Reply With Quote
View Public Profile
 
Old 09-19-2004, 09:01 PM
Yizit's Avatar
Experienced Talker

Posts: 44
Location: CT
Trades: 0
Hi Chris,

Quote:
The length property holds the length of the string (obviously), but the indexOf() method returns the index of the string.
Thanks Chris! That's what was escaping me.

Now if I could only come up with a routine to prevent memory loss in my old age...

I appreciate your help!

--Yizit
Yizit is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to What's wrong with this routine?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.21413 seconds with 12 queries