Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery validator with asp.net

Tags:

jquery

asp.net

i need to understand how i can get the access to objects asp with jquery because i have a file that it's register in page load of the code behind of my aspx that have the script need to do a validation, i read in different's site that i can do it with this <%=txtName.UniqueID %> but doesn't work i going to put my code and anyone could help me. thanks

the button that trigger the validation this method in the script file

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

the js

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

function validateData() {

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

my code behind where i registered the 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 the text input that i need to validate

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

With '<%=txtName.UniqueID %>' and <%=txtName.UniqueID %> doesn't work also with '.txtLayerName'

UPDATE 2

this is the render name by asp

<input name="ctl00$ContentPlaceHolder1$txtLayerName" type="text" id="ctl00_ContentPlaceHolder1_txtLayerName" class="txtLayerName required" />
like image 379
Jorge Avatar asked Jun 10 '26 10:06

Jorge


1 Answers

I think you want:

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

Also, keep in mind that you can use a CSS class instead of defining the rules like that, which is a bit easier. E.g.:

<asp:TextBox ID="txtLayerName" CssClass="txtLayerName required" runat="server" />
like image 54
Dave Ward Avatar answered Jun 12 '26 10:06

Dave Ward