2012-03-21 128 views
1

我試圖創建一個jqGrid形式的密碼確認輸入,但我相信我做錯了方式。原因是在我的實現中,編輯已定義的用戶時,兩個字段(password和password_confirm)都包含帶星號的用戶密碼。jqGrid密碼確認

這沒什麼錯,但我相信它最好在編輯時都有兩個字段爲空,並且只有在它們包含值時才驗證它們。這是部分代碼:

colNames: 「姓名」, 「用戶名」, 「電子郵件」, 「密碼」, 「確認密碼」],

colModel:[{名:「名稱」索引: 「名字」,可編輯:真正的,editrules:{要求:真正}},

等領域 ....

{名: 「密碼」,索引: 「密碼」, editable:true,edittype:「password」,hidden:true,editrules:{edithidden:true,required:true}}, {name:「confirm_password」,index:「confirm_password 」,可編輯:真正的,edittype: 「密碼」,隱藏的:真實,editrules:{edithidden:真,要求:真正}},

正如你可以看到我已經定義的兩個對象,一個是實際密碼和另一個用於確認,當我填充網格時,我返回兩個字段相同的值。

有更好實施的想法嗎?

非常感謝。

回答

1

一直在尋找這個答案,我想出了我自己的解決方案。我不想在DB - Redis中添加一個字段 - 所以我需要創建一個密碼檢查字段,並驗證它是否與第一個密碼字段匹配。密碼檢查字段將被提交到後端,這不是一個錯誤,而是一個功能。

關於代碼。

我們定義的第一個功能是一個將創建密碼校驗字段,並追加到原來的密碼字段:

function createPassCheck(el) { 
      // Create the containing table row 
      var passCheck = $("<tr></tr>").addClass("FormData") 
        .attr({ 
          id: "tr_passwordCheck", 
          rowpos: 20 
        }); 
      // Create a cell for the label and add it to the row 
      var passCheckLabelTd = $("<td></td>") 
        .addClass("CaptionTD").text("Password Check"); 
      passCheck.append(passCheckLabelTd); 
      // Prepare the cell for the input box. All 
      // the cells have a non breaking space, we add 
      // one as well to keep aligned. We then add it to the row. 
      var passCheckTd = $("<td>&nbsp;</td>") 
        .addClass("DataTD"); 
      passCheck.append(passCheckTd); 
      // Create an input box, and add it to the input cell 
      var passCheckInput = $("<input></input>") 
        .addClass("FormElement ui-widget-content ui-corner-all") 
        .attr({ 
          id: "passwordCheck", 
          name: "passwordCheck", 
          role: "textbox", 
          type: "password" 
        }); 
      passCheckTd.append(passCheckInput); 
      // Finally append the row to the table, we have been called after 
      // the creation of the password row, it will be appended after it. 
      var tbodyEl = el.parentNode.parentNode.parentNode; 
      tbodyEl.appendChild(passCheck[0]); 
    } 

之前,我們可以繼續前進,我們需要添加其他功能,一鍵一:它會檢查兩個密碼是否匹配。

function customPassCheck(cellvalue, cellname) { 
      // Get a reference to the password check input box. You see 
      // the 'tr_passwordCheck' id we are using? We set that id in 
      // the function "createPassCheck". 
      var passCheckVal = $("#tr_passwordCheck input").val() 

      // If both fields are empty or the passwords match 
      // we can submit this form. 
      if (
        (cellvalue == "" && passCheckVal == "") 
        || 
        cellvalue == passCheckVal  
       ) { 
        return [true, ""]; 
      } 

      // Can you guess why we are here? 
      return [false, "Password and password check don't match."]; 
    } 

最後,我們將用來執行對編輯密碼爲空的功能,我們將通過註冊爲自定義格式做到這一點。

function customPassFormat(cellvalue, options, rowObject) { 
      // When asked to format a password for display, simply 
      // show a blank. It will make it a bit easier when 
      // we editing an object without changing the password. 
      return ""; 
    } 

我們可以在現在的jqGrid定義了密碼字段,並使其特殊:

jQuery("#crud").jqGrid({ 
.... 
.... 
colModel:[ 
    .... 
    { 
    name:'password', 
    index:'password', 
    width:80, 
    align:"right", 
    editable:true, 
    // It is hidden from the table view... 
    hidden: true, 
    editrules:{ 
     // ...but we can edit it from the panel 
     edithidden: true, 
     // We are using a custom verification 
     custom:true, 
     // This is the function we have created 
     // to verify the passwords 
     custom_func: customPassCheck 
    }, 
    edittype: 'password', 
    // Our custom formatter that will blank the 
    // password when editing it 
    formatter: customPassFormat, 
    editoptions: { 
     // This is where the magic happens: it will add 
     // the password check input on the fly when editing 
     // from the editing panel. 
     dataInit: createPassCheck 
    } 
    }, 
.... 
.... 

這是所有鄉親!

法比奧