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.

hima
  • 610
  • 3
  • 10
  • 24
  • [Again, you **must** use the `name` attribute!](http://stackoverflow.com/a/18962679/594235) – Sparky Sep 25 '13 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. – hima Sep 25 '13 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: http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js – Sparky Sep 25 '13 at 15:16

2 Answers2

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.

AhmadF
  • 353
  • 1
  • 2
  • 10
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" />
Nunners
  • 3,047
  • 13
  • 17
  • @hima, exactly what I've been trying to tell you [here](http://stackoverflow.com/a/18962679/594235) and in the comments [here](http://stackoverflow.com/a/18981812/594235) – Sparky Sep 25 '13 at 15:04