Thursday 25 October 2012

Javascript validation of people picker control in sharepoint 2010

This post is to get the People Picker contol value or

to validate the People picker using javascript or

you can use this code to compare the current user name with PP(Refer my post 'ECMA Script to Get Current User Name in sharepoint list form')

Steps:

1. Just Add a content editor webpart in you list form(NewForm.aspx or EditForm.aspx) and put the script below:

To add CWEP to you list form you can refer Link

2. Add your 'Control UserField_upLevelDiv ID'(Using View Source you can get upLevelDiv  id of that contol)

<script type="text/javascript">
function getPickerInputElement(identifier)
   {   
      var tags = document.getElementsByTagName('DIV');   
      for (var i=0; i < tags.length; i++)
      {       
       var tempString = tags[i].id;           
       if ((tempString.indexOf('UserField_upLevelDiv') > 0))
    {   
        if(identifier == tempString)
       { 
       var innerSpans = tags[i].getElementsByTagName("SPAN");     
         for(var j=0; j < innerSpans.length; j++)
          {   
           if(innerSpans[j].id == 'content')
             {   
              return innerSpans[j].innerHTML; 
             } 
          }   
       }     
       }   
      }   
      return null;   
   }

var PickerPerson = getPickerInputElement('Control UserField_upLevelDiv ID'); 
alert(PickerPerson);

// For Validation 
   if(PickerPerson == null)
    {
     alert('Please enter PP value.');
     return false;
    }

</script>

Done!

2 comments:

  1. Hi Ankur, There is no upLevelDiv in the SharePoint 2013 view source. Do you have any suggestions or script to help with people picker control in 2013?

    ReplyDelete
  2. People title "Assign To" column validation

    function fnassign(){
    try{


    var len3=$("div [title='Assign To']").find(".sp-peoplepicker-resolveList").find("span").length;
    if(len3!=3){
    alert("Please enter the AssignTo value");
    return false;
    }
    }

    }
    catch(e)
    {
    alert(e);
    }
    }

    Click here more details

    ReplyDelete