1

Please someone help me, I really stuck on this for the whole day. I have a php form (multiple page) with different types of input(radio, checkbox, etc). what I need is :

  1. If user didn't fill required fields and pressed the continue button, he should receive an error message BESIDE or ABOVE that field.

  2. If he forgot to fill some fields, other input fields should not be cleared (since I don't want him/her to get bored of re-answering all those questions again)

This is what I have tried so far with JQuery plugin: (sorry, I could not paste all the code since it was too long)

<form name="MovieSurvey" id="formId" action="page2.php"  method="post" />  
<fieldset id = "q1"> <legend class="Q1"></legend>
<label> What is your gender?<span>*</span></label>
<div class="fieldset content">
<div class="error_message_holder"></div> 

 <Input type = 'radio' Name ='q1' value = 'male'>Male
 <Input type = 'radio' Name ='q1' value = 'female'>Female
</div>
</fieldset>
....
....
<input type="submit"  name= "continue1" value="Continue" />
</form>

<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/jquery.validate.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/additional-methods.min.js"></script>

<script>
$(document).ready(function() { 
 $('#formId').validate({ // initialize the plugin
    rules: {
        q1: {
            required: true,
        },
        q2: {
            required: true,
        },
        q5: {
            required: true,
        },
        q6: {
            required: true,
        }
    },

    errorPlacement: function(error, element) {
       error.html('Please fill this field');

  if(element.closest('.fieldset content').find('label.error').length==0){
         error.insertBefore(element.closest('.fieldset content').find('.error_message_holder'));

     }
    }      

 }); 
});
</script>
</div>
</fieldset>

</body>
</html>

It works almost well, the only problem is that the error message is shown between radio-button and label! (This is when I don't write the errorPlacement: function part), in order to fix it I added the placement part but now it shows nothing!

Could someone help me fix it?

Thanks,

Sparky
  • 98,165
  • 25
  • 199
  • 285
mOna
  • 2,341
  • 9
  • 36
  • 60
  • Why not use HTML5's required attribute? – Edward Sep 24 '14 at 18:04
  • You should absolutely NOT be doing this: `error.html('Please fill this field');` The plugin has other methods for changing the wording of the error message. I suggest that you read through the documentation. – Sparky Sep 25 '14 at 02:19

2 Answers2

4

Quote OP:

"If user didn't fill required fields and pressed the continue button, he should receive an error message BESIDE or ABOVE that field."

You're making this way more complicated than it needs to be.

  • Simply use insertBefore, instead of the default insertAfter, in the errorPlacement callback.

    errorPlacement: function (error, element) {
        // error.insertAfter(element);  // default function
        error.insertBefore(element);
    },
    
  • Then use the errorElement option to change the label into a div, which forces the message onto its own line. This results in a message above your radio button group, just as you requested.

    errorElement: 'div'  // default is 'label'
    

Quote OP:

"If he forgot to fill some fields, other input fields should not be cleared (since I don't want him/her to get bored of re-answering all those questions again)"

That's already the default behavior. This plugin will not, and cannot, change any values typed into the fields. It only blocks submission and shows/hides error messages.

DEMO: http://jsfiddle.net/9bkjsg8o/

Documentation (all options): http://jqueryvalidation.org/validate


NOTES:

Your form element is prematurely closed with a /> in the opening tag...

<form name="MovieSurvey" id="formId" action="page2.php"  method="post" />

The form is a container element and therefore already has a closing tag called </form>.

Your opening tag should look like this...

<form name="MovieSurvey" id="formId" action="page2.php"  method="post">
Sparky
  • 98,165
  • 25
  • 199
  • 285
  • thank you :) Actually I just did it and it is ok :) even without adding errorElement: div , it worked the same :p – mOna Sep 25 '14 at 09:29
  • Sorry, I just faced a new problem in my second page! :( In the second page I used auto-complete (by javascript and ajax) for one of my questions! after I added jquery validation codes, the auto-completeion does not work any more :( do u have any idea? – mOna Sep 25 '14 at 09:53
  • I posted my problem as a new post. could you please kindly see it? http://stackoverflow.com/questions/26036771/auto-complete-not-work-wth-jquery-validation-plugin-at-the-same-time – mOna Sep 25 '14 at 11:09
0

Well yeah, that's because you insert it before the error-message-holder.

I think it should be:

error.appendTo(element.closest('.fieldset content').find('.error_message_holder'));

BTW: This question shouldn't be tagged as "PHP". PHP has nothing to do with it.

vrijdenker
  • 1,371
  • 1
  • 12
  • 25
  • Thanks for your answer but nothing has changed :(, actually when I insert the placement part (last 4 line of the code above), it even does not show the messege, but without placement part, I receive it in the wrong place! – mOna Sep 24 '14 at 18:16
  • Ok it is little bit hard to find the bug this way. Can you get an example to "work" like it does on http://jsfiddle.net/ ? That will help us help you debug your problem. – vrijdenker Sep 24 '14 at 18:20
  • sorry, I am very newbie, I don't know how does it work:( Is there any other information I can share with you that may help? :( – mOna Sep 24 '14 at 18:24
  • I did it for you: just paste your HTML in the HTML field (I completed it by html and body openingtags). Now we have a working example we can play with: http://jsfiddle.net/dc6qbru3/ – vrijdenker Sep 24 '14 at 18:27
  • Indeed when removing the placement-part I can see the error-message. I'll look into it... – vrijdenker Sep 24 '14 at 18:28
  • Ok, I could fix it by this : errorPlacement: function(error, element) { if (element.attr("type") == "radio") { error.insertBefore(element); } else { error.insertAfter(element); } } – mOna Sep 24 '14 at 18:31
  • Wow! Thanks, I just saw your comment, I will check it now:) – mOna Sep 24 '14 at 18:32
  • I looked into the documentation and there actually is an example with radiobuttons like this. You could try that solution as well: http://jqueryvalidation.org/category/methods/#example:-makes-the-gender-radio-buttons-required – vrijdenker Sep 24 '14 at 18:35