Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery validation error message - change the location of message

I am using jquery validation plugin. 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.

like image 322
truthSeekr Avatar asked Nov 30 '25 14:11

truthSeekr


1 Answers

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);
        }
    }
});
like image 130
Dustin Laine Avatar answered Dec 03 '25 03:12

Dustin Laine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!