email validation not catching space

Why is this email validation script not stopping an email address like this: debs@gmail. com
Am i doing something wrong?
if (document.form1.E_mail.value != "") {
		var email = document.form1.E_mail.value;
	  AtPos = email.indexOf("@");
	  StopPos = email.lastIndexOf(".");
	  SpacePos = email.lastIndexOf(" ");
	  CommaPos = email.lastIndexOf(",");
	
	if (AtPos == -1 || StopPos == -1) {
		themessage = (themessage + "Not a valid email address" + '\n');
	
	}
	
		if (StopPos < AtPos) {
			themessage = (themessage + "Not a valid email address" + '\n');
		
		}
	
			
		if (StopPos - AtPos == 1) {
			themessage = (themessage + "Not a valid email address" + '\n');
		
		}
	
		if (SpacePos > 0 ) {
		themessage = (themessage + "Not a valid email address" + '\n');
			
		} 
	
	
		
		if (CommaPos > 0 ) {
		themessage = (themessage + "Not a valid email address" + '\n');
		
			} 
		
	}
	

Open in new window

rivkamakAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tom BeckCommented:
Works in my test. Open your javascript console and view this jsfiddle.

http://jsfiddle.net/9p73sbfe/

On a side note, lots of email addresses have a period in the characters before the @ so you might want to rethink this part.
if (StopPos < AtPos) {
			themessage = (themessage + "Not a valid email address" + '\n');
		
		}

Open in new window

0
skijCommented:
I recommend simplifying this with REGEX:
if (document.form1.E_mail.value != "") {
    var email = document.form1.E_mail.value;
    if(!/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i.test(email)) alert('Not a Valid Email');
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.