Jquery Validate and Ajax Call

I have a little project, which allows customers to change their password.

When customer fulfill the textbox, and click the button, there will be a ajax call, and on success, return something.

This function works great. However, when I tried to add a Jquery validate plugin, the plugin doesn't work at all.

Here is my code.

$(document).ready(function () {
  ValideteForm();

$("#btnChangePassword").click(function (e) {

    var oldPassword = $("#txtOldPassword").val();
    var newPassword = $("#txtNewPassword").val();
    var userID = parseInt($("#txtUserID").val());
    e.preventDefault();
    ChangePassword(userID, oldPassword, newPassword);

   });
});

function ChangePassword(UserID, OldPassword, NewPassword) {

$.ajax({
    type: "POST",
    url: "/WebServices/EditUserInformation.asmx/ChangePassword",
    data: '{"UserID":"' + UserID + '","OldPassword":"' + OldPassword + '","NewPassword":"' + NewPassword + '"}',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
        $("#UpdateResult").hide();
        $("#UpdateResult").fadeIn(300);
        $("#UpdateResult").html(msg.d);
        $("#UpdateResult").fadeOut(5000)
    },
    error: function () {
        alert("Error Happened");
    }
  });
}


function ValideteForm() {
$("#passwordForm").validate(
{
    rules: {
        txtOldPassword: {
            required: true
        },
        txtNewPassword: {
            required: true
        },
        txtConfirmNewPassword: {
            required: true,
            equalTo: "#txtNewPassword"
        }
    },
    messages: {
        txtOldPassword: {
            required: "Please enter old password"
        },
        txtNewPassword: {
            required: "Please enter your new password"
        },
        txtConfirmNewPassword: {
            required: "Please re-enter your password",
            equalTo: "Please enter the same password as above" 
        }
    }
});
}

What should I do? Any help please? Thanks a lot!

Answers


Use jqueryValidateEngine

Here is the Demo URL,

http://www.position-relative.net/creation/formValidator/demos/demoValidators.html

EX: 
$("[class^=validate]").validationEngine({
           success: false,
           failure : function() {      
               }
});

Your HTML should be ,
<input type="text" name="username" class="validate[required]">

Add both validation of the form in button click and in the success of form validation call the method via ajax request.

$("#passwordForm").validate(
{

    submitHandler: function (form) {
        var oldPassword = $("#txtOldPassword").val();
        var newPassword = $("#txtNewPassword").val();
        var userID = parseInt($("#txtUserID").val());    
        ChangePassword(userID, oldPassword, newPassword);
    },

    rules: {
        txtOldPassword: {
            required: true
        },
        txtNewPassword: {
            required: true
        },
        txtConfirmNewPassword: {
            required: true,
            equalTo: "#txtNewPassword"
        }
    },
    messages: {
        txtOldPassword: {
            required: "Please enter old password"
        },
        txtNewPassword: {
            required: "Please enter your new password"
        },
        txtConfirmNewPassword: {
            required: "Please re-enter your password",
            equalTo: "Please enter the same password as above" 
        }
    }
});

Need Your Help

How can I manipulate dynamically created elements wth jquery?

javascript jquery events dom

I am a jquery newbie and i am creating boxes with jquery and then "deleting" them. But I want to use the same code to delete the box in the scope of the created element and the scope of a already c...

How do I use the Selenium IDE plugin with Debian Linux?

selenium debian

I'm trying to install the firefox plugin 'Selenium IDE' (from here: http://seleniumhq.org/download/ )

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.