Go Back  HTML Forums - Free Webmaster Forums and Help Forums > WEBSITE DEVELOPMENT > Server Side Programming > ASP and ASP.NET
User Name:
Password:
 

Reply
Thread Tools   Display Modes
  View First Unread
 
Old 01-10-2009, 03:57 PM
  #1
Sedativechunk
Champion (Level 13)
 
Sedativechunk's Avatar
 
Join Date: Jul 2006
Location: Irwin, PA
Posts: 241
iTrader: (0)
Sedativechunk is an unknown quantity at this point
Why does this program not work right?

I've been taking up VB.net in college and I installed Visual Web developer the other day to dive into learning ASP.net. So far, it seems pretty familiar and easy to me, but I 'm starting to discover the stupid sides to ASP.net.

I started by making a simple registration form, nothing that uses a database, just collecting server and form variables, and for some reason, it does not work right. Here is the code:
Code:
<script runat="server" language="vbscript">
    Sub registration_form(ByVal username As String, ByVal realname As String, _
                          ByVal email As String, ByVal password As String)
        ' Sub procedure that loads the registration form
        ' Also shows post data in fields that were already typed
        Response.Write("<h1>Registration Form Script</h1>" _
                       & "<p>This is a registration form programmed in ASP.net. You simply enter " _
                       & "the data and a session will be registered for you. If you log out, you can log " _
                       & "back in until your close your browser.</p><br />" _
                       & "<form method='post' action='index.aspx?act=1'><table cellpadding='6'>" _
                       & "<tr><td><label name='uname'>Enter a Username:</label></td>" _
                       & "<td><input type='text' name='uname' size='32' value='" & username & "' /></td></tr>" _
                       & "<tr><td><label for='realname'>Enter your real name: </label></td>" _
                       & "<td><input type='text' name='realname' size='32' value='" & realname & "' /></td></tr>" _
                       & "<tr><td><label for='password'>Enter a password: </label></td>" _
                       & "<td><input type='password' name='password' size='32' value='" & password & "' /></td></tr>" _
                       & "<tr><td><label for='password2'>Enter password again: </label></td>" _
                       & "<td><input type='password' name='password2' size='32' value='' /></td></tr>" _
                       & "<tr><td><label for='email'>Enter your email: </label></td>" _
                       & "<td><input type='text' name='email' size='32' value='" & email & "' /></td></tr></table>" _
                       & "<br /><input type='submit' value='Register!' /></form>")
    End Sub
</script>
<%  
    ' Declare variables
    ' Set values to variables
    Dim act As String
    Dim username As String
    Dim realname As String
    Dim email As String
    Dim upass As String
    Dim upass2 As String
    act = Request.QueryString("act")
    username = Request.Form("uname")
    realname = Request.Form("realname")
    email = Request.Form("email")
    upass = Request.Form("password")
    upass2 = Request.Form("password2")
    Dim errors(12) As String
    Dim total_errors As Integer
    
    ' Determine what to do from here
    Select Case act
        Case "1"
            ' Process post data
            ' Check for blank fields
            If ((String.IsNullOrEmpty(username)) Or (String.IsNullOrEmpty(realname)) _
               Or (String.IsNullOrEmpty(email)) Or (String.IsNullOrEmpty(upass))) Then
                If (String.IsNullOrEmpty(username)) Then
                    errors(0) = "Username cannot be blank"
                End If
                If (String.IsNullOrEmpty(realname)) Then
                    errors(1) = "Real name cannot be blank"
                End If
                If (String.IsNullOrEmpty(email)) Then
                    errors(2) = "Email cannot be blank"
                End If
                If (String.IsNullOrEmpty(upass)) Then
                    errors(3) = "Password cannot be blank"
                End If
            End If
            
            ' Check if passwords match
            If upass <> upass2 Then
                errors(4) = "Both passwords must match"
                ' Reset the passwords
                upass = String.Empty
                upass2 = String.Empty
            End If
            
            ' Validate the email address
            If Len(email) < 3 Or Len(email) > 35 And InStr(email, "@") = 0 Then
                errors(5) = "You must enter a valid email"
            End If
            ' Validate the other fields
            If Len(username) > 20 Then
                errors(6) = "Your email cannot be more than (20) characters long"
            End If
            If Len(realname) > 30 Then
                errors(7) = "Your real name cannot be more than (30) characters long"
            End If
            If Len(upass) > 20 Then
                errors(8) = "Your password cannot be more than (20) characters long"
            End If
            
            ' Determine if any errors occured
            ' First, determine how many array variables are not blank
            total_errors = 0
            For i As Integer = 0 To 12
                If errors(i) <> "" Then
                    If total_errors = 0 Then
                        total_errors = 1
                    Else
                        total_errors = total_errors + 1
                    End If
                Else
                    i = 13
                End If
            Next
            ' Now display each error that occured
            If (total_errors > 0) Then
                Response.Write("<div style='color: red; font-weight: bold;'>The following errors occured:<ul>")
                For i = 0 To total_errors Step 1
                    ' Double check to ensure this error is not blank
                    If errors(i) <> "" Then
                        Response.Write("<li style='color: Red;'>" & errors(i) & "</li>")
                    End If
                Next
                Response.Write("</ul><br />Please fix your errors and try again.</div><br />")
                ' Display the registration form
                registration_form(username, realname, email, upass)
            Else
                Response.Write("<h3>Successful!</h3>")
                Response.Write("<br />Data:<br /><table cellpadding='4'><tr><th>Username:</th><td>" & username & _
                               "</td></tr><tr><th>Real Name:</th><td>" & realname & "</td></tr>" _
                                & "<tr><th>Email:</th><td>" & email & "</td></tr></table>")
            End If
        Case Else
            ' Display the registration form
            registration_form(username, realname, email, upass)
    End Select
%>
To break it down, all I am doing is making a simple HTML form that collects a username, two passwords, email, and real name, and then validates the information. The trouble occurs at the validation part. For some reason, when the passwords don't match, it still allows it to go through. Is this not the correct syntax to match 2 simple string variables?
Code:
            ' Check if passwords match
            If upass <> upass2 Then
                errors(4) = "Both passwords must match"
                ' Reset the passwords
                upass = String.Empty
                upass2 = String.Empty
            End If
In addition to this, the len function does not work correctly either... I put aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa in one of the input fields and it didn't check it for being two long of a string??

Another problem that is occuring is the email validaton. Now for the the code I used, I found on the web so I'm not sure if it's write, but how would I correctly validate an email?

Lastly, when my program is done executing, it displays "Successful!" on the screen but when I view the source, the code shows error messages displaying saying that are the fields were blank... why is this showing up in my HTML and not displaying on the page? And when there are no blank string variables, why is this even being printed in the code?
This is not an urgent matter, I am just trying to learn ASP.net on the side and it seems that I run into alot of stupid problems that never occur in PHP. It's really frustrating, as it seems that nearly none of my code is working right. Are there different functions I should be using or what? BTW, I always like to collect errors into an array, check for errors at the end of the program, and then display the form again at the program. This is one of the most efficient ways in my opinion to make a registration form, hence the use of the array for the errors.
__________________
Ed
Server Side Programmer, Software Developer

Kumite Kid Martial Arts | SedativeChunk | Larimer Firehall

Last edited by Sedativechunk : 01-10-2009 at 04:00 PM.
Sedativechunk is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 01-10-2009, 05:39 PM
  #2
Sedativechunk
Champion (Level 13)
 
Sedativechunk's Avatar
 
Join Date: Jul 2006
Location: Irwin, PA
Posts: 241
iTrader: (0)
Sedativechunk is an unknown quantity at this point
Hmm, I just noticed ASP.net has built in functions to validate fields... pretty useful! I haven't had time to research or look up things like this in the .net framework, but I think it's hecka more useful than writing tons of code in PHP.

Any other input is still welcome on my ASP programming.
__________________
Ed
Server Side Programmer, Software Developer

Kumite Kid Martial Arts | SedativeChunk | Larimer Firehall
Sedativechunk is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 01-12-2009, 09:22 AM
  #3
afterburn
Can't say much here
 
afterburn's Avatar
 
Join Date: May 2004
Posts: 2,851
iTrader: (0)
afterburn will become famous soon enough
yucky code. you should use controls if you are using .net not request.form variables.

txtUserName.Text gets the value of a .net textbox called txtUserName however adding controls on the fly at run time requires that you put them back when you do a postback. I would create a login page that has this hardcoded instead of dynamically added to the page.

Also those if statements make the code ugly.... (I prefer C# as it has curly brackets to tell where the statements begin and end) however the comparison code is just ugly and i would use events of a .net button to trigger the code instead.
__________________
ASP.net nice bits
Code Smith rocking tool for Code Generation in any language (Written in .net)
Red Gate SQL tools for DBA
Blog Personal blog
.afterburn
afterburn is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 02-05-2009, 08:10 AM
  #4
kulrom
Fighter (Level 4)
 
kulrom's Avatar
 
Join Date: Jan 2007
Posts: 37
iTrader: (0)
kulrom is an unknown quantity at this point
Obviously that total_errors doesn't return what you expect. Sorry i couldn't follow the code with more attention. However you need an exit statement.

For instance, you can have an if statement as follows:

if error > 0 Then Exit Select/Sub/Whatever

Btw, you don't need most of the code above. Why you count the errors at all.
Don't you think that it's better if you say:

Code:
If String.IsNullOrEmpty(username) Then
       lblWarning.Text = "Username cannot be blank"
       Exit Sub 
End If
kulrom is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote

Reply
KEEP TABS
SPONSORS
 
Boxedart



 
 


 
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
  
 
 
 



 
  POSTING RULES
 
 
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Thread Tools
Display Modes

Forum Jump

 

All times are GMT -5. The time now is 03:44 AM.

   

Mascot team created by Drawshop.com

Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.

Server Monitoring by ENIACmonitor 0.01
HTMLforums.com © Big Resources, Inc. Web Design by BoxedArt.com
vRewrite 1.5 beta SEOed URLs completed by Tech Help Forum and Chalo Na.