Jquery validator with asp.net

I need to understand how I can access asp objects with jquery, because I have a file that it registers in the loading page of the code behind my aspx, whose script needs to be checked, I read elsewhere that I I can do it with this <%=txtName.UniqueID %>, but it doesn’t work, I am going to put my code and someone can help me. thank

button that starts checking this method in the script file

    <asp:Button ID="btnOk" CssClass="btnOk" runat="server" Text="Ok" /> 

js

$(".btnOk").click(validateData);

function validateData() {

$("form").validate({
    rules: 
    {
        '<%=txtName.UniqueID %>': "required"        
    }
});
}

my code where I registered scripts

ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", "<script language=javascript src='js/SetIndicators.js'> </script>");
ClientScript.RegisterClientScriptBlock(this.GetType(), "Validator", "<script language=javascript src='js/jquery.validate.js'> </script>");

UPDATE

this is text input i need to check

<asp:TextBox ID="txtLayerName" CssClass="txtLayerName" runat="server"> </asp:TextBox>

C '<%=txtName.UniqueID %>'and <%=txtName.UniqueID %>does not work well with'.txtLayerName'

UPDATE 2

this is the name of the asp render

<input name="ctl00$ContentPlaceHolder1$txtLayerName" type="text" id="ctl00_ContentPlaceHolder1_txtLayerName" class="txtLayerName required" />
+3
source share
4 answers

jquery , NAME html, . , -, , . .

EDIT: " " :

    $(function(){
       $("form").validate(); 

       $('.txtLayerName').each(function () {

            //add the required validator to these!
            $(this).rules('add', {required: true});

        }); 
    });
+1

, :

'<%= txtLayerName.ClientID %>': "required"

, , CSS , . :.

<asp:TextBox ID="txtLayerName" CssClass="txtLayerName required" runat="server" />
+2

jQuery "" , .

, - "ctl00 $foo $bar $btnOk" jQuery :

$("[id$=btnOk]").click(validateData);

TextBox.

rules: 
{
    $("[id$=txtLayerName]").attr("id") : "required"        
}

UPDATE:

:

rules = {};
rules[$("[id$=txtLayerName]").attr("id")] = "required";

, .

+1
source

This works as I use it, although I would suggest that it '<%=txtName.UniqueID %>'does not need work ''...

If you are using .NET 4 and you have clientIDMode = Static, you can also use the following script and use the names as usual.

    $("#aspForm input:not(:hidden)").each(function () {
        var i = $(this).get(0);
        i.name = i.id;
    });
0
source

All Articles