0

I am using below .aspx code to validate textbox..this is working perfectly

<asp:TextBox ID="tbnooflecture" runat="server"  Width="113px" Height="33px">
</asp:TextBox>
  <asp:RegularExpressionValidator ID="RegularExpressionValidator1"  
       ForeColor="#6600FF" runat="server" 
       ErrorMessage="Total Attendence Should be Like 3 or 50" 
       ValidationGroup="upper" Display="Dynamic" 
       ControlToValidate="tbnooflecture" 
       ValidationExpression="[0-9][0-9]|[0-9]">*
    </asp:RegularExpressionValidator>

What I want that above this textbox there is dropdownlist named batchname and if length of batchname is 2, I want to put validation that Attendence should be even no.

I have used below code on button click

 if (lenghth == 2)
 {
    if (!System.Text.RegularExpressions.Regex.IsMatch(name, "[1-9][02468]"))
    {
        Label5.Text = "Only Even Entry for Labs";
        Label5.Visible = true;
    }
}

I want to do it on client-side. How can I do it in C#?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
user2053138
  • 141
  • 2
  • 6
  • 16

2 Answers2

0

You need to use custom-validation control of asp.net.
Few useful links
https://web.archive.org/web/20211020145934/https://www.4guysfromrolla.com/articles/073102-1.aspx
http://msdn.microsoft.com/en-us/library/f5db6z8k%28v=vs.71%29.aspx
http://www.w3schools.com/aspnet/control_customvalidator.asp
Custom validator error text through javascript?

Community
  • 1
  • 1
शेखर
  • 17,412
  • 13
  • 61
  • 117
  • i used protected void cusCustom_ServerValidate1(object source, ServerValidateEventArgs e) { it display's error message but it also allows Gridview to populate,while it should not be.. if (lengthofbatch == 2) { e.IsValid = true; } else { e.IsValid = false; } } – user2053138 Mar 15 '13 at 16:26
0

That is server side validation @user2053138, you mentioned in comment.

Check out the following example:

<asp:TextBox id="Text1" 
       runat="server" />

  &nbsp;&nbsp;

  <asp:CustomValidator id="CustomValidator1"
       ControlToValidate="Text1"
       ClientValidationFunction="ClientValidate"
       OnServerValidate="ServerValidation"
       Display="Static"
       ErrorMessage="Not an even number!"
       ForeColor="green"
       Font-Name="verdana" 
       Font-Size="10pt"
       runat="server"/>

<script language="javascript"> 
  function ClientValidate(source, arguments)
  {
    if (arguments.Value % 2 == 0 ){
        arguments.IsValid = true;
    } else {
        arguments.IsValid = false;
    }
  }
</script>
Nilesh Thakkar
  • 2,877
  • 1
  • 24
  • 43
  • :-but even no of attendance constraint should only fire when Dropdownlist named batch name should have value of length 2,it should not fire when Dropdownlist named batch name should have value of length 1..Currently it is firing on both type of batch names..Plz help – user2053138 Mar 15 '13 at 17:30