Verify that the jquery form is placed in the <div> container

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

the script looks 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 checks one of the fields of my form

<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 put the error for "RID []" in a specific div <div id="errorcontainer2">.

How can I make this possible?

Thanks in advance..:)

blasteralfred

+3
source share
3 answers

I solved this using my own 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() );
    }
}
+7
source
  • Create a div with id.

  • use the selector to grab your div.

  • use function . add () to add the HTML error code to it.

0
source

errorPlacement :

errorPlacement: function(error, element){
    if("RID[]" == element.attr("name")){
        // Place error for RID[] as you want
    }else{
        // Place errors normally
    }
}
0

Source: https://habr.com/ru/post/1787321/


All Articles