3

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" />
1
  • See my Edit! I'm sure it will work for you! Commented Jun 15, 2011 at 17:10

4 Answers 4

2

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" />
Sign up to request clarification or add additional context in comments.

4 Comments

When you say it doesn't work, what exactly do you mean? Is it not validating, throwing JavaScript errors, allowing the form to submit unvalidated, etc?
is not making validations at all notes that if i put the generated name by asp it works but that's not the idea
What is that name that ASP.NET is rendering?
i modify the question when the render name
1
+50

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});

        }); 
    });

Comments

1

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.

1 Comment

hi, and thank's for asking but the problem it's that i can't put that selector like an option in the rules of the jquery validator. but the selector works
0

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;
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.