aWebDeveloper
aWebDeveloper

Reputation: 38342

jquery validate plugin

In my case name and id's of form are different and names are of format data[password] so thsi is the solution that i found on net and it works

$(document).ready(function() 
{               
    var passwd = $("#old-password").attr("name");
    var $params = {debug:false, rules:{}, messages:{}};

    $params['rules'][passwd]    = {"required": true, "maxlength": 100};  
    $params['messages'][passwd] = {"required": "Please Enter Old Password ",'maxlength':'Old Password should not exceed 100 characters'};

    $("#GamerAccountSettingsForm").validate($params);
});

This outputs a label with class error but this is what i want it to do. If there is a div with class error-message next to this input field it should show error inside it else it needs to append a div with class error-message to the input field

Upvotes: 0

Views: 137

Answers (1)

Thizzer
Thizzer

Reputation: 16653

You can use the errorPlacement options for this http://docs.jquery.com/Plugins/Validation/validate#options

And if I am correct, this should work;

$params['errorPlacement'] = function(error, element) 
{
   if($(element).next("div.error-message"))
   {
       $(element).next("div.error-message").html(error.html());
   }
   else
   {
       divError = $("<div class='error-message'>" + error.html() + "</div>");
       divError.insertAfter(element);
   }
};

Upvotes: 1

Related Questions