Solved

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

Posted on 2007-11-29
4
410 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now