Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery validate errorplacement with a specific place

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>
like image 490
casper Avatar asked Feb 17 '23 18:02

casper


2 Answers

In your specific situation you can do something like this:

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

http://jsfiddle.net/kqTQu/

like image 103
dfsq Avatar answered Mar 24 '23 18:03

dfsq


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.

like image 29
tom-19 Avatar answered Mar 24 '23 19:03

tom-19