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.