正则表达式在RegularExpressionValidator上工作,但在Regex.IsMatch上返回false
本文关键字:IsMatch Regex 返回 false 但在 RegularExpressionValidator 工作 正则表达式 | 更新日期: 2023-09-27 18:05:50
我有以下RegularExpressionValidator:
<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 & og %."
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;
为什么当txtPassword.Value
和txtPasswordRepeat中的值为0时,IsMatch返回false ?值通过测试的前端(regulareexpression validator)?
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);