View Full Version : ErrorForm message
Suzzle
04-22-2004, 04:28 PM
ok, i have some code here, basically as u can see i want to it inform the user that the username or password they have entered is too long (if it is)
the code is working, however it is not displaying the message, its processing and not outputting anything.
here is ALL the code im working with, if anyone can help with this that would be great
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%
password=request.form("Password")
username=request.form("Username")
firstname=request.form("Firstname")
surname=request.form("Surname")
SET cn=server.CreateObject("ADODB.Connection")
cn.open "Pupils"
SET rs=Server.CreateObject("ADODB.Recordset")
rs.open "SELECT * FROM Users WHERE username='" & username & "'", cn, 3, 3
IF rs.EOF THEN
rs.addnew
rs("Username") = username
rs("Firstname") = Firstname
rs("Surname") = Surname
rs("password") = password
rs("level") =2
rs.Update
rs.Close
SET rs=nothing
cn.Close
SET cn=nothing
response.Redirect("success.asp")
ELSE
*****this part below is the problem code****
IF username = ">10" THEN
errorForm "The UserName entered is too long"
END IF
IF password= ">6" THEN
errorForm "The Password you entered is too long. Please remeber to make it no more than 6 characters"
END IF
END if
%>
miranda
04-22-2004, 08:47 PM
well I see a couple things. Let's start with the easiest part.
IF username = ">10" THEN
errorForm "The UserName entered is too long"
END IF
the way you wrote this, it is looking for a string with an assigned value of >10. So you need to dump the equal sign and the double quotes around the >10. Now if that is all you do you will generate an error because you haven't asked for the length of the string. so now add Len(username) to determine the length of the string. Now we have
If Len(username) > 10 Then
Ok now let's look at what you do if this condition is true. You have
errorForm "The UserName entered is too long". This is not valid either. Is there a seperate function you wrote that you aren't showing called errorForm? if so then it should be
errorForm("The UserName entered is too long")
or do you just want to output that error message to the screen directly? if so then it should be response.write("The UserName entered is too long")
Do the same with the next If/Then statement checking the password
now we have the following (assuming you have a function written to handle errors)
If Len(username) > 10 Then
errorForm("The UserName entered is too long")
End If
If Len(password) > 6 Then
errorForm("The Password you entered is too long. Please remember to make it no more than 6 characters")
End If
Next I see a problem with how you are even getting to the else part because your If/Then statement only checks the length of the username and password if the condition rs.EOF is false. It is too late then. because here it has checked to see if the username existed and it does. so lets make this change
IF rs.EOF THEN
If Len(username) <= 10 And Len(password) <= 6 Then
'add the user to the database
Else
If Len(username) > 10 Then
errorForm("The UserName entered is too long")
End If
If Len(password) > 6 Then
errorForm("The Password you entered is too long. Please remember to make it no more than 6 characters")
End If
Else
Response.write "That username already exists please pick another"
End If
one other thing should be mentioned, it is faster and less load on the server to dump the ado and just use a sql insert to insert the information into the database so i did that in this example but you can use the ado example instead of the sql insert
Final code would be like this
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%
Sub CleanUp
rs.Close
SET rs=nothing
cn.Close
SET cn=nothing
End Sub
Function errorForm(string)
'simple example of a function since all it does is write the message out
Response.Write (string)
End Function
password=request.form("Password")
username=request.form("Username")
firstname=request.form("Firstname")
surname=request.form("Surname")
SET cn=server.CreateObject("ADODB.Connection")
cn.open "Pupils"
SET rs=Server.CreateObject("ADODB.Recordset")
rs.open "SELECT Username FROM Users WHERE username='" & username & "'", cn, 3, 3
IF rs.EOF THEN
If Len(username) <= 10 And Len(password) <= 6 Then
'add the user to the database
dim sql
sql = "INSERT INTO Users (Username, Firstname, Surname, Password, Level)" &_
" VALUES("' & username & "', "' & firstname & "', "' & surname & "', "' & password & "', 2)"
cn.Execute(sql)
call CleanUp()
response.Redirect("success.asp")
Else
If Len(username) > 10 Then
errorForm("The UserName entered is too long")
End If
If Len(password) > 6 Then
errorForm("The Password you entered is too long. Please remember to make it no more than 6 characters")
End If
End If
Else
Response.write "That username already exists please pick another"
call CleanUp()
End If
%>
Suzzle
04-22-2004, 10:13 PM
miranda, you are a god send!
Thank you very much for that!!
:) :thumbsup:
miranda
04-22-2004, 10:33 PM
Glad I could help. There is one little thing I forgot to mention. You should also protect your code from sql interjection. This will keep people with knowledge of sql from making changes to your database.
to do that change this line username=request.form("Username")
to this
username=Trim(request.form("Username"))
username = Replace(username, "'", "''")
username = Replace(username, ";", " ")
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.