0

I have master and content page in asp .net web application. In my content page I am doing jquery validation. I have html element without runat="server" and I don't know how to refer to this id in jquery validation.

Here is my HTML code:

<table>
    <tr>
        <td>
            Title
        </td>
        <td>
            <select id="nameTitle" runat="server" class="InputText">
                <option value="-1">--Select--</option>
                <option value="Mr.">Mr</option>
                <option value="Mrs.">Mrs</option>
                <option value="Miss.">Miss</option>
            </select>
        </td>
    </tr>
    <tr>
        <td width="130px">
            First Name:<span class="RedMainText"><sup>*</sup></span>
        </td>
        <td>
            <input type="text" id="firstName" class="InputText" size="30" />
        </td>
    </tr>
    <tr>
        <td width="130px">
            Surname:<span class="RedMainText"><sup>*</sup></span>
        </td>
        <td>
            <input type="text" id="surname" class="InputText" size="30" />
        </td>
    </tr>
</table>

Jquery validation:

rules: 
{
    <%=nameTitle.UniqueID %>: { required: true },
    firstName: { required: true },
    surname: { required: true }
}, 
messages: 
{
    <%=nameTitle.UniqueID %>: { required: "Title is required" },
    firstName: { required: "First Name is required" },
    surname: { required: "Surname is required" }
}

Here firstName and lastName are not validated while nameTitle is validated. How to refer them? please advise.

3
  • Again, you must use the name attribute! Commented Sep 25, 2013 at 15:05
  • @Sparky I agree name is required but you please note that nameTitle does not have name and it works as expected with just the id. Commented Sep 25, 2013 at 15:10
  • Please show me a jsFiddle demo proving what you claim. I've shown you examples and I've shown you the official documentation. None of it changes the fact that this plugin absolutely requires a name attribute to keep track of inputs. Now I can only refer you to the source code of the plugin itself: ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/… Commented Sep 25, 2013 at 15:16

2 Answers 2

1

After taking a quick look at the API Documentation it appears to me that the firstName / surname items are supposed to link to the Name of an element

Could you please try it after setting the inputs up like so:

<input type="text" id="firstName" name="firstName" class="InputText" size="30" />
<input type="text" id="surname" name="surname" class="InputText" size="30" />
Sign up to request clarification or add additional context in comments.

1 Comment

@hima, exactly what I've been trying to tell you here and in the comments here
1

for writhing jquery-validate rules you should use name of elements not their ids!

set name for your elements and then use the name:

<select name="nameOfTitle" id="nameTitle" runat="server" class="InputText">
            <option value="-1">--Select--</option>
            <option value="Mr.">Mr</option>
            <option value="Mrs.">Mrs</option>
            <option value="Miss.">Miss</option>
</select>

and in jquery validate use:

rules: 
{
    nameOfTitle: { required: true },
}, 
messages: 
{
    nameOfTitle: { required: "Title is required" },
}

you should do the same for your other inputs as well.

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.