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!

Basic Form Validation has me stumped! 2

Status
Not open for further replies.

mainmast

Programmer
Jun 26, 2003
176
US
I have two asp pages, the first one dynamically posts certain records from a MSACCESS database, and below it, has a form, just a textarea, a couple hidden fields, and a submit button:

<form name=&quot;3&quot; method=&quot;post&quot; action=&quot;addssreply.asp&quot;>
<br>
<b>Subject:</b> Re:
<b><% Response.Write(Session(&quot;rsubject&quot;)) %></b><br>
<b>Message:</b><br>
<textarea name=&quot;rpost&quot; cols=&quot;50&quot; rows=&quot;15&quot; class=&quot;dark&quot;>
<% Response.Write(Session(&quot;rpost&quot;)) %>
</textarea>
<input type=&quot;hidden&quot; name=&quot;parentid&quot; value=&quot;
<% Response.Write(befid) %>&quot;>
<input type=&quot;hidden&quot; name=&quot;done&quot; value=&quot;yes&quot;>
<input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Submit!&quot;>
</form>

On the second page, before I enter it into the database, I do a simple check to make sure the textarea (named rpost in the form above) isn't blank:

if ptext = &quot;&quot; then
Response.Write(&quot;Error, you have left out your message in your reply, please go back in your browser and fill in the field.&quot;)
else
enteri
End if

The problem with this is that it always displays the results of the sub enteri, even if the post is blank.

Any Suggestions?

&quot;Everything is possible, it just depends on how much you want to pay for it.&quot;

-James
 
u can approach this another way ie use javascript to check whether the textarea is blank.

but what u say stumps me:
if ptext = &quot;&quot; then
Response.Write(&quot;Error, you have left out your message in your reply, please go back in your browser and fill in the field.&quot;)
else
enteri
End if

ptext is the texarea value right?(ptext=request(&quot;rpost&quot;))

so even if ptext is empty &quot;enteri&quot; is executed?



Known is handfull, Unknown is worldfull
 
perhaps use Len(Request(&quot;rpost&quot;)) instead? request values are either populated or &quot;&quot; typically so you wont have to worry about a null value kicking an error

 
I know of ways to do this is javascript, but if at all possible I would like to avoid using them. I forgot to mention that I did this exact same thing (except for the variable names) before in a different page and different form, and it works fine. A null value won't really cause any errors for me, I'm working on a forum, and I just dont want to post blank threads. Yes, vbkris, even if ptext is blank, it will still execute &quot;enteri&quot;.

I have never heard of Len(Request()), could you please recomend a link to explain this? Thanks a lot for your help.

&quot;Everything is possible, it just depends on how much you want to pay for it.&quot;

-James
 
For exactly, you should use REQUEST.FORM(&quot;ObjectName&quot;) to get value of that object...
 
I have tried using Request.Form before also.

Any other Suggestions?

&quot;Everything is possible, it just depends on how much you want to pay for it.&quot;

-James
 
simplify!

Get your 'erring' if-then-else statement and try it by itself on a page of its own - is it working now?

If so, then it must be something in your code outside the if-else that's breaking .. check that for an unclosed statement or the like.

If the if-else is still not working on a page by itself, could you post the code for that page in its entirety?


Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
I have also tried using it alone, sorry for not mentioning that I did, and, if you haven't guessed, it didn't work.

I'm assuming when you say 'If the if-else is still not working on a page by itself, could you post the code for that page in its entirety?' that your talking about the page with the if-else statement is not alone, and with the rest of the code, and below is that code:

Code:
<%
Dim conn
Dim rs
Dim sql
Dim pid
Dim fid
Dim psubject
Dim pauthorname
Dim pdate
Dim ptext
Dim pfirst
Dim done
Dim tempvar

pid = Request.Form(&quot;parentid&quot;)
fid = 3
psubject = &quot;Re: &quot; & Session(&quot;rsubject&quot;)
pauthorname = Session(&quot;username&quot;)
pdate = Now()
ptext = Request.Form(&quot;rpost&quot;)
pfirst = &quot;No&quot; 
done = Request.Form(&quot;done&quot;)

'Response.Write(ptext)

if ptext = &quot;&quot; then
Response.Write(&quot;Error, you have left out your message in your reply, please go back in your browser and fill in the field.&quot;)
else
enteri
End if

Sub enteri

Response.Write(&quot;worked&quot;)

End Sub 

'Response.Write(&quot;Parent ID: &quot; & pid & &quot;<br>Forum ID: &quot; & fid & &quot;<br> Subject: &quot; & psubject & &quot;<br>Author Name: &quot; & pauthorname & &quot;<br>Date: &quot; & pdate & &quot;<br>Message: &quot; & ptext & &quot;<br>First: &quot; & pfirst)

Set conn = Server.CreateObject(&quot;ADODB.Connection&quot;)
Set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)

conn.Open &quot;DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=&quot;  & Server.MapPath(&quot;db/forum.mdb&quot;)

'if Request.Form(&quot;done&quot;) = &quot;yes&quot; then
'Validate
'Else
'Response.Write(&quot;Sorry, you have reached this page in error.&quot;)
'End if

'Sub Validate

'if tempvar = &quot;&quot; then
'Response.Write(&quot;You have left out your message in your reply, please go back in your browser and fill in the field.&quot;)
'Else
'Response.Write(&quot;OK&quot;)
'End if

'End Sub





%>

Most of the comments were just previous tests done, so just ignore them.


Any suggestions?




&quot;Everything is possible, it just depends on how much you want to pay for it.&quot;

-James
 
If you dont catch it, these lines of code:

Code:
'Response.Write(&quot;Parent ID: &quot; & pid & &quot;<br>Forum ID: &quot; & fid & &quot;<br> Subject: &quot; & psubject & &quot;<br>Author Name: &quot; & pauthorname & &quot;<br>Date: &quot; & pdate & &quot;<br>Message: &quot; & ptext & &quot;<br>First: &quot; & pfirst)

are all commented out, not just the first, the lines break when viewing it in the forum.

&quot;Everything is possible, it just depends on how much you want to pay for it.&quot;

-James
 
Looking at your form, maybe you've got a return or space or something coming in with the ptext field:

Code:
<textarea name=&quot;rpost&quot; cols=&quot;50&quot; rows=&quot;15&quot; class=&quot;dark&quot;>
<% Response.Write(Session(&quot;rpost&quot;)) %>
</textarea>

Try:

Code:
Response.Write(&quot;[&quot; & ptext & &quot;]&quot;)

to see if you get:

[]

If not, there's a space or return in there...


-------


Also, to explain the Len() function, it's a built in function in VBScript to check the length of a string.

Why don't you try:

Response.Write(Len(ptext))

This should tell you if ptext is a zero-length string or not.

Your IF statement could then be made:

Code:
If Len(ptext) = 0 Then
Response.Write(&quot;Error, you have left out your message in your reply, please go back in your browser and fill in the field.&quot;)
else
enteri
End if




Dave Mc Donald
 
Thank you davemcdonaldireland, I tried writing ptext with the brackets around it as you suggested and got [ ], so I looked back into the form, and, sure enough, there was a space before writing the session to it, I deleted it, and that problem was fixed, but then another was revealed. Typing nothing in the textarea would write the error to the screen, and typing stuff in the textarea would execute 'enteri'. The problem with: &quot;typing stuff in the textarea&quot;, is that now if I just type a bunch of spaces in the textarea alone, it will still execute 'enteri'. Is there away around this?

&quot;Everything is possible, it just depends on how much you want to pay for it.&quot;

-James
 
use the trim() function to get rid of all leading and trailing spaces in a string.

so
Code:
dim strC
strC = &quot;   clarkin    &quot; 
strC = trim(strC)
'strC is now &quot;clarkin&quot;

check for the VBscript reference (has all the functions you can use)


Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Thanks clarkin, I heard of trim() before, but I wasn't aware of its use.

&quot;Everything is possible, it just depends on how much you want to pay for it.&quot;

-James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top