Jump to content

Syntax? What is wrong?


Recommended Posts

I went over this plenty of times and no matter how many times I go over it, something goes wrong and I can't seem to figure it out.

function validateEmail(email){var atNum=0;for(var i=0; i<email.length; i++){ //Checks for number of @  if(email.charAt(i)=="@"){   atNum++;  }}alert("atNum: " + atNum);var atPos=email.indexOf("@");alert("atPos: " + atPos);var lastDotPos=email.lastIndexOf(".");alert("lastDotPos: " + lastDotPos);var user=email.substr(0,atPos);alert("user: " + user);var domain=email.substr(atPos+1,lastDotPos-1); //<-- ISSUEfor(var i=atPos+1; i<lastDotPos; i++){  alert("email.charAt(" + i + "): " + email.charAt(i));}alert("domain: " + domain);var tld=email.substr(lastDotPos+1,email.length-1);alert("tld: " + tld);return false;}

This is not fully functioning. Every time I run this, the domain always displays like gmail.com instead of gmail. I even tried to set the lastDotPos-1 to something I know that is suppose to give me gmail, but it still comes out as gmail.com. What is wrong?

Link to post
Share on other sites

Actually JS is a bit odd. There is a substr() and a substring() and they are DIFFERENT.

<!DOCTYPE html><html><body><script type="text/javascript">var str="0123456789";document.write(str.substring(1,4));document.write(" ");document.write(str.substr(1,4));</script></body></html>

the output is "123 1234"

Edited by davej
Link to post
Share on other sites
Actually JS is a bit odd. There is a substr() and a substring() and they are DIFFERENT.
<!DOCTYPE html><html><body><script type="text/javascript">var str="0123456789";document.write(str.substring(1,4));document.write(" ");document.write(str.substr(1,4));</script></body></html>

the output is "123 1234"

Oooo, did not know this. Thanks for this :D
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...