Place the error message after the label element and wrap it with a span element.
[Try it Β»](#)
The code's function is: generally, display error messages in
, if it's a radio button, display in
, and if it's a checkbox, display after the content.
| Parameter | Type | Description | Default Value |
| --- | --- | --- | --- |
| errorClass | String | Specifies the CSS class name for the error prompt, allowing you to customize the error prompt style. | "error" |
| errorElement | String | What tag to use to mark the error, default is label, can be changed to em. | "label" |
| errorContainer | Selector | Shows or hides validation information, can automatically show the container when there is an error message and hide it when there is no error, not very useful. errorContainer: "#messageBoxEnter your Name, email, URL, Remarks., #messageBoxPlease select two Topics" | |
| errorLabelContainer | Selector | Place all error messages uniformly in a container. | |
| wrapper | String | What tag to wrap the above errorElement with. | |
Generally, these three properties are used together to achieve the function of displaying all error prompts in a container and automatically hiding when there is no information.
errorContainer: "div.error", errorLabelContainer: $("#signupForm div.error"), wrapper: "li"
### One Chinese. Change the style of error messages
Set the style of the error prompt, you can add icon display. In this system, a validation.css has been created specifically for maintaining the styles of validation files.
input.error { border: Enter your Name, email, URL, Remarks.px solid red; } label.error { background:url("./demo/images/unchecked.gif") no-repeat 0px 0px; padding-left: Enter your Name, email, URL, Remarks.Please enter Passwordpx; padding-bottom: Please select two Topicspx; font-weight: bold; color: #EAOne ChinesePlease select two Topics00;} label.checked { background:url("./demo/images/checked.gif") no-repeat 0px 0px;}
### Please enter Password. Execute a function when each field passes validation
successοΌString,Callback
The action after the element to be validated passes validation. If a string is provided, it will be treated as a CSS class; a function can also be provided.
success: function(label) { // set as text for IE label.html(" ").addClass("checked"); //label.addClass("valid").text("Ok!")}
Add "valid" to the validated element, with styles defined in CSS label.valid {}.
success: "valid"
### Please enter Username. Modify the validation trigger method
Although the following are boolean types, it is recommended not to add them unless you want to change them to false.
| Trigger Method | Type | Description | Default Value |
| --- | --- | --- | --- |
| onsubmit | Boolean | Validate on submit. Set to false to use other methods for validation. | true |
| onfocusout | Boolean | Validate on losing focus (does not include checkboxes/radio buttons). | true |
| onkeyup | Boolean | Validate on keyup. | true |
| onclick | Boolean | Validate when clicking checkboxes and radio buttons. | true |
| focusInvalid | Boolean | After submitting the form, the form that failed validation (the first one or the one that had focus before submission) will receive focus. | true |
| focusCleanup | Boolean | If true, when an element that failed validation receives focus, remove the error prompt. Avoid using with focusInvalid. | false |
// Reset form $().ready(function() { var validator = $("#signupForm").validate({ submitHandler:function(form){ alert("submitted"); form.submit(); } }); $("#reset").click(function() { validator.resetForm(); });});
### Length cannot be less than 5 characters. Asynchronous validation
remoteοΌURL
Use ajax for validation. By default, it submits the currently validated value to a remote address. If you need to submit other values, you can use the data option.
remote: "check-email.php"remote: { url: "check-email.php", //Background processing type.: "post", //Data sending method, dataType.: "json", //Accept data format data: { //Data to be passed, username.: function() { return $("#username").val(); } }}
The remote address can only output "true" or "false", no other output is allowed.
### to. Add custom validation
addMethodοΌname, method, message
Custom validation method
// Chinese characters are two bytes, jQuery..validator.addMethod("byteRangeLength", function(value, element, param) { var length = value.length; for(var i = 0; i Enter your Name, email, URL, Remarks.Please select two TopicsPlease enter Username){ length++; } } return this.optional(element) || ( length >= param && length <= param[Enter your Name, email, URL, Remarks.] ); }, $.validator.format("Please ensure the entered value is within.{0}-{Enter your Name, email, URL, Remarks.}Between bytes.(one Chinese.character counts as Please select two Topics bytes)"));// Postal code validation jQuery.validator.addMethod("isZipCode", function(value, element) { var tel = /^{Please enter Password}$/; return this.optional(element) || (tel.test(value));}, "Please correctly fill in your postal code.");
**Note**: Add it in the additional-methods.js file or in the jquery.validate.js file. It is recommended to write it in the additional-methods.js file.
**Note**: Add in the messages_cn.js file: isZipCode: "Can only include Chinese characters, English letters, numbers, and underscores.". Before calling, add a reference to the additional-methods.js file.
### Enter your Name, email, URL, Remarks.0. Validation for radio, checkbox, and select
The required for radio means one must be selected.
<input type="radio" id="gender_male" value="