0

I have the following script which validates pages using jquery.validate.js through a quiz and works perfectly. Im looking to have the error message for required fields to be placed into a div called errormessage. jQuery validate usually returns a message "this field is required".

Here is the script

<script type="text/javascript">
var curPage=1;
function NextPage() {
    if (curPage < <?= $num; ?>) {
        var group = "#page_" + curPage.toString();
        var isValid =true;
         $(group).find(':input').each(function (i, item) {
              if (!$(item).valid())
                isValid = false;
            });

        if( !isValid){ alert("PLEASE MAKE A SELECTION"); }
        else{
            $("#page_" + curPage.toString()).hide(); //hide current page div
            curPage++;  //increment curpage
            $("#page_" + curPage.toString()).show(); //show new current page div
        }
    }
}
 function PrevPage() {

     if (curPage > 1) {
         $("#page_" + curPage.toString()).hide(); //show new current page div
         curPage--;  //increment curpage
         $("#page_" + curPage.toString()).show(); //hide current page div
     }
 }

 function InitializePage() {
     $(".wizardPage").hide();
     $("#page_" + curPage.toString()).show();


     $("#surveyForm").validate({onsubmit: false});

 }




 $(document).ready(InitializePage
    );
</script>
3
  • 1
    This code and your question don't seem to relate? Commented Jun 22, 2012 at 16:46
  • any chance of seeing your html? Commented Jun 22, 2012 at 17:03
  • By default the jQuery Validate plugin appends the error messages to each validated form element inside a <label> tag that has class="error". Do you want to disable that functionality and list all error messages inside another div element? Commented Jun 22, 2012 at 17:06

2 Answers 2

1

Try setting up the error container element like explained here:

http://mitchlabrador.com/2010/11/05/how-to-change-the-way-jquery-validate-handles-error-messages/

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

Comments

0

Switch this line - $("#surveyForm").validate({onsubmit: false}); out for the following -

$("#surveyForm").validate({
    rules: {
    xyz: {
        required: true
    }
},
messages: {
    xyz: {
    required: $('div#errormessage').append("xyz is a required field!");
    }
}
}, onsubmit: false)

It will check if xyz is filled or not. If not, it'll put xyz is a required field! in a div with id errormessage.

For more details about rules, check out this page. Scroll down until you get to 'rules'.

The above code, by putting in rules for each field, will allow you to specify different locations for each error message. The blog post in spider's answer contains a simpler way of doing things, and is therfore preferable if you don't require this customisability.

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.