开发者

How to solve problem when use jquery datepicker and validation in the same time

开发者 https://www.devze.com 2022-12-25 15:16 出处:网络
When I used datepicker with trigger icon so that users could choose date from clicking this icon or type directly in textbox (txtDate)开发者_如何学JAVA, I also used jquery validation to require textbo

When I used datepicker with trigger icon so that users could choose date from clicking this icon or type directly in textbox (txtDate)开发者_如何学JAVA, I also used jquery validation to require textbox must be not empty. But when a user submit the form with empty textbox (txtDate.Text=""), the error message of validation push trigger icon to the right. Could you tell me the solution? Thank you very much!


$('#some_form').validate({
  rules: {...},
  messages: {...},
  errorPlacement: function(error, element) { //solve you problem
    var trigger = element.next('.ui-datepicker-trigger');
    error.insertAfter(trigger.length > 0 ? trigger : element);
  }
});


The errorPlacement function receives two parameters - the error message and the validated element. Use the latter to decide whether or not to customize the placement for your fields (for example, by adding a class):

$('form').validate({
      rules: {...},
      errorPlacement: function(error, element) {
            if (element.hasClass('customError')) {
                  // custom error placement
            }
            else {
                  element.after(error); // default error placement
            }
      }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消