troubleshooting Question

email validator problem

Avatar of -Dman100-
-Dman100-Flag for United States of America asked on
JavaScript
13 Comments1 Solution356 ViewsLast Modified:
I have a javascript function that validates my web form. When I add a emailcheck function it causes my validation to fail.  I know the email validation fuction works because I use it in other pages, so I'm not quite sure why it is breaking on this page.

I've attached validation code with and without the email check function.  It works when I don't include the email check function, but as soon as I introduce the email check function it causes the validation to fail.

I can't figure out what is causing the email check to fail?

For example, I can enter myname@centurytel.net and it won't accept it?  

Thanks for any help.
function validate(){
		var flag = true;
		var container = document.getElementById("inputContainer");
		var ckbxContainer = document.getElementById("checkboxContainer");
		var firstnameErrMsg = document.getElementById('fnameErrMsg');
		var lastnameErrMsg = document.getElementById('lnameErrMsg');
		var emailErrMsg = document.getElementById('emailErrMsg');
		var checkboxErrMsg = document.getElementById('ckbxErrMsg');
		var inputs = container.getElementsByTagName("input");
		var checkboxes = ckbxContainer.getElementsByTagName("input");
		var isSelected = false;
		
		for (var i = 0; i < checkboxes.length; i++){
			if (checkboxes[i].type == "checkbox") {
				if (checkboxes[i].checked == true) {
					isSelected = true;
				}
			}
		}
		
		for(var i = 0; i < inputs.length; i++){
			var str = inputs[i].name;
			if (str.indexOf('fname') != -1) {
				if (inputs[i].value == "") {
					firstnameErrMsg.style.display='block';
            		flag = false;
				}	
				else {
					firstnameErrMsg.style.display='none';
				}		
			}
			if (str.indexOf('lname') != -1) {
				if (inputs[i].value == "") {
					lastnameErrMsg.style.display='block';
            		flag = false;
				}	
				else {
					lastnameErrMsg.style.display='none';
				}		
			}
			if (str.indexOf('email') != -1) {
				if (inputs[i].value == "") {
					emailErrMsg.style.display='block';
            		flag = false;
				}
             	else {
                	emailErrMsg.style.display='none';
             	}			
			}
			if (!isSelected) {
				checkboxErrMsg.style.display='block';
				flag = false;
			}
			else {
				checkboxErrMsg.style.display='none';
			}
		}	
		if(flag == false){
			return flag;
		}else{
			return flag;
		}
	}
function checkemail(email){
        var str=email.value;
        var filter = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;

        if (filter.test(str)) {
            return true;
        }else{
            return false;
        }
    }
	
	  
    function validate(){
		var flag = true;
		var container = document.getElementById("inputContainer");
		var ckbxContainer = document.getElementById("checkboxContainer");
		var firstnameErrMsg = document.getElementById('fnameErrMsg');
		var lastnameErrMsg = document.getElementById('lnameErrMsg');
		var emailErrMsg = document.getElementById('emailErrMsg');
		var checkboxErrMsg = document.getElementById('ckbxErrMsg');
		var inputs = container.getElementsByTagName("input");
		var checkboxes = ckbxContainer.getElementsByTagName("input");
		var isSelected = false;
		
		for (var i = 0; i < checkboxes.length; i++){
			if (checkboxes[i].type == "checkbox") {
				if (checkboxes[i].checked == true) {
					isSelected = true;
				}
			}
		}
		
		for(var i = 0; i < inputs.length; i++){
			var str = inputs[i].name;
			if (str.indexOf('fname') != -1) {
				if (inputs[i].value == "") {
					firstnameErrMsg.style.display='block';
            		flag = false;
				}	
				else {
					firstnameErrMsg.style.display='none';
				}		
			}
			if (str.indexOf('lname') != -1) {
				if (inputs[i].value == "") {
					lastnameErrMsg.style.display='block';
            		flag = false;
				}	
				else {
					lastnameErrMsg.style.display='none';
				}		
			}
			if (str.indexOf('email') != -1) {
				if (inputs[i].value == "") {
					emailErrMsg.style.display='block';
            		flag = false;
				}
             	else if (inputs[i].value != "") {
					var strEmail = inputs[i].value;
            		if (!checkemail(strEmail)) {
                		emailErrMsg.style.display='block';
                		flag = false;
             		} 
             		else {
                		emailErrMsg.style.display='none';
             		}	
				}			
			}
			if (!isSelected) {
				checkboxErrMsg.style.display='block';
				flag = false;
			}
			else {
				checkboxErrMsg.style.display='none';
			}
		}	
		if(flag == false){
			return flag;
		}else{
			return flag;
		}
	}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 13 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 13 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros