18

I have an input that I want to display a custom error message on when the user doesn't fill it out.

    <input type="text" class="foo" value="" data-prop="foo" data-rules="{ required: true }"></input>

How do I add a custom error message to this specific input?

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
Jesse Atkinson
  • 10,586
  • 13
  • 42
  • 45
  • Would something similar to what soupenvy suggests here http://stackoverflow.com/questions/6777634/jquery-validation-plugin-custom-message?rq=1 solve your issue? – Benjamin Gruenbaum May 22 '13 at 00:01
  • @BenjaminGruenbaum Most-likely no. I'd prefer to avoid jQuery to override the message on just one. Isn't there a way to set this with an attribute? – Jesse Atkinson May 22 '13 at 00:02

2 Answers2

40

Use:

data-msg-required="Please enter something here!"

Here's a demo HTML code: https://github.com/jzaefferer/jquery-validation/blob/master/demo/custom-messages-data-demo.html

JS Fiddle demo: http://jsfiddle.net/leniel/VuPPy/48/

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
0

If you use jquery-validate.js library (https://jqueryvalidation.org/), you can also use js to do this job.

See my html part:

<input type="text" id="TBBusquedaDescripcionCategoriaEd" name="TBBusquedaDescripcionCategoriaEd" maxlength="300" class="form-control" required>

Then in js:

var validator = $("#formName").validate({
        rules: {
            TBBusquedaDescripcionCategoriaEd: {
                required: true,
                minlength: 4
            }
        },
        messages: {
            TBBusquedaDescripcionCategoriaEd: {
                required: "Mandatory field custom msg",
                minlength: "Minleght custom msg"
            }
        },
        submitHandler: function (form) {...});
Barry
  • 29
  • 4