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

Asp Validation Message. 1

Status
Not open for further replies.

micawber

Programmer
Dec 10, 2002
71
GB
I have a validation page, the code I have taken from a example I found on a web page.
The page/validation works great.
Does anyone know how to use similar validation but instead of going to a new page to show the validation messages, the message appears in a message or alert box over the present page?
Thanks.

Here is the code
Code:
<%
    DIM strEmail, strSubject, strComments
    strEmail = Request.Form("Email")
    strSubject = Request.Form("Subject")
    strComments = Request.Form("Comments")

    IF strEmail <> "" AND strSubject <> "" AND strComments <> "" THEN

    ' Process the form as you like here
    ' For example enter form to your database or send it via email

    ELSE

    Response.Write "<p>Please click back on your browser and complete the following fields:</p>"
    IF strEmail <> "" THEN
    ELSE
    Response.Write "<b>• Email</b><br>"
    END IF
    IF strSubject<> "" THEN
    ELSE
    Response.Write "<b>• Subject</b><br>"
    END IF
    IF strComments <> "" THEN
    ELSE
    Response.Write "<b>• Comments</b><br>"
    END IF

    END IF
%>
 
I am not a big fan of sessions but this is one place for (for non critical data in forms) that they can be helpful:

Try this:

If form sent successfully then


Code:
Response.Redirect Request.ServerVariables("HTTP_REFERER")&"?sent=1"


otherwise::===========
Code:
    IF strEmail <> "" THEN
    ELSE
    errorlist = errorlist & "- Email" & "<br>"
    END IF
    IF strSubject<> "" THEN
    ELSE
     errorlist = errorlist & "- Subject" & "<br>"
    END IF
    IF strComments <> "" THEN
    ELSE
    errorlist = errorlist & "- Comments" & "<br>"
    END IF

    END IF

If errorlist <> "" Then
errorlist =  "Please complete the following fields.:<br>" & errorlist 
	          End If

Session("errorlist")= errorlist
                     'Redirect to form that posted here
                     Response.Redirect Request.ServerVariables("HTTP_REFERER")

Then at the top of your form somewhere (probably needs formatting)

Code:
<% If Request.Querystring("sent")= "1" Then
  Response.Write "Your message was sent successfully."
    End If
  If Session("errorlist")<> "" Then

  Response.Write Session("errorlist"))

	
	Session.Abandon()
	End If
	%

Play with this and see if it works.
 
Thanks so much cr84net2!
That works great!

Couple of questions:
1. Could this be manipulated so that the message is in a popup alert box rather than on the page?

2. If there is an alert because the form has not been filled in completely, is it possible that the form could retain the information that has been filled in? It removes it at present.

Many thanks.
 
You'd be best off using client-side validation to meet your requirements. That's Javascript (unless you have IE-only viewers).

Lee
 
I would have to agree with Lee. You could however use an error page which writes out the errorlist with a jscript back button (return to form). I have done this and it retains the fields that are already filled in. You would probably want more of a message for each error in that case. Like: A valid email is required to process this form instead of just "Email".
Then you need a form a process page, an error page and a thank you page. Probablynot worth it unless it is a very long form.

But for a popup msg box use client side validation like Lee suggested.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top