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!

Form Validation Before a new page is displayed

Status
Not open for further replies.

Kinl

Programmer
Mar 19, 2001
168
US
I have a form that has data in it, such as an email address or a name, or last name. Heres what what I want to do: When the user clicks on the submit button, I would like to do fhe form validation before it refreshes to the next page (which is in the 'action="nextpage.asp"' part of the form. How can I do this? I have read some resources that state I can do it by the use of Sub MyButton_onclick event, but that hasnt seemed to work.

Any Ideas?

Thanx,

Donn
 
This is a variation on a very ecxcellent theme posted by a member a few days ago. It takes data posted in a form and allows you to preview it in an confirmation page, and the re-posts it by placing the values in the querystring.


====================== FORM.ASP ======================
<%@ Language=VBScript %>
<HTML><HEAD></HEAD><BODY>

<form action=&quot;formpreview.asp&quot; method=&quot;post&quot;>
<input type=&quot;text&quot; name=&quot;name&quot;>
<input type=&quot;text&quot; name=&quot;address&quot;>
<input type=&quot;submit&quot;>
</form>

</BODY></HTML>



=================== FORMPREVIEW.ASP =====================

<%@ Language=VBScript %>
<HTML><HEAD></HEAD><BODY>

<%
name = request.form(&quot;name&quot;)
address = request.form(&quot;address&quot;)
%>
<table>
<tr>
<td>
name:
</td>
<td>
<%=name%>
</td>
</tr>
<tr>
<td>
address:
</td>
<td>
<%=address%>
</td>
</tr>
</table>
<form action=&quot;send.asp?name=<%=name%>&address=<%=address%>&quot; method=&quot;post&quot;>
<input type=&quot;submit&quot;>
</form>

</BODY></HTML>

================================= SEND.ASP ===============================
<%@ Language=VBScript %>
<HTML><HEAD></HEAD><BODY>

<h2>The following information can be entered into a DB or posted to another
form...etc.
<hr width=85% color=blue>
<%
Dim name, address
name=request.querystring(&quot;name&quot;)
address=request.querystring(&quot;address&quot;)

Response.Write &quot;The name is: &quot; & name & &quot;<br>&quot;
Response.Write &quot;The addy is: &quot; & address
%>

</BODY></HTML>
 
Use javascript to validate input in the elements in the form. This way, the page never even makes a trip to the server and back to tell them they forgot to input data or it's the incorrect format...


---page1.asp-------------------

<html>
<head>
<script language='javascript'>
<!--
function checkForm(form){
if(form.fname.value == &quot;&quot;){
alert(&quot;Enter your first name.&quot;);
form.fname.focus();
return false;
}
if(form.lname.value == &quot;&quot;){
alert(&quot;Enter your last name.&quot;);
form.lname.focus();
return false;
}
if(form.email.value == &quot;&quot;){
alert(&quot;Enter your email address.&quot;);
form.email.focus();
return false;
}
if(form.email.value.indexOf ('@',0) == -1 || form.email.value.indexOf ('.',0) == -1) {
alert(&quot;This is not a valid email address, please check it and enter it again.&quot;);
form.email.focus();
return false;
}
else{
return true;
}
}
// -->
</script>
</head>
<body>

<form onSubmit='return checkForm(this)'action='nextpage.asp' method='post'>
First Name:<input type='text' name='fname'><BR>
Last Name:<input type='text' name='lname'><BR>
Email:<input type='text' name='email'><BR>
<input type='submit' value='Submit'>
</form>


</body>
</html>


---------------------

That should accomodate you nicely I think.
-Ovatvvon :-Q
 
WHOOPS! My bad...right intention...wrong subject! Sorry Kinl!
 
Ovatvvon,
Like what you've done with form Validation and JavaScript...could you please tell me how I could send user to confirmation / thank you page AND e-mail the information that the user input into the form.

Thanks !
 
Well, for the confirmation / thank you page, that is easy. After the javascript goes through all the checks and finds them ok and submit's the form to process.asp (or whatever the action page is) and the information is processed however you want it (into a database or to email, both, or whatever, use a response.redirect to go to the next page after that which you can design to display the information one more time if you wanted to and/or display a thank you page.

You can actually design it so they don't have to hop to anther page again, but that can somtimes cause problems if they hit the back button and forward again, or refresh, the page can end up resending the inforamtion to the email, or the database, or whatever it is that you are doing on the page, so I'd just redirect it to another page if I were you and also use a response.refresh to zero so they don't come back to the page.


<%@ Language=VBScript%>
<%
response.expires = 0
%>

and

response.redirect(&quot;confirmation.asp&quot;)



Hope this helps...
-Ovatvvon :-Q
 
To send an email, you need some form of an email server. Exchanger server, or what-not. If you don't have these, then you can sign-up for a free account on an email forwarding service such as Bravenet.com or w3Jmail from
<%
')This needs to be edited to use your own email component

Set smtp = Server.CreateObject(&quot;SmtpMail.SmtpMail.1&quot;)
MailServer = &quot;mail1.abc.com&quot;
Recipients = email
Sender = &quot;your_Email@your_Domain.com&quot;
Subject = &quot;ASP Forum - Reply to your posting&quot;
msg = &quot;Hello &quot; & user_name & vbcrlf & vbcrlf
msg = msg & &quot;You have received a reply to your posting in the Forum.&quot;
msg = msg & &quot;Regarding the subject - &quot; & Request.Form(&quot;topic_title&quot;) & &quot;.&quot; & vbcrlf & vbcrlf
msg = msg & &quot;You can view the reply at &quot; & Request.Form(&quot;refer&quot;) & vbcrlf

smtp.MailServer = MailServer
smtp.Recipients = Recipients
smtp.Sender = Sender
smtp.Subject = Subject
smtp.Message = msg
on error resume next ' Ignore Errors
smtp.SendMail2
Set smtp = Nothing


Response.redirect(&quot;confirmationPage.asp&quot;)
%>


-Ovatvvon :-Q
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top