dalawh Posted July 13, 2012 Share Posted July 13, 2012 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 More sharing options...
justsomeguy Posted July 13, 2012 Share Posted July 13, 2012 The second parameter of the substr method is the length, not the stop position. The substring method lets you specify the from and to positions. Link to comment Share on other sites More sharing options...
davej Posted July 15, 2012 Share Posted July 15, 2012 (edited) 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 July 15, 2012 by davej Link to comment Share on other sites More sharing options...
dalawh Posted July 15, 2012 Author Share Posted July 15, 2012 The second parameter of the substr method is the length, not the stop position. The substring method lets you specify the from and to positions.Oh stupid me. Thanks a lot for the help. Link to comment Share on other sites More sharing options...
dalawh Posted July 15, 2012 Author Share Posted July 15, 2012 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 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now