2011-12-13 90 views
1

我有两个密码字段,我试图确认它们在允许发布之前是相同的。这是我的代码,它返回“密码不匹配!”每次都不管。当我在var pass1 = ...行后执行alert(pass1)时,它会给我undefined。我也试过var pass1 = formPanel.findField("txt_newPIN").getValue(),它返回相同的东西。代码如下:Extjs确认密码

{ 
    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

当使用findField()方法时,您需要传递字段名称而不是字段标识。

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

或只是简单地得到其数值直接

var pass1 = Ext.getCmp('txt_newPIN').getValue(); 
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,如果它验证,并以一个错误消息的字符串,如果它没有。