teenup
teenup

Reputation: 7667

How to get "name" of a server control just like "ClientID" in javascript?

I want to apply validation through jquery in my forms and for that I require the "name" attribute of the TextBoxes on my form which are generated by the ASP.Net automatically. Is there any way I can get the name of elements in javascript just like:

<script>
'<%= txt.ClientID %>'
</script>

I am using the following type of jquery validation:

$(".selector").validate({
   rules: {
     // simple rule, converted to {required:true}
     name: "required",
     // compound rule
     email: {
       required: true,
       email: true
     }
   }
})

Upvotes: 1

Views: 818

Answers (3)

dhinesh
dhinesh

Reputation: 4764

You can use the same way you have mentioned

<script type="text/javascript" language="javascript">
    $(document).ready(function() {
        $("#aspnetForm").validate({
            errorLabelContainer: "#messageBox",
            wrapper: "li",
            onsubmit: false,
            rules: {
                <%=txtName.UniqueID %>:{
                    required: true,
                    maxlength: 50
                },
                <%=ddlState.UniqueID %>:{
                    selectOne: true
                },
                <%=ddlCity.UniqueID %>:{
                    selectOne: true
                }

            }, 
            messages: 
            {
                <%=txtName.UniqueID %>:{
                    required: "<%=Resources.EnterNameValidationMessage %>",
                    maxlength: "<%=Resources.MaxLengthEnterNameValidationMessage %>"
                },
                <%=ddlState.UniqueID %>:{
                    selectOne: "<%=Resources.SelectStateValidationMessage %>"
                },
                <%=ddlCity.UniqueID %>:{
                    selectOne: "<%=Resources.SelectCityValidationMessage %>"
                }
            }
        });
    });

Upvotes: 1

Yves M.
Yves M.

Reputation: 3318

You might do it like this...

<script> 
  var textbox = '<%= txt.UniqueID %>';
</script> 

Or even...

<script> 
  $('#<%= txt.UniqueID %>').DoYourThing();
</script> 

Upvotes: 1

breez
breez

Reputation: 494

<script>
'<%= txt.UniqueID %>'
</script>

Upvotes: 1

Related Questions