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 11 of 11
  1. #1
    Regular Coder
    Join Date
    Oct 2002
    Location
    USA
    Posts
    255
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Email form problem

    I am using the following script to check empty fields in my email forms.

    However, it has two problems:

    1- Although the intended alert is "Fill out all fields", the script returns "undefined" for empty fields.
    2- After returning the alert "undefined" it still tries to submit the page, although the intended function is just the alert itself.

    In what way should I edit the script in order to make it work the right way.

    Thanks!


    Code:
    function validate()
    { 
    	for(i=0;i<document.form2.elements.length;i++)
    	{
    		if(document.form2.elements[i].type.toLowerCase()=='checkbox')
    		{  
    			if(document.form2.elements[i].checked==false)
    			{
    				if(document.form2.elements[i].faultDescription=="")
    				{                             
    					alert('Fill out all fields');
    					return false;
    				}
    				else
    				{
    					alert(document.form2.elements[i].faultDescription); 
    					return false;
    				}
    			}
    		}
    		else
    		{
    			if(document.form2.elements[i].value=="")
    			{
    				if(document.form2.elements[i].faultDescription=="")
    				{                             
    					alert('Fill out all fields');
    					return false;
    				}
    				else
    				{
    					alert(document.form2.elements[i].faultDescription); 
    					return false;
    				}		
    			}
    		}
    	}
    	document.form2.submit();
    }

  • #2
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Try using getElementById instead of the short-hand (document.form.element).

  • #3
    Regular Coder
    Join Date
    Oct 2002
    Location
    USA
    Posts
    255
    Thanks
    1
    Thanked 0 Times in 0 Posts
    So wherever I see document.form2.elements, I will replace it with getElementById?

    Did I get it right?

    And how will I name the form?

  • #4
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by ssskaya
    And how will I name the form?
    you are having a laugh, aren't you?
    *keep it simple (TM)

  • #5
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Post all your code.

  • #6
    Regular Coder
    Join Date
    Oct 2002
    Location
    USA
    Posts
    255
    Thanks
    1
    Thanked 0 Times in 0 Posts
    All is all the .js code that I use.

    For HTML, I use the following to submit:

    Code:
    <input name="Button" type="Submit" onclick="validate();" value="Send">
    Do you mean that there is something missing in the .js code ?

  • #7
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    No, I mean all the HTML.

  • #8
    Regular Coder
    Join Date
    Oct 2002
    Location
    USA
    Posts
    255
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Here it is:


    Code:
      <form method="post" action="submitcontent.pl" name="form2" >
      <input type="hidden" name="recipient" value="info@site.com">
      <input type="hidden" name="feedback" value="Discuss/Edit submitted!">
      <input type="hidden" name="redirect" value="thanks.html">      
          <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr> 
              <td><hr /> <br /></td>
            </tr>
            <tr> 
              <td>Name</td>
            </tr>
            <tr> 
              <td><input type="text" size="25" name="Name" faultdescription="Please fill out all fields." /></td>
            </tr>
            <tr> 
              <td>&nbsp;</td>
            </tr>
            <tr> 
              <td>City</td>
            </tr>
            <tr> 
              <td><input type="text" size="25" name="City" faultdescription="Please fill out all fields." /></td>
            </tr>
            <tr> 
              <td>&nbsp;</td>
            </tr>
            <tr> 
              <td>State/Country</td>
            </tr>
            <tr> 
              <td><input type="text" size="25" name="State/Country" faultdescription="Please fill out all fields." /></td>
            </tr>
            <tr> 
              <td>&nbsp;</td>
            </tr>
            <tr> 
              <td>E-mail</td>
            </tr>
            <tr> 
              <td><input type="text" size="25" name="Email" faultdescription="Please fill out all fields." /></td>
            </tr>
            <tr> 
              <td>&nbsp;</td>
            </tr>
            <tr> 
              <td>Name of Topic:</td>
            </tr>
            <tr> 
              <td><input type="text" size="25" name="Name of Topic" faultdescription="Please fill out all fields." /></td>
            </tr>
            <tr> 
              <td>&nbsp;</td>
            </tr>
            <tr> 
              <td>Text Area</td>
            </tr>
            <tr> 
              <td><textarea name="Message" rows="15" cols="40"></textarea></td>
            </tr>
            <tr> 
              <td>&nbsp;</td>
            </tr>
            <tr> 
              <td><input type="checkbox" name="agree" value="yes" faultdescription="You have to agree to the terms to subscribe."> 
                &nbsp;&nbsp;By clicking &quot;Send&quot;, I agree to the <a href="termsofuse.html">Terms 
                of Use</a>.</td>
            </tr>
            <tr> 
              <td>&nbsp;</td>
            </tr>
            <tr> 
              <td><input name="Button" type="Submit" onclick="validate();" value="Send"></td>
            </tr>
            <tr> 
              <td><br /> <hr /></td>
            </tr>
          </table>
      </form>

  • #9
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    	<head>
    		<title>50157</title>
    		<style type="text/css">
    			label
    			{
    				width:13em;
    				float:left;
    			}
    		</style>
    	</head>
    	<body>
    		<form id="form2" name="form2" method="post" action="submitcontent.pl" onsubmit="return validate()">
    			<input type="hidden" name="recipient" value="info@site.com">
    			<input type="hidden" name="feedback" value="Discuss/Edit submitted!">
    			<input type="hidden" name="redirect" value="thanks.html">
    
    			<fieldset>
    				<legend>User</legend>
    				<div>
    					<label for="name">Name</label>
    					<input id="name" name="name" type="text" size="25" faultdescription="Please fill out all fields.">
    				</div>
    
    				<div>
    					<label for="city">City</label>
    					<input id="city" name="city" type="text" size="25" faultdescription="Please fill out all fields.">
    				</div>
    
    				<div>
    					<label for="state_country">State/Country</label>
    					<input id="state_country" name="state_country" type="text" size="25" faultdescription="Please fill out all fields.">
    				</div>
    
    				<div>
    					<label for="email">E-mail</label>
    					<input id="email" name="email" type="text" size="25" faultdescription="Please fill out all fields.">
    				</div>
    			</fieldset>
    
    			<fieldset>
    				<legend>Topic</legend>
    				<div>
    					<label for="topic">Name</label>
    					<input id="topic" name="topic" type="text" size="25" faultdescription="Please fill out all fields.">
    				</div>
    
    				<div>
    					<label for="message">Text Area</label>
    					<textarea id="message" name="message" rows="15" cols="40"></textarea>
    				</div>
    
    				<div>
    					<label for="agree">I agree to the <a href="termsofuse.html">Terms of Use</a>.</label>
    					<input id="agree" name="agree" type="checkbox" value="yes" faultdescription="You have to agree to the terms to subscribe.">
    				<div>
    			</fieldset>
    
    			<input type="submit" value="Send">
    		</form>
    
    		<script type="text/javascript">
    			function validate()
    			{
    				var elements = document.getElementById("form2").getElementsByTagName("input");
    				for(var i = 0; i < elements.length; i++)
    				{
    					if(elements[i].getAttribute('type').toLowerCase == 'hidden' || elements[i].getAttribute('type').toLowerCase == 'submit')
    					{
    						continue;
    					}
    
    					if(elements[i].getAttribute('type').toLowerCase() == 'checkbox')
    					{
    						if(elements[i].checked == false)
    						{
    							if(elements[i].getAttribute('faultDescription') == "")
    							{
    								alert('Fill out all fields');
    								return false;
    							}
    							else
    							{
    								alert(elements[i].getAttribute('faultDescription'));
    								return false;
    							}
    						}
    					}
    					else
    					{
    						if(elements[i].value == '')
    						{
    							if(elements[i].getAttribute('faultDescription') == '')
    							{
    								alert('Fill out all fields');
    								return false;
    							}
    							else
    							{
    								alert(elements[i].getAttribute('faultDescription'));
    								return false;
    							}
    						}
    					}
    				}
    				return true;
    			}
    		</script>
    	</body>
    </html>

  • #10
    Regular Coder Crake's Avatar
    Join Date
    Dec 2004
    Location
    Loose, Maidstone, KENT
    Posts
    577
    Thanks
    0
    Thanked 3 Times in 3 Posts

    hemebond

    hemebonds code works i tested tht

  • #11
    Regular Coder
    Join Date
    Oct 2002
    Location
    USA
    Posts
    255
    Thanks
    1
    Thanked 0 Times in 0 Posts
    you did not only a fix the problem but also a improved the scripts with tableless code.

    Plus, it looks much better.

    Your help is highly appreciated.

    Thank youvery much.


  •  

    Posting Permissions

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