Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   JavaScript and Client-Side Browser Scripting (http://www.programmingforums.org/forum23.html)
-   -   How to check a <textarea> field in Javascript? (http://www.programmingforums.org/showthread.php?t=7782)

aznluvsmc Dec 31st, 2005 10:17 PM

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?");
    }


aznluvsmc Dec 31st, 2005 10:22 PM

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. :)

Uday Jan 7th, 2006 11:59 PM

:

<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.

Ooble Jan 8th, 2006 6:45 AM

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.

Cerulean Jan 8th, 2006 10:30 AM

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.

tempest Jan 8th, 2006 12:29 PM

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.


All times are GMT -5. The time now is 11:25 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC