Jquery form validation custom error placement in <div> container

27,218

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() );
    }
}
Share:
27,218
Alfred
Author by

Alfred

I am a Full Stack developer and a DevOps Engineer, who always to learn from, and contribute to, the technology community. I was a beginner in programming once. What all I had was pieces of scattered and crude knowledge (don't know if i can call it knowledge) then. Later, I joined for Master of Computer Applications in a college and there, I got a great teacher. It was her, who taught me when and where to use 'while' and 'for' loops even.What all knowledge I have now, and what all achievements I've made till now, is only because of her. Compared to her, I am ashes. Hats off my dear teacher Sonia Abraham, you are the best of your kind. Sonia Abraham is a professor of the Department of Computer Applications, M.A College of Engineering, Mahatma Gandhi University

Updated on December 30, 2020

Comments

  • Alfred
    Alfred over 3 years

    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

  • Alfred
    Alfred over 13 years
    where should i add <div id="errorcontainer2"> in ur syntax??