3

I'm using jQuery validate and I'm having problems with changing the color of the error messages and the input field name.. I tried using only CSS, here is the code:

#news_action input.error {
  color: red;
}

but this changes only the color of the input field text.. How can I select the input field name and the error messages with CSS? If anyone have another solution without using CSS, it's welcome at all. Can anyone help me? Thanks in advance

Here is my html:

<div class="info">
  Type your name:<input id="input-name" type="text" name="name"/> <br>
  Type your email:<input type="text" name="email"/>
</div>
7
  • 1
    We might need some more code for context. But maybe you can just add the same class to both elements, and change the color of that class? Commented Oct 20, 2014 at 13:03
  • 2
    What's the relevant HTML? Commented Oct 20, 2014 at 13:03
  • Write a Class for Error message contained span Commented Oct 20, 2014 at 13:04
  • jsfiddle.net/arunpjohny/ptumjuc8/1 Commented Oct 20, 2014 at 13:10
  • this fiddle almost solve my problem.. it changes the color of the input field names, but the error messages still at the same color. Commented Oct 20, 2014 at 13:21

5 Answers 5

8

You need to wrap each input control and its label in a container, then you can use the highlight and unhighlight methods to add the error class to the container

jQuery(function ($) {
    var validator = $('#news_action').validate({
        rules: {
            name: {
                required: true
            },
            email: {
                required: true,
                email: true
            }
        },
        messages: {},
        highlight: function (element) {
            $(element).parent().addClass('error')
        },
        unhighlight: function (element) {
            $(element).parent().removeClass('error')
        }
    });
});
#news_action .error {
    color: red;
}
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.12.0/jquery.validate.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.12.0/additional-methods.js"></script>
<form id="news_action" method="post" action="">
    <div class="info">
        <div>Type your name:<input id="input-name" type="text" name="name"/></div>
        <div>Type your email:<input type="text" name="email"/></div>
    </div>
    <input type="submit" class="button" value="Submit" />
</form>

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

1 Comment

That's it !! Thank you guys! PS: I noticed when the error message appears, the validate add a label with an id based on the input field name, I just made a class for that new id. And about the input field name, the highlight and unhighlight methods works fine for me.
2

I'm using javascript to do the same (validate a form with upladed files), i hope it will be convient to what you want to do

 $(document).ready(function() {

$(".verif_form_ajax").submit(function(){   
        var $inscription_form = $('#inscription_form');

        var process;

        process=1;

        $(".obg").each(function(){
            var length = $(this).val();
                 if(length==""){
                     $(this).css('border-color','#c89494'); 
                      $(this).css('background-color','#F9D3D3');    
                     process=0;                      
                 }else{
                    $(this).css('border-color','#9ac894');
                     $(this).css('background-color','#dbfcd7'); 
                 }  
        });

        $(".obg").blur(function(){
            var length = $(this).val();
                 if(length==""){
                     $(this).css('border-color','#c89494'); 
                      $(this).css('background-color','#F9D3D3');    
                     process=0;                      
                 }else{
                    $(this).css('border-color','#9ac894');
                     $(this).css('background-color','#dbfcd7'); 
                 }  
        });

        if(process=="0"){   
          return false;         
        }
        else{
        file = $('#userfile').val();
        contentType:attr( "enctype", "multipart/form-data" ),
        $.post($(this).attr("action"),  {file:file}, $(this).serialize(), function(data){
            // do wathever you want to do when form is posted
            $inscription_form.html(data);
        });
        return false;

        }
});



});

then in the form whetever you want a validate filed you put it a class ="obg" like this

 <label>Namen :</label>
 <input class="obg" type="text" placeholder="Your name" name="denomination" id="denomination" > 

PS : please excuse my poor English!!

2 Comments

It's a little different of what I want, but thank you for your attention. I will try it! ;]
'$(this).css('background-color','#F9D3D3');' has helped me. Thanks @Marline.
2

Well its pretty easy now. If you inspect the page, you will find they add labels for error and those labels have a class named error .

Thus you may add the CSS:

.error { color:red !important; }

Comments

0

You can use the required attribute from html5:

<!-- add 'required' -->
Type your name: <input id="input-name" type="text" name="name" required/> <br>

<!--change type to 'email' and add 'required' -->
Type your email: <input type="email" name="email" required/> 

or handle your form submit: http://jsfiddle.net/vcarvalho/77o94fzw/

1 Comment

Question is asked about jQuery validate. And plus, required tag is not supported in Safari browser.
0

I used below style in my css after finding that JQuery validation generates em elements for error messages

<style type="text/css">
    em {
        color: Red;           
    }
</style>

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.