views:

18801

answers:

8

I'm trying to over ride the default error message label with a div instead of a label. I have looked at this post as well and get how to do it but my limitations with CSS are haunting me. How can I display this like some of these examples:

Example #1 (Dojo) - Must type invalid input to see error display
Example #2

Here is some example code that overrides the error label to a div element

$(document).ready(function(){
            $("#myForm").validate({
                rules: {
                    "elem.1": {
                        required: true,
                        digits: true
                    },
                    "elem.2": {
                        required: true
                    }
                },
                errorElement: "div"
            });                  
        });

Now I'm at a loss on the css part but here it is:

div.error {
        position:absolute;
        margin-top:-21px;
     margin-left:150px;
     border:2px solid #C0C097;
        background-color:#fff;
        color:white;
        padding:3px;
        text-align:left;
        z-index:1;
        color:#333333;
     font:100% arial,helvetica,clean,sans-serif;
     font-size:15px;
     font-weight:bold;  
    }

UPDATE:

Okay I'm using this code now but the image and the placement on the popup is larger than the border, can this be adjusted to be dynamic is height?

if (element.attr('type') == 'radio' || element.attr('type') == 'checkbox') {
   element = element.parent();

   offset = element.offset();
   error.insertBefore(element)
   error.addClass('message');  // add a class to the wrapper
   error.css('position', 'absolute');
   error.css('left', offset.left + element.outerWidth());
   error.css('top', offset.top - (element.height() / 2)); // Not working for Radio, displays towards the bottom of the element. also need to test with checkbox
} else {
   // Error placement for single elements
   offset = element.offset();
   error.insertBefore(element)
   error.addClass('message');  // add a class to the wrapper
   error.css('position', 'absolute');
   error.css('left', offset.left + element.outerWidth());
   error.css('top', offset.top - (element.height() / 2));
}

the css is the same as below (your css code)

Html

<span>
<input type="radio" class="checkbox" value="P" id="radio_P" name="radio_group_name"/>
<label for="radio_P">P</label>
<input type="radio" class="checkbox" value="S" id="radio_S" name="radio_group_name"/>
<label for="radio_S">S</label>
</span>
A: 

If you could provide some reason as to why you need to replace the label with a div, that would certainly help...

Also, could you paste a sample that'd be helpful ( http://dpaste.com/ or http://pastebin.com/)

weisjohn
the default action is to place the label next to the element with the error, this causes the elements to the right of the error element to shift. wanted the error element to display as a tooltip like popup that hovers/floats over the other elements.
Phill Pafford
Sorry don't think I could post an image of the error div I want but the two examples above both have something similar to what I would like. Thanks --Phill
Phill Pafford
+1  A: 

I use jQuery BeautyTips to achieve the little bubble effect you are talking about. I don't use the Validation plugin so I can't really help much there, but it is very easy to style and show the BeautyTips. You should look into it. It's not as simple as just CSS rules, I'm afraid, as you need to use the canvas element and include an extra javascript file for IE to play nice with it.

Paolo Bergantino
Or, if you don't want to be able to quickly customize the bubble's colors, you can use a background image like the 2nd example.
Paolo Bergantino
I have looking into Beauty Tips but can't get it to work with the validation plugin with the error display. Any other thoughts?
Phill Pafford
+37  A: 
Nadia Alramli
Thanks I like the errorPlacement option but the CSS is more of what I'm looking for. If you look at the two example links I have given I wanted something to look like those. Thanks again.
Phill Pafford
I updated the answer to produce the exact look you want.
Nadia Alramli
also one other thing, when I have a radio or checkbox group the error div displays over some of the options I need to select. is there a way to display after the group?
Phill Pafford
I expanded the answer a little more. If you follow the instructions you will get the result you want.
Nadia Alramli
Updated my code above please review and things are very close to what I would like. Thanks :)
Phill Pafford
added HTML sorry about that :)
Phill Pafford
using the validation plugin with you code I hit submit and the popups come as expected but when I go to eneter in values to make the popups go away my whole page goes blank. any idea as to why?
Phill Pafford
The pop over display works well thanks but when I enter in a value into the text field the whole page goes blank. Any thoughts as to why?
Phill Pafford
Don't know if your still interested in helping but I did find this jQuery plug-in with kinda what I'm looking for in a CSS sense:http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/Wanted to know how to combine the CSS popup to the validation plugin I'm using instead of this one. I like that it floats above the element and looks like a bubble.Thanks again for any help on this
Phill Pafford
@Phill, I'd like to help. But it is better to post this as another question. Mainly because If I edited this post anymore it will end up as a community wiki. And comments are not suitable for answers. Also posting this as another question will get more attention and possible better solutions. As a first look on the link you posted, it seems they are doing a difficult hack to display the arrow. There should be a better way of doing it.
Nadia Alramli
@Nadia - wow, you really posted a great answer here. It helped me immensely with a similar issue I had. I am really impressed by your grasp of CSS and jQuery. Nice contribution!!
David Robbins
+2  A: 

A few things:

First, I don't think you really need a validation error for a radio fieldset because you could just have one of the fields checked by default. Most people would rather correct something then provide something. For instance:

   Age: (*) 12 - 18 | () 19 - 30 | 31 - 50

is more likely to be changed to the right answer as the person DOESN'T want it to go to the default. If they see it blank, they are more likely to think "none of your business" and skip it.

Second, I was able to get the effect I think you are wanting without any positioning properties. You just add padding-right to the form (or the div of the form, whatever) to provide enough room for your error and make sure your error will fit in that area. Then, you have a pre-set up css class called "error" and you set it as having a negative margin-top roughly the height of your input field and a margin-left about the distance from the left to where your padding-right should start. I tried this out, it's not great, but it works with three properties and requires no floats or absolutes:

   <style type="text/css">
    .error {
        width: 13em; /* Ensures that the div won't exceed right padding of form */
        margin-top: -1.5em;  /*Moves the div up to the same level as input */
        margin-left: 11em;   /*Moves div to the right */
        font-size: .9em;     /*Makes sure that the error div is smaller than input */
    }

   <form>
   <label for="name">Name:</label><input id="name" type="textbox" />
   <div class="error"><<< This field is required!</div>
   <label for="numb">Phone:</label><input id="numb" type="textbox" />
   <div class="error"><<< This field is required!</div>
   </form>
Anthony
Thanks for your reply but the client would like to have the radio button blank and force the user to select an option. I will look into your CSS :)
Phill Pafford
I like this except for the radio and checkboxes, could you allow for that as well?
Phill Pafford
this doesn't doesn't display correctly in IE6 (Havent check other versions yet)
Phill Pafford
As far as radio and checkboxes, this depends on if you have a group of boxes (fieldset) or just the one. If it's a group, I suggest setting the width of the fieldset so that it leaves that padding mentioned before, and then having your error flag pointing to the whole thing, maybe even adding a class to the fieldset so that it gets a border when it has the flag, so the user knows you mean that group....
Anthony
But you wouldn't really want a flag for one check box, as that doesn't make sense. What if the answer is No to the box, then it's still "done". If you have a group of boxes and they need to check at least two, then again, have the error for the fieldset, not the box.
Anthony
So something like:<fieldset><legend>Hobbies</legend><label for="fishing">Fishing</label><input type="checkbox" id="fishing" value="fishing" /><label for="poking">Poking</label><input type="checkbox" id="poking" value="poking" /></fieldset>Then, have the error point to the whole group, not one box.
Anthony
As for IE6, how is off? I don't have IE6 so I can only imagine. My numbers were a bit shabby because I didn't set specific heights and widths to the inputs, fieldsets, etc. So being more precise might fix it, depending on the issue.Then again, my company (a university) as official stated that it will no longer make accommodations for IE6 (which is to say, backwards compatibility won't be taken into account for IE6). I think this is going to be the position taken almost universally over the next year or so.
Anthony
I'm using the name attribute to group the radios/checkboxes together would this work?
Phill Pafford
You use name to group them together as data for the script that handles the data (and for JS), but you should use the fieldset element to group inputs, like a wrapper. Try it out, most browsers automatically put a border around fieldsets, which is nice.
Anthony
A: 

Unfortunately I can't comment with my newbie reputation, but I have a solution for the issue of the screen going blank, or at least this is what worked for me. Instead of setting the wrapper class inside of the errorPlacement function, set it immediately when you're setting the wrapper type.

$('#myForm').validate({
    errorElement: "div",
    wrapper: "div class=\"message\"",
    errorPlacement: function(error, element) {
        offset = element.offset();
        error.insertBefore(element);
        //error.addClass('message');  // add a class to the wrapper
        error.css('position', 'absolute');
        error.css('left', offset.left + element.outerWidth() + 5);
        error.css('top', offset.top - 3);
    }

});

I'm assuming doing it this way allows the validator to know which div elements to remove, instead of all of them. Worked for me but I'm not entirely sure why, so if someone could elaborate that might help others out a ton.

Tommy
Thanks for your post, will look it over
Phill Pafford
+1  A: 

That answer really helped me, in my case i had to filter some elements out and have special aligment on their error div,

errorPlacement:function(error,element) {
    if (element.attr("id") == "special_element") {
        // special align
    } else { // default error scheme
        error.insertAfter(element);
    }
}
Rodrigo
Thanks for the tip/code
Phill Pafford
A: 

Going back to the errorplacement solution by Nadia

I have tried it and it works like a charm in Safari and Firefox but causes IE08 to bypass the jqueryvalidator and go straight to the server side validator.

My code is this - as soon as I insert 'error element.... it is unstable in IE08. All help much appreciated

<script type="text/javascript"> 
    $(document).ready(function() { 
          $("#sampleform").validate({ 
            rules: { 
                    dinername: "required",
                    venue: "required",
                    contact: "required",
                    dinertelephone: "required",
                    venuetime: "required",
                    numberdiners: "required", 
                    dineremail: { required: true, email: true}, 
                    datepicker: { required: true,date: true}
                  }, 

          messages: { 
                    dinername: "Your name?",
                    numberdiners: "How many guests?",
                    dinertelephone: "Your mobile?",
                    venue: "Which restaurant?",
                    venuetime: "Your arrival time?",
                    datepicker: "Your booking date?",
                    dineremail: "Please enter a valid email address",
                    }, 

                    errorElement: "div",
                    wrapper: "div",  // a wrapper around the error message
                    errorPlacement: function(error, element) {
                            element.before(error);
                            offset = element.offset();
                            error.css('right', offset.right);
                            error.css('right', offset.right - element.outerHeight());
                                                             }


                });
    });

      </script> 

 <script type="text/javascript">
    $(function() {
        $("#datepicker") .datepicker({minDate: 0, maxDate: '+6M +0D',dateFormat: 'DD, d M yy',onClose: function() {$(this).valid();}

        });

    });
    </script>

The relevant webpage is http://www.johnslaytor.com.au/nilgiris/forms/bookingform/bookingform.html

John Slaytor
Please edit your post and use proper formatting on the code or people will start downvoting you because that is impossible to follow.
FreekOne
Also, a friendly advice: you would have more chances of getting an answer if you'd post your own question and link back to this solution. "Post your answer" is meant for answers, not related questions.
FreekOne
I agree with @freekOne this should be it's own question and link back to this question. Also if this helped out in anyway you could be noce and upvote :) Post a new question and we can go from there @John Slaytor
Phill Pafford
apologies for poor protocol - a day of desperate searching is no excuse - have posted anew
John Slaytor
A: 

So how do i place error messages from two fields, say for example Password and confirm password in a single div?

Baloch
This should be a comment and not an answer, but I believe your looking for 'elem.password' maybe
Phill Pafford