Wednesday, September 19, 2012

jQuery validation against User type fields in SharePoint

A field with Type="User" specified in SharePoint is a login control. It allows for you to fish for someone in Active Directory or otherwise speced as a user!

Share photos on twitter with Twitpic

I rolled some jQuery validations for these controls like so:

var inputuserfields = new Array();
inputuserfields = $('.ms-inputuserfield');
var isEven = true;
var inputuserdivs = new Array();
$.each(inputuserfields, function() {
   if (isEven){
      inputuserdivs.push($(this));
      isEven = false;
   } else {
      isEven = true;
   }
});
var doesNotMeetUserInputRequirements = false;
$.each(inputuserdivs, function() {
   var divhtml = $(this).html();
   if ((divhtml).indexOf("SPAN") >= 0)
   {
      if ((divhtml).indexOf("ms-entity-unresolved") >= 0)
      {
         doesNotMeetUserInputRequirements = true;
      }
   } else {
      doesNotMeetUserInputRequirements = true;
   }
});
if(doesNotMeetUserInputRequirements)
{

 
 

You'll notice two .each loops above:

  1. I first get everything with the ms-inputuserfield class and then cast the even items to a different array in the name of trimming out some noise. Each User type control will have two elements with ms-inputuserfield associated. ms-inputuserfield will not appear anywhere else on a form. Of the two elements for each control, we only want the first element which is a div containing the public text for the control in a manner that sort of fakes a text input field. The element we don't want is a textarea. I don't know what the textarea does. Is it a storage space for SharePoint to do some needed acrobatics in the name of wrangling user data?
  2. If a User control div is left empty it will only contain   and if it has only been typed in it will hold some copy. One has to try to validate some typing with the check icon at a User control's right in order to make a match (or attempt a match) in a User control between an entered value and a User. After clicking the check a span tag will end up in the div, so I check for this span tag in my code in the name of determining if a User control is properly populated. However, if the ms-entity-unresolved class appears inspite of the presence of a span tag, that means an attempt to find a user has failed.
    Share photos on twitter with Twitpic

I got pulled into SharePoint in my 11th hour at HDRI. It turns out I like it! I'm going to miss it when I hand in this badge at week's end:

Share photos on twitter with Twitpic

No comments:

Post a Comment