Submit Your Article Forum Rules

Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Form Validation

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    0

    Form Validation

    I have my form working with my validation, but when I try to validate the message area and I hit send, it sends the message to the email address with no message. I am also trying to validate my captcha code, but not to sure how to go about it. I couldnt post my whole page because I am new poster and not allow to due to the fact that I only have 5 posts.



    Code:
    <script type="text/javascript">
    function ValidateContactForm()
    {
         var name = document.ContactForm.name;
        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 (comment.value == "")
        {
            window.alert("Please provide a detailed description or comment.");
            text.focus();
            return false;
        }
        return true;
    }
    </script>

  2. #2
    Junior Member
    Join Date
    Aug 2011
    Posts
    0

    Form

    Code:
    <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="author" 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>

  3. #3
    Administrator weegillis's Avatar
    Join Date
    Oct 2003
    Posts
    5,788
    First thing I spotted was a label that's misidentifying its respective control:

    Code:
    <label for="name">Name (* Required):</label> <input name="name" type="text" class="input_field" id="author" maxlength="40" />


    LABEL identifies its associated control by ID=, not NAME=. Your label should read 'for="author".



  4. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    0
    Quote Originally Posted by weegillis View Post
    First thing I spotted was a label that's misidentifying its respective control:

    Code:
    <label for="name">Name (* Required):</label> <input name="name" type="text" class="input_field" id="author" maxlength="40" />


    LABEL identifies its associated control by ID=, not NAME=. Your label should read 'for="author".


    I didnt notice that. I thought the label and ID were suppose to be named the same.

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    0
    Do I need to define my variables? So far, I've checked around some examples and its not included.

  6. #6
    Administrator weegillis's Avatar
    Join Date
    Oct 2003
    Posts
    5,788
    Quote Originally Posted by starr_2005 View Post
    I didnt notice that. I thought the label and ID were suppose to be named the same.
    The NAME on your control is the variable identifying the associated field content that the handler receives, parses out, validates and reconstitutes into the outgoing email.

    The ID is used by the browser to locate and identify the control in the DOM. This is why LABEL refers to ID, not NAME.

  7. #7
    Administrator weegillis's Avatar
    Join Date
    Oct 2003
    Posts
    5,788
    Quote Originally Posted by starr_2005 View Post
    Do I need to define my variables? So far, I've checked around some examples and its not included.
    Your variables are defined by the NAME that you assign to the control input data. If you have three controls, one for the person's name, one for their e-mail and one for their comment, then their names respectively might be (arbitrarily) "myname", "myemail", & "mycomments".

    The handler will parse these names out of the $POST_VARIABLES, and then refer to their associated data by $myname, $myemail, and $mycomments.

  8. #8
    Administrator weegillis's Avatar
    Join Date
    Oct 2003
    Posts
    5,788
    I should add to above that the NAME on the control does not have to match the ID, but sometimes it makes it easier to debug. There is good reason for these two remaining independent--it fits in with dynamic creation of forms from templates.

  9. #9
    Junior Member
    Join Date
    Aug 2011
    Posts
    0
    So, my validation will work fine without how I defined my variables before my first IF statement? And I will agree, sometimes for me, I do find it easy that the name on control matches the ID.

  10. #10
    Senior Member
    Join Date
    Jul 2003
    Posts
    114
    You didn't post your entire form, but I noticed in your javascript that you check for an empty value in "comment" then place focus on "text". Which is the name you gave to your textarea?
    I would also note that javascript client-side validation is strictly for user friendliness and has no bearing on security. You need to validate in your php file, server-side. The javascript validating is ok, but optional.
    The name and id attributes are two separate things. They could have the same value or not. If you should ever wish to apply labels to radio buttons, they would have to have different values.

Posting Permissions

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