如果同一页面上的用户名和密码错误,则显示错误消息

时间:2013-12-03 06:35:32

标签: jquery ajax asp-classic

我知道如果用户名和密码不匹配,可以使用jquery或ajax在同一页面上的div中显示和错误消息。任何人都可以帮助我做到这一点

HTML:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
body,td,th {
    font-family: "Myriad Pro";
    font-size: 14px;
    color: #000;
}
body {
    background-color: #194370;
}
.textfield {
    font-family: "Myriad Pro";
    font-size: 14px;
    word-spacing: normal;
}
.style1 {
    color: #FFFFFF;
    font-weight: bold;
}
</style>
</head>
<body>
<form name="form1" method="POST" action="<%=MM_LoginAction%>">
  <table width="323" border="0" cellpadding="3" cellspacing="3">
    <tr>
      <td colspan="2">&nbsp;</td>
    </tr>
    <tr>
      <td colspan="2"><p>&nbsp;</p>
        <p>&nbsp;</p>
      <p>&nbsp;</p></td>
    </tr>
    <tr>
      <td colspan="2" bgcolor="#FFFFFF"><div align="center">Enter Username and Password</div></td>
    </tr>
    <tr>
      <td width="84"><span class="style1">Username</span></td>
      <td width="218"><label for="username"></label>
      <input name="username" type="text" class="textfield" id="username"></td>
    </tr>
    <tr>
      <td><span class="style1">Password</span></td>
      <td><label for="password"></label>
      <input name="password" type="password" class="textfield" id="password"></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="button" id="button" value="  Log In  "></td>
    </tr>
  </table>
</form>
</body>
</html>

ASP

<%@LANGUAGE="VBSCRIPT"%>
<!--#include file="../Connections/VT.asp" -->
<%
' *** Validate request to log in to this site.
MM_LoginAction = Request.ServerVariables("URL")
If Request.QueryString <> "" Then MM_LoginAction = MM_LoginAction + "?" + Server.HTMLEncode(Request.QueryString)
MM_valUsername = CStr(Request.Form("username"))
If MM_valUsername <> "" Then
  Dim MM_fldUserAuthorization
  Dim MM_redirectLoginSuccess
  Dim MM_redirectLoginFailed
  Dim MM_loginSQL
  Dim MM_rsUser
  Dim MM_rsUser_cmd

  MM_fldUserAuthorization = ""
  MM_redirectLoginSuccess = "source.asp"
  MM_redirectLoginFailed = "error.html"

  MM_loginSQL = "SELECT Username, Password"
  If MM_fldUserAuthorization <> "" Then MM_loginSQL = MM_loginSQL & "," & MM_fldUserAuthorization
  MM_loginSQL = MM_loginSQL & " FROM dbo.Test_Register_Users WHERE Username = ? AND Password = ?"
  Set MM_rsUser_cmd = Server.CreateObject ("ADODB.Command")
  MM_rsUser_cmd.ActiveConnection = MM_VT_STRING
  MM_rsUser_cmd.CommandText = MM_loginSQL
  MM_rsUser_cmd.Parameters.Append MM_rsUser_cmd.CreateParameter("param1", 200, 1, 70, MM_valUsername) ' adVarChar
  MM_rsUser_cmd.Parameters.Append MM_rsUser_cmd.CreateParameter("param2", 200, 1, 50, Request.Form("password")) ' adVarChar
  MM_rsUser_cmd.Prepared = true
  Set MM_rsUser = MM_rsUser_cmd.Execute

  If Not MM_rsUser.EOF Or Not MM_rsUser.BOF Then 
    ' username and password match - this is a valid user
    Session("MM_Username") = MM_valUsername
    If (MM_fldUserAuthorization <> "") Then
      Session("MM_UserAuthorization") = CStr(MM_rsUser.Fields.Item(MM_fldUserAuthorization).Value)
    Else
      Session("MM_UserAuthorization") = ""
    End If
    if CStr(Request.QueryString("accessdenied")) <> "" And true Then
      MM_redirectLoginSuccess = Request.QueryString("accessdenied")
    End If
    MM_rsUser.Close
    Response.Redirect(MM_redirectLoginSuccess)
  End If
  MM_rsUser.Close
  Response.Redirect(MM_redirectLoginFailed)
End If
%>

我想要这样,点击提交按钮,它会验证,如果它是正确的,它会重定向到主页,如果错误,错误信息应显示在同一页面上。如果用户名和密码不匹配,我目前将其重定向到不同的页面。

1 个答案:

答案 0 :(得分:0)

在ASP中,设置错误重定向以返回到您的登录页面,但是使用一个表示发生错误的查询字符串(修改链接以适合您的项目,我只是猜测):

MM_redirectLoginFailed = "login.asp?error=true"

在登录页面的HTML中,添加这样的内容,以便显示错误:

<% If Request.QueryString("error") = "true" Then %>
Response.Write "<p class=""error"">An error occurred, please try again.</p>"
<% End If %>

显然,您可以根据自己的需要自定义错误标记以适合您的设计。只有在登录错误重定向中设置查询字符串时,才会写出IfEnd If之间的任何内容。

如果你是从头开始,有更好的方法来实现这一点,但在这个阶段,它将意味着大量的重写代码;我建议的解决方案是实现你想要的一种相当快速和肮脏的方式。