Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery form validation custom error placement in <div> container

I am using jquery validate plugin to validate my form fields.

The script is like below

jQuery(document).ready(function($){
     $.validator.addMethod(
    "mydate",
    function(value, element) {
        return value.match(/^\d\d?\-\d\d?\-\d\d\d\d$/);
    },
    "Please enter a date in the format dd-mm-yyyy"
    );
    var validator = $("#signupform").validate({
  rules: {
            User: {
    required: true,
                remote: {
        url: "user.php",
        type: "post",
          },
   },
   "RID[]": {
    required: true,
                remote: {
        url: "resource.php",
        type: "post",
          },
   },
            Date: {
    required: true,
                mydate : true
            },
  },
  messages: {
   User: "Specify Active User",
   "RID[]": "Specify Available Resource",
            Date: "Specify Date"
  },
  errorPlacement: function(error, element) {
    error.appendTo( element.parent().next() );
  },
  success: function(label) {
   label.html("OK").addClass("checked");
  }
 });

which validates one of my form field

<tr>
<td style="width: 70px" class="style22" valign="top">Resource ID</td>
<td id="resource" style="width: 267px;">
<input id="resource" name="RID[]" type="text" value="" style="width: 232px" /></td>
<td class="style21" style="width: 160px" valign="top">
<img id="addScnt" alt="[+]" src="+.gif"/>
</td>
<td>
    &nbsp;
</td>
</tr>

But I want to place error for "RID[]" in a specific div <div id="errorcontainer2">.

How can I make this possible??

Thanks in advance.. :)

blasteralfred

like image 723
Alfred Avatar asked Feb 25 '23 07:02

Alfred


1 Answers

I solved it using custom error placement in a specific div <div id="errorbox">

errorPlacement: function(error, element){
    if(element.attr("name") == "RID[]"){
        error.appendTo($('#errorbox'));
    }else{
        error.appendTo( element.parent().next() );
    }
}
like image 139
Alfred Avatar answered Apr 07 '23 03:04

Alfred