这就是我所拥有的,除了白色空间规则之外的其他所有规则。
((?=.*\d)(?=.*[A-Z]).{8,15})
要为此添加什么?
非常感谢! 语言:c#,asp:RegularExpressionValidator
答案 0 :(得分:4)
只是匹配:
^(?!.* )(?=.*\d)(?=.*[A-Z]).{8,15}$
工作原理:
.{8,15}
表示: 8到15个字符
(?!.* )
表示:不包含“”
(?=.*\d)
表示:至少包含一位数字。
(?=.*[A-Z])
表示:至少包含一个大写字母
答案 1 :(得分:0)
^((?!.*[\s])(?=.*[A-Z])(?=.*\d).{8,15})
答案 2 :(得分:0)
作为RegEx的替代方案,您是否考虑过基本的字符串解析?换句话说,如果您需要协助编写RegEx,随着时间的推移可维护性会发生什么?
对于我们大多数人来说,简单的字符串解析更容易理解。那些跟随我们的脚步的人将更容易理解代码并添加其他要求。
这是一个使用字符串解析的示例,即使没有错误消息也是自我记录的。
/// <summary>
/// Determines whether a password is valid.
/// </summary>
/// <param name="password">The password.</param>
/// <returns>A Tuple where Item1 is a boolean (true == valid password; false otherwise).
/// And Item2 is the message validating the password.</returns>
public Tuple<bool, string> IsValidPassword( string password )
{
if( password.Contains( " " ) )
{
return new Tuple<bool, string>( false, "Password cannot contain white spaces." );
}
if( !password.Any( char.IsNumber ) )
{
return new Tuple<bool, string>( false, "Password must contain at least one numeric char." );
}
// perhaps the requirements meant to be 1 or more capital letters?
// if( !password.Any( char.IsUpper ) )
if( password.Count( char.IsUpper ) != 1 )
{
return new Tuple<bool, string>( false, "Password must contain only 1 capital letter." );
}
if( password.Length < 8 )
{
return new Tuple<bool, string>( false, "Password is too short; must be at least 8 characters (15 max)." );
}
if( password.Length > 15 )
{
return new Tuple<bool, string>( false, "Password is too long; must be no more than 15 characters (8 min)." );
}
return new Tuple<bool, string>( true, "Password is valid." );
}