Check user belongs to a SharePoint Group and Hide some controls in New/Edit/Display Forms on SharePoint Custom List. If you are using SharePoint list, you have to use the below steps to meet this requirement

Solution

The following Script which will help you in checking if the current logged in SharePoint user belongs to a SharePoint user group and based on it hide some controls in New/Edit/Display aspx forms on Custom List.

ExecuteOrDelayUntilScriptLoaded(hideControls, "sp.js");

function hideControls() {
clientContext = new SP.ClientContext();
groupCollection = clientContext.get_web().get_siteGroups();
group = groupCollection.getById(10); //The ID of the SharePoint user group
users = group.get_users();
clientContext.load(group);
clientContext.load(users);
currentUser = clientContext.get_web().get_currentUser();
clientContext.load(currentUser);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onOperationSucceeded),
Function.createDelegate(this, this.onOperationFailed)
);
RefreshCommandUI();
}

function onOperationSucceeded() {
if (users.get_count() > 0) {
UserExistInGroup = false;
for (var i = 0; i < users.get_count(); i++) {
if (users.itemAt(i).get_loginName() == this.currentUser.get_loginName()) {
UserExistInGroup = true;
break;
}
}
}

if (UserExistInGroup) {
} else {
document.getElementById("testid").style.display = "none";
}
}

function onOperationFailed(sender, args) {
document.getElementById("testid").style.display = "none";
}

Thank you Anil for your contribution to fix this issue