Click here to Skip to main content
16,013,489 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I m working with Master page and contentpage. I want to validate content page controls in javscript using button click event.

I give that following coding. but it is not access it.
C#
if (document.getElementById("txtBloodGroup").value == "") {
        alert("Please Enter Blood Group Name");
        return false;
    }



So I give following coding

C#
var id = document.getElementById("<%=txtBloodGroup.ClientID");
  if (id.value == "") {
      alert("Please Enter Blood Group Name");
      id.focus();
      return false;
  }


But this is also not working. If i check that name at runtime using firebug, the id of the control is like 'ctl00_ContentPlaceHolder1_txtBloodGroup'

If i give this id, it is working. but it is not a correct method. what can i do i solve this problem.
Posted
Comments
Mahesh Bailwal 14-May-13 6:57am    
I think there is syntax error at var id = document.getElementById("<%=txtBloodGroup.ClientID") as your closing server tag (%>) is missing. If I am not wrong.

Yes you are right.. Using ContentPageID_ControlID not good,

But we have an option try like below it will help you...

On the Submit Button Client Click Event call a user defined function and on that function try the below code...

JavaScript :

C#
var elems = document.getElementsByTagName("*");
for (var i = 0; i < elems.length; i++) {
    if (elems[i].id.indexOf("txtBloodGroup") != -1)
     {
      if (elems[i].value == "") {
        alert("Please Enter Blood Group Name");
        elems[i].focus();
        return false;
      }
    }
}


Also, Read this Finding Controls in a Master Page with jQuery Article, Here they discuss about this issue and solution....
 
Share this answer
 
v2
Comments
UshaCbe 14-May-13 7:56am    
Hi thank u that is working
Please use JQUERY
JavaScript
$(document).ready(function(){
     $("[$id='btnSubmit']").click(function(){
          if($("[$id='txtBloodGroup']").val() == ''){
               alert("Please enter blood group name");
          }
     });
});
 
Share this answer
 

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