SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with javascript code

    Hi everyone hope all well.

    I need your expertise. I have a problem with the email part of this javascript. Everything is working fine except for It will not validate when i add a period (.) before the @ sign. I am wanting to be able to put in an email like this

    abc.def@gmail.com

    or

    abc+def@gmail.com

    Here is the code for you to look at

    Code:
    <script type="text/javascript">
    function validate_form() {
    
        if (!ValidatePhoneNumber(document.second.HomePhone.value)) {
            return false;
        }
    	if(!ValidateZip(document.second.ZipCode.value))
    	{
    			return false;
    	}
        if ( document.second.degreeLevel.selectedIndex == 0 )
        {
            alert ( "Please select degree level" );
            return false;
        }
    	if ( document.second.ProgramInterest.selectedIndex == 0 )
        {
            alert ( "Please select area of study" );
            return false;
        }
    	if ( document.second.LevelofEducationCompleted.selectedIndex == 0 )
        {
            alert ( "Please select level of education" );
            return false;
        }
    
    	 if ( document.second.FirstName.value == '' )
        {
            alert ( "Please Enter a First Name" );
            return false;
        }
    
    	 if ( document.second.LastName.value == '' )
        {
            alert ( "Please Enter a Last Name" );
            return false;
        }
    
    	 if ( document.second.Address.value == '' )
        {
            alert ( "Please Enter a Address" );
            return false;
        }
    
    	 if ( document.second.City.value == '' )
        {
            alert ( "Please Enter a City" );
            return false;
        }
    
    	 if ( document.second.State.selectedIndex == 0 )
        {
            alert ( "Please Select a State" );
            return false;
        }
    	if(!ValidateName(document.second.FirstName.value))
    	{
    		return false;
    	}
    	if(!ValidateName(document.second.LastName.value))
    		{
    			return false;
    	}
    
    	
    	function ValidatePhoneNumber(field) {
    	    var valid = "0123456789";
    	    var hyphencount = 0;
    	    if (field.length != 10) {
    	        alert("Please enter your 10 digits phone.");
    	        return false;
    	    }
    	    for (var i = 0; i < field.length; i++) {
    	        temp = "" + field.substring(i, i + 1);
    	        if (valid.indexOf(temp) == "-1") {
    	            alert("Invalid characters in your phone.  Please try again.");
    	            return false;
    	        }
    
    	    }
    	    return true;    
    	}  
    
        
          
        if(!EmailValid(document.second.Email.value))
        {
        	return false;
        }
        function ValidateZip(field) {
    		var valid = "0123456789";
    		var hyphencount = 0;
    		if (field.length!=5 ) {
    			alert("Please enter your 5 digit zip code.");
    			return false;
    		}
    		for (var i=0; i < field.length; i++) {
    			temp = "" + field.substring(i, i+1);
    			if (valid.indexOf(temp) == "-1") {
    				alert("Invalid characters in your zip code.  Please try again.");
    				return false;
    			}
    
    		}
    
    	return true;
    	}
    	function ValidateName(field)
    	{
    		var invalid="0123456789()-+=@#$%^&*!~`{}][|:;<>,?/";
    		for (var i=0; i < field.length; i++) {
    			temp = "" + field.substring(i, i+1);
    			if (invalid.indexOf(temp) != "-1") {
    				alert("Invalid characters in your name field.  Please try again.");
    				return false;
    			}
    
    		}
    		return true;
    	}
       function EmailValid(email)
       	{
       		if(email=="")
       		{
       			alert("Email is required field!")
       			return false
       		}
       		len = email.length
    
       		if((email.charAt(1)=='.')||(email.charAt(1)=='@')||(email.charAt(1)=='.'))
       		{
    			alert("Invalid Email Please try again!")
    			return false
       		}
       		if((email.charAt(len-2)=='@')||(email.charAt(len-2)=='.'))
       		{
    			alert("Invalid Email Please try again!")
    			return false
       		}
    
    
    
       		count=0
       		dotcount=0
    		for (i=0; i< email.length; i++)
    			{
    				if(email.charAt(i)=='@')
    				count++
    				if(email.charAt(i)=='.')
    				dotcount++
       		 	}
    
    		 if((count !=1)||(dotcount !=1))
    			{
    			alert("Invalid Email Please try again!")
    			return false
    			}
    
    
       	return true
    	}
    
    }
    </script>
    Im sure it is something small. i have tried changing things around but it will not work for me.

    Any help would be great.

    Thanks everyone

    Nim

  2. #2
    SitePoint Member
    Join Date
    Sep 2010
    Location
    Shanghai, China
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi, alibaba01, I checked your code and found the way you validated email was too complext. Why not using regular expression to reach it? For something like this;

    Code:
    function EmailValidate(emailAddr){ 
      if(!emailAddr){return false; }
      //trim email string
      return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(emailAddr);
    }
    I've tested the email format you given: abc.def@gmail.com abc+def@gmail.com. Both of them work well!

  3. #3
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi goddy128, thank you for your super fast reply. Just to make sure i have it right, is this how it will be added then

    Code:
    function validate_form() {
    
        if (!ValidatePhoneNumber(document.second.HomePhone.value)) {
            return false;
        }
    	if(!ValidateZip(document.second.ZipCode.value))
    	{
    			return false;
    	}
        if ( document.second.degreeLevel.selectedIndex == 0 )
        {
            alert ( "Please select degree level" );
            return false;
        }
    	if ( document.second.ProgramInterest.selectedIndex == 0 )
        {
            alert ( "Please select area of study" );
            return false;
        }
    	if ( document.second.LevelofEducationCompleted.selectedIndex == 0 )
        {
            alert ( "Please select level of education" );
            return false;
        }
    
    	 if ( document.second.FirstName.value == '' )
        {
            alert ( "Please Enter a First Name" );
            return false;
        }
    
    	 if ( document.second.LastName.value == '' )
        {
            alert ( "Please Enter a Last Name" );
            return false;
        }
    
    	 if ( document.second.Address.value == '' )
        {
            alert ( "Please Enter a Address" );
            return false;
        }
    
    	 if ( document.second.City.value == '' )
        {
            alert ( "Please Enter a City" );
            return false;
        }
    
    	 if ( document.second.State.selectedIndex == 0 )
        {
            alert ( "Please Select a State" );
            return false;
        }
    	if(!ValidateName(document.second.FirstName.value))
    	{
    		return false;
    	}
    	if(!ValidateName(document.second.LastName.value))
    		{
    			return false;
    	}
    
    	
    	function ValidatePhoneNumber(field) {
    	    var valid = "0123456789";
    	    var hyphencount = 0;
    	    if (field.length != 10) {
    	        alert("Please enter your 10 digits phone.");
    	        return false;
    	    }
    	    for (var i = 0; i < field.length; i++) {
    	        temp = "" + field.substring(i, i + 1);
    	        if (valid.indexOf(temp) == "-1") {
    	            alert("Invalid characters in your phone.  Please try again.");
    	            return false;
    	        }
    
    	    }
    	    return true;    
    	}  
    
        
          
        if(!EmailValid(document.second.Email.value))
        {
        	return false;
        }
        function ValidateZip(field) {
    		var valid = "0123456789";
    		var hyphencount = 0;
    		if (field.length!=5 ) {
    			alert("Please enter your 5 digit zip code.");
    			return false;
    		}
    		for (var i=0; i < field.length; i++) {
    			temp = "" + field.substring(i, i+1);
    			if (valid.indexOf(temp) == "-1") {
    				alert("Invalid characters in your zip code.  Please try again.");
    				return false;
    			}
    
    		}
    
    	return true;
    	}
    	function ValidateName(field)
    	{
    		var invalid="0123456789()-+=@#$%^&*!~`{}][|:;<>,?/";
    		for (var i=0; i < field.length; i++) {
    			temp = "" + field.substring(i, i+1);
    			if (invalid.indexOf(temp) != "-1") {
    				alert("Invalid characters in your name field.  Please try again.");
    				return false;
    			}
    
    		}
    		return true;
    	}
      
    function EmailValidate(emailAddr){ 
      if(!emailAddr){return false; }
      //trim email string
      return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(emailAddr);
    }
    
    }
    When i test it like the above code, it allows me to submit the form with out validating the email. So i can leave the email section blank and the form gets submitted. Maybe i am missing something.

    Here is my html code for the email part

    HTML Code:
    <input name="Email" type="text" class="third_box" id="email_address" />
    Thank you for your time in responding

    Nim

  4. #4
    SitePoint Member
    Join Date
    Sep 2010
    Location
    Shanghai, China
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi, What is the "second" element? If possible, ou can send your entire code including html and script to me via email(goddy128@gmail.com). I'll help you find the problem and fix it to you via email back!

  5. #5
    SitePoint Member
    Join Date
    Sep 2010
    Location
    Shanghai, China
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi, nim, you declare the function with name: EmailValidate
    But, you call it via the name: EmailValid
    That's why it doesn't work!

  6. #6
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow that works great thanks you so much for your hard work. Just a quick question do you know how to make it display

    Email is required field!

    so when they fill the form out with the wrong information a window pops up saying Email is required field!

    Thanks for all your help

    Ni,

  7. #7
    SitePoint Member
    Join Date
    Sep 2010
    Location
    Shanghai, China
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi , nim. If possible, you can chat with me via gtalk to describe your question more concisely.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •