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!

ASP Page Textarea validation

Status
Not open for further replies.

smurfer

Programmer
Jun 8, 2001
57
US
I am trying to create a page that has one textarea on it, i.e. named textarea1. The form on this page posts to a new page, which is generating an email. I want to check on page 2 if any values were entered in the textarea, and if so include it in the message, otherwise exclude it. The problem is the statement,
IF Request.Form(&quot;textarea1&quot;) <> &quot;&quot; Then
happens either way if someone enteres or someone does not?
Is there an easy way to validate that the box is not blank?
Please help...
Thanks,
SM
 
Be sure to have a name attribute for the form and then at bottom after </form> do your IF statement
And on the the Forms type for submitting info, do Button instead of Submit.

(<input type=&quot;button&quot; value=&quot;Add to Database&quot; name=&quot;btnSubmit&quot;> )

... below is what mine looks like...


<SCRIPT LANGUAGE=vbscript>
Sub btnSubmit_onclick()

If Len(frmNew.title.value) = 0 THen
Alert &quot;Please enter a Title!&quot;
frmNew.title.focus
exit sub
End IF
Call frmNew.submit()
end sub
</script>

... something like that ... (frmNew is name of my form)
Mary :)

Rule 1: Don't sweat the small stuff.
Rule 2: EVERYTHING is small stuff!! X-)
 


smurfer,

This does client-side validation so it doesn't need to go to the server to make sure someone input something in the textarea.

fengshui1998

<form name=&quot;usrform&quot; method=&quot;post&quot; action=&quot;Submit.asp&quot; onsubmit=&quot;return check()&quot;>
Comments
<textarea rows=3 name=&quot;comments&quot; cols=50></textarea>
</form>



<SCRIPT LANGUAGE=&quot;Javascript&quot;>
function check()
if (document.usrform.comments.value.length = 0 ) {
alert(&quot;You must enter something.&quot; )
return false
}
else {
return true
}
</SCRIPT>
 
Mary,
Is there a way to do it without requiring an entry? See the textarea is an additional comments box, (not required) so I only want to include it if it has text entered by user. But since a textarea value seems to count whitespace or something, it thinks theres an entry there. So I wanted something on my page 2 that checks for its value, then if it is not blank, include it, otherwise ignore it?
SM
 
Well, then I don't think you need to do anything special on that page then. Just a regular for and regular Submit. THen when you get to the next page, check to see if there is data in the textbox by doing a request.form(&quot;textbox&quot;) and do something with it if there is. Mary :)

Rule 1: Don't sweat the small stuff.
Rule 2: EVERYTHING is small stuff!! X-)
 


smurfer,

Do not check for a null string. Check and see if there was an entry.

If len( Trim( Request.Form(&quot;textarea1&quot;) ) ) > 0 then
your code....
Else


End if

Cheers,
fengshui1998
 
fengshui,
thanks for the tip however it still did not work as it appears as though the textarea is assigned a length of 10, I guess due to the whitespace? I added a j-script function called on the submit, to alert the length of the textarea, and it returns as 10..I even added an entry and it adds the # of chars entered to 10, so I guess by default it has a length of 10 somehow, unexplicably..the value itself still appears as blank, so my code that works is now,
If Len(Request.Form(&quot;comments&quot;)) = 10 Then
' do nothing as entry is blank
Else
strbody = strBody & &quot;The comments are: &quot;
srBody = strBody & Request.Form(&quot;Comments&quot;)

This seems to have fixed it.

Thanks for the help...
SM
 


smurfer,

There should be no way that if you left the text area blank, it returns a length of 10. This should tell you from the client-side what is inserted in the textarea.

fengshui1998

<SCRIPT LANGUAGE=&quot;Javascript&quot;>
function check()
if (document.usrform.comments.value.length <> 0 ) {
alert(&quot;value: &quot; + document.usrform.comments.value + &quot; end&quot;)
return false
}
else {
return true
}
</SCRIPT>
 
if request.form(&quot;textarea1&quot;)=&quot;&quot; then
response.redirect(&quot;your original previous page url&quot;)
end if

But try to do with client side validation

Regards,
Mathan.
 
If TRIM(Request.Form(&quot;textarea1&quot;) = &quot;&quot; then
Do something if no text entered
Else
Do something if text is entered
End If

-GTM Solutions, Home of USITE-
-=
 
I think the problem is on your html page where the TEXTAREA is. I had same problem but now is solved...

If u want to use TEXTAREA tag u must close him and put the closing tag on the same line cuz if not you will not receive an empty TEXTAREA value at server
this is the right way...
<TEXTAREA name=TEXTAREA1 id=TEXTAREA1></TEXTAREA>
the wrong way
<TEXTAREA name=TEXTAREA1 id=TEXTAREA1>
</TEXTAREA>

Hope this solve your problem
And do not use TRIM function in an text field cuz u may alter the text inside...
________

George
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top