-1

There is a span generated by jQuery Validatation.

<span for="field1" class="field-error"> There is an error in the field 1 </span>
<span for="field2" class="field-error"> There is an error in the field 2 </span>
<span for="field3" class="field-error"> There is an error in the field 3 </span>

How can I override the field 2 error message only? Thanks.

Sparky
  • 98,165
  • 25
  • 199
  • 285
user2307087
  • 423
  • 11
  • 25
  • Show the rest of the code... where is your call to `.validate()`? How are you setting these messages now? What about the HTML of your form? Are you sure you're using the jQuery Validate plugin, because by default, the messages are much more generic and they're contained within `label` elements with a class of `error`. – Sparky Dec 09 '14 at 00:50

2 Answers2

0

There are quite a few ways to do this but since we cannot see how you're presently setting any of these messages, I will give you the most generic version...

$(document).ready(function() {

    $('#yourform').validate({
        errorElement: "span",
        errorClass: "field-error",
        rules: {
            field1: {
                required: true
            },
            field2: {
                required: true
            },
            field3: {
                required: true
            }
        },
        messages: {
            field1: {
                required: "There is an error in the field 1"
            },
            field2: {
                required: "Some other error message"
            },
            field3: {
                required: "There is an error in the field 3"
            }
        }
    });

});

DEMO: http://jsfiddle.net/mb2jfeLs/

Sparky
  • 98,165
  • 25
  • 199
  • 285
-1

You can target that attribute in JQuery like this.

$('span[for="field2"]').text('This is the new error message for field 2');

JSFiddle

EternalHour
  • 8,308
  • 6
  • 38
  • 57
  • He's using the jQuery Validate plugin so that's not really how you'd customize a validation message. – Sparky Dec 09 '14 at 00:37