Jorge
Jorge

Reputation: 18237

jquery validator with 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" />

Upvotes: 3

Views: 1451

Answers (4)

Dave Thieben
Dave Thieben

Reputation: 5427

I have had good luck using jQuery's "ends with" selector because the server side ID of the control is always the last bit of the client ID string.

for instance, your button's client ID may be something like "ctl00$foo$bar$btnOk" then your jQuery would be:

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

then you can do the same thing with the TextBox.

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

UPDATE:

try this instead:

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

since objects are essentially just arrays with direct named access to the elements, you can create an array and use it like an object.

Upvotes: 1

Patricia
Patricia

Reputation: 7802

jquery validate using the NAME property of html inputs, not the ID. it's been a while since i used webforms, so i don't know if the name and id match. but it might be worth looking into.

EDIT: i've added validation on the fly with great success like so:

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

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

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

        }); 
    });

Upvotes: 1

Dave Ward
Dave Ward

Reputation: 60580

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" />

Upvotes: 2

ShaneBlake
ShaneBlake

Reputation: 11096

That does work, as I'm using it, though I would guess that '<%=txtName.UniqueID %>' doesn't need the '''s to work...

If you're using .NET 4 and have clientIDMode = Static, you can also use the following script and and use the names as you normally would.

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

Upvotes: 0

Related Questions