I have an HTML form that I'm processing with Perl CGI, like so:
<form action="process.cgi" id="main">
<input type="text" name="foo" class="required" />
<input type="text" name="foo" class="required" />
<input type="text" name="foo" class="required" />
</form>
And I'm validating it with the jQuery Validation plugin. Trouble is, when I call $('#main').valid(), it only checks that the first field is non-empty. How to I get it to check all the fields?
OFFICIAL DOCUMENTATION:
jqueryvalidation.org/reference/#link-markup-recommendations
"Mandated: A 'name' attribute is required for all input elements needing validation, and the plugin will not work without this. A 'name' attribute must also be unique to the form, as this is how the plugin keeps track of all input elements. However, each group of radio or checkbox elements will share the same 'name' since the value of this grouping represents a single piece of the form data."
<input type="text" name="foo" class="required" />
<input type="text" name="foo" class="required" />
<input type="text" name="foo" class="required" />
Quote OP:
Trouble is, when I call $('#main').valid(), it only checks that the
first field is non-empty. How to I get it to check all the fields?
It has nothing specifically to do with .valid() and it will fail the same using other jQuery Validate methods. That's because all three fields have the same name of foo. For this plugin each input must have a unique name.
DEMO: jsfiddle.net/xaFZj/
EDIT:
You cannot have duplicate names while using this plugin. This demo clearly shows that the plugin will fail when the name attribute is duplicated. There is no workaround since the name attribute is how the plugin keeps track of the form elements.
jsfiddle.net/ed3vxgmy/
The only exception is a radio or checkbox group where the elements in each grouping will share the same name.
Related
I created a view component that needs as a parameter the name of the input generated when using "asp-for".
For example this case:
<input type="text" class="form-control" asp-for="InputModel.Description" />
I need in the code behind to have a string with the value "InputModel.Description".
I tried using nameof() but that only returns "Description".
Any ideas?
I'm building a webform in c# .net mvc using mongodb to store information. The form works with a company object that has a property that is a List of Addresses, called addressdata. When the form is submitted, the company object is sent to the controller and then upserted into MongoDB. The input names take the form
<input type="text" name="Company.addressdata[a].city" />
Where "a" is the index in the list. This all works great! The list of address objects is created upon submission and inserts into mongoDB.
However, I just added the ability to delete addresses, and now I'm running into trouble. I have noticed that when a user deletes the first row, all the rows after are lost. So, if they delete the 0 index, the Company object will not populate the list of Addresses and thus they will not go into MongoDB.
Is there a way to work around this? Is this how it's designed to work? It seems like too much to renumber all of the following rows with the new index, but is that what it takes? Or is there another way?
In my experience, that's by design. The indexes must start from 0, or you have to define your own indexes for each of them with a special element.
This article shows an example of that: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/
<form method="post" action="/Home/Create">
<input type="hidden" name="products.Index" value="cold" />
<input type="text" name="products[cold].Name" value="Beer" />
<input type="text" name="products[cold].Price" value="7.32" />
<input type="hidden" name="products.Index" value="123" />
<input type="text" name="products[123].Name" value="Chips" />
<input type="text" name="products[123].Price" value="2.23" />
<input type="hidden" name="products.Index" value="caliente" />
<input type="text" name="products[caliente].Name" value="Salsa" />
<input type="text" name="products[caliente].Price" value="1.23" />
<input type="submit" />
</form>
So you have options, either:
Update indices when deleting, or
Define arbitrary indices
I'm working in c# using web forms,
when integrating parsley for input validation it doesn't make the input with the 'equalto' arttirbute invalid if the equal to field changes.
<input type="password" runat="server" id="passwordTB" name="passwordTB" class="form-control" data-parsley-required="true" data-parsley-trigger="change" autocomplete="off" data-parsley-length="[7,40]" />
Repeat Password:
<input type="password" runat="server" id="passwordTBRepeat" name="passwordTBRepeat" class="form-control" data-parsley-required="true" data-parsley-trigger="change" data-parsley-equalto="#passwordTB" autocomplete="off" data-parsley-length="[7,40]" />
I would expect the passwordTBRepeat to validate itself and become invalid if passwordTB changed but it doesn't.
I'm using parsley v2.1.2 and jquery 2.1.4.
Is this by design or am I doing something wrong?
Currently, Parsley doesn't deal very well with interdependencies.
For now, you could manually call validate when the source input updates.
I have several checkboxes in a form. These are not server side checkboxes. When any box is checked, it never comes across in the form post. I'm using the following:
foreach (string key in Request.Form.AllKeys)
{...}
Any example checkbox is in this format:
<input id="chkFirstName" type="checkbox"/>
Any ideas what I'm doing wrong?
You're missing the name attribute. It is required to submit its value. The id attribute does not doe this.
<input id="chkFirstName" name="chkFirstName" type="checkbox"/>
You haven't set the name="" or value="" attributes on your <input /> element. The id attribute is only used client-side, whereas the name="" attribute corresponds to the Request field set, finally an input element must have a value for it to be submitted too.
If you're using WebForms, then use ASP.NET Controls (either <input runat="server" /> or <asp:CheckBox />) to take advantage of stronger-typing, validation, and other benefits.
What's the best way to pass values from a HTML tag/elements to code behind without using the runat="server". Example
<input id="tags" type="text" />
Now, I want to pass the value of tags to code behind.
string tagsvalue = this.Request.Form.Get( "tags" );
I'm trying to use the code above but is not working. Any help will be really appreciate it.
You use the name attribute to specify the name in the name/value pair not the id
<input name="tags" id="tags" type="text" />