none
Validate PERSON Column Using javascript RRS feed

  • Question

  • Hey is it possible with javascript to check if Input email/name in team lead matches in team members to give an alert message saying both field cant contain same email or name? I am using person column and there is no possible way to validate in sharepoint. Please See image


    • Edited by lahsin Friday, July 19, 2019 3:41 PM Image remove for privacy
    Thursday, July 18, 2019 7:14 PM

Answers

  • Hi Iahsin,

    If you need an alert when the field is blank then simply set the field is required, i have modified the code it shouldn't do that. try again with the below code ?

    First line try with https & if that doesn't work then try with http.

    <script src="https://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
    <script type="text/javascript" src="/_layouts/15/clientpeoplepicker.js"></script>
    <script type="text/javascript">
    function PreSaveAction()
    {
        var value1=getUserFromPeoplePicker('Team Lead');
        var value2=getUserFromPeoplePicker('Team Member');
        console.log(value1);
        console.log(value2);
    if (typeof value1 !== 'undefined' && typeof value2 !=='undefined')
    {
        if(value1==value2)
        {
           alert("Two Person fields can't  select the same person");
           return false;
        }
        else
        {
          return true;
        }
    }
    else
    {
    return true;
    }
    
    }
    
    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];
                console.log(userInfo);
                var userLoginName;
                
                //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) {
                    
                    userLoginName=userInfo.Key;
                }
    
                return userLoginName;
            }
    </script>
    Thanks & Regards,


    sharath aluri


    Monday, July 22, 2019 3:50 PM

All replies

  • Hi Iahsin,

    I just tested on my end & it worked. Just Paste the below code in the script editor for newform.aspx.

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/2014.02/jquery.SPServices.min.js" type="text/javascript"></script>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    
    _spBodyOnLoadFunctionNames.push("initEndDateEvent");
    
    function PreSaveAction() {
    
       var label1 = "TS Manager";
       var variable1 = "fldTSManager";      
       var label2 = "Change Installer";
       var variable2 = "fldChangeInstaller";
       
       var userNameTS = checkPPC(label1, variable1);   
       var userNameCI = checkPPC(label2, variable2);   
       comparePPC(userNameTS, userNameCI);
       
       function checkPPC(label, variable){
       
    	   var tempcon;
    	   var fieldValue = "";	   
    	   var fieldLabel = label;
    	   var tr = GetFieldRow(fieldLabel);
    	   var controls;
    		
    		// retrieve the table row that assigned to is found
    		controls = tr.getElementsByTagName("DIV");
    		for(var k=0; k < controls.length; k++) {
    			if(controls[k].id.indexOf("UserField_upLevelDiv") > 0) {
    				tempcon = controls[k].textContent.trim();
    				if (controls[k].textContent.trim() == fieldValue || controls[k].textContent == null){
    					alert("Please assign a " + fieldLabel + "."); 
    					return false; // Cancels the save item process
    				} else {					
    					alert(fieldLabel + ": User = " + tempcon);
    					var userName = tempcon;
    					return userName;
                                            //return true;
                                            //return;
    				}
    			}
    		}
       }
    	
    	// gets the parent node of assigned to 
    	function GetFieldRow(fieldLabel) {
    		var nobrs = document.getElementsByTagName("nobr");
    		for(var i=0; i < nobrs.length ; i++) {
    			if (nobrs[i].textContent == fieldLabel) {
    				var tr = GetParentByTagName("TR", nobrs[i]);
    				return tr;
    			}
    		}
    	}
           	
    	// finds the parent node of the assigned to field
    	function GetParentByTagName(parentTagName, childElementObj) {
    		var parent = childElementObj.parentNode;
    		while(parent.tagName.toLowerCase() != parentTagName.toLowerCase()) {
    			parent = parent.parentNode;
    		}
    		return parent;
    	}
    } 	
    
    function comparePPC(variable1, variable2){
    
         //alert("test");
         alert("variable1 " + variable1 + " variable2 " + variable2);
         
         if(variable1 === variable2){
    
           //alert("Test");
           alert("The TS Manager is " + variable1 +  " and the Change Installer " + variable2 + ", these two fields cannot be the same person.");
           return false;
    
         } else {
    
           alert("The variables do not match");     
    
         }
    }
    
    </script>
    Thanks & Regards,


    sharath aluri

    Thursday, July 18, 2019 8:03 PM
  • hey sharath do i have to modify the code? For example i Named My column TEAM LEAD and TEAM MEMBERS, do i have to add the field title in the code? Please let me know what i have to change, See image above for my form.

    In addition, not sure you read my requirements but lets say for example if Team Lead = Email1 and Team Members= Email1, it should give an alert saying to use a different name or email.

    Team lead = only one email/name can be entered.

    Team members= only 4 to 6 email/name can be entered.

    Thursday, July 18, 2019 9:47 PM
  • Friday, July 19, 2019 1:45 AM
  • Hi, 

    Try below sample code(set team lead Allow multiple selections false).

     <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script language="javascript" type="text/javascript">
            function PreSaveAction() {
                var check = false;
                var TeamLead = 'Team Lead';
                var TeamMembers = 'Team Members';
                // Get the people picker object from the page.
                var peoplePickerTeamLeadDiv = $("[id$='ClientPeoplePicker'][title='" + TeamLead + "']");
                var peoplePickerTeamLead = SPClientPeoplePicker.SPClientPeoplePickerDict[peoplePickerTeamLeadDiv[0].id];
    
                var peoplePickerTeamMembersDiv = $("[id$='ClientPeoplePicker'][title='" + TeamMembers + "']");
                var peoplePickerTeamMembers = SPClientPeoplePicker.SPClientPeoplePickerDict[peoplePickerTeamMembersDiv[0].id];
    
                if (!peoplePickerTeamLead.IsEmpty()) {
                    if (peoplePickerTeamLead.HasInputError) return false; // if any error
                    else if (!peoplePickerTeamLead.HasResolvedUsers()) return false; // if any invalid users
                    else if (peoplePickerTeamLead.TotalUserCount > 0) {
                        var leader = peoplePickerTeamLead.GetAllUserInfo()[0].Key;
                        if (!peoplePickerTeamMembers.IsEmpty()) {
                            if (peoplePickerTeamMembers.HasInputError) return false; // if any error
                            else if (!peoplePickerTeamMembers.HasResolvedUsers()) return false; // if any invalid users
                            else if (peoplePickerTeamMembers.TotalUserCount > 0) {
                                var members = peoplePickerTeamMembers.GetAllUserInfo();
    
                                // Get information about all users.
                                if (peoplePickerTeamMembers.TotalUserCount < 4 || peoplePickerTeamMembers.TotalUserCount > 6) {
                                    alert('fail');
                                    //to do
                                } else {
                                    for (var i = 0; i < members.length; i++) {
                                        if (members[i].Key == leader) {
                                            alert('leader in members');
                                            return false;
                                        }
                                    }
                                    check = true;
                                }
                            }
                        }
                    }
                } else {
                    alert('No valid user');
                }
                return check;
            }
        </script>

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Friday, July 19, 2019 2:27 AM
  • Question
    You cannot vote on your own post

    The code works but even the fields are blank its still giving an alert, how can this be fixed?
    <script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
    <script type="text/javascript" src="/_layouts/15/clientpeoplepicker.js"></script>
    <script type="text/javascript">
    function PreSaveAction()
    {
        var value1=getUserFromPeoplePicker('person1');
        var value2=getUserFromPeoplePicker('person2');
        console.log(value1);
        console.log(value2);
        if(value1==value2)
        {
           alert("Two Person fields can't  select the same person");
           return false;
        }
        else
        {
          return true;
        }
    
    }
    
    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];
                console.log(userInfo);
                var userLoginName;
                
                //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) {
                    
                    userLoginName=userInfo.Key;
                }
    
                return userLoginName;
            }
    </script>


    nk


    • Marked as answer by lahsin Friday, July 19, 2019 3:42 PM
    • Unmarked as answer by lahsin Friday, July 19, 2019 3:43 PM
    • Edited by lahsin Friday, July 19, 2019 3:44 PM
    Friday, July 19, 2019 3:42 PM
  • The above code works but even the fields are blank its still giving an alert, how can this be fixed?

    nk

    Friday, July 19, 2019 3:44 PM
  • Hi Iahsin,

    Try the below one. I just update a little in the Script. which should work for your scenario.

    <script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
    <script type="text/javascript" src="/_layouts/15/clientpeoplepicker.js"></script>
    <script type="text/javascript">
    function PreSaveAction()
    {
        var value1=getUserFromPeoplePicker('Team Lead');
        var value2=getUserFromPeoplePicker('Team Member');
        console.log(value1);
        console.log(value2);
    if (typeof value1 !== 'undefined' && typeof value2 !=='undefined')
    {
        if(value1==value2)
        {
           alert("Two Person fields can't  select the same person");
           return false;
        }
        else
        {
          return true;
        }
    }
    else
    {
    return true;
    }
    
    }
    
    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];
                console.log(userInfo);
                var userLoginName;
                
                //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) {
                    
                    userLoginName=userInfo.Key;
                }
    
                return userLoginName;
            }
    </script>
    Thanks & Regards,


    sharath aluri

    Friday, July 19, 2019 6:45 PM
  • Hi Iahsin,

    did you get a chance to try the code that i sent ?

    Thanks & Regards,


    sharath aluri

    Monday, July 22, 2019 3:09 PM
  • Hello just tried but whenever the field is blank it does not give an alert and i am unable to save the form data. The orginal code works except it gives alert even when they are no email entered

    nk

    Monday, July 22, 2019 3:34 PM
  • Hi Iahsin,

    If you need an alert when the field is blank then simply set the field is required, i have modified the code it shouldn't do that. try again with the below code ?

    First line try with https & if that doesn't work then try with http.

    <script src="https://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
    <script type="text/javascript" src="/_layouts/15/clientpeoplepicker.js"></script>
    <script type="text/javascript">
    function PreSaveAction()
    {
        var value1=getUserFromPeoplePicker('Team Lead');
        var value2=getUserFromPeoplePicker('Team Member');
        console.log(value1);
        console.log(value2);
    if (typeof value1 !== 'undefined' && typeof value2 !=='undefined')
    {
        if(value1==value2)
        {
           alert("Two Person fields can't  select the same person");
           return false;
        }
        else
        {
          return true;
        }
    }
    else
    {
    return true;
    }
    
    }
    
    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];
                console.log(userInfo);
                var userLoginName;
                
                //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) {
                    
                    userLoginName=userInfo.Key;
                }
    
                return userLoginName;
            }
    </script>
    Thanks & Regards,


    sharath aluri


    Monday, July 22, 2019 3:50 PM
  • Hi,

    I am checking to see how things are going there on this issue. Please let us know if you would like further assistance.

    You can mark the helpful post as answer to help other community members find the helpful information quickly.

    You can also share your own solution here and mark it as answer, so it may help others who has similar requirement.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Wednesday, July 24, 2019 6:59 AM
  • Hi sharath,

    i tried your code and its the same issue. I am unable to save the form when i added your code. The original code that share with you works but the only is whenever the fields are blank it still gives the same alert.


    nk

    Wednesday, July 24, 2019 7:16 PM
  • @sharath I finally was able to make it work. I was using TEAM MEMBER Instead of TEAM MEMBERS.

    Thanks a lot.


    nk

    Friday, July 26, 2019 2:21 PM