1

我有一个文本框..我想根据下拉列表值验证它的值..所以我正在使用后面的代码显示消息RegularExpressionValidator..但它不起作用请给我建议..

<asp:TextBox ID="tbnooflecture" runat="server"  Width="119px" Height="33px"></asp:TextBox>

              <asp:RegularExpressionValidator ID="RegularExpressionValidator1"  ForeColor="#6600FF" 
                runat="server"  ValidationGroup="upper" 
                     ControlToValidate="tbnooflecture"></asp:RegularExpressionValidator>
 <asp:Button ID="bfillattendence" runat="server" Text="To Fill Attendence Click Here"
                            onclick="FillAttendence_Click" Width="218px"  Height="33px" CausesValidation="true" ValidationGroup="upper"/>

我在按钮点击事件中写下面的代码

string  batchname = dpbatchname.SelectedItem.Text.ToString();
        int lengthofbatch=batchname.Length;
        if(lengthofbatch==2)
        {
            RegularExpressionValidator1.ValidationExpression = "[1-9][02468]|[02468]";

        RegularExpressionValidator1.ErrorMessage = "Only Even No. of Attendence is Valid for Lab.";
        }
        else if (lengthofbatch == 1)
        {
            RegularExpressionValidator1.ValidationExpression = "[0-9][0-9]|[0-9]";
            RegularExpressionValidator1.ErrorMessage = "Attendence Shold be Like 9,50";
        }
        else
        {
            RegularExpressionValidator1.ValidationExpression = "[0-9][0-9]|[0-9]";
            RegularExpressionValidator1.ErrorMessage = "Attendence Shold be Like 9,50";
        }
4

2 回答 2

0

你在验证文本框的价值吗?如果您在服务器端使用正则表达式,那么您需要验证输入(您的文本框),如下所示,并显示消息。

if (Regex.IsMatch(text, reg))
{

}
于 2013-03-12T06:56:54.033 回答
0

添加这一行RequiredFieldValidator1.IsValid = false;

RegularExpressionValidator1.ValidationExpression = "[0-9][0-9]|[0-9]"; RegularExpressionValidator1.ErrorMessage = "出席人数应为 9,50";

于 2013-03-12T07:01:26.030 回答