0

I am using jquery for checkbox validation(atleast one should be checked), here i need to display error message at the end of all checkboxes, but it always display after the label element and moves all checkboxes at the right side, which looks very weird. Below is my html code:

           <div>
            <label>Please Select a color:</label> 
                <input type="checkbox" id="color"  name="color" value="red"> red</input>
                <input type="checkbox" id="color"  name="color" value="green"> green</input>
                <input type="checkbox" id="color" name="color" value="yellow"> yellow</input>
                <input type="checkbox" id="color"  name="color" value="black"> black</input>
           </div>

Can anyone please tell me how to display error message at the right side after the end of all checkboxes without moving form structure.

Thanks in advance..

~Yash

priyank
  • 857
  • 2
  • 18
  • 35
  • 1
    show the js validation and css – man_luck Jun 30 '15 at 09:45
  • Hi , here is js validation http://pastebin.com/4T9UB6sU for css i have not done any modification , thanks for response – priyank Jun 30 '15 at 09:50
  • wrap your checkboxes inside div and provide 'clear: both' property to that div. – Pratik Galoria Jun 30 '15 at 10:07
  • Hi , thanks , actually I am getting error message after label wen using this js http://pastebin.com/xijgVZ0E without any errorplacement attribute which is what id ont want. If i use errorplacement (as here http://pastebin.com/4T9UB6sU )it doesnt show any message at all :( – priyank Jun 30 '15 at 10:16
  • any help on this would be appreciated , waiting for the response – priyank Jun 30 '15 at 10:23

1 Answers1

9

try this:

error.appendTo("div");

instead of this:

 error.appendTo( element.parent("div").next("div"));

Edit

<form id="myForm" method="post" name="myForm" action="">
        <div>
        <label>Please Select a color:</label> 
        <input type="checkbox" id="color"  name="color" value="red"> red</input>
        <input type="checkbox" id="color1"  name="color" value="green"> green</input>
        <input type="checkbox" id="color2" name="color" value="yellow"> yellow</input>
        <input type="checkbox" id="color3"  name="color" value="black"> black</input>
        <span id="errorToShow"></span> // Note this
        </div>
        <button type="submit">save</button>
    </form>

script:

$("#myForm").validate({
            rules: {
                color: "required"
            },
            messages: {
                color: "select atleast one color"
            },
            errorPlacement: function(error, element) {
                if (element.attr("name") == "color") {
                    error.appendTo("#errorToShow");
                } else {
                    error.insertAfter(element);
                }
            }
        });
chirag
  • 474
  • 4
  • 13
  • Hi, thats not working , I think is appending error after each div tag , my entire html is full of error messages when I use this "error.appendTo("div");" any other suggestions pls?? as i am very new with jquery , not able to find the solution. – priyank Jun 30 '15 at 10:48
  • Hi @chirag , thanks for the detailed solution but that also seems not working, may be something wrong with my code not sure what because I am exactly doing the same , let me try again – priyank Jun 30 '15 at 11:26
  • still error message is displaying before all checkboxes and moves checkboxes towards right :| – priyank Jun 30 '15 at 11:32
  • Are getting any kind of error? please provide your html & script that you are trying. so that i can help you further. – chirag Jun 30 '15 at 11:33
  • HTML and js are same as provided by u in previous answer , I would have send you the screenshot of my deployment but it not allowing me – priyank Jun 30 '15 at 11:45
  • Hi @chirag , have posted an image , pls have a look – priyank Jun 30 '15 at 11:53
  • Are you applying any kind of css on validation error text? – chirag Jun 30 '15 at 12:06
  • No, I am not, its taking default validation css. – priyank Jun 30 '15 at 12:18
  • i think there is the problem in default validation css. do you have online link for that validation css? – chirag Jun 30 '15 at 12:24
  • hey thats working now after modifying span tag with thanks you sooo much for helping continuously :) – priyank Jun 30 '15 at 12:37
  • thats good. congo. welcome – chirag Jun 30 '15 at 12:45