vbscript server side validation - asp-classic

Basically I have a page named dvds.asp with a form that goes to action="process.asp". The process.asp script validates the Name, Surname and Email fields. I would like that if the email does not exist, a redirect will occur to the original page (dvd.asp) and also display a custom error.
The validation of the email field and redirect I know how to do. The only problem is how I am going to get the custom error value in the dvds.asp.
Any help please? :)
FYI:
I am using this code in process.asp to validate:
if txtEmail = "" then
Server.Transfer(Request.ServerVariables("HTTP_REFERER"))
else
Now the error I want to show on the dvds.asp page is: "Please fill in your email" .. How I can do that please?

Hard to say without seeing any source, (For instance, how are you validating the email? What is this "Custom error value"?
Assuming you have a function that validates the email and returns false if it fails and the matching error value is "1" then you could simply include this in your redirect:
Dim EmailAddress
EmailAddress = Request.Form("EmailAddress")
If ValidateEmail(EmailAddress) = False Then
Response.Redirect("dvd.asp?ErrorID=1")
End If
and on the Origional Source page, you could just display errors back to the user as follows:
'#### Output errors
If Trim(Request.QueryString("ErrorID")) <> "" Then
Select Case Request.QueryString("ErrorID")
Case "1" ' #### Email Error
Response.Write("<p><span style=""color: red"">Error: Invalid email address</span></p>")
Case "2" ' #### Bad Name
Response.Write("<p><span style=""color: red"">Error: Invalid Name</span></p>")
Case Else
Response.Write("<p><span style=""color: red"">Error: Unknown / Generic Error</span></p>")
End Select
End If
One small suggestion by the way, its often preferable for pages such as this to post to the same page and include any email functionality you need from a class / SSI. That way you cold auto-populate the form again if an error occurred rather than expecting your end user to re-type the whole form if they get an error.

Related

How to validate TextBox?

I have a textbox for a zipcode in which i hav made a validation that the user cannot enter number less then or more then 6, but even if user enters 3 nos it is working.SO i want an error msg to display if user enters something wrong
Private Shared Function ValidateZip(ByVal pintZip As String, ByRef pobjErrMsg As Common.ErrorMessage) As Boolean
If pintZip.Length <> 6 Then
ElseIf IsNumeric(pintZip) Then
End If
Return True
End Function
You have no code in either the If or ElseIf blocks and you are always returning True
Private Shared Function ValidateZip(ByVal pintZip As String, ByRef pobjErrMsg As Common.ErrorMessage) As Boolean
If pintZip.Length <> 6 Then
MsgBox("Zip code should have 6 characters")
pintZip.Focus
Return False
ElseIf IsNumeric(pintZip) Then
MsgBox("Congratulations, you have entered right Zip code")
Return True
End If
End Function
Try like this
UPDATE #1
Seems like you are using ASP.net not VB.net, here is the answer:
You can't show dialog box ON SERVER from ASP.NET application, it makes no sense since your user is using browser and it can't see message boxes on server. You have to understand how web sites work, server side code (ASP.NET in your case) produces html, javascript etc on server and then browser loads that content and displays it to the user, so in order to present modal message box to the user you have to use Javascript, for example alert function.
Here is the example for asp.net :
example
SOURCE

Http.Page.Request[" "] not returning correct values

Situation:-
There is a Home.aspx page, which can be opened by a unique user ("userName" variable).
This page has a popup window control name 'alertWindow'.
In the pageLoad event of Home.aspx.cs, Welcome.aspx page is opened in the 'alertWindow' using NavigateUrl property.
The querystring passed to Welcome.aspx page contains a parameter "UserName" and this parameter is set to the logged in user's name ("userName" variable).
Now when the code execution comes to Welcome.aspx.cs page, "Request["UserName"]" is used to get\retrieve the current "userName" paramerter existing in the query string.
Issue:-
When a logged-in user's name has space or other non-usual characters, then "Request["UserName"].ToString()" doesn't retrieve the actual and correct value.
For Ex. if the logged in "userName" = "A&T Telecom", then "Request["UserName"].ToString() retrieves only "A" and nothing else.
But if the userName string is a proper value like "micheal", then "Request["UserName"].ToString() retrieves only "Micheal" correctly
Requirement:-
Please provide a way so that I get the correct value from Request["UserName"] for any kind of "userName" string value.
Home.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (user is valid)
alertWindow.NavigateUrl = "Welcome.aspx?userName=" + currentUser.ToString();
}
Welcome.aspx.cs:-
currentUserName = Request["userName"].ToString();
This is logical because you do not Encode your url. Try this:
alertWindow.NavigateUrl = "Welcome.aspx?userName=" + Server.UrlEncode(currentUser.ToString());
To say few more, they are some special characters that used on the URL, like the
: / # ? & # % + (and the space).
All that characters must be encode to a different format, so the url will not break, the UrlEncode do exactly that.
Two notes.
I select the Server to call the UrlEncode because is not depend from the Request, and you can use it inside a thread, or any function that is not called from the Page.
The Request.QueryString make UrlDecode when you use it. To get the encode url you call the Request.RawUrl
You cannot add white spaces within your url, it needs encoding so :
//uses HttpUtility.UrlEncode internally
Server.UrlEncode("something with spaces");
or
HttpUtility.UrlEncode("something with spaces");

Classic ASP VBScript - refresh given page

I think that this is something very easy, but I cannot get this to work.
I have a form with a button "Delete". It calls www.mypage.com/adm/ads.asp?del=12.
So list.asp sees that there is a querystring with del=12 and deletes correspoding item.
After delete I want to refresh this page (something like Response.Redirect www.mypage.com/adm/ads.asp), so that querystring del=12 disappears.
I cannot get it working.
If (Request.QueryString("del").Count > 0) Then
id = Request.QueryString("del")
sql = "delete from Ads where ID = " & id & ""
on error resume next
conn.Execute sql
If err<>0 then
Response.Write("Delete error!")
Else
Response.Redirect http://www.mypage.com/adm/ads.asp
//Call opener.location.reload()
End if
The page is reloaded, but del doesn't disappear from query string.
The parameter to Response.Redirect should be a string - what you have is a syntax error:
Response.Redirect http://www.mypage.com/adm/ads.asp
Should be:
Response.Redirect "http://www.mypage.com/adm/ads.asp"
To make it generic and not mess with raw URLs you can have such code instead:
Response.Redirect(Request.ServerVariables("SCRIPT_NAME"))
The SCRIPT_NAME server variable will return the relative path of the currently executing script, no matter what the page is called and where it's located.

Asp.net pass value from txtbox to an another page

This the end of my code
...
If lblErrMsg.Text = "" Then
Response.Redirect("UserPage.aspx")
End If
I want to pass the value of txtUser(I create It in the current page...) to the UserPage.aspx.
Thank's for helping me ...
This is in VB.net not in c# Please
C# Version
1) Use querystring
Response.Redirect("user.aspx?val="+txtBox.Text);
and in userp.aspx.cs,
string strVal=Request.QueryString["val"];
2)Use Session
Setting session in first page before redirecting
Session["val]=txtBox.Text;
Response.Redirect("user.aspx");
and in user.aspx.cs
String strVal=(string) Session["val"];
EDIT :VB.NET VERSION
1) Use Querystring
Response.Redirect("user.aspx?val=" + txtBox.Text)
and in user.aspx.vb
Dim strVal As String = Request.QueryString("val")
2)Use Session
Setting Session in firstpage
Session("val")=txtBox.Text
Response.Redirect("user.aspx")
and in user.aspx.vb.
Dim strVal As String = DirectCast(Session("val"), String)
You can pass it in the query string, like this:
Response.Redirect("UserPage.aspx?user=" + HttpUtility.UrlEncode(txtUser.Text));
And then retrieve it via:
string user = Request.QueryString["user"];
If you're worried about users messing with a query string (be sure to validate it), you could also store a Session variable before doing the redirect.
warning: this is a gross but easy solution
Session("myTextbox")= txtUser.Text
this will persist the value so on the page_load of the next page you can say
txtUser.Text=Session("myTextBox")
What are you passing form page to page? Is it a list of things. You could have an object with different properties and could then pass it through a session. If you have multiple pages I would suggest doing this if you could end up reusing it else where. Passing it through the url, you would then need to validate it, because if someone types the url with the correct information or something that is being directly input into a database they could cause problems and/or unexpected results.

redirecting pages using session

i'm new to ASP.NET,so plz b patient :D
i want to redirect one of my pages to the other one,and i keep the username!
i tried to use session.add and session[],but when i want to insert the username inside the brackets,it says use must int!!!but i thought i should use session["username"]
i used another way(request.querystring[]),but both have problems
here is my code
//first solution
string username="asal";
session.Add(username,username);
Response.Redirect("~/Doctor/DoctorsMainPage.aspx");
//in the other page
Label1.Text= Session["username"].ToString();//this one says use int?!
//i used this one instead of it
Label1.Text= Session[0].ToString();//with this one i get the username in other page,but one i want to pass another string like "id" with session,I can not!
//the second solution
string username="asal";
Response.Redirect("~/Doctor/DoctorsMainPage.aspx?username");
Label1.Text = Request.QueryString["username"];//this one redirect to doctors main page but set the value of username to "" !
session.Add(string, string) where the first string is the name of the variable and the second is the value.
You are adding the value twice.
//first solution
string username="asal";
session.Add("username",username); <-- this is your problem
Response.Redirect("~/Doctor/DoctorsMainPage.aspx");
//in the other page
Label1.Text= Session["username"].ToString();
Now, as for
//the second solution
string username="asal";
Response.Redirect("~/Doctor/DoctorsMainPage.aspx?username");
Label1.Text = Request.QueryString["username"];//this one redirect to doctors main page but set the value of username to "" !
In this case you're creating a url "~/Doctor/DoctorsMainPage.aspx?username"
Ok - so what is username? The code is looking for a param in the query string named username but it's not finding a value.
You need:
Response.Redirect("~/Doctor/DoctorsMainPage.aspx?username="+username);
That will give you "~/Doctor/DoctorsMainPage.aspx?username=asal"
string username = "asal";
Session["username"] = username;
Response.Redirect("~/Doctor/DoctorsMainPage.aspx");
//Other page
Label1.Text = Session["username"].ToString().Trim();
You have to add the session like..
session.Add("username",username); instead session.Add(username,username);
And then you can access the value like..Label1.Text= (String)Session["username"];
Check out this article related to the session State ASP.NET Session State Overview that will help you to understand Session State management.
Seconly querystring should be like, as you have not passing your string parameter and it should be like...
Response.Redirect("~/Doctor/DoctorsMainPage.aspx?username=" + username);

Resources