简体   繁体   English

验证来自文本框C#的用户输入

[英]Validating user input from textbox c#

I am trying to validate a user input to a given string. 我正在尝试验证给定字符串的用户输入。 I am able to do this in java with this code 我可以使用以下代码在Java中执行此操作

        String username = request.getParameter("txtUsername");
        String password = request.getParameter("txtPassword"];)

   if (username.equals("john") && (password.equals("smith"))){

        out.println("Success");

    }
    else{

         out.println("validation failed");
     }

but it returns a NullReferenceException in C# using this same code. 但是它使用相同的代码在C#中返回NullReferenceException。

       {

    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        String username = Request.QueryString["txtUsername"] ?? "";
        String password = Request.QueryString["txtPassword"] ?? "";

        try {
        if(username.Equals("john") && (password.Equals("smith"))){

            lblLogin.Text = "Success";
            Response.Redirect("ModelProfile.aspx");
        }
        else

        {
            lblLogin.Text = "Failed";
             Response.Redirect("Login.aspx");
        }

        }
        catch
        {
            lblLogin.Text = "Please type in some valid credentials";

        }

    }

Here is the text boxes in the aspx page looks like: 这是aspx页面中的文本框,如下所示:

            <div id="loginUsername">
                <asp:Label ID="lblUsername" runat="server" Text="Username:"></asp:Label>

                <asp:TextBox ID="txtUsername" runat="server"   CssClass="mytext"></asp:TextBox>
            </div>

        <div id="loginPassword">
            <asp:Label ID="lblPassword" runat="server" Text="Password:"></asp:Label>
            <asp:TextBox ID="txtPassword" runat="server" CssClass="mytext"></asp:TextBox>

        </div>
        <div id="loginButton">

            <asp:Button ID="btnLogin" runat="server" Text="Login" CssClass="button" OnClick="btnLogin_Click" />
            <asp:Label ID="lblLogin" runat="server" Text=""></asp:Label>

        </div>
    </div>

Please any ideas on how I can solve this will be appreciated. 请任何有关如何解决此问题的想法将不胜感激。 Thanks 谢谢

You could easily fix your problem with 您可以轻松地解决您的问题

    String username = Request.QueryString["txtUsername"] ?? "";
    String password = Request.QueryString["txtPassword"] ?? "";

The ?? ?? is the C# Null Coalescing operator C#Null合并运算符

However looking at your code, it seems that you don't have to work with the QueryString because the button login is on the same page of your textboxes. 但是,查看代码,您似乎不必使用QueryString,因为按钮登录位于文本框的同一页面上。 If this is correct then you should reference the textbox directly. 如果正确,那么您应该直接引用文本框。 No need to check for null because the textbox text property is never null (if you really want to be very precise you could add a Trim) 无需检查null,因为textbox文本属性永远不会为null(如果您确实想非常精确,则可以添加Trim)

    String username = txtUsername.Text.Trim();
    String password = txtPassword.Text.Trim();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM