Results 1 to 7 of 7

Thread: Form Validation

  1. #1
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Form Validation

    I need some help validating my form. I have what I think is right, but when I run it, it stills sends the email even without the name of the person. Also, I having trouble validating my captcha code, not too sure how to go about validating that.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Contact</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
    
    <script type="text/javascript">
    function ValidateContactForm()
    {
        var author = document.ContactForm.author;
        var email = document.ContactForm.email;
        var text = document.ContactForm.text;
    
        
    	if (name.value == "")
        {
            window.alert("Please enter your name.");
            name.focus();
            return false;
        }
        
        if (email.value == "")
        {
            window.alert("Please enter a valid e-mail address.");
            email.focus();
            return false;
        }
        if (email.value.indexOf("@", 0) < 0)
        {
            window.alert("Please enter a valid e-mail address.");
            email.focus();
            return false;
        }
        if (text.value == "")
        {
            window.alert("Please provide a detailed description or comment.");
            text.focus();
            return false;
        }
        return true;
    }
    </script>
    </head>
    <body>
    
    <div id="templatemo_wrapper"> 
    
    	<div id="templatemo_header">
    
     
            
            <ul id="social_box">
                        <li><a href="http://www.facebook.com/"><img src="images/facebook.png" alt="facebook" /></a></li>
                <li><a href="http://www.twitter.com/"><img src="images/twitter.png" alt="twitter" /></a></li>              
            </ul>
            
          	<div id="site_title">
                <h1><a href="index.htm"><img src="images/logo2.png" alt="logo" /><span></span></a></h1>
            </div> <!-- end of site_title -->
            
          
        </div> <!-- end of templatemo_header -->
        
    
    
    <!-- end of templatemo_menu -->
        
        <div id="templatemo_content_wrapper">
        	<div id="templatemo_content_top"></div>
            <div id="templatemo_content">
            
                <h2>Contact</h2>
    
    
    
    <p><b>E-mail: starr05@gmail.com </b></p>
    
    <p><b><a href="http://www.twitter.com/">Twitter</a></b></p>
            
                <div class="cleaner_h50"></div>
                
                	<div id="contact_form">
                
                        <h4>Quick Contact</h4>
                        
                        <form method="post" name="ContactForm" id="contact" action="email.php" onsubmit="return ValidateContactForm();">
                            
                            <div class="col_w340 float_l">
                            
                                <label for="name">Name (* Required):</label> <input name="name" type="text" class="input_field" id="name" maxlength="40" />
                              	<div class="cleaner_h10"></div>
    
     <label for="subject">Subject:</label> <input name="subject" type="text" class="input_field" id="subject" maxlength="40" />
                              	<div class="cleaner_h10"></div>
    
                                
                                <label for="email">Email (* Required):</label> <input name="email" type="text" class="input_field" id="email" maxlength="40" />
                              	<div class="cleaner_h10"></div>
                                
                                
    Type verification image:<br />
    <input name="verif_box" type="text" id="verif_box" style="padding:2px; border:1px solid #CCCCCC; width:180px; height:14px;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px;"/>
    <img src="verificationimage.php?<?php echo rand(0,9999);?>" alt="verification image, type it in the box" width="50" height="24" align="absbottom" /><br />
    <br />                        
    
    </div>
                            
    
    
                            <div class="col_w340 float_r">
                            
                                <label for="text">Message (*Required):</label> <textarea id="text" name="text" rows="0" cols="0" class="required"></textarea>
                                <div class="cleaner_h10"></div>
                                
                                <input type="submit" class="submit_btn float_l" name="submit" id="submit" value="Send" />
                                <input type="reset" class="submit_btn float_r" name="reset" id="reset" value="Reset" />
                                
                            </div>
                            
                       </form>
    
                </div> 
    
                <div class="cleaner"></div>
                
            </div>
            <div id="templatemo_content_bottom"></div>
    	</div>
        
        <div id="templatemo_sp_box">
        
        	<div class="col_w340 float_l">
             
    			</div>
            </div>
            
            <div class="col_w340 float_r">
            
    			</div>
            </div>
        
        </div>
        
        <div id="templatemo_footer">
    
          
            Copyright  2011 <a href="www.twitter.com/">Starr</a><br/> 
            <a href="http://www.iwebsitetemplate.com" target="_parent">Website Templates</a> 
            by <a href="http://www.templatemo.com" target="_parent">Free CSS Templates</a>
        
        </div> <!-- end of templatemo_footer -->
        
    </div> <!-- end of templatemo_wrapper -->    
    
    </body>
    </html>
    PHP Code:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Contact</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
    </head>
    <body>


    <?php


    $first_name
    =$_POST['name'];
    $email_address=$_POST['email'];
    $subject=$_POST['subject'];
    $message=$_POST['text'];

    mail("starr05@gmail.com","Subject: $subject",
    $message"From: $first_name <$email_address>");

    echo 
    "Thank you for using our mail form.<br/>";
    echo 
    "Your email has been sent.";
    ?>

    PHP Code:


    <?php


    header
    ('Content-type: image/jpeg');

    $width 50;
    $height 24;

    $my_image imagecreatetruecolor($width$height);

    imagefill($my_image000xFFFFFF);

    // add noise
    for ($c 0$c 40$c++){
        
    $x rand(0,$width-1);
        
    $y rand(0,$height-1);
        
    imagesetpixel($my_image$x$y0x000000);
        }

    $x rand(1,10);
    $y rand(1,10);

    $rand_string rand(1000,9999);
    imagestring($my_image5$x$y$rand_string0x000000);

    setcookie('tntcon',(md5($rand_string).'a4xn'));

    imagejpeg($my_image);
    imagedestroy($my_image);
    ?>

  2. #2
    Join Date
    Dec 2004
    Posts
    177
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default

    The name part is easy, your variables don't match. You create the variable "author" and set it to the form field, but then you try to reference the field using a variable called "name".
    Verzeihung!

  3. #3
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Minos View Post
    The name part is easy, your variables don't match. You create the variable "author" and set it to the form field, but then you try to reference the field using a variable called "name".
    So it should I should reference the field using author instead of name?

  4. #4
    Join Date
    Dec 2004
    Posts
    177
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default

    Well,

    Code:
    function ValidateContactForm()
    {
        var author = document.ContactForm.author;
        var email = document.ContactForm.email;
        var text = document.ContactForm.text;
    
        
    	if (name.value == "")
        {
            window.alert("Please enter your name.");
            name.focus();
            return false;
        }
    You could either:
    - change the name.value and name.focus to author.value and author.focus
    - change var author to var name...
    Verzeihung!

  5. #5
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Minos View Post
    Well,

    Code:
    function ValidateContactForm()
    {
        var author = document.ContactForm.author;
        var email = document.ContactForm.email;
        var text = document.ContactForm.text;
    
        
    	if (name.value == "")
        {
            window.alert("Please enter your name.");
            name.focus();
            return false;
        }
    You could either:
    - change the name.value and name.focus to author.value and author.focus
    - change var author to var name...
    I just made the change, and I still get two errors in the error console.
    1. Return statement outside of function line: 19
    2. Object expected line: 45 char: 1

  6. #6
    Join Date
    Dec 2004
    Posts
    177
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default

    Do you have a link to the site? I don't know which files or which lines specifically the error console is referring to.
    Verzeihung!

  7. #7
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    No, not right now sorry.

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
  •