Email JavaScript Validation needed if field have any value...

Hello,

Thought I could figure this one out but my brain is not working today. I have a script to validate a few fields.  I want to add there that if these 3 fields contain any info that that info needs to be a valid email:

This is what I tried, but it does not work 100% since the fields is not required, only if there is a value that it needs to be an email address: (I tried a couple of ways just to let you see what I already tried)

-----

<script type="text/javascript">
function validate_form ()
      {
          var errors=new Array();
                  
          var f =  document.getElementById("KMKW_1_INFO");
             
           
                 if (f.First_Name.value =="")
            {
                  errors.push("Given First Name");
            }
           
            if (f.Preferred_First_Name.value =="")
            {
                   errors.push("Preferred First Name");
            }
           
            if (f.Last_Name.value =="")
            {
                   errors.push("Last Name");
            }
                  
            if (f.Camp_Session.value =="")
            {
                   errors.push("Session");
            }                  
                  
            if (f.Grade.selectedIndex==0)
            {
                   errors.push("Grade");
            }

            if (f.Camper_Home_Phone.value =="")
            {
                   errors.push("Camper Home Phone");
            }            

                  if ((f.Email_Mother.length <= 0) || (f.Email_Mother.value.indexOf("@") == -1))
                  {
                              errors.push("Please enter a valid email address in 'Mother's Email'.");
                  }

                  if ((f.Email_Father.value.indexOf("@") == -1) || (f.Email_Father.length <= 0))
                  {
                              errors.push("Please enter a valid email address in 'Father's Email'.");
                  }

                  if ((f.Email_Camper.value.indexOf("@") == -1) && (f.Email_Camper.length <= 0))
                  {
                              errors.push("Please enter a valid email address in 'Camper's Email'.");
                  }

            if (f.Parent_Name.value =="")
            {
                   errors.push("Parent Name");                          
            }

            if(errors.length)
            {
                   alert("Please fill in the following: \n\n"+errors.join("\n"));
           
            return false;

            }
           
             return true;
      }
        
</script>
lanivanvuurenAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
netsmithcentralConnect With a Mentor Commented:
I probably missed a closing parentheses on one of the conditionals.  Here's some sample code of how you would to implement this.  If you post your full source code, I could also find and correct the problem.
<html>
<head>
<script type="text/javascript">
function chk() {
	var f = document.forms[0];
	
	if(f.email.value.length > 0 && !f.email.value.match(/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i)) {
		alert('Need valid e-mail');
		return false;
	} else return true;
}
</script>
</head>
<body>
<form name="f1" onsubmit="return chk();">
	E-Mail: <input type="text" name="email">
	<br><input type="submit" value="Submit!">
</form>
</body>
</html>

Open in new window

0
 
rallsaldoCommented:
Hi,

This might help you out:

http://www.smartwebby.com/DHTML/email_validation.asp

or you could use regular expressions:

http://www.regular-expressions.info/email.html

Hope that helps,
R
0
 
netsmithcentralCommented:

<script type="text/javascript">
function validate_form ()
      {
          var errors=new Array();
                  
          var f =  document.getElementById("KMKW_1_INFO");
             
           
                 if (f.First_Name.value =="")
            {
                  errors.push("Given First Name");
            }
           
            if (f.Preferred_First_Name.value =="")
            {
                   errors.push("Preferred First Name");
            }
           
            if (f.Last_Name.value =="")
            {
                   errors.push("Last Name");
            }
                  
            if (f.Camp_Session.value =="")
            {
                   errors.push("Session");
            }                  
                  
            if (f.Grade.selectedIndex==0)
            {
                   errors.push("Grade");
            }
 
            if (f.Camper_Home_Phone.value =="")
            {
                   errors.push("Camper Home Phone");
            }            
 
                  if ((f.Email_Mother.length > 0) && (!f.Email_Mother.value.match(/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i)))
                  {
                              errors.push("Please enter a valid email address in 'Mother's Email'.");
                  }
 
                  if ((!f.Email_Father.value.match(/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i)) && (f.Email_Father.length > 0))
                  {
                              errors.push("Please enter a valid email address in 'Father's Email'.");
                  }
 
                  if ((!f.Email_Camper.value.match(/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i)) && (f.Email_Camper.length > 0))
                  {
                              errors.push("Please enter a valid email address in 'Camper's Email'.");
                  }
 
            if (f.Parent_Name.value =="")
            {
                   errors.push("Parent Name");                          
            }
 
            if(errors.length)
            {
                   alert("Please fill in the following: \n\n"+errors.join("\n"));
           
            return false;
 
            }
           
             return true;
      }
        
</script>

Open in new window

0
 
lanivanvuurenAuthor Commented:
netsmithcentral,

Just tried that, basically it ignores those 3 steps in the script or rather that is what it looks like.  If I leave the fields empty it does not ask for it which is great!  But if I put something like "goodemail" in there it is suppose to let me know that I need to give a valid email address... it does not do that at all, it just skips right thru to parent name.

Lani
0
All Courses

From novice to tech pro — start learning today.