Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

Error Placement - Jquery Validation

How to change error placement according to the details entered in field. i.e if field is empty then display error after submit button else if the field is not empty but the min length or max length is not matched the display error after element.

I tried this but its not working.

errorPlacement: function(error, element) {
if(error.text() === "Please fill the field.")
{ 
  if(element.val() === ""){
    $(".login-empty-error").html(error);
  } else {
    error.insertAfter(element);
  }
} else {
  error.insertAfter(element);
}
}

How to find whether the type is required?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How to change error placement according to the details entered in field.

The errorPlacement option cannot do anything like this. Once the error element container is placed within the layout using errorPlacement, the plugin simply toggles this element as needed. Its location is not changed as the element is already there but hidden.

In other words, you cannot dynamically move the error element around the form using any of the provided options or methods.

However, if you want a message to appear near the button when there are errors within the form, look at the showErrors option.

showErrors: function (errorMap, errorList) {
    if (this.numberOfInvalids() > 0) {
        $("#summary").html("Your form contains " + this.numberOfInvalids() + " errors, see details above.");
    } else {
        $("#summary").empty();
    }
    this.defaultShowErrors(); // <- default messages that appear within form
}

Basic usage demo: http://jsfiddle.net/v2h2a2mb/

You could tweak this function so that a message only appears under certain conditions. The built-in arguments are described below...

errorMap, Type: Object, Key/value pairs, where the key refers to the name of an input field, values the message to be displayed for that input.

errorList, Type: Array, An array for all currently validated elements. Contains objects with the following two properties:

  • message, Type: String, The message to be displayed for an input.

  • element, Type: Element, The DOMElement for this entry.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...