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 4 of 4
  1. #1
    Banned
    Join Date
    Jan 2004
    Location
    Minneapolis, MN
    Posts
    387
    Thanks
    0
    Thanked 0 Times in 0 Posts

    simple form validation, please tell me what i'm doing wrong

    it's running the function, because if i put an alert as the first statement, it will alert me. however the if condition is somenow not being met, or my DOM access is incorrect.. i dunno.. either way, the form will go through with no data, sending an empty email.

    Code:
    <script type="text/javascript">
    function validate_contact() {
    	var contact = document.getElementById("contact");
    	if (contact.comments.value == "") {
    		alert("Comments are required.");
    		return false;
    	}
    }
    </script>
    Code:
    <form id="contact" method="post" action="contact.cfm">
    	<fieldset>
    		<label for="name">Name:</label>
    		<input type="text" name="name" id="name" />
    		<label for="email">Email:</label>
    		<input type="text" name="email" id="email" />
    		<label for="phone">Phone:</label>
                    <input type="text" name="phone" id="phone" />
    	</fieldset>
    	<fieldset>
    		<label for="comments" id="comments_label">Comments:</label>
    		<textarea name="comments" id="comments" rows="5" cols=""></textarea>
    	</fieldset>
    	<fieldset>
    		<input type="submit" name="submit" id="submit" value="Send" class="button" onclick="return validate_contact()" />
                    <input type="reset" name="reset" id="reset" value="Clear" class="button" />
    		<input type="hidden" name="submitted" value="yes" />
    	</fieldset>
    </form>

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Why do you go through the form, why not access the textarea directly?

    Code:
    <script type="text/javascript">
    function validate_contact() {
     var comments = document.getElementById("comments");
     if (comments.value == "") {
      alert("Comments are required.");
      return false;
     }
    }
    </script>
    But maybe you have some reason for doing it that way? I don't know that you can just do that: formName.formElement, especially because your inputs aren't directly beneath the form, they're within a fieldset. Perhaps if you gave an id to the fieldset, you could do something like fieldsetId.comments? or even contact.fieldsetId.comments?

    Let me know, as I'd be curious to your solution.

    Sadiq.

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I put your code into a simple page and it works just fine in both IE 6 and Firefox.

    Are you sure that no other part of the page that you didn't post isn't interfering somehow?
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #4
    Banned
    Join Date
    Jan 2004
    Location
    Minneapolis, MN
    Posts
    387
    Thanks
    0
    Thanked 0 Times in 0 Posts
    well, i guess something MUST be interfering, because it's such a simple script, and i use essentially the same thing on my site and it works.

    *edit: well, i switched it to access comments directly, and that works. *shrug* thanks.
    Last edited by coder_seth; 05-05-2004 at 11:03 PM. Reason: solved problem


  •  

    Posting Permissions

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