jquery.validate prompts error message location method_jquery
The example in this article describes the jquery.validate prompt error message location method. Share it with everyone for your reference, the details are as follows:
I haven’t used the jquery.validate.js plug-in for a long time, and I almost forgot about it. Alas, good things should always be taken out and looked at. Today I used jquery.validate to make a small thing. I encountered a problem, which is the location of the error message. If you know it, it is very simple. I have encountered it before, but I forgot it. Mark it now. If I forget it in the future, I will look back. As the saying goes, a good memory is worse than a bad writing.
As an example, everyone will know what’s going on.
rules: { name:{ required:true, rangelength:[1,20] }, validateCode: { required:true, number:true, rangelength:[5,5] } }, messages: { name: { required: "请输入命令名", rangelength: jQuery.format("长度请控制在{0} ~ {1}") }, validateCode: { required: "请输入验证码", number: "请输入数字", rangelength: jQuery.format("长度必须是5位") } }, success: function(label) { label.addClass("error checked"); }, submitHandler: function(form) { if($("#RegionId").val() == '0'){ $("#citySelect").attr("class","error").html('请选择区域').show(); $("#RegionId").attr("class","error"); }else{ $("#RegionId").attr("class","valid"); $("#citySelect").attr("class","valid").html('success').show(); form.submit(); } } });
In the above code, I did not add a method to store the error message. Let’s take a look at the effect
<td> <input type="text" maxlength="30" value="" id="name" name="name"> <label for="name" generated="true">请输入命令名</label> //错误信息会自动根在输入框的后面。 </td>
If we add the errorPlacement method of error message location, let’s see what the effect looks like.
rules: { name:{ required:true, rangelength:[1,20] }, validateCode: { required:true, number:true, rangelength:[5,5] } }, messages: { name: { required: "请输入命令名", rangelength: jQuery.format("长度请控制在{0} ~ {1}") }, validateCode: { required: "请输入验证码", number: "请输入数字", rangelength: jQuery.format("长度必须是5位") } }, errorPlacement: function(error, element) { //错误信息位置设置方法 error.appendTo( element.parent().next() ); //这里的element是录入数据的对象 }, success: function(label) { label.addClass("error checked"); }, submitHandler: function(form) { if($("#RegionId").val() == '0'){ $("#citySelect").attr("class","error").html('请选择区域').show(); $("#RegionId").attr("class","error"); }else{ $("#RegionId").attr("class","valid"); $("#citySelect").attr("class","valid").html('success').show(); form.submit(); } } });
Let’s see the effect
<tr> <th><label for="name" id="lname"><code title="必填">*</code>命令名称</label></th> <td><input type="text" maxlength="30" value="" id="name" name="name"></td> <td><label for="name" generated="true">请输入命令名</label></td> //错误信息跑到这儿来了, </tr>
It’s simple enough. Even simple things will be forgotten if you don’t use them for a long time.
Readers who are interested in more content related to jQuery plug-ins can check out the special topic of this site: "Summary of common jQuery plug-ins and usage"
I hope this article will be helpful to everyone in jQuery programming.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



What should I do if Google Chrome prompts that the content of this tab is being shared? When we use Google Chrome to open a new tab, we sometimes encounter a prompt that the content of this tab is being shared. So what is going on? Let this site provide users with a detailed introduction to the problem of Google Chrome prompting that the content of this tab is being shared. Google Chrome prompts that the content of this tab is being shared. Solution: 1. Open Google Chrome. You can see three dots in the upper right corner of the browser "Customize and control Google Chrome". Click the icon with the mouse to change the icon. 2. After clicking, the menu window of Google Chrome will pop up below, and the mouse will move to "More Tools"

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

Baidu Tieba app prompts that the operation is too frequent. This prompt is usually to maintain the normal operation and user experience of the platform to prevent malicious screen spam, advertising spam and other inappropriate behaviors. For specific handling methods, you can read the tutorial shared by the editor. Baidu Tieba app prompts that the operation is too frequent. Sharing how to deal with it 1. When the system prompts [Operation is too frequent], we need to wait for a while. If you are anxious, you can do something else first. Generally, after waiting for a while, this prompt message will It will disappear automatically and we can use it normally. 2. If after waiting for a long time, it still displays [Operation Too Frequent], we can try to go to Tieba Emergency Bar, Tieba Feedback Bar and other official Tieba, post to report this phenomenon and ask official personnel to solve it. 3.

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

This article will explain in detail the numerical encoding of the error message returned by PHP in the previous Mysql operation. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. . Using PHP to return MySQL error information Numeric Encoding Introduction When processing mysql queries, you may encounter errors. In order to handle these errors effectively, it is crucial to understand the numerical encoding of error messages. This article will guide you to use php to obtain the numerical encoding of Mysql error messages. Method of obtaining the numerical encoding of error information 1. mysqli_errno() The mysqli_errno() function returns the most recent error number of the current MySQL connection. The syntax is as follows: $erro

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute
