Solved

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

Posted on 2007-11-29
4
413 Views
Last Modified: 2012-05-05
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>
0
Comment
Question by:lanivanvuuren
  • 2
4 Comments
 
LVL 4

Expert Comment

by:rallsaldo
ID: 20376670
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
 
LVL 12

Expert Comment

by:netsmithcentral
ID: 20376898

<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
 

Author Comment

by:lanivanvuuren
ID: 20377145
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
 
LVL 12

Accepted Solution

by:
netsmithcentral earned 500 total points
ID: 20377205
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

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question