SharePoint中的人员选取器验证

时间:2019-05-06 01:32:42

标签: javascript sharepoint

我正在尝试验证SharePoint中的2个人字段,如果它们相同则显示错误。我目前有以下代码:

function PreSaveAction() {
if (document.getElementById('idAttachmentsRow').style.display=='none' )
  {
     alert('Please attach supporting documents.');
     return false ;
  }
if ($("select[title='Vendor & Co Code Required Field'] option:selected").val() == 279) {
                alert("Please select vendor.")
                return false;
}
else {  return true;  }
}

我已经尝试了下面的2个代码(目前已注释掉)

此功能不执行任何操作,并取消了现有的验证

/*if ($("input[title='Project Manager']).val() == $("input[title='GOA Approver']).val())
    { 
        alert("Project Manager and GOA Approver cannot be the same.");
        return false;
    }*/

这与我已经完成的工作差不多—它保留了现有的验证,但是即使两个人的选择器字段不同,它也会返回错误。

/*if (document.getElementById('ProjectManager_a553beb7-f694-4e6d-b35c-727accadf301_$ClientPeoplePicker_EditorInput').value == document.getElementById('GOA_x0020_Approver_f7d3aad1-fb6e-4bba-a5c3-9933b2a58c3f_$ClientPeoplePicker_EditorInput').value)
{ 
    alert("Project Manager and GOA Approver cannot be the same.");
    return false;
}*/

1 个答案:

答案 0 :(得分:0)

尝试下面的示例代码以使用户从人员选择器控件中获取。

function getUserFromPeoplePicker(title) {
        //Get the people picker field
        var ppDiv = $("div[title='" + title + "']")[0];
        //cast the object as type PeoplePicker
        var peoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[ppDiv.id];

        //Get list of users from field (assuming 1 in this case)
        var userList = peoplePicker.GetAllUserInfo();
        var userInfo = userList[0];            

        //The description field contains the login info without the lookup extras.  In new forms this 
        //field can be undefined depending on the field being checked.  Only check if userInfo is
        //initialized.
        if (userInfo != null) {
            // todo use user information
            if (userInfo.EntityData.Email.startsWith('test')) {
                console.log(userInfo.EntityData.Email);
                return true;
            } else {
                alert('Invalid User');
                return false;
            }
        }

        //return userEmail;
    }