I have RegularExpressionValidator
in ascx
page that validates TextBox1
.
我在ascx页面中有RegularExpressionValidator验证TextBox1。
<asp:RegularExpressionValidator ID ="RegularExpressionValidatorStateNumber" runat = "server" ControlToValidate="TextBox1"
ErrorMessage="Error message" Display ="Dynamic" ValidationExpression=""/>
I need to change ValidationExpression
dynamically depending on choice ofdropdownlist
.
我需要根据dropdownlist的选择动态更改ValidationExpression。
Here what I do in codeBehind:
这是我在codeBehind中所做的:
protected void DropDlSelectedIndexChanged(object sender, EventArgs e)
{
string region = DropDl.SelectedItem.Text;
RegularExpressionValidatorStateNumber.ValidationExpression = GetRegex(region);
}
Function GetRegex(region)
that returns regex expression works fine. But validation not works. Validator doesn't show error message when wrong data inserted.
返回正则表达式的函数GetRegex(区域)工作正常。但验证不起作用。插入错误数据时,Validator不显示错误消息。
Why validation not works? Or how to set validation expression dynamically?
为什么验证不起作用?或者如何动态设置验证表达式?
2 个解决方案
#1
1
Show your error message not as ErrorMessage, but as Text. Text="Not correct format";
将错误消息显示为ErrorMessage,而不是Text。 Text =“格式不正确”;
Also it would be good if you will color it: ForeColor = Color.Red
如果你将它着色它也会很好:ForeColor = Color.Red
#2
0
Use MATCH
which returns a Match object with all matches in the input string. This is useful when more than one match exists in the input text. The following code results in more than one match:
使用MATCH返回一个Match对象,其中包含输入字符串中的所有匹配项。当输入文本中存在多个匹配项时,这很有用。以下代码导致多个匹配:
string region = DropDl.SelectedItem.Text; // @"^([\w]+) ([\w]+)$";
string yourText="abcdxyz"
Match m = Regex.Match(yourText, region , RegexOptions.CultureInvariant);
foreach (Group g in m.Groups)
{
Console.WriteLine(g.Value);
}
#1
1
Show your error message not as ErrorMessage, but as Text. Text="Not correct format";
将错误消息显示为ErrorMessage,而不是Text。 Text =“格式不正确”;
Also it would be good if you will color it: ForeColor = Color.Red
如果你将它着色它也会很好:ForeColor = Color.Red
#2
0
Use MATCH
which returns a Match object with all matches in the input string. This is useful when more than one match exists in the input text. The following code results in more than one match:
使用MATCH返回一个Match对象,其中包含输入字符串中的所有匹配项。当输入文本中存在多个匹配项时,这很有用。以下代码导致多个匹配:
string region = DropDl.SelectedItem.Text; // @"^([\w]+) ([\w]+)$";
string yourText="abcdxyz"
Match m = Regex.Match(yourText, region , RegexOptions.CultureInvariant);
foreach (Group g in m.Groups)
{
Console.WriteLine(g.Value);
}