Sawyer Sawyer - 3 months ago 6x
jQuery Question

Jquery ajax password validation

I want to implement a functionality of changing user's password. It's basically a form with three fields. The ids of the three input are


When the user lost focus of the oldpassword input or submit the form , I want to check against the database , to see if the oldpassword is correctly entered.
How can I achieve this by using jquery validation ?

I add a custom rule like this :

$.validator.addMethod("checkOldPass",function (value ,element) {
var validPass = false ;
$.post("/SMS/admin/users/" , {"empId":empId , "oldEmpPass":$.md5(value)},function (data) {
var msg = eval('('+data+')');
if(msg.resultFlag == 1) {
validPass = true ;

return validPass ;
} , "error´╝ü");

But the problem is, before my post method returns a result flag, the method already finished, so I always get the error message.
I know jquery validation has a remote method, but I can't use it, because I am using struts2-json plugin, my json result always encapsulate in a object, and remote method need a boolean json result. I can't directly get true or false from the result.


You can use use $.ajax and set async:false:
This will wait for the call to complete, blocking your page.

That said, I'm not sure you sould use JavaScript to validate a password. This is something you typically want to do on the server, and it has zero security anyway.