1

我有以下正则表达式验证器:

<asp:RegularExpressionValidator CssClass="Validator_alert" ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtPassword"
        ErrorMessage="Dit kodeord skal være mindst 6 tegn langt og indholde et tal, et stort bogstav og lille bogstav. Kodeordet må ikke indeholde tegnene &amp; og &#37;."
        ValidationExpression="(?!^[0-9]*$)(?!^[a-z])(?!^[A-Z]*$)^([a-zA-Z0-9!@_:;+]{6,50})$"
        SetFocusOnError="true" />

以下代码也用于检查表达式:

string pattern = @"(?!^[0-9]*$)(?!^[a-z])(?!^[A-Z]*$)^([a-zA-Z0-9!@_:;+]{6,50})$";

                Boolean meetsRequirements = false;

                meetsRequirements = System.Text.RegularExpressions.Regex.IsMatch(pattern, txtPassword.Value);
                meetsRequirements = System.Text.RegularExpressions.Regex.IsMatch(pattern, txtPasswordRepeat.Value);

                return meetsRequirements;

为什么IsMatch在txtPassword.Value和txtPasswordRepeat.Value中的值通过前端(RegularExpressionValidator)的测试时返回false?

4

1 回答 1

1

Regex.IsMatch接受 2 个参数 - 第一个是输入文本,第二个是模式。您的参数顺序不正确。利用:

string pattern = @"(?!^[0-9]*$)(?!^[a-z])(?!^[A-Z]*$)^([a-zA-Z0-9!@_:;+]{6,50})$";

Boolean meetsRequirements = false;

meetsRequirements = System.Text.RegularExpressions.Regex.IsMatch(txtPassword.Value, pattern);
meetsRequirements = System.Text.RegularExpressions.Regex.IsMatch(txtPasswordRepeat.Value, pattern);

return meetsRequirements;

很可能您应该使用以下方法检查两个输入是否正确:

meetsRequirements = 
    System.Text.RegularExpressions.Regex.IsMatch(txtPassword.Value, pattern)
    && System.Text.RegularExpressions.Regex.IsMatch(txtPasswordRepeat.Value, pattern);
于 2013-03-26T11:44:54.483 回答