Reputation: 117
I have this code for validation but it show error even it returns true. I change all return false to true but error is shown. second I want show result.message instead "username is already taken".
$.validator.addMethod("uniqueUserName", function (value, element) {
$.ajax({
type: "post",
url: "/validation/search",
data: {
nationalCode: $('#karkonan_MelliCode').val(),
personType: $('#karkonan_MelliatCode').val()
},
dataType: "json",
async: false,
success: function (result) {
// alert(result.message);
// alert(result.resultCode);
// if the user exists, it returns a string "true"
if (result.resultCode == "0") {
alert($('#karkonan_MelliatCode').val());// already exists
//j = result.message;// already exists
return false;
}
else {
alert($('#karkonan_MelliatCode').val());
return true; // username is free to use
}
},
error: function (result) {
// if the user exists, it returns a string "true"
alert("error");
return false;
// username is free to use
}
})
}, "Username is Already Taken");
and it's rule is :
'karkonan.MelliCode': {
number:true,
minlength: 10,
maxlength: 10,
required: true,
withmelliat:true,
uniqueUserName: true
},
Upvotes: 0
Views: 45
Reputation: 31839
The error
is function to be called if the AJAX request fails, not if your script returns some error!
The function receives three arguments:
The jqXHR
(in jQuery 1.4.x, XMLHttpRequest
) object, a string describing the type of error that occurred and an optional exception object, if one occurred.
Possible values for the second argument (besides null
) are timeout
, error
, abort
, and parsererror
. When an HTTP error occurs, errorThrown
receives the textual portion of the HTTP status, such as Not Found
or Internal Server Error
.
As of jQuery 1.5, the error setting can accept an array of functions. Each function will be called in turn. Note: This handler is not called for cross-domain script and cross-domain JSONP requests.
Controversially, the success
is a function to be called if the AJAX request itself succeeds, i.e. if the browser can send the request properly.
The function gets passed three arguments:
The data returned from the server, formatted according to the dataType
parameter; a string describing the status; and the jqXHR
(in jQuery 1.4.x, XMLHttpRequest) object.
Upvotes: 0
Reputation: 2596
Your addMethod
probably want a callback response immediately.
You perform an Ajax and the response is async. return
statements in your success
and error
handlers don't apply for the addMethod
return value (done immediately after the Ajax request and before responses).
To check that, add a return true;
here:
});
return true;
}, "Username is Already Taken");
I havn't the solution yet, I have (and you have) to read your validator plugin documentation.
You also have to read the remote
property of this plugin: here the documentation.
Upvotes: 1