Hi All
I am new to Javascript and am unable to get the validation to work for the text area or comments section of my form. I got it to work for the regular text fields but for some reason it doesn't work for the text area. I also need to validate the rating area. I appreciate your help!!
Here's what I tried first:
<script type="text/javascript">
function validateForm()
{
if (document.forms[0].comments.value == "" )
{
alert("Please enter your comments.");
form[0].comments.focus();
return false;
} // end if
return true;
}
// end function validateForm
</script>
Then I did some research and found this other method. Here's the entire file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Julie Mazza Web Form</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
<script type="text/javascript">
function validateForm()
{
var comments = document.getElementById("comments");
if ($.trim(comments.value) == '') {
alert("Please Write Your Comments");
return false;
} else {
return true;
}
}
</script>
</head>
<body>
<div id="wrapper">
<div id="header"></div>
<h1>Sample Feedback Form</h1>
<form method="post" action="index.xhtml" onsubmit="return validateForm()" >
<fieldset>
<label>First Name:<input type="text" name="firstname" id="firstname" /></label>
<br /> <br />
<label> Last Name:<input type="text" name="lastname" id="lastname" /></label>
<br /> <br />
<label>Street Address:<input type="text" name="stradd" id="stradd" /></label>
<br /> <br />
<label>City:<input type="text" name="city" id="city" /></label>
<br /> <br />
<label>State:<input type="text" name="state" id="state" /></label>
<br /> <br />
<label>Zip:<input type="text" name="zip" id="zip" /></label>
<br /> <br />
<label>Country:<input type="text" name="Country" id="Country" /></label>
<br /> <br />
<label>Your email:<input type="text" name="FromAddress" id="FromAddress" /></label>
<br /> <br />
</fieldset>
<fieldset>
Your Comments:<textarea name="comments" id="comments" cols="40" rows="2" >
Enter your comments here </textarea>
<br /> <br />
</fieldset>
<fieldset>
Rate this site<br />
<input type="radio" name="rating" id="rating5" value="5" /> 5 - Excellent <br />
<input type="radio" name="rating" id="rating4" value="4" /> 4 - Very Good <br />
<input type="radio" name="rating" id="rating3" value="3" /> 3 - Average <br />
<input type="radio" name="rating" id="rating2" value="2" /> 2 - Below Average <br />
<input type="radio" name="rating" id="rating1" value="1" /> 1 - Poor <br />
</fieldset>
<p>
<input type="hidden" name="ToAddress" value="jmazza3@nycap.rr.com" />
<input type="hidden" name="CCAddress" value="" />
<input type="hidden" name="Subject" value="WSD: Assignment 2.2 - Web Form for Julie Mazza" />
</p>
<fieldset>
<input type="reset" value="Reset" />
<input type="submit" value="Submit" />
</fieldset>
</form>
</div>
</body>
</html>