我正在使用正则表达式验证数据,我的用户名和电子邮件有效,但密码返回值为 null

本文关键字:有效 电子邮件 密码 null 返回值 我的 正则表达式 数据 验证 用户 | 更新日期: 2023-09-27 18:30:22

这是我的代码,我不明白为什么它不起作用,我使用了与我的电子邮件和用户名相同的模板,它们都工作正常。

string Password = Request["Password"];
System.Text.RegularExpressions.Regex regex2 =
    new System.Text.RegularExpressions.Regex(@"^([^0-9]*)$");
// does the string follow the regex structure?
System.Text.RegularExpressions.Match match2 = regex2.Match(Password);
//if yes
if (match2.Success)
{
}
else
{
    passwordAlert = "password is not valid";
}
<form class="text-center" method="post">
    <label>Email: <input type="text" name="email"/> </label> <p>@emailAlert</p> <br />
    <label>Username: <input type="text" name="username" /> </label> <p>@usernameAlert</p> <br />
    <label>Password: <input type="password" name="Password" /> </label> <p>@passwordAlert</p>
    <input type="submit" name="Submit" />
</form>
    string emailAlert = "";
    string usernameAlert = "";
    string passwordAlert = "";
    if(IsPost)
    {
    string email = Request["email"];
    System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"^(['w'.'-]+)@(['w'-]+)(('.('w){2,3})+)$");
    // does the string follow the regex structure?
    System.Text.RegularExpressions.Match match = regex.Match(email);
    //if yes
    if (match.Success){

    } else {
        emailAlert = "email is not valid";    
    }
    string username = Request["username"];
    System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z0-9_@.-]*$");
    // does the string follow the regex structure?
    System.Text.RegularExpressions.Match match1 = regex1.Match(username);
    //if yes
    if (match1.Success)
    {

    }
    else
    {
        usernameAlert = "username is not valid";
    }    
}
    string Password = Request.Form["Password"];
    System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@"^([0-9],{6,})$");
    // does the string follow the regex structure?
    System.Text.RegularExpressions.Match match2 = regex2.Match(Password);
    //if yes
    if (match2.Success)
    {

    }
    else
    {
        passwordAlert = "password is not valid";
    }
}
<form runat="server" class="text-center" method="post">
      <label>Email: <input type="text" name="email"/> </label> <p>@emailAlert</p> <br />
      <label>Username: <input type="text" name="username" /> </label> <p>@usernameAlert</p> <br />
      <label>Password: <input Request.Form["Password"]; type="password" name="Password" /> </label> <p>@passwordAlert</p>
      <input type="submit" name="Submit" />

</form>

我现在添加了完整的代码,以防万一在错误点之前发生冲突

提前致谢

我正在使用正则表达式验证数据,我的用户名和电子邮件有效,但密码返回值为 null

您需要从此
更改正则表达式 ^([^0-9]*)$这个^([0-9]*)$

您还需要使用读取密码字段

Request.Form["Password"];

在表单标记中添加 runat=服务器