views:

89

answers:

1

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 oldpassword , and newPassword and newPasswordAgain .

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/checkOldPass.do" , {"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 .

+1  A: 

You can use use $.ajax and set async:false: http://api.jquery.com/jQuery.ajax/
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.

Kobi
Yes , there indeed is a server side validation in my application , client side validation is just an user experience enhancement. And the async works. thanks!
ZZcat