开发者

jquery validation error message - change the location of message

开发者 https://www.devze.com 2023-02-24 16:56 出处:网络
I am using jquery validation plu开发者_如何学Cgin. I have an html that looks like: <h2>Select A City</h2>

I am using jquery validation plu开发者_如何学Cgin. I have an html that looks like:

<h2>Select A City</h2>

<select class="required city-selection-list" id="city_city_id" name="city[city_id]">
  <option value=""></option>

  <option value="146">San Francisco Bay Area</option>
  <option value="147">San Francisco</option>
  <option value="311">Los Angeles</option>
  <option value="344">New York</option>
  <option value="395">San Diego</option>
 </select>

The error message is currently displaying after the select box. This causes my page to shift left and messes up how it looks.

How do I display it after Select A City instead of inlining it.

Thanks for your help.


You can change you selectors and placement, but this should give you an idea on how it works.

jQuery.validator.setDefaults({
    errorPlacement: function(error, element) {
        if (element.hasClass("city-selection-list")) {
           error.insertBefore(element);
        }
    }
});


You can also add a container div below h2

<div id='errorContainer'></div>

add errorPlacement: line with your validate code and it will display all errors inside errorCotainer div

$( "#frm_register" ).validate(
{

//  errorElement: "span",// by default em is used to display each error message

    errorPlacement: function(error, element) {
        error.appendTo( $('#errorContainer'));
    }

});
0

精彩评论

暂无评论...
验证码 换一张
取 消