Jump to content

Syntax? What is wrong?


dalawh
 Share

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 comment
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 comment
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 comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...