locked
Validate SharePoint People picker using Javascript RRS feed

  • Question

  • I have a People Picker control in a SharePoint list. I want to validate the user to enter at least 3 users in it. First I wrote a Event handler to do the validation. But the client wants it to do using the Java script. This control is find something complex to validate using Javascript. Have anybody already tried Validation using Javascript for a People picker control please help me.

     

    Wednesday, March 31, 2010 5:59 AM

Answers

  • you can validate this using javascript . plz check this link for how to access 'Person or Group' field using javascript(in Newform.aspx and editform.aspx) , once u have the value you can manipulate the string and check for no of users entered (split string on seperator ; ). 

    http://gj80blogtech.blogspot.com/2009/12/client-side-programming-in-sharepoint-3.html



    Thanks
    Ganesh Jat [My Blog | LinkedIn | Twitter ]
    Wednesday, March 31, 2010 6:56 AM
  • I would like to thank Ganesh Jat for pointing me to the link he supplied, because it got me going on the right track in validating the People Picker for the Assigned To Field.  But I had to modify to fit what I was doing but here is what I came up with.

     

    <script type="text/javascript">
     	function PreSaveAction() {
     		var tempcon;
    		var fieldValue = "";
     		var fieldLabel = "Assigned To";
    	 	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 the request to someone."); 
    					return false; // Cancel the item save process
    				} else {
    					return true;
    				}
    			}
    		}
    		
    		// gets the parent node of assinged 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 assined to field
    		function GetParentByTagName(parentTagName, childElementObj) {
    			var parent = childElementObj.parentNode;
    			while(parent.tagName.toLowerCase() != parentTagName.toLowerCase()) {
    				parent = parent.parentNode;
    			}
    			return parent;
    		}
    	} 	
    </script>
    

    Tuesday, October 26, 2010 9:11 PM

All replies

  • you can validate this using javascript . plz check this link for how to access 'Person or Group' field using javascript(in Newform.aspx and editform.aspx) , once u have the value you can manipulate the string and check for no of users entered (split string on seperator ; ). 

    http://gj80blogtech.blogspot.com/2009/12/client-side-programming-in-sharepoint-3.html



    Thanks
    Ganesh Jat [My Blog | LinkedIn | Twitter ]
    Wednesday, March 31, 2010 6:56 AM
  • I would like to thank Ganesh Jat for pointing me to the link he supplied, because it got me going on the right track in validating the People Picker for the Assigned To Field.  But I had to modify to fit what I was doing but here is what I came up with.

     

    <script type="text/javascript">
     	function PreSaveAction() {
     		var tempcon;
    		var fieldValue = "";
     		var fieldLabel = "Assigned To";
    	 	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 the request to someone."); 
    					return false; // Cancel the item save process
    				} else {
    					return true;
    				}
    			}
    		}
    		
    		// gets the parent node of assinged 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 assined to field
    		function GetParentByTagName(parentTagName, childElementObj) {
    			var parent = childElementObj.parentNode;
    			while(parent.tagName.toLowerCase() != parentTagName.toLowerCase()) {
    				parent = parent.parentNode;
    			}
    			return parent;
    		}
    	} 	
    </script>
    

    Tuesday, October 26, 2010 9:11 PM