4

i want to validate my form with jquery validate but i want to put my error label to a specific place. an example below show error label put in the <span class="error_label"></span>. my question is how can i make errorplacement with a specific place, with an example below, please help me..thanks?

<script>
$(document).ready(function (){
$("#fm_regis").validate({
    rules :{
        name: {
            required: true,
        },
    },
    errorPlacement: function(error, element) {      
        //$(element).next('span .error_label :first').html(error);
        //$('.error_label').html(error);
        //$('.error_label').html(error);
        $(element).closest('.error_label').html(error);
    },
    messages: {
        name: {
            required: "Please input a username",
        },
    }
});
});
</script>

    <form name="fm_regis" id="fm_regis">
    <table>   
      <tr>
         <td width="100">Nama</td>
         <td>:</td>
         <td><input type="text" name="name" id="name" size="30"/></td>
      </tr> 
      <tr>
         <td width="100"></td>
         <td></td>
         <td><span class="error_label"></span></td>
      </tr>
   </table>
     <input type="submit" value="Register"/>
   </form>

3 Answers 3

4

In your specific situation you can do something like this:

$(element).closest('tr').next().find('.error_label').html(error);

http://jsfiddle.net/kqTQu/

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

Comments

4

Try this:

$("#fm_regis").validate({
  rules :{
    name: {
      required: true,
    },
  },   
  errorLabelContainer: "#id-of-your-specific-place",
  // More of your code
})

And get rid of the errorPlacement: function.

2 Comments

This is very useful! errorPlacement: doesn't allow to restore you error container (to empty) once validation is success. errorLabelContainer: works great.
cool! So tired of my multiple if statements in bloated errorPlacement, which adjust error positions when the input is radio group, select list, input in table cell...
0

Not sure if I have understood the question correctly, try to change this line

$(element).closest('.error_label').html(error);

to

$(element).append( "<span class="error_label">" + error + "</span>") ;

make sure that you remove all error labels before initiating validation

2 Comments

sorry if i make u confused i just want put a error label to bottom of element
In that case simply use div instead of span .... $(element).append( "<div class="error_label">" + error + "</div>") ;

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.