简短的回答是——这可能是可能的,但非常困难,而且解决方案非常脆弱,需要大量维护。
理论上,如果您可以在使用正则表达式验证器时找出 ASP.NET 生成的 JS 代码,然后对其进行逆向工程,您就可以做到这一点。这里的主要问题是新字段是在客户端而不是服务器端创建的。
如果您可以更新您的页面以便在服务器端动态创建新的文本框,那么您所要做的就是在 OnInit 方法中创建新的文本框和新的验证器,这将起作用。
这是它的样子。
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
//set the appropriate conditions for your solution
if (true)
{
//create text box
TextBox txtNewField = new TextBox();
txtNewField.ID = "txtNewField";
//create and initialize validator
RegularExpressionValidator regexR1 = new RegularExpressionValidator();
regexR1.ValidationExpression = "set the regex here";
regexR1.ControlToValidate = txtNewField.ID;
regexR1.ErrorMessage = "you are doing something wrong";
//add both of these to page wehre needed.
//I assume there is a panel control where you are adding these
//but you can customize it more
pnlFields.Controls.Add(txtNewField);
pnlFields.Controls.Add(regexR1);
}
}