如何允许只能输入仅接受M和C单词以及5位数字(0-9)的2个单词的用户?例如MC04326这是到目前为止的代码
else if (!(new Regex(@"^(MC)(([][0-9])$")).IsMatch(txtStudentIDReg.Text))
{
}
如此简单的验证就不需要正则表达式:
// str is inputted string
var isValid =
str.StartsWith("MC") && // starts with MC
str.Substring(2).All(ch => char.IsDigit(ch)) && // after second character, all are digits
str.Length == 7; // is of length 7
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句