4

I am using jquery validate plugin to validate number in a form , how can i place the returned error message under the input button , i am unable to achieve,

Here is my image with the issue and what i need,pls check - https://i.sstatic.net/LsRko.png

Please suggest me how to put the error message in a custom Div under the button instead of default underneath.

Here is my code:

<section id="contact" class="content">
            <div class="container">
                <h2>Contact Form Demo</h2>
                <div class="row">
                    <div class="span12 contact-form">

                        <form action="mail.php" method="POST" class="form-horizontal contact-form" id="contact-form">
                          <!--<?php $formKey->outputKey(); ?> -->
                          <fieldset>  

                              <div class="control-group">
                                <label class="control-label" for="inputPhone">Phone</label>
                                <div class="controls">
                                  <input class="input-80" name="phone" type="text" id="inputPhone" placeholder="inc. country &amp; area code">
                                </div>
                              </div>


                              <div class="control-group">
                                <div class="controls">
                                  <button type="submit" value="Send" class="btn" id="btn-place">Send!</button>
                                </div>
                                <div >place here</div>
                              </div>
                            </fieldset>
                        </form>
                    </div>
                </div>
            </div>
        </section>

        <!-- javascript -->
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script src="js/jquery.validate.min.js"></script>



        <script>

            // contact form validation
            $(document).ready(function(){

             $('#contact-form').validate(
                 {
                  rules: {
                    name: {
                      minlength: 3,
                      required: true
                    },
                    email: {
                      required: true,
                      email: true
                    },
                    phone: {
                      minlength: 11,
                      required: false,
                      number: true
                    },
                    subject: {
                      minlength: 3,
                      required: true
                    },
                    message: {
                      minlength: 20,
                      required: true
                    }
                  },
                            highlight: function(label) {
                    $(label).closest('.control-group').addClass('error');
                  },
                  success: function(label) {
                    label
                      .text('OK!').addClass('valid')
                      .closest('.control-group').addClass('success');
                  }
                 });

                // contact form submission, clear fields, return message,script no shown here

        </script>
1
  • 2
    Try to limit your code to the relevant bits (in this case you probably only need to show us the rules for phone) Commented Jun 25, 2013 at 20:41

2 Answers 2

20

All you need to do is specify the errorLabelContainer in your options and specify the div you want the errors to go into:

$('#contact-form').validate({
     //all your other options here
        errorLabelContainer: '#errors'
});

Then just make sure you specify that your place here div has the id errors like this:

<div id="errors"></div>

See a working example here: http://jsfiddle.net/ryleyb/aaEFQ/ (note that I also specified the wrapper option, which you'll probably want if there are multiple errors).

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

3 Comments

I Tackled the issue in a Different manner using: errorPlacement
Can you suggest me how to ajax POST LATER only after VALIDATION ?
use the submitHandler option of Validate - it is only triggered when the form is valid.
-1
errorPlacement: function(error, element) {
  if((element.attr('name') === 'color[]')){
    error.appendTo("div#errors");  
  }
  else if(element.attr('name') === 'font[]' ){
    error.appendTo("#font-error");  
  }
  else{
    element.before(error);
  }
}

3 Comments

color[]' or font[] is the name of checkboxes div#errors or #font-error is div where u want to show your custom error message
You should edit that information into the post, not leave it as a comment.
Welcome to Stack Overflow! While this answer is probably correct and useful, it is preferred if you include some explanation along with it to explain how it helps to solve the problem. This becomes especially useful in the future, if there is a change (possibly unrelated) that causes it to stop working and readers need to understand how it once worked.

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.