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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Programming required fields in asp

Status
Not open for further replies.

zonash001

Programmer
Mar 10, 2004
15
PK
i need to design a form that will hold some required fields. I searched the forum for my prob but just got confused. So i m now posting my qs.

I've two files mainpg.htm and mail_info.asp. mainpg.htm holds the form (with few fields like company, email, address, phone etc.) which submits results to mail_info.asp. The email is the required field i.e. if the user submits the form without entering an email address, he should be re-directed back to mainpg.asp with a message appearing above the form "You must enter an email address before proceeding further". Moreover, the fields already entered should be retained i.e. the user don't have to start all over again.


Given below is is the code tht i m trying to work out.....

Mainpg.htm
===========

<form method="POST" action="mail_info.asp">
<table border="1" cellpadding="0" width="60%" >
<tr>
<td width="40%" bgcolor="#F2F2F2">
<font face="Verdana" size="1"> <b>Company</b></font></td>
<td width="60%" bgcolor="#FFFFFF">
<font size="1" face="Verdana"><input type="text" name="company"></font></td>
</tr>

<<tr>
<td width="40%" bgcolor="#F2F2F2">
<font face="Verdana" size="1"> <b>Email *</b></font></td>
<td width="60%" bgcolor="#FFFFFF">
<font size="1" face="Verdana"><input type="text" name="email"></font></td>
</tr>
.
.
.
</table>

Mail_info.asp
==========
<%

Dim strCompany
Dim strEmail

strCompany = Request.Form("Company")
strEmail = Request.Form("Email")
.
.

IF strEmail = "" THEN
Response.Redirect "mail_info.htm"
End If
.
.
.
.
Rest of the code...

 
Why reload the page when you can use javascript to client-side validate the form?? View the source of this page to see it at work.

[sup]-------- __@ __@ __~@
----- _`\<,_ _`\<,_ _`\<,_
---- (*)/ (*) (*)/ (*) (*)/ (*)
[/sup]
 
bye the way, I noticed you're redirecting back to mail_info.htm and NOT Mainpg.htm

[sup]-------- __@ __@ __~@
----- _`\<,_ _`\<,_ _`\<,_
---- (*)/ (*) (*)/ (*) (*)/ (*)
[/sup]
 
tht was by mistake...it should have been to mainpg.htm
 
well i can understand what you're going for i had problems on a checkout sequence using java validation, but them buggers with java disabled could get through the checkout with say, no CC number or no Name.... so i descided to take drastic measures, extremely dynamic measures :)

easiest thing to do is alter your form ... minimally.. add default values with variable values ( i'll give example in a sec ) and add a hidden input with a delimited list of your required fields .. then use this in your post to page in a validation script, which can post back or whatever you need in order to force the info...

way i did it in the past is the validation page would auto redirect to the "follow up" page and the validation page would accept the info, post back to itself, taking all the old info and re-structuring the form from the include you're already using.

Translations :
Page1 = First Form
Page1a = Form include ( the actual form )
Page2 = Validation page (invisible if all form fields are accounted for and does the accepting/work stuff, then redirects to the thank you page whatever)
Page3 = thank you page whatever

from here out i'll just use name and addr as the form elements to keep the typed code shorter...

Sample page1a :
<!-- Add copious amounts of layout as necessary -->
<form action="page2" method="post">
<!-- note that you dont need to change the destination of the form, because on page2 in validation it will post to itself... -->
<input type="hidden" name="RequiredFields" value="name,addr">
<input type="text" name="name" value="<%=name%>">
<input type="text" name="addr" value="<%=addr%>">
<input type="text" name="firstborn" value="<%=firstborn%>">

etc etc etc
( reasons for the variables will become appearant in a few )

In Page1, the first form, if you have any desire of return client "pre-fill" form, you can take all the values from the fields in the DB and assign them to the variables, and boom .. auto filled form...2 ways to do this ..

' option 1
name = Rs("name")
addr = Rs("addr")
bloodtype = Rs("bloodtype")

'option 2
<!-- EVIL SMILE -->
'cycle through the field names ( this makes the variables the same name as your field names much easier on the backside )
for each field in rs.fields
Execute(Field.name & " = RS(""" & Field.name & """)")
next
' this method makes it easy to add/remove form fields at a whim, and all you have to do is make sure the field is in the recordset.

Now Page2 :

'Boolean argument to force fixing the submission, OR to autoredirect/post values etc...
FailedValidation = False
' with this method on the boolean, if there are no required fields, it just defaults to handle the inputs and redirect.. spiffy :)
RequiredFields = Request("RequiredFields")
If RequiredFields <> "" then
' split the delimited fields into an array
RequiredArr = Split (RequiredFields,",")
' cycle through the required fields, make sure there's a value for them, and build error messages as need be ( if you'd like to add "red starring" of the fields i'll add that in here with the supplimental updates to the form do make it work )

For each Field in RequiredArr
' checking for failed value
If Request(Field) = "" Then
' this is for any single required field failed, update the Boolean to reflect more form filling...
FailedValidation = True
' you can add a cross reference array or whatever to make the field names more "friendly" in the error
ErrMsg = ErrMsg & Field & ", "
' This is the red star handling i was mentioning
Execute(Field & "Flag = ""*""")
End If
Next
' strip off the last comma and space of the error message
ErrMsg = Left(ErrMsg, Len(ErrMsg)-2)

' Making the Error message more pretty .. and doing this in a variable because this is still before any HTML output is doen due to the redirect....
ErrMsg = "<font color=red>The following fields were not Entered, these are required : " & ErrMsg & "</font>"
End If

' Now we cycle through ALL the form elements to RE-fill the form If Validation Failed

If FailedValidation Then

'Same loop as before but only for the requested object not the DB fields ..

For each element in request.form
'Fetches the value and assigns it to a variable by the same name
Execute(Element & " = request(""" & element & """)")
Next

'then ... just re-include your form and tada auto filled again, with the same stuff they put in/changed before..

' and of course if they fill it out "right" then the else condition
Else

'all your insert,update, hokey pokey stuff needed....
'recommend assigning a single session variable to carry across, since you cant post to the third page unless you go hog wild with XML HTTP ...

Response.Redirect(Page3)
End If


Now ... for the Red star feature .. the variable say NameFlag before was assigned in the requiredfield loop .. to make this work in the form, you'd need to change the form include a little more...

( same as above with LABELS :) )

<form action="page2" method="post">
<input type="hidden" name="RequiredFields" value="name,addr">
Full Name <font color=red><%=NameFlag%></font>: <input type="text" name="name" value="<%=name%>">
Address <font color=red><%=AddrFlag%></font>: <input type="text" name="addr" value="<%=addr%>">
First Born <font color=red><%=FirstBornFlag%></font>: <input type="text" name="firstborn" value="<%=firstborn%>">

then on the reposting, the red stars will show up cause the variable has a value, otherwise it's invisible, and this is all done with the very same form include on both pages, make a change to it and it reflects everywhere...

hope the info helps, and i put this on email notification if you have any questions/confusion on the matter.
 
I would recommend you use javascript to validate your input. It prevents the redirection, reload etc.

Code:
<script language=javascript>
function Validate()
{
    if(document.myForm.email.value == "")
    {
        return false
    }
    
    return true
}
</script>

<form method="POST" action="mail_info.asp" name="myForm" onsubmit="return Validate();">
....


rsshetty.
It's always in the details.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top