View Full Version : input validation > values still there?
So you have a form which the user needs to fill in and some fields are of course required. Javascript is out since since not every one has it enabled/supported so you use asp to check. The thing is though that if the user has missed and needs to correct it you still want all of the info he entered to be there in the input boxes and menus etc.
There a re a few ways to do it but I'm looking for least bloated method to do this using less code?
I thought about having the form submit to itself -if all feilds a re fine then it response.redirects to the next step -if not it will be back and the input will still be there by doing something like:
<input type="text" name="name" value='<%if Request.Form("name") <> "" then response.write name&"'" else%>' >
:confused:
oracleguy
01-14-2003, 11:48 PM
Originally posted by mat
<input type="text" name="name" value="<%=Request.Form("name")%>" >
Just do that. No need for an if statement or the response.write
whammy
01-14-2003, 11:55 PM
I usually take that idea a step further... I just post the form to itself, over and over. Different things are displayed/processed using subroutines, depending upon the conditions.
The nice thing about this is you have all of your logic in one page this way - and you only have to write the code to request the variables, etc. once.
For a VERY VERY basic example, see my "contact us" script here at:
http://www.solidscripts.com/email.txt
And sample usage:
http://www.solidscripts.com/email.asp
As you can see, that basically only has two subroutines that display/process information depending upon certain conditions, but you can take that idea as far as you want...
As for shortening the code used to redisplay values, there are a couple of ways you should do that (since you'll be posting the values back to the same page)...
<%
Function IsChecked(val1,val2)
If val1 = val2 Then IsChecked = " checked=""checked"""
End Function
Function IsSelected(val1,val2)
If val1 = val2 Then IsSelected = " selected=""selected"""
End Function
%>
<input type="text" name="mytext" value="<% = Server.HTMLEncode(mytext) %>" />
<select name="myselect">
<option value="1"<% = IsSelected(myselect,"1") %>>1</option>
<option value="2"<% = IsSelected(myselect,"2") %>>2</option>
</select>
<radio name="myradio"" value="1"<% = IsChecked(myradio,"1") %>> 1
<radio name="myradio"" value="2"<% = IsChecked(myradio,"2") %>> 1
Notes:
• Server.HTMLEncode() is very important... try NOT using it, and then type something in a textbox like:
<"" Hi! I just broke your HTML! ;)
There are other lame ways to avoid this, but they do not preserve the integrity of the data, especially if for instance someone was submitting a script or something of that nature. Keep in mind though, not to use it when Request() ing a value, etc. - only when displaying a value!
• The IsSelected() and IsChecked() functions are just something I wrote to avoid having to write out:
<% If myradio = "1" Then Response.Write(" checked=""checked""") %>
etc., every time - and of course when using values like 1-12 (i.e. months), you can always loop through the numbers and then you only really write each option/radio/etc. one time in the code.
Hope these ideas help!
P.S. As oracleguy mentioned above, you can also use Request.Form("whatever"), however I usually assign them to a variable if I'm not sure how many times I'll be using them in a page, that way ASP also only has to get the variable from the Request collection once - I don't know if that's more efficient or not, but the pages I write run fast enough! And that way I only have to type the variable name, not Request.Form("variablename") all over the place...
Thanks.
:thumbsup: Yeah for sure, if i use it more than once it goes into a variable.
Hey umm, this might be obvious but if the user input is all o.k therefore it's good to go ahead with a redirect, how should i go about sending the data to the new page? (because i have submitted the form to itself but then wnat to send the data on-> )
querystring is not preferred so..
hidden fields ? but then how to submit- (no javascript)
Once again :confused:
whammy
01-15-2003, 12:56 AM
Like I said, even better than sending the information to a new page, just submit the form to itself yet again, and using if/then logic (or whatever) then process the information with yet another subroutine.
Check out my register.zip file to see what I mean...
http://www.solidscripts.com/downloads/register.zip
Look at the register.asp file within, you'll see how I call different subroutines depending on the criteria that are met, and decide whether to display the form again, or process the information and redirect, etc.
Don't pay too much attention to the various subroutines (as I don't want to confuse you), just take a few minutes to look at where it says:
'********** MAIN PROGRAM
' stuff here <!-- look in here!
'****** END MAIN PROGRAM
As you can see by that, I just post the form to itself over and over, and depending upon the conditions I specify, either redisplay the form, or process the information by calling different subroutines.
The main thing to note using this technique is you need to Dim (declare or dimension) your variables outside of the subroutines if they are going to be used in more than one place, so they have global scope. That's why I use Option Explicit... :)
Once you get the concept, you'll be like "duh!". It also makes your code much more reusable, since you can just copy and paste different subroutines into another application. :D
[o_O]
01-15-2003, 04:16 AM
Cheers whammy
-i'll be using that ;)
allida77
01-15-2003, 06:53 AM
Thats a pretty interesting way to do it. I usually set a field usually called hdnAction. Then just do a select case and perform the appropiate action depending on what is passed. It is interesting looking at how other people code.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.