0

How avoid jquery validation css to field too. I have a dropdown. If validation fails. dropdown text color also changing to red. How to avoid that? and also validation error message show under field. suppose to next to field.

enter image description here

class
    .validationError {
        color: #ff0000;
    }


<select class="form-control validationError" data-val="true" data-val-required="The CommunicationLocation field is required." id="CommunicationLocation" name="CommunicationLocation" aria-required="true" aria-invalid="true">
 <option value="">Select</option>
 <option value="HOME">Personal</option>
 <option value="WORK">Work/University</option>
</select>
<label id="CommunicationLocation-error" class="validationError" for="CommunicationLocation">Required</label>

script

 $.validator.addMethod("selectNone",
        function (value, element) {
            alert(value);
            if (value == 'Select')
                return false;
            else
                return true;
    });
$("#frmEmail").validate({
    rules: {
        CommunicationLocation: {
            required: true
        }

    },
    messages: {
        CommunicationLocation: {
            required: "Required"
        }
    },
   errorClass:"validationError",
4
  • 1
    Change the CSS rule from .validationError to label.validationError Commented Mar 23, 2015 at 19:38
  • you need to remove error class .removeClass('error'); Commented Mar 23, 2015 at 19:43
  • @gon250, why would he want to do that? Commented Mar 23, 2015 at 22:42
  • @Sparky Well.. you are right, my mistake. Commented Mar 23, 2015 at 22:46

1 Answer 1

2
  1. According to your HTML, the <label> element will be on the right side of the <select> element. There is nothing in the JavaScript or HTML shown to us that would make it appear underneath. HOWEVER, if your parent container is not wide enough, then this content will automatically wrap to the next line. You'll need to inspect your DOM and fix the width of the relevant parent container to allow the <select> and <label> to appear side by side. Otherwise, you'll have to show us the actual code that causes this problem.

  2. If you don't want the input element to use the same CSS properties as the error message container, then your CSS target needs to be more specific. Use label.validationError to target only the label containing the error message.

    label.validationError {
        color: #ff0000;
    }
    

DEMO: http://jsfiddle.net/afeag0sy/

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

Comments

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.