Vimalan Jaya Ganesh Vimalan Jaya Ganesh - 12 days ago 5
jQuery Question

jquery validation: display error label inside a custom div

I am trying to display the error label inside a div but, it is not working.

<div class="panel-body">
<div class="well-custom datecontrol-fixed datecontrol-fixed-xs">
<div class="input-group">
<input type="text" class="form-control input-sm" id="TextBoxProdMoveDate" name="TextBoxProdMoveDate"/>
<label class="input-group-addon btn" for="TextBoxProdMoveDate">
<span class="glyphicon glyphicon-calendar"></span>
</label>
</div>
<div id="DivProdMoveDateErrorContainer"></div>
</div>
</div>


And, setting the rule like this,

required: true,
messages: {required: "Production move date is required."},
errorLabelContainer: '#DivProdMoveDateErrorContainer',


but, it is not working.

All I am expecting is, to display the error label inside the DivProdMoveDateErrorContainer.

Any suggestion will be greatly appreciated.

Answer

Jquery validation generates a label for each field whose data is improper like:

<label id="reg_name-error" class="error" for="reg_name" style="display: inline-block;">Please enter name</label>

just copy the entire label from DOM and paste it inside the div or where you want the error msg by removing the error msg text from it like:

<label id="reg_name-error" class="error" for="reg_name" style="display: inline-block;"></label>

And refresh the page and try again.

Explanation: If the error lable is already present in the page then validation jquery will not produce the label, instead it will use the already existing label.

Or you can simply place in your custom place like:

$("#fm_regis").validate({
  rules :{
    name: {
      required: true,
    },
  },   
  errorLabelContainer: "#id-of-your-specific-place",
  // More of your code
})
Comments