2013-03-12 17 views
1

我有一個文本框.. 我想驗證它是一個基於下拉列表值值.. 讓我從代碼中顯示的訊息後面使用RegularExpressionValidator .. 但它不工作PLZ ,給我的建議..如何從代碼dislay驗證消息的背後

<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"; 
     } 
+0

是否有任何錯誤可以看到........ – 2013-03-12 06:50:56

回答

0

你驗證文本框的值? 如果您在服務器端使用正則表達式,那麼您需要驗證下面的輸入(您的文本框),如 ,並顯示消息。

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

} 
0

添加此行 RequiredFieldValidator1.IsValid = false;

RegularExpressionValidator1.ValidationExpression = 「[0-9] [0-9] | [0-9]」; RegularExpressionValidator1.ErrorMessage =「Attendence Shold be Like 9,50」;

+0

添加此行 RequiredFieldValidator1.IsValid = false; RegularExpressionValidator1.ValidationExpression =「[0-9] [0-9] | [0-9]」; RegularExpressionValidator1.ErrorMessage =「Attendence Shold be Like 9,50」; – 2013-03-12 07:04:36