Extjs确认密码

时间:2011-12-13 21:01:14

标签: validation passwords extjs confirm

我有两个密码字段,并且在允许帖子发生之前我试图确认它们是相同的。这是我的代码,它返回“密码不匹配!”每一次无论如何。当我在alert(pass1)行之后执行var pass1 = ...时,它会给我undefined。我也试过var pass1 = formPanel.findField("txt_newPIN").getValue()并返回相同的内容。这是代码:

    {
    fieldLabel:"PIN/Password",
    actionText:"Edit",
    fieldValue:"****",
    dialog:new MyAccount.DialogBox({
        id:"win_editPIN",
        name:"editPIN",
        headerContent:"Edit Password:",
        updateURL:"/uiapi/myaccount/setAccountPIN",
        items:[{
            id:"txt_currentPIN",
            name: "currentPIN",
            fieldLabel: "Current Password",
            validationEvent:"blur",
            allowBlank: false,
            maxLength:20,
            inputType:"password"
        },{
            id:"txt_newPIN",
            name: "newPIN",
            fieldLabel: "New Password",
            vtype:"confirmPassword",
            validationEvent:"blur",
            allowBlank: false,
            maxLength:20,
            inputType:"password"
        },{
            id:"txt_confirmPIN",
            fieldLabel: "Confirm Password",
            vtype:"confirmPassword",
            validationEvent:"blur",
            initialPin:"txt_newPIN",
            allowBlank: false,
            maxLength:20,
            inputType:"password"
        }], 


 validateForm:function() {
     var formPanel = Ext.getCmp("win_editPIN").formPanel.getForm();
         // Save the fields we are going to insert values into
         var pass1 = formPanel.findField("txt_newPIN");
     var pass2 = formPanel.findField("txt_confirmPIN");

         if (pass1 != pass2)
             return {success:false,  errorMessage:"Passwords do not match!"}
         }

    })

2 个答案:

答案 0 :(得分:3)

谢谢Nghia,你的回答让我在那里,允许我选择字段。剩下的就是制作一个自定义validator选项,这里是代码,只是为了简洁而显示最后一项。

     {
            id:"txt_confirmPIN",
            name: "newPIN_confirm",
            fieldLabel: "Confirm Password",
            validationEvent:"blur",
            initialPin:"txt_newPIN",
            allowBlank: false,
            maxLength:20,
            inputType:"password",
            // This custom validator option expects a return value of boolean true if it
            // validates, and a string with an error message if it doesn't
            validator: function() {
                   var formPanel = Ext.getCmp("win_editPIN").formPanel.getForm();
                   // Save the fields we are going to insert values into
                   var pass1 = Ext.getCmp('txt_newPIN').getValue();
                   var pass2 = Ext.getCmp('txt_confirmPIN').getValue();
                   console.log("pass 1 = " + pass1 + "--pass 2 = " + pass2);

                    if (pass1 == pass2)
                        return true;

                    else 
                        return "Passwords do not match!";
            }
        }

如果验证器验证,则验证器选项要求返回值为true,如果不验证,则返回带错误消息的字符串。

答案 1 :(得分:2)

使用findField()方法时,需要传递字段名而不是字段id。

var pass1 = formPanel.findField("newPIN");

或只是直接获取其价值

var pass1 = Ext.getCmp('txt_newPIN').getValue();