0

I have some custom JS code that would help me validate fields on a list on the client side. Just in case, if fields pass the client side validations, I'm displaying an alert saying "Your item has been saved".

A couple of days back I ran into a situation where the user entered an invalid name (for eg: some randoms string such as 'asdf' and that wouldn't be resolved on PP field) on a people picker field and it wouldn't resolve it. But my Client side validation passed and the alert was shown. After that Server Validation ran and it tried to resolve 'asdf' on the PP field, which was not happening and gave an error message beneath the control. The user didn't see that and he thought as the form has already shown an alert saying "Your item has been saved", the item might have been saved and hence he closed the browser. The user reported later that he can not find his item on the list. I want to, at least, get the result of Server side validation using AJAX or any other possible way. Can anyone please guide me on how to achieve this? One-liner: I want to know if there is any way to get the Server side validation on the Client side?

1

I would suggest optimizing the client-side validation rather than trying to get a response server-side.

Here are some links which should give you an idea on what to do:

Code examples from posted links

Validate SharePoint People picker using Javascript

<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>

Javascript validation of people picker control in sharepoint 2010

<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>
  • 1
    I see you declare a GetParentByTagName(tagname,element) function. SharePoint (core.js) has llready provided this as functionality as the GetAncestor(element,tagname) function – Danny '365CSI' Engelman Feb 24 '16 at 9:38
1

If you don't mind using a third party JavaScript library, then you can have a look at SharepointPlus that I have developed. This is not fully compatible yet with Sharepoint 2013.

Then you can use it to check the mandatory fields:

$SP().formfields({mandatory:true}).each(function checkMandatory() {
  if (this.val().length === 0) {
    alert("The field "+ this.name() +" should contain a value because it's a mandatory field");
  }
});

Then for a People Picker field (e.g. "Manager Name") you can verify that the entered name is correct with $SP().addressbook(); see below a code example:

var name = $SP().formfields("Manager Name").val();
$SP().addressbook(name, {limit:100}, function(people) {
  if (typeof people === "string") {
    alert("Error: " + people);
    return;
  }
  if (people.length === 1) {
    alert("Name "+people[0].DisplayName+" is OK");
    return;
  }
  if (people.length === 0) {
    alert("Name is incorrect");
    return;
  }

  // do some advanced checks if several names have been found
  if (people.length > 1) {
    // in that case we verify if we find an exact match
    var ok = false;
    var regexpAccountName = new RegExp("\\\\" + name+ "$");
    var regexpName = new RegExp("^" + name+ "$");
    for (var i=0; i < people.length; i++) {
      if (people[i].AccountName.match(regexpAccountName) || people[i].DisplayName.match(regexpName)) {
        alert("Name "+people[i].DisplayName+" is OK");
        ok=true;
        break;
      }
    }
    if (!ok) alert("The entered name is ambigous");
  }
})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.