3

I am using jQuery validation plugin to check my inputs, and having troubles of adjusting the location of the error messages. Here is a demo of my code.

I would like to show the error message under the problematic cell, not next to it. I checked the reference and found I should focus on the errorPlacement function. So if I would like to display the error message under the cell, should I create an new <tr> and then insert the error message? Thanks for any suggestions.

$("#myform").validate({
  errorPlacement: function(error, element) {
     error.appendTo( element.parent("td").next("td") );
   },
   debug:true
 })
2
  • do you have to use a <table> for your <form>? Commented Mar 8, 2013 at 0:23
  • I can change <table> to <div>, but that requires to change a lot more stuffs... Commented Mar 8, 2013 at 0:42

2 Answers 2

4

The only reason the error message is "next" to the element is because both are inline elements (<select> and <label>). You can move the error message underneath by simply:

1) Targeting the error label with some CSS that gives it a display: block;

or

2) Wrapping the error label in some block element before inserting it (you can use the code you posted above and just wrap the error in a div before appending it)

Sign up to request clarification or add additional context in comments.

2 Comments

I tried the first approach. It works, but not lined up properly... Is there a way to make sure the size of error message has the same size of examined cell? Thanks!
I mean the error messages are floating around and change the layout of current format of input cells. But it seems like using white-space: nowrap solves the problems a little bit.
1

Just add the display:block to your CSS. Works for me.

.error{
    border-color: #F78181;
    color:red;
    display:block;
}

To make the label the same width as the select, you can style it with CSS next sibling's nomeclature + like this:

select + label{
    width:300px;
    background: blue;
}

1 Comment

If the error message is too long, then it could not automatically wrap. I think using white-space: nowrap could solve this issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.