JQuery Validation Plug-in Remote authentication method bug resolution _jquery

Source: Internet
Author: User
But strangely enough, the recent use of it feels a bit odd, because it seems a bit rigid, the ability to respond to existing functions is not strong, and even a strange bug. Any project has a bug in fact also normal, but this bug is actually a document has been recorded, but did not realize the function, which is a bit indefensible. This problem will be in remote verification method, fortunately modified to be very easy, in this record, but also convenient for future reference.
When validating a form, it is sometimes necessary to send an AJAX request to the server for judgment, such as checking that the user name exists when the user registers. The JQuery validation plug-in provides a remote way to achieve this. For example, I can validate a form like this:
Copy Code code as follows:

<form id= "RegForm" >
<input type= "text" name= "UserName"/>
</form>
<script language= "JavaScript" >
$ (' #regForm '). Validate ({
' Rules ': {
' UserName ': {
' Required ': true,
' Remote ': '/account/verify '
}});
</script>

This way, the JQuery validation will request a URL such as "/account/verify?username=jeffz" to get true/false. Unfortunately, when we use asp.net mvc, we tend to write the name of input as a specific form to take advantage of the powerful binding capabilities of Defaultmodelbinder. For example:
<form id= "RegForm" >
<input type= "text" id= "UserName" name= "user. Name "/>
</form>
At the same time, the action method that we use to validate it may also have different parameter names:
Copy Code code as follows:

Public ActionResult Verify (string name) {...}

According to the document description, we should write this at this point:
Copy Code code as follows:

$ (' #regForm '). Validate ({
' Rules ': {
' User. Name ': {
' Remote ': {
URL: '/account/verify ',
Data: {
Name:function () {return $ ("#userName"). Val ();
}}}}});

In practical terms, however, jquery is still asking for "/account/verify?user." Name=jeffz ", baffled. Confirm after three had to turn to Jquery.validation.js source code, looked almost fainted:
Copy Code code as follows:

Remote:function (value, element, param) {
if (this.optional (Element))
return "Dependency-mismatch";
...
param = typeof param = = "string" && {Url:param} | | Param
if (previous.old!== value) {
Previous.old = value;
var validator = this;
This.startrequest (Element);
var data = {};
Data[element.name] = value; Data or Element.name?
$.ajax ($.extend (True, {
Url:param,
Mode: "Abort",
Port: "Validate" + Element.name,
DataType: "JSON",
Data:data,
Success:function (response) {
...

I'm surprised I don't know why I did this, so I don't have the function of specifying the parameter name at all. So, change it:
Copy Code code as follows:

Remote:function (value, element, param) {
if (this.optional (Element))
return "Dependency-mismatch";
...
param = typeof param = = "string" && {Url:param} | | Param
if (previous.old!== value) {
Previous.old = value;
var validator = this;
This.startrequest (Element);
var data = {};
Data[element.name] = value;
$.ajax ($.extend (True, {
Url:param,
Url:param.url,
Mode: "Abort",
Port: "Validate" + Element.name,
DataType: "JSON",
Data:data,
Data:param.data | | Data
Success:function (response) {
...

You can change the two places to solve the problem. Unfortunately, jquery.validate.min.js similar files can only be compressed by themselves.
It is puzzling that such a problem would arise.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.