When applying rule to a class, custom messages applied only to first element · Issue #2448 · jquery-validation/jquery-validation · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

When applying rule to a class, custom messages applied only to first element #2448

Open
@enboig

Description

@enboig

Your environment

  • Version of jquery-validate: Master
  • Browser name and version: Firefox

Current behavior

When aplying a rule to $(".myclass") it is only applied to the first element.

Expected behavior

When selecting a class, rule should be applied to all elements of the class.

Live demo

https://jsbin.com/lozamas/edit?html,console,output

I also codded a unit test when trying to find a solution:

	<form id="testForm29" class="testttt">
	<div>
		<input type="text" name="form29field1" id="form29field1" class="required-field-custom-message">
	</div>
	<div>
		<input type="text" name="form29field2" id="form29field2" class="required-field-custom-message">
	</div>
	<div>
		<input type="text" name="form29field0" id="form29field0">
	</div>
	</form>
QUnit.test( "add rule to class with custom messages", function( assert ) {
	assert.expect( 3 );
	var form = $( "#testForm29" );
	var v = form.validate();

	$( "#form29field0" ).rules( "add", {
		required: true,
		messages: {
			required: "Custom message"
		}
	} );

	$( ".required-field-custom-message" ).rules( "add", {
		required: true,
		messages: {
			required: "Custom message"
		}
	} );

	v.form();

	var label1 = $( "#form29field1" ).next( ".error:not(input)" ).text();
	assert.equal( label1, "Custom message" );

	var label2 = $( "#form29field2" ).next( ".error:not(input)" ).text();
	assert.equal( label2, "Custom message" );

	var label0 = $( "#form29field0" ).next( ".error:not(input)" ).text();
	assert.equal( label0, "Custom message" );

} );

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      TMZ Celebrity News – Breaking Stories, Videos & Gossip

      Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

      Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

      🎥 Watch TMZ Live

      TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.