Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
JavaScript
function mycheck1() {

          var txt1 = document.getElementById("<%=txt1.clientid%>");

          if (txt1.value == "") {

             // alert("It is Empty");

              txt1.style.background = "#FFF000";
          }

      }


C#
asp:TextBox runat='server' ID='txt1'></asp:TextBox>
       <asp:CustomValidator runat="server" ID='cv1' ControlToValidate='txt1' ClientValidationFunction='mycheck1'
           ValidateEmptyText='true'  ></asp:CustomValidator>
           <asp:Button runat='server' ID='btn1' Text='submit' />
Posted
Updated 23-Aug-12 0:41am
v2
Comments
Anuja Pawar Indore 23-Aug-12 6:42am    
Added code block

XML
var v = document.getElementById("<%=TextBox1.ClientID%>");
            v.style.backgroundColor = "red";
 
Share this answer
 
Comments
Manas Bhardwaj 23-Aug-12 6:55am    
Correct +5!
Anuja Pawar Indore 23-Aug-12 7:03am    
Thanks Manas
 
Share this answer
 
I don't think you need to look at client id. This itself should work:

var txt1 = document.getElementById('txt1');
 
Share this answer
 
Comments
Christian Graus 23-Aug-12 6:45am    
No, you are completely wrong. In ASP.NET, the clientId property gives you the client side id, which can't be known for sure any other way
Manas Bhardwaj 23-Aug-12 6:50am    
Well, not completely :).
You can control this in ASP.Net 4.0. Have a look here:
http://weblogs.asp.net/asptest/archive/2009/01/06/asp-net-4-0-clientid-overview.aspx
kumar2233 23-Aug-12 7:04am    
code is working but i have minor mistake in this code txt1.style.backgroundcolor in replace of txt1.style.backgroundColor
Manas Bhardwaj 23-Aug-12 7:05am    
yup. I noticed and thats why voted for Anuja's answer which looks correct.
kumar2233 23-Aug-12 7:08am    
he has written same code That's why i vote

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900