Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Senior Coder
    Join Date
    May 2004
    Posts
    1,441
    Thanks
    15
    Thanked 0 Times in 0 Posts

    debug validation code

    I have the following validation code. For some strange reason it is letting the form submit even when the fields are empty.
    It is strange as it works for a few of the fields. it works for name,zip,email (but just the validation for blank email and not valid email) The rest don't seem to work.

    Can anyone spot what I am doing wrong?

    Code:
    function checkMyForm() {
    
     if ( document.Form.firstname.value == "" )
            {
                    alert ( "Please fill in the 'First Name' box." );
    				setFocus(document.Form.firstname);
                    return false;
            }
           
    	     
    	else if ( document.Form.lastname.value == "" )
            {
                    alert ( "Please fill in the 'Last Name' box." );
                   setFocus(document.Form.lastname);
    			    return false;
            }
    
          else if ( document.Form.address.value == "" )
            {
                    alert ( "Please fill in the 'Address' box." );
    				setFocus(document.Form.lastname);
                    return false;
            }
    
             else if ( document.Form.city.value == "" )
            {
                    alert ( "Please fill in the 'City' box." );
    				setFocus(document.Form.city);
                    return false;
            }
    
            
    		 else if (document.Form.zip.value.length <= "0"){
    	alert("Please enter a zip.");
    	setFocus(document.Form.zip);
    		return false;
    }
    
     
    
    
     else if (document.Form.zip.value.length < "5"){
    	alert("Please enter a zip.");
    	setFocus(document.Form.zip);
    		return false;
    }
    
    else if (document.Form.zip.value.length > "5"){
    	alert("Zip code can only be 5 numbers");
    	setFocus(document.Form.zip);
    		return false;
    }
    
    		
    else  if (document.Form.email.value == "")
      {
         alert ( "Please fill in the Email box." );
           setFocus(document.Form.email);
    	         return false;
            }
    		else if (!isEmailAddr(document.form.email.value)){
      alert("Please Enter a valid email address"); 
    	setFocus(document.Form.email);
    		 return false;
    		 
      }
    	else if (document.Form.home_phone1.value== "")
    		 
       {
    	alert("Area Code cannot be blank");
    	setFocus(document.Form.home_phone1);
    		return false;
    }
    
     else if (document.Form.home_phone2.value== "")
    		 
       {
    	alert("Home phone cannot be blank");
    	setFocus(document.Form.home_phone2);
    		return false;
    }
    
    
    else if (document.Form.home_phone3.value== "")
    		 
       {
    	alert("Home phone cannot be blank");
    	setFocus(document.Form.home_phone3);
    		return false;
    }
     
    else if (document.Form.amount.value== "0"){
    	alert("Please choose a amount");
    	setFocus(document.Form.amount);
    		return false;
    }
    
    else { 
    		return true; 
    	} 
    } 
    
    function isEmailAddr(email)
    {
      var result = false
      var theStr = new String(email)
      var index = theStr.indexOf("@");
      if (index > 0)
      {
        var pindex = theStr.indexOf(".",index);
        if ((pindex > index+1) && (theStr.length > pindex+1))
    	result = true;
      }
      return result;
    }
    
    function setFocus(aField) {
    	aField.focus();
    }

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Look here:
    Code:
    else if (!isEmailAddr(document.form.email.value)){

  • #3
    Senior Coder
    Join Date
    May 2004
    Posts
    1,441
    Thanks
    15
    Thanked 0 Times in 0 Posts
    Thanks


  •  

    Posting Permissions

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