Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

form validation

Status
Not open for further replies.

rastaIT34

Technical User
Sep 9, 2009
103
US
hey all just a quick question.

i'm creating a very simple validation for 3 input fields, name, email and a text area. i figured out the first 2 but i cant get the text area to work.

any thoughts?

this is the javascript code:

Code:
<script type="text/javascript">
function validateForm()

{
var x=document.forms["myForm"]["Full_Name"].value
if (x==null || x=="")
  {
  alert("Full Name must be filled out");
  return false;
  }

var x=document.forms["myForm"]["Email"].value
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
  {
  alert("Not a valid e-mail address");
  return false;
  }

var x=document.forms["myForm"]["Comments_Questions"].value
if (x==null || x=="")
  {
  alert("Please leave a comment.");
  return false;
  }

}
</script>

=========================================

this is the html:


Code:
<form name="myForm" onsubmit="return validateForm()" action="formmail.php" method="post">

<div class="row"><label class="col1">Full Name:&nbsp;&nbsp;</label>
  <span class="col2">
  <input name="Full_Name" class="input" type="text" id="First Name" size="20" tabindex="1"  />
  </span></div>

<br />
<br />
<br />

<div class="row"><label class="col1">Email:&nbsp;&nbsp;</label>
  <span class="col2">
  <input name="Email" class="input" type="text" id="Email" size="20" tabindex="2" />
  </span></div>
  
<br />
<br />
<br />
  
<div class="row"><label class="col1">Comments:&nbsp;<br />Questions&nbsp;&nbsp; </label>
  <span class="col2">
  <textarea name="Comments_Questions" class="input" type="text" id="comments" cols="18" rows="5" tabindex="2" />  
  </textarea>    
  </span></div>
  
  
    <div id="formbuttonsContainer">
      <input type="submit"  class="submitButton"/>
    </div>
 
</form>
 
How does it not work? Copying your code as is and running it works for me. It checks that the textarea is filled in. Showing an alert if its not.

As a suggestion always check the error console in which ever browser you are using. Its there to help you debug.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
I see the problem now.
Your textarea is not empty. It has 5 spaces in it which make neither null nor empty ever. So your check is never true. Notice where the cursor appears in the textbox when you click in it.

Code:
<textarea name="Comments_Questions" class="input" type="text" id="comments" cols="18" rows="5" tabindex="2" />[COLOR=white red]  
  [/color]</textarea>

Make sure the textarea closing tag is flush with the opening tag.





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
AWESOME!!! Thanks PHIL! Ya know i've been having that problem with a lot of my forms and didn't even notice it... thanks a bunch!

Artist/Designer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top