2

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, 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 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.