Asp.NET RequiredFieldValidator and OnClientClick asp:Button

1.2k Views Asked by At

I'm developing a classic WebForms Application in C# and I have a custom aspx page where the users can modify their data (email,pwd,etc..) and I have some 3 textbox and one RequiredFieldValidator for each textbox and one

            <asp:ValidationSummary ID="ChangeUserPasswordValidationSummary" runat="server" CssClass="failureNotification" 
             ValidationGroup="ChangeUserPasswordValidationGroup"/>

I have a submit button:

<asp:Button ID="ChangePasswordPushButton" runat="server" Text="Cambia password"   OnClick="ChangePwd_Click" OnClientClick="return ValidatePasswordFields();"
                     ValidationGroup="ChangeUserPasswordValidationGroup"/>

In this form when I click on ChangePasswordPushButton before going to server side 'ChangePwd_Click' function, I want to disable (client side) the validators if one of the textbox is empty.

These are the client side functions I wrote:

function ValidatePasswordFields() {

    var oldpwd = $('#<%=txtCurrentPassword.ClientID %>').val();
    if (oldpwd == '') {
        setValidators(false);
        return true;
    } else { setValidators(true);return false; }


}


function setValidators(status) {
    ValidatorEnable(document.getElementById('<%= txtCurrentPasswordRequired.ClientID %>'), status);
    ValidatorEnable(document.getElementById('<%= ConfirmNewPasswordRequired.ClientID %>'), status);
    ValidatorEnable(document.getElementById('<%= NewPasswordRequired.ClientID %>'), status);
    return false;

}

The problem is that this code cause always post back.

can you help me? thanks

1

There are 1 best solutions below

2
On

Your problem is in ValidatePasswordFields you are returning true in beginning. Just remove it:

function ValidatePasswordFields() {
    var oldpwd = $('#<%=txtCurrentPassword.ClientID %>').val();
    if (oldpwd == '') {
        setValidators(false);
        return true;
    } else { setValidators(true);return false; }


}