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

Making form fields required

Status
Not open for further replies.

karenken

Technical User
Oct 16, 2001
9
US
I have a simple form that a user fills out. When they click Submit, aspmailer.asp parses the form and then e-mails the results to my client. I need to make some of the fields on the form, such as name and e-mail, required fields. If they aren't completed, they can't send the form.

I've tried using a javascript solution to pop up an alert if the field is not filled out, but it doesn't work. I'm thinking that ASP and javascript can't coexist peacefully in the same html file?

I have samples of what I'm working on available. Any help you can give me in making the fields required will be greatly appreciated.

BTW, I'm very, very new to programming. I know there is a simple solution to this, but I don't know what it is yet.
 
it can be done in javascript. i'm not as familiar with that, but it can also be done with asp, by having field validation done before email is sent.

example:
If Request.Form("mandatory_field") = "" Then
sError = "You must fill in this field."
End If
If sError = "" Then
sendEmail
Else
redisplayForm (sError)
End If


And display the error message above the field, or at the top of the form.

 
Where would I put this? In the html file or the asp file?
 
<%
'Validate all of the fields
txtValidateError = &quot;&quot;

'check field one - and you can get pretty fancy here!
if request(&quot;fieldone&quot;) = &quot;&quot; then
txtValidateError = &quot;You must fill in Field One<BR>&quot;
end if

'check field two - same goes for this. You can even
'see if the @ sign is in the string. There are some
'routines to check if the e-mail address is valid too!
if instr(request(&quot;fieldtwo&quot;), &quot;@&quot;) <> true then
txtValidateError = txtValidateError & &quot;This e-mail
address appears invalid.<BR>&quot;
end if

'now check to see if error is blank
'if not, set the session(error) and return them to page
'if it is blank, then go ahead and e-mail the info
if txtValidateError <> &quot;&quot; Then
session(&quot;txtValidateError&quot;) = txtValidateError
response.redirect(&quot;formpageaddress&quot;)
'be sure to start your formpageaddress with
'<center><%=session(&quot;txtValidateError&quot;)%></Center>
'and then make the next line clear that error
'session(&quot;txtValidateError&quot;) = &quot;&quot;
else
....complete your code for mailing info
end if
%>

It is a good idea to set each of the values as
session(&quot;variable&quot;)
so you can fill the form back in for your &quot;visitor&quot; when you send them back if there is an error. I'll post more if you don't understand that. =====================
Dennis B

Remember - YOU ARE UNIQUE!!!... Just like EVERYONE ELSE! ;o)
 
Hey Dennis, thanks for the code! This seems to be what I need. I'm not sure what to put in this code though:

response.redirect(&quot;<center><%=session(&quot;txtValidateError&quot;)%></Center>&quot;)
'be sure to start your formpageaddress with
'<center><%=session(&quot;txtValidateError&quot;)%></Center>
'and then make the next line clear that error
'session(&quot;txtValidateError&quot;) = &quot;&quot;

Also, in the response.redirect, the %> (just before the center tag) is appearing as if it's the end of the script, which is preventing the rest of the script from executing. How can I fix that?
 
When I try to submit the code, it's telling me that the page cannot be displayed. Here is the entire code. I really think the problem is the <% and %> in the response.redirect code. It renders the rest of the page as text, not code.

<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>

<html>
<head>
<title>ASP mailer</title>
</head>

<body>
<%
'Validate all of the fields
txtValidateError = &quot;&quot;

'check field one - and you can get pretty fancy here!
if request(&quot;name&quot;) = &quot;&quot; then
txtValidateError = &quot;Please type in your name<BR>&quot;
end if
'check field two - the address
if request(&quot;address&quot;) = &quot;&quot; then
txtValidateError = &quot;Please type in your address, city, state and ZIP code<BR>&quot;
end if
'check field three - the e-mail address
if request(&quot;email&quot;) = &quot;&quot; then
txtValidateError = &quot;Please type in your e-mail address<BR>&quot;
end if
'check field two - same goes for this. You can even
'see if the @ sign is in the string. There are some
'routines to check if the e-mail address is valid too!
if instr(request(&quot;email&quot;), &quot;@&quot;) <> true then
txtValidateError = txtValidateError & &quot;This e-mail
address appears invalid.<BR>&quot;
end if

'now check to see if error is blank
'if not, set the session(error) and return them to page
'if it is blank, then go ahead and e-mail the info
if txtValidateError <> &quot;&quot; Then
session(&quot;txtValidateError&quot;) = txtValidateError
response.redirect(&quot;<center><%=session(&quot;txtValidateError&quot;)%></Center>&quot;)
'be sure to start your formpageaddress with
'<center><%=session(&quot;txtValidateError&quot;)%></Center>
'and then make the next line clear that error
'session(&quot;txtValidateError&quot;) = &quot;&quot;
else
'-- Now grab the values from the form and
'-- build the message to be sent.
'-- Note the vbCrLf - it is a carriage return
'-- and line feed, which gives you line breaks
'-- in the email message.
Msg = &quot;&quot;
Msg = Msg & &quot;Name: &quot; & request(&quot;name&quot;) & vbCrLf
Msg = Msg & &quot;Address: &quot; & request(&quot;address&quot;) & vbCrLf
Msg = Msg & &quot;City: &quot; & request(&quot;city&quot;) & vbCrLf
Msg = Msg & &quot;State: &quot; & request(&quot;state&quot;) & vbCrLf
Msg = Msg & &quot;Zip or Postal Code: &quot; & request(&quot;zip&quot;) & vbCrLf
Msg = Msg & &quot;Country: &quot; & request(&quot;Country&quot;) & vbCrLf
Msg = Msg & &quot;Telephone: &quot; & request(&quot;phone&quot;) & vbCrLf
Msg = Msg & &quot;E-mail: &quot; & request(&quot;email&quot;) & vbCrLf
Msg = Msg & &quot;Comments:&quot; & vbCrLf
Msg = Msg & request(&quot;Comments&quot;) & vbCrLf
'-- Now let's instanciate the component
Set Mailer = Server.CreateObject(&quot;SMTPsvg.Mailer&quot;)
'-- the next line enables queue functions and can
'-- be set to false if you don't want to queue
Mailer.QMessage = true
Mailer.FromName = SenderName
Mailer.FromAddress= SenderEmail
'-- The next line should be set for the SMTP server
'-- address of your ISP or webhost
Mailer.RemoteHost = &quot;mailhub.registeredsite.com&quot;
'-- AddRecipient sets the name of the recipient and
'-- the email address of the recipient - customize!!
Mailer.AddRecipient &quot;Kim Pearson&quot;, &quot;storykim@home.com&quot;
Mailer.Subject = &quot;I'd like to know more about Primary Sources!&quot;
Mailer.BodyText = &quot;Dear Kim,&quot; & vbCrLf & Msg
if Mailer.SendMail then
'-- confirm mail was sent - customize as desired
response.write(&quot;<div align=&quot;center&quot;><img src=&quot;images/logos/primary.gif&quot; alt=&quot;logo&quot;> <img src=&quot;images/logos/logo.jpg&quot; alt=&quot;Primary Sources&quot; width=60 height=75> <img src=&quot;images/logos/sources.gif&quot; alt=&quot;Primary Sources&quot;>&quot;)
response.write&quot;Thank you for contacting Primary Sources. The information you submitted is below.
response.write(request.form(&quot;name&quot;))
response.write(&quot;<p>&quot;)
response.write(request.form(&quot;address&quot;))
response.write(&quot;<p>&quot;)
response.write(request.form(&quot;city&quot;))
response.write(&quot;, &quot;)
response.write(request.form(&quot;state&quot;))
response.write(&quot; &quot;)
response.write(request.form(&quot;zip&quot;))
response.write(&quot;<p>&quot;)
response.write(request.form(&quot;email&quot;))
response.write(&quot;<p>&quot;)
response.write(request.form(&quot;comments&quot;))
response.write(&quot;<p>&quot;)
Response.Write &quot;We appreciate your interest in Primary Sources. We will be contacting you shortly. Sincerely, Kim Pearson&quot;

else
'-- return error message - customized as desired
Response.Write &quot;Mail send failure. Error was &quot;
Response.Write Mailer.Response
end if
'-- destroy the mailer object
set Mailer=nothing

end if
%>
<%
'-- get the sender's address, and if empty, put your own
'If request(&quot;email&quot;)=&quot;&quot; then
'-- put your email address between the quotes below
'SenderEmail = &quot;storykim@home.com&quot;
'Else
'SenderEmail= request(&quot;email&quot;)
'End If
'If request(&quot;name&quot;)<>&quot;&quot; then
' SenderName = request(&quot;name&quot;)
'Else
'-- this can be customized to anything you want
' SenderName = &quot;Primary Sources&quot;
'End If
%>
</body>
</html>
 
The javascript validation script that pgferro gave me works and the aspmailer.asp file is doing what it's supposed to. The form is now working just the way I want it to. Thank you for all your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top