ClientSideEvents中的ASP.NET密码和确认

时间:2012-04-17 12:45:45

标签: javascript asp.net devexpress

我有2个ASPxTextBox,ASPxValidationSummary和ASPxButton
在JS文件中有OnPasswordValidation函数
但是当我输入密码然后单击Tab按钮时,SetIsValid(false)
on txt_password控件不起作用,但它适用于txt_ConfirmPassword

为什么?

<dx:ASPxTextBox ID="txt_password" runat="server" Password="true" AssociatedControlID="txt_password">
    <ClientSideEvents Validation="OnPasswordValidation" />
</dx:ASPxTextBox>

<dx:ASPxTextBox ID="txt_ConfirmPassword" runat="server" Password="true" AssociatedControlID="txt_ConfirmPassword">
    <ClientSideEvents Validation="OnPasswordValidation" />
</dx:ASPxTextBox>

<dx:ASPxButton ID="btnSubmit" runat="server" Text="Submit" ClientInstanceName="btnSubmit" onclick="btnSubmit_Click" AutoPostBack="False">
<ClientSideEvents Click="function(s, e) {onClickBtnSubmit();}"/>
</dx:ASPxButton>

function OnPasswordValidation(s, e) {
    var objpassword = GetObj('txt_password');
    var objConfirmPassword = GetObj('txt_ConfirmPassword');
    var password = aspxGetControlCollection().Get(objpassword.id);
    var ConfirmPassword = aspxGetControlCollection().Get(objConfirmPassword.id);

    if (password.GetValue() == null) {
        password.SetIsValid(false);
        ConfirmPassword.SetIsValid(false);
        return;
    }
    if (ConfirmPassword.GetValue() == null) {
        password.SetIsValid(false);
        ConfirmPassword.SetIsValid(false);
        return;
    }
    if (password.GetValue().length > 5 || ConfirmPassword.GetValue().length > 5) {
        if (password.GetValue() == ConfirmPassword.GetValue()) {
            password.SetIsValid(true);
            ConfirmPassword.SetIsValid(true);
        }
        else {
            password.SetIsValid(false);
            ConfirmPassword.SetIsValid(false);
            password.SetErrorText = "Password must equal with Confirm Password";
            ConfirmPassword.SetErrorText = "Password must equal with Confirm Password";
        }
    }
    else {
        ConfirmPassword.SetIsValid(false);
        password.SetIsValid(false);
    }
}

1 个答案:

答案 0 :(得分:0)

这不是实施验证的正确方法。这种方式OnPasswordValidation函数执行两次,每个文本框一次 这是一张带有示例项目的票证,可以满足您的需求:    http://www.devexpress.com/Support/Center/p/Q233058.aspx

我建议您阅读DevExpress控件validation overview,以了解如何在devex控件上实现验证。