![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 3
![]() |
How to check a <textarea> field in Javascript?
Hi,
I created a little form on my website that contains a textarea for users to enter comments. Upon clicking submit, my validation function will check various things. I would like to check to see if the user entered anything in the comments section. If they didn't, I'd like to pop-up a confirm dialog box asking if they're sure they want to submit with no comments. The problem is that I'm not sure how the if condition should look like to check the field. I tried the following but it doesn't seem to work. The <textarea> is named comment and there is no default value for it. if (form.comment.value == "") {
answer = confirm("No comments entered, are you sure?");
} |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 3
![]() |
Sorry, please ignore this. I found out I have an odd problem on my form. I need to also use a regular expression to check for spaces in the comments.
![]() |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Oct 2005
Location: India
Posts: 30
Rep Power: 0
![]() |
<html>
<head>
<title>Programming forum
</title>
</head>
<script language="javascript">
document.write("ENTER YOUR COMMENT");
function valuetest()
{
if(test.comment.value=="")
{
alert("No comments entered");
}
}
</SCRIPT>
<form Name="test">
<input type="text" name="comment" value="">
<P><input type="Submit" onClick="valuetest()"></P>
</form>
</body>
</html>This code need to be linked with CGI script. |
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Not only is that code far more complicated than it should be, it's also wrong. Your function doesn't return anything.
Oh, and don't use document.write. |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
And remember to open your body tags (you have a closing one, strangely enough), and not mix uppercase and lowercase tags, or just don't even use uppercase tags. Tag soup is bad.
|
|
|
|
|
|
#6 |
|
Programming Guru
![]() |
Just for future reference, in case anyobdy searches for this later... the basic ways to access a form are:
document.forms["form_name"]["input_name"] document.forms[form_index][input_index] document.forms.form_name.Input_name You can also do some weird hacks, but that covers the rudiments.
__________________
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|