Ana*_*ion 2 c# regex asp.net-mvc unobtrusive-validation
我对regexp真的很不好,我想要的是一个不匹配任何html标记(用于用户输入验证)的regexp。
我想要的是负面的:
<[^>]+>
Run Code Online (Sandbox Code Playgroud)
我现在有的是
public class MessageViewModel
{
[Required]
[RegularExpression(@"<[^>]+>", ErrorMessage = "No html tags allowed")]
public string UserName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
但这与我想要的相反-只能使用html标签的用户名
正则表达式不能进行“负”匹配。
但是他们可以进行“正”匹配,然后您就可以将找到的所有内容排除在字符串之外。
编辑-更新问题后,事情变得更加清晰了。尝试这个:
public class MessageViewModel
{
[Required]
[RegularExpression(@"^(?!.*<[^>]+>).*", ErrorMessage = "No html tags allowed")]
public string UserName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
说明:
^ # start of string
(?! # negative look-ahead (a position not followed by...)
.* # anything
<[^>]+> # something that looks like an HTML tag
) # end look-ahead
.* # match the remainder of the string
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2901 次 |
| 最近记录: |