通过正则表达式目录
List<string>
这样的:
var animals = new List<string>
{
"Dog",
"Cat"
};
animals
只能包含2个值:Dog
和Cat
。所以,如果值为Tiger
或Lion
,那么这是无效的。
这里是我用来验证的基本途径:
var regex = new Regex(@"Dog|Cat");
foreach (string animal in animals)
{
if (!regex.IsMatch(animal))
{
// throw error message here...
}
}
现在,我要声明的模型Animal
存储列表:
class Animal
{
//[RegularExpression(@"Dog|Cat", ErrorMessage = "Invalid animal")]
public List<string> Animals { get; set; }
}
在一些行动:
public ActionResult Add(Animal model)
{
if (ModelState.IsValid)
{
// do stuff...
}
// throw error message...
}
所以,我的问题是:如何使用正则表达式来验证这一点List<string>
值 案件?
非常感谢!这是帮助:) –